summaryrefslogtreecommitdiff
blob: 5bc45a0fd4ac04645f0319cb75dd82e56435beaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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;
   private static Document XML_ROOT;
   private static OutputFile MAIN_OUTPUT;

   static
   {
      XPE_FIND_ALL_VHDL_FILES =
         XMLManager.compile_or_die
            (
               //"./*/*/el[@kind=\"design_file\"][@file]"
               "//el[@kind=\"design_file\"][@file]"
            );
   }

   public static void main (final String... args)
   {
      final Collection<Node> vhdl_files;

      PARAMETERS = new Parameters(args);

      if (!PARAMETERS.are_valid())
      {
         return;
      }

      try
      {
         XML_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;
      }

      MAIN_OUTPUT =
         OutputFile.new_output_file
         (
            PARAMETERS.get_main_output_filename()
         );

      try
      {
         vhdl_files =
            XMLManager.node_list_to_node_collection
            (
               (NodeList) XPE_FIND_ALL_VHDL_FILES.evaluate
               (
                  XML_ROOT,
                  XPathConstants.NODESET
               )
            );
      }
      catch (final XPathExpressionException xpee)
      {
         System.err.println
         (
            "[E] Something went wrong when looking for the VHDL files:"
         );

         xpee.printStackTrace();

         return;
      }

      parse_content(vhdl_files);

      OutputFile.close_all();
   }

   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
         {
            waiting_list.pop().parse(waiting_list);
         }
         catch (final XPathExpressionException xpee)
         {
            System.err.println
            (
               "[E] Something went wrong while parsing the XML file:"
            );

            xpee.printStackTrace();

            return;
         }
      }
   }

   public static boolean node_is_function_or_literal (final String xml_id)
   throws XPathExpressionException
   {
      final XPathExpression xpe_find_el_from_id;
      final Node n;
      final String kind;

      xpe_find_el_from_id =
         XMLManager.compile_or_die
         (
            ".//el[@id=\""
            + xml_id
            + "\"]"
         );

      n =
         (Node) xpe_find_el_from_id.evaluate
         (
            XML_ROOT,
            XPathConstants.NODE
         );

      if (n == (Node) null)
      {
         return true;
      }

      kind = XMLManager.get_attribute(n, "kind");

      if (kind.equals("function_declaration"))
      {
         return true;
      }
      else if (kind.equals("enumeration_literal"))
      {
         return true;
      }

      return false;
   }

   public static Document get_xml_root ()
   {
      return XML_ROOT;
   }

   public static OutputFile get_main_output ()
   {
      return MAIN_OUTPUT;
   }
}