eb363d019134f06ea7627a05c95f1058754814fe
[cdsspec-compiler.git] / src / edu / uci / eecs / utilParser / UtilParser.java
1 /* UtilParser.java */
2 /* Generated By:JavaCC: Do not edit this line. UtilParser.java */
3 package edu.uci.eecs.utilParser;
4 import edu.uci.eecs.specExtraction.FunctionHeader;
5 import edu.uci.eecs.specExtraction.QualifiedName;
6 import edu.uci.eecs.specExtraction.VariableDeclaration;
7 //import edu.uci.eecs.specExtraction.WrongAnnotationException;
8
9 import java.io.FileInputStream;
10 import java.io.FileNotFoundException;
11 import java.io.InputStream;
12 import java.io.ByteArrayInputStream;
13 import java.io.File;
14 import java.util.ArrayList;
15
16
17 public class UtilParser implements UtilParserConstants {
18         public static void main(String[] argvs)
19         throws ParseException, TokenMgrError {
20                 try {
21                         File f = new File("./grammer/spec1.txt");
22                         FileInputStream fis = new FileInputStream(f);
23                         UtilParser parser = new UtilParser(fis);
24
25                         //parser.Test();
26                         System.out.println("Parsing finished!");
27                 } catch (FileNotFoundException e) {
28                         e.printStackTrace();
29                 }
30         }
31
32         public static ArrayList<VariableDeclaration> getTemplateArg(String line)
33         throws ParseException {
34                 InputStream input = new ByteArrayInputStream(line.getBytes());
35                 UtilParser parser = new UtilParser(input);
36                 return parser.TemplateParamList();
37         }
38
39         public static FunctionHeader parseFuncHeader(String line)
40         throws ParseException {
41                 InputStream input = new ByteArrayInputStream(line.getBytes());
42                 UtilParser parser = new UtilParser(input);
43                 return parser.FuncDecl();
44         }
45
46         public static VariableDeclaration parseDeclaration(String line)
47         throws ParseException {
48                 InputStream input = new ByteArrayInputStream(line.getBytes());
49                 UtilParser parser = new UtilParser(input);
50                 return parser.Declaration();
51         }
52
53         public static String stringArray2String(ArrayList<String> content) {
54                 StringBuilder sb = new StringBuilder();
55                 if (content.size() == 1)
56                         return content.get(0);
57                 for (int i = 0; i < content.size(); i++) {
58                         sb.append(content.get(i) + "\u005cn");
59                 }
60                 return sb.toString();
61         }
62
63   final public String Type() throws ParseException {String type;
64         String str;
65         QualifiedName name;
66 type = "";
67     switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
68     case CONST:{
69       jj_consume_token(CONST);
70 type = "const";
71       break;
72       }
73     default:
74       jj_la1[0] = jj_gen;
75       ;
76     }
77     switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
78     case STRUCT:
79     case CLASS:
80     case UNSIGNED:{
81       switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
82       case STRUCT:{
83         str = jj_consume_token(STRUCT).image;
84         break;
85         }
86       case CLASS:{
87         str = jj_consume_token(CLASS).image;
88         break;
89         }
90       case UNSIGNED:{
91         str = jj_consume_token(UNSIGNED).image;
92         break;
93         }
94       default:
95         jj_la1[1] = jj_gen;
96         jj_consume_token(-1);
97         throw new ParseException();
98       }
99 type = type.equals("") ? type + str : type + " " + str;
100       break;
101       }
102     default:
103       jj_la1[2] = jj_gen;
104       ;
105     }
106     name = ParseQualifiedName();
107 if (!type.equals(""))
108                         type = type + " " + name.fullName;
109                 else
110                         type = name.fullName;
111     label_1:
112     while (true) {
113       switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
114       case CONST:
115       case STAR:
116       case AND:{
117         ;
118         break;
119         }
120       default:
121         jj_la1[3] = jj_gen;
122         break label_1;
123       }
124       switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
125       case CONST:{
126         str = jj_consume_token(CONST).image;
127 if (!type.equals(""))
128                         type = type + " " + str;
129                 else
130                         type = str;
131         break;
132         }
133       case STAR:{
134         str = jj_consume_token(STAR).image;
135 if (!type.equals(""))
136                         type = type + " " + str;
137                 else
138                         type = str;
139         break;
140         }
141       case AND:{
142         str = jj_consume_token(AND).image;
143 if (!type.equals(""))
144                         type = type + " " + str;
145                 else
146                         type = str;
147         break;
148         }
149       default:
150         jj_la1[4] = jj_gen;
151         jj_consume_token(-1);
152         throw new ParseException();
153       }
154     }
155 {if ("" != null) return type;}
156     throw new Error("Missing return statement in function");
157   }
158
159   final public void Test() throws ParseException {String str;
160         FunctionHeader func;
161     /*
162         str = Type()
163         {
164                 System.out.println(str);
165         }
166         */
167             func = FuncDecl();
168 System.out.println(func);
169   }
170
171   final public String ParameterizedName() throws ParseException {String res = "";
172         String str;
173     str = jj_consume_token(IDENTIFIER).image;
174 res = str;
175     switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
176     case LESS_THAN:{
177       jj_consume_token(LESS_THAN);
178       str = Type();
179 res = res + "<" + str;
180       label_2:
181       while (true) {
182         switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
183         case COMMA:{
184           ;
185           break;
186           }
187         default:
188           jj_la1[5] = jj_gen;
189           break label_2;
190         }
191         jj_consume_token(COMMA);
192         str = Type();
193 res = res + ", " + str;
194       }
195       jj_consume_token(GREATER_THAN);
196 res = res + ">";
197       break;
198       }
199     default:
200       jj_la1[6] = jj_gen;
201       ;
202     }
203 {if ("" != null) return res;}
204     throw new Error("Missing return statement in function");
205   }
206
207   final public FunctionHeader FuncDecl() throws ParseException {String ret;
208         QualifiedName funcName;
209         ArrayList<VariableDeclaration> args;
210     label_3:
211     while (true) {
212       switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
213       case INLINE:
214       case STATIC:{
215         ;
216         break;
217         }
218       default:
219         jj_la1[7] = jj_gen;
220         break label_3;
221       }
222       switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
223       case STATIC:{
224         jj_consume_token(STATIC);
225         break;
226         }
227       case INLINE:{
228         jj_consume_token(INLINE);
229         break;
230         }
231       default:
232         jj_la1[8] = jj_gen;
233         jj_consume_token(-1);
234         throw new ParseException();
235       }
236     }
237     ret = Type();
238     funcName = ParseQualifiedName();
239     args = FormalParamList();
240 FunctionHeader res = new FunctionHeader(ret, funcName, args);
241                 //System.out.println(res);
242                 {if ("" != null) return res;}
243     throw new Error("Missing return statement in function");
244   }
245
246   final public QualifiedName ParseQualifiedName() throws ParseException {String qualifiedName, str;
247 qualifiedName = "";
248     str = ParameterizedName();
249 qualifiedName = qualifiedName + str;
250     label_4:
251     while (true) {
252       switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
253       case DOUBLECOLON:{
254         ;
255         break;
256         }
257       default:
258         jj_la1[9] = jj_gen;
259         break label_4;
260       }
261       jj_consume_token(DOUBLECOLON);
262       str = ParameterizedName();
263 qualifiedName = qualifiedName +
264         "::" + str;
265     }
266 QualifiedName res = new QualifiedName(qualifiedName);
267                 //System.out.println(res);
268                 {if ("" != null) return res;}
269     throw new Error("Missing return statement in function");
270   }
271
272   final public ArrayList<VariableDeclaration> TemplateParamList() throws ParseException {ArrayList<VariableDeclaration> params;
273         String type;
274         String name;
275 params = new ArrayList<VariableDeclaration>();
276     jj_consume_token(TEMPLATE);
277     jj_consume_token(LESS_THAN);
278     type = jj_consume_token(IDENTIFIER).image;
279     name = jj_consume_token(IDENTIFIER).image;
280 params.add(new VariableDeclaration(type, name));
281     label_5:
282     while (true) {
283       switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
284       case COMMA:{
285         ;
286         break;
287         }
288       default:
289         jj_la1[10] = jj_gen;
290         break label_5;
291       }
292       jj_consume_token(COMMA);
293       type = jj_consume_token(IDENTIFIER).image;
294       name = jj_consume_token(IDENTIFIER).image;
295 params.add(new VariableDeclaration(type, name));
296     }
297     jj_consume_token(GREATER_THAN);
298 //System.out.println(params);
299                 {if ("" != null) return params;}
300     throw new Error("Missing return statement in function");
301   }
302
303   final public ArrayList<VariableDeclaration > FormalParamList() throws ParseException {ArrayList<VariableDeclaration > typeParams;
304         VariableDeclaration varDecl;
305 typeParams = new ArrayList<VariableDeclaration >();
306     jj_consume_token(OPEN_PAREN);
307     switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
308     case CONST:
309     case STRUCT:
310     case CLASS:
311     case UNSIGNED:
312     case IDENTIFIER:{
313       varDecl = TypeParam();
314 typeParams.add(varDecl);
315       label_6:
316       while (true) {
317         switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
318         case COMMA:{
319           ;
320           break;
321           }
322         default:
323           jj_la1[11] = jj_gen;
324           break label_6;
325         }
326         jj_consume_token(COMMA);
327         varDecl = TypeParam();
328 typeParams.add(varDecl);
329       }
330       break;
331       }
332     default:
333       jj_la1[12] = jj_gen;
334       ;
335     }
336     jj_consume_token(CLOSE_PAREN);
337 {if ("" != null) return typeParams;}
338     throw new Error("Missing return statement in function");
339   }
340
341   final public VariableDeclaration Declaration() throws ParseException {String type, param;
342     type = Type();
343     param = jj_consume_token(IDENTIFIER).image;
344     jj_consume_token(SEMI_COLON);
345 {if ("" != null) return new VariableDeclaration(type, param);}
346     throw new Error("Missing return statement in function");
347   }
348
349   final public VariableDeclaration TypeParam() throws ParseException {String type, param;
350     type = Type();
351     param = jj_consume_token(IDENTIFIER).image;
352 {if ("" != null) return new VariableDeclaration(type, param);}
353     throw new Error("Missing return statement in function");
354   }
355
356   /** Generated Token Manager. */
357   public UtilParserTokenManager token_source;
358   JavaCharStream jj_input_stream;
359   /** Current token. */
360   public Token token;
361   /** Next token. */
362   public Token jj_nt;
363   private int jj_ntk;
364   private int jj_gen;
365   final private int[] jj_la1 = new int[13];
366   static private int[] jj_la1_0;
367   static private int[] jj_la1_1;
368   static private int[] jj_la1_2;
369   static {
370       jj_la1_init_0();
371       jj_la1_init_1();
372       jj_la1_init_2();
373    }
374    private static void jj_la1_init_0() {
375       jj_la1_0 = new int[] {0x40,0x380,0x380,0x20000040,0x20000040,0x4000000,0x0,0x1800,0x1800,0x0,0x4000000,0x4000000,0x103c0,};
376    }
377    private static void jj_la1_init_1() {
378       jj_la1_1 = new int[] {0x0,0x0,0x0,0x1,0x1,0x0,0x200,0x0,0x0,0x100000,0x0,0x0,0x0,};
379    }
380    private static void jj_la1_init_2() {
381       jj_la1_2 = new int[] {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,};
382    }
383
384   /** Constructor with InputStream. */
385   public UtilParser(java.io.InputStream stream) {
386      this(stream, null);
387   }
388   /** Constructor with InputStream and supplied encoding */
389   public UtilParser(java.io.InputStream stream, String encoding) {
390     try { jj_input_stream = new JavaCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
391     token_source = new UtilParserTokenManager(jj_input_stream);
392     token = new Token();
393     jj_ntk = -1;
394     jj_gen = 0;
395     for (int i = 0; i < 13; i++) jj_la1[i] = -1;
396   }
397
398   /** Reinitialise. */
399   public void ReInit(java.io.InputStream stream) {
400      ReInit(stream, null);
401   }
402   /** Reinitialise. */
403   public void ReInit(java.io.InputStream stream, String encoding) {
404     try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
405     token_source.ReInit(jj_input_stream);
406     token = new Token();
407     jj_ntk = -1;
408     jj_gen = 0;
409     for (int i = 0; i < 13; i++) jj_la1[i] = -1;
410   }
411
412   /** Constructor. */
413   public UtilParser(java.io.Reader stream) {
414     jj_input_stream = new JavaCharStream(stream, 1, 1);
415     token_source = new UtilParserTokenManager(jj_input_stream);
416     token = new Token();
417     jj_ntk = -1;
418     jj_gen = 0;
419     for (int i = 0; i < 13; i++) jj_la1[i] = -1;
420   }
421
422   /** Reinitialise. */
423   public void ReInit(java.io.Reader stream) {
424     jj_input_stream.ReInit(stream, 1, 1);
425     token_source.ReInit(jj_input_stream);
426     token = new Token();
427     jj_ntk = -1;
428     jj_gen = 0;
429     for (int i = 0; i < 13; i++) jj_la1[i] = -1;
430   }
431
432   /** Constructor with generated Token Manager. */
433   public UtilParser(UtilParserTokenManager tm) {
434     token_source = tm;
435     token = new Token();
436     jj_ntk = -1;
437     jj_gen = 0;
438     for (int i = 0; i < 13; i++) jj_la1[i] = -1;
439   }
440
441   /** Reinitialise. */
442   public void ReInit(UtilParserTokenManager tm) {
443     token_source = tm;
444     token = new Token();
445     jj_ntk = -1;
446     jj_gen = 0;
447     for (int i = 0; i < 13; i++) jj_la1[i] = -1;
448   }
449
450   private Token jj_consume_token(int kind) throws ParseException {
451     Token oldToken;
452     if ((oldToken = token).next != null) token = token.next;
453     else token = token.next = token_source.getNextToken();
454     jj_ntk = -1;
455     if (token.kind == kind) {
456       jj_gen++;
457       return token;
458     }
459     token = oldToken;
460     jj_kind = kind;
461     throw generateParseException();
462   }
463
464
465 /** Get the next Token. */
466   final public Token getNextToken() {
467     if (token.next != null) token = token.next;
468     else token = token.next = token_source.getNextToken();
469     jj_ntk = -1;
470     jj_gen++;
471     return token;
472   }
473
474 /** Get the specific Token. */
475   final public Token getToken(int index) {
476     Token t = token;
477     for (int i = 0; i < index; i++) {
478       if (t.next != null) t = t.next;
479       else t = t.next = token_source.getNextToken();
480     }
481     return t;
482   }
483
484   private int jj_ntk_f() {
485     if ((jj_nt=token.next) == null)
486       return (jj_ntk = (token.next=token_source.getNextToken()).kind);
487     else
488       return (jj_ntk = jj_nt.kind);
489   }
490
491   private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
492   private int[] jj_expentry;
493   private int jj_kind = -1;
494
495   /** Generate ParseException. */
496   public ParseException generateParseException() {
497     jj_expentries.clear();
498     boolean[] la1tokens = new boolean[80];
499     if (jj_kind >= 0) {
500       la1tokens[jj_kind] = true;
501       jj_kind = -1;
502     }
503     for (int i = 0; i < 13; i++) {
504       if (jj_la1[i] == jj_gen) {
505         for (int j = 0; j < 32; j++) {
506           if ((jj_la1_0[i] & (1<<j)) != 0) {
507             la1tokens[j] = true;
508           }
509           if ((jj_la1_1[i] & (1<<j)) != 0) {
510             la1tokens[32+j] = true;
511           }
512           if ((jj_la1_2[i] & (1<<j)) != 0) {
513             la1tokens[64+j] = true;
514           }
515         }
516       }
517     }
518     for (int i = 0; i < 80; i++) {
519       if (la1tokens[i]) {
520         jj_expentry = new int[1];
521         jj_expentry[0] = i;
522         jj_expentries.add(jj_expentry);
523       }
524     }
525     int[][] exptokseq = new int[jj_expentries.size()][];
526     for (int i = 0; i < jj_expentries.size(); i++) {
527       exptokseq[i] = jj_expentries.get(i);
528     }
529     return new ParseException(token, exptokseq, tokenImage);
530   }
531
532   /** Enable tracing. */
533   final public void enable_tracing() {
534   }
535
536   /** Disable tracing. */
537   final public void disable_tracing() {
538   }
539
540 }