| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-07-20 11:06:59 +0200 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-07-20 11:06:59 +0200 | 
| commit | cd9254b65410c0f8a0fc3437ee43d88375244508 (patch) | |
| tree | 7c53e99feac2313caae6393a64792a6c605d4139 | |
| parent | 3787309dc8553f02bde69cded902d69c650c31a7 (diff) | |
Continuing the implementation of AST-to-Instr.
| -rw-r--r-- | ast-to-instr/src/Main.java | 133 | ||||
| -rw-r--r-- | ast-to-instr/src/Parameters.java | 48 | ||||
| -rw-r--r-- | ast-to-instr/src/XMLManager.java | 104 | 
3 files changed, 285 insertions, 0 deletions
| diff --git a/ast-to-instr/src/Main.java b/ast-to-instr/src/Main.java index e69de29..1193f52 100644 --- a/ast-to-instr/src/Main.java +++ b/ast-to-instr/src/Main.java @@ -0,0 +1,133 @@ +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; + +import java.util.Collection; +import java.util.Stack; + +public class Main +{ +   private static final XPathExpression XPE_FIND_ALL_VHDL_FILES; +   private static Parameters PARAMETERS; + +   static +   { +      XPathExpression xpe; + +      try +      { +         xpe = XMLManager.compile("./*/*/el[@kind=\"design_file\"][@file]"); +      } +      catch (final XPathExpressionException xpee) +      { +         xpe = null; + +         System.err.println +         ( +            "[P] Invalid XPath expression (report as bug):" +         ); + +         xpee.printStackTrace(); +      } + +      XPE_FIND_ALL_VHDL_FILES = xpe; +   } + +   public static void main (final String... args) +   { +      final Document root; +      final Collection<Node> vhdl_files; + +      PARAMETERS = new Parameters(args); + +      if (!PARAMETERS.are_valid()) +      { +         return; +      } + +      try +      { +         root = XMLManager.get_document(PARAMETERS.get_xml_file()); +      } +      catch (final Exception e) +      { +         System.err.println +         ( +            "[E] Could not load XML file \"" +            + PARAMETERS.get_xml_file() +            + "\":" +         ); + +         e.printStackTrace(); + +         return; +      } + +      try +      { +         vhdl_files = +            XMLManager.node_list_to_node_collection +            ( +               (NodeList) XPE_FIND_ALL_VHDL_FILES.evaluate +               ( +                  root, +                  XPathConstants.NODESET +               ) +            ); +      } +      catch (final XPathExpressionException xpee) +      { +         System.err.println +         ( +            "[E] Something went wrong when looking for the VHDL files:" +         ); + +         xpee.printStackTrace(); + +         return; +      } +   } + +   private static void parse_content (final Collection<Node> vhdl_files) +   { +      /* Stack highly recommended over FIFO (you don't want to grow large). */ +      final Stack<ParsableXML> waiting_list; + +      waiting_list = new Stack<ParsableXML>(); + +      for (final Node f: vhdl_files) +      { +         waiting_list.push(new VHDLFile(null, f)); +      } + +      while (!waiting_list.isEmpty()) +      { +         final Collection<ParsableXML> children; + +         try +         { +            children = waiting_list.pop().parse(); +         } +         catch (final XPathExpressionException xpee) +         { +            System.err.println +            ( +               "[E] Something went wrong while parsing the XML file:" +            ); + +            xpee.printStackTrace(); + +            return; +         } + +         for (final ParsableXML c: children) +         { +            waiting_list.push(c); +         } +      } +   } +} diff --git a/ast-to-instr/src/Parameters.java b/ast-to-instr/src/Parameters.java index e69de29..70d716f 100644 --- a/ast-to-instr/src/Parameters.java +++ b/ast-to-instr/src/Parameters.java @@ -0,0 +1,48 @@ +import java.util.List; +import java.util.ArrayList; + +public class Parameters +{ +   private final String xml_file; + +   private final boolean are_valid; + +   public static void print_usage () +   { +      System.out.println +      ( +         "AST-to-Instr\n" +         + "USAGE:\n" +         + "\tjava Main <XML_FILE>\n" +         + "PARAMETERS:\n" +         + "\t- <XML_FILE>\tThe AST (XML format)." +      ); +   } + +   public Parameters (String... args) +   { +      if (args.length != 1) +      { +         print_usage(); + +         xml_file = new String(); +         are_valid = false; + +         return; +      } + +      are_valid = true; + +      xml_file = args[0]; +   } + +   public String get_xml_file () +   { +      return xml_file; +   } + +   public boolean are_valid () +   { +      return are_valid; +   } +} diff --git a/ast-to-instr/src/XMLManager.java b/ast-to-instr/src/XMLManager.java new file mode 100644 index 0000000..03fbe6d --- /dev/null +++ b/ast-to-instr/src/XMLManager.java @@ -0,0 +1,104 @@ +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.ParserConfigurationException; + +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +import java.io.File; +import java.io.IOException; + +import java.util.ArrayList; +import java.util.Collection; + +public class XMLManager +{ +   /* ... */ +   /* private static final DocumentBuilderFactoryCreatorMakerInstanciator; */ +   /* private static final DocumentBuilderFactoryCreatorMaker; */ +   /* private static final DocumentBuilderFactoryCreator; */ +   private static final DocumentBuilderFactory DOC_BUILDER_FACTORY; +   private static final DocumentBuilder DOC_BUILDER; + +   private static final XPathFactory XPATH_FACTORY; +   private static final XPath XPATH; + +   static +   { +      DocumentBuilder i_dont_even; + +      DOC_BUILDER_FACTORY = DocumentBuilderFactory.newInstance(); + +      try +      { +         i_dont_even = DOC_BUILDER_FACTORY.newDocumentBuilder(); +      } +      catch (final Exception e) +      { +         i_dont_even = null; + +         System.err.println +         ( +            "[E] Err... You somehow managed to trigger an exception from purely" +            + " static members:" +         ); + +         e.printStackTrace(); + +         System.exit(-1); +      } + +      DOC_BUILDER = i_dont_even; + +      XPATH_FACTORY = XPathFactory.newInstance(); +      XPATH = XPATH_FACTORY.newXPath(); +   } + +   private XMLManager () {} /* Utility Class. */ + +   public static Document get_document (final String filename) +   throws +      SAXException, +      IOException +   { +      final File file; + +      file = new File(filename, "r"); + +      return DOC_BUILDER.parse(file); +   } + +   public static XPathExpression compile (final String expression) +   throws XPathExpressionException +   { +      return XPATH.compile(expression); +   } + +   public static Collection<Node> node_list_to_node_collection +   ( +      final NodeList nl +   ) +   { +      final Collection<Node> result; +      final int nl_length; + +      result = new ArrayList<Node>(); + +      nl_length = nl.getLength(); + +      for (int i = 0; i < nl_length; ++i) +      { +         result.add(nl.item(i)); +      } + +      return result; +   } +} | 


