edits
[cdsspec-compiler.git] / grammer / util.jj
1 /* util.jj Grammer definition for utility functions */
2
3 options {
4         STATIC = false;
5         JAVA_UNICODE_ESCAPE = true;
6 }
7
8 PARSER_BEGIN(UtilParser)
9 package edu.uci.eecs.utilParser;
10 import edu.uci.eecs.specExtraction.FunctionHeader;
11 import edu.uci.eecs.specExtraction.QualifiedName;
12 import edu.uci.eecs.specExtraction.VariableDeclaration;
13 //import edu.uci.eecs.specExtraction.WrongAnnotationException;
14
15 import java.io.FileInputStream;
16 import java.io.FileNotFoundException;
17 import java.io.InputStream;
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.util.ArrayList;
21
22         
23 public class UtilParser {
24         public static void main(String[] argvs)
25         throws ParseException, TokenMgrError {
26                 try {
27                         File f = new File("./grammer/spec1.txt");
28                         FileInputStream fis = new FileInputStream(f);
29                         UtilParser parser = new UtilParser(fis);
30                         
31                         //parser.Test();
32                         System.out.println("Parsing finished!");
33                 } catch (FileNotFoundException e) {
34                         e.printStackTrace();
35                 }
36         }
37
38         public static ArrayList<VariableDeclaration> getTemplateArg(String line)
39         throws ParseException {
40                 InputStream input = new ByteArrayInputStream(line.getBytes());
41                 UtilParser parser = new UtilParser(input);
42                 return parser.TemplateParamList();
43         }
44
45         public static FunctionHeader parseFuncHeader(String line)
46         throws ParseException {
47                 InputStream input = new ByteArrayInputStream(line.getBytes());
48                 UtilParser parser = new UtilParser(input);
49                 return parser.FuncDecl();
50         }
51
52         public static VariableDeclaration parseDeclaration(String line)
53         throws ParseException {
54                 InputStream input = new ByteArrayInputStream(line.getBytes());
55                 UtilParser parser = new UtilParser(input);
56                 return parser.Declaration();
57         }
58
59         public static String stringArray2String(ArrayList<String> content) {
60                 StringBuilder sb = new StringBuilder();
61                 if (content.size() == 1)
62                         return content.get(0);
63                 for (int i = 0; i < content.size(); i++) {
64                         sb.append(content.get(i) + "\n");
65                 }
66                 return sb.toString();
67         }
68 }
69 PARSER_END(UtilParser)
70
71 SKIP :
72 {
73         " "
74 |
75         "\n"
76 |
77         "\r"
78 |
79         "\r\n"
80 |
81         "\t"
82 }
83
84 TOKEN :
85 {
86 /*   Specification & C/C++ shared tokens   */
87 // Reserved keywords
88         <CONST: "const">
89 |
90         <STRUCT: "struct">
91 |
92         <CLASS: "class">
93 |
94         <UNSIGNED: "unsigned">
95 |
96         <TEMPLATE: "template">
97 |
98         <INLINE: "inline">
99 |
100         <STATIC: "static">
101 |
102         <FOR: "for">
103 |
104         <#DIGIT: ["0"-"9"]>
105 |
106         <#LETTER: ["a"-"z", "A"-"Z"]>
107 |
108         <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
109 |
110         <POUND: "#">
111 |
112         <OPEN_BRACKET: "[">
113 |
114         <CLOSE_BRACKET: "]">
115 |
116         <EQUALS: "=">
117 |
118         <OPEN_PAREN: "(">
119 |
120         <CLOSE_PAREN: ")">
121 |
122         <OPEN_BRACE: "{">
123 |
124         <CLOSE_BRACE: "}">
125 |
126         <HB_SYMBOL: "->">
127 |
128         <COMMA: ",">
129 |
130 /*   C/C++ only token*/
131         <DOT: ".">
132 |
133         <DOLLAR: "$">
134 |
135         <STAR: "*">
136 |
137         <NEGATE: "~">
138 |
139         <EXCLAMATION: "!">
140 |
141         <AND: "&">
142 |
143         <OR: "|">
144 |
145         <MOD: "%">
146 |
147         <PLUS: "+">
148 |
149         <PLUSPLUS: "++">
150 |
151         <MINUS: "-">
152 |
153         <MINUSMINUS: "--">
154 |
155         <DIVIDE: "/">
156 |
157         <BACKSLASH: "\\">
158 |
159         <LESS_THAN: "<">
160 |
161         <GREATER_THAN: ">">
162 |
163         <GREATER_EQUALS: ">=">
164 |
165         <LESS_EQUALS: "<=">
166 |
167         <LOGICAL_EQUALS: "==">
168 |
169         <NOT_EQUALS: "!=">
170 |
171         <LOGICAL_AND: "&&">
172 |
173         <LOGICAL_OR: "||">
174 |
175         <XOR: "^">
176 |
177         <QUESTION_MARK: "?">
178 |
179         <COLON: ":">
180 |
181         <DOUBLECOLON: "::">
182 |
183         <DOUBLELESSTHAN: "<<">
184 |
185         <DOUBLEGREATERTHAN: ">>">
186 |
187         <TRIPLEGREATERTHAN: ">>>">
188 |
189         <PLUS_EQUALS: "+=">
190 |
191         <MINUS_EQUALS: "-=">
192 |
193         <TIMES_EQUALS: "*=">
194 |
195         <DIVIDE_EQUALS: "/=">
196 |
197         <MOD_EQUALS: "%=">
198 |
199         <XOR_EQUALS: "^=">
200 |
201         <OR_EQUALS: "|=">
202 |
203         <AND_EQUALS: "&=">
204 |
205         <SEMI_COLON: ";">
206 |
207         <STRING_LITERAL:
208         "\""
209         ((~["\"","\\","\n","\r"])
210         | ("\\"
211                 ( ["n","t","b","r","f","\\","'","\""]
212                 | ["0"-"7"] ( ["0"-"7"] )?
213                 | ["0"-"3"] ["0"-"7"]
214                         ["0"-"7"]
215                 )
216                 )
217         )*
218         "\"">
219 |
220         <CHARACTER_LITERAL:
221         "'"
222         ((~["'","\\","\n","\r"])
223         | ("\\"
224                 (["n","t","b","r","f","\\","'","\""]
225                 | ["0"-"7"] ( ["0"-"7"] )?
226                 | ["0"-"3"] ["0"-"7"]
227                 ["0"-"7"]
228                 )
229                 )
230         )
231         "'">
232 |
233         < INTEGER_LITERAL:
234         <DECIMAL_LITERAL> (["l","L"])?
235       | <HEX_LITERAL> (["l","L"])?
236       | <OCTAL_LITERAL> (["l","L"])?>
237 |
238         < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
239 |
240         < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
241 |
242         < #OCTAL_LITERAL: "0" (["0"-"7"])* >
243 |
244         < FLOATING_POINT_LITERAL:
245         <DECIMAL_FLOATING_POINT_LITERAL>
246       | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
247 |
248         < #DECIMAL_FLOATING_POINT_LITERAL:
249         (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
250       | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
251       | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
252       | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
253 |
254         < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
255 |
256         < #HEXADECIMAL_FLOATING_POINT_LITERAL:
257         "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
258       | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
259 |
260         < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
261 |
262         < #SPACE: (" " | "\t")+>
263 |
264         < #TO_END_OF_LINE: (~["\n"])+>
265 |
266         /* Macro token */
267         <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
268 |
269         <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
270 }
271
272 String Type() :
273 {
274         String type;
275         String str;
276         QualifiedName name;
277 }
278 {
279         { type = ""; }
280         (<CONST>
281         { type = "const"; }
282         )?
283         (
284                 ((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) {
285                 type = type.equals("") ? type + str : type + " " + str;
286                 })? 
287         (
288         name = ParseQualifiedName() {
289                 if (!type.equals(""))
290                         type = type + " " + name.fullName;
291                 else
292                         type = name.fullName;
293         })
294         )
295         ((str = <CONST>.image {
296                 if (!type.equals(""))
297                         type = type + " " + str;
298                 else
299                         type = str;
300         }) |
301         (str = <STAR>.image {
302                 if (!type.equals(""))
303                         type = type + " " + str;
304                 else
305                         type = str;
306         }) |
307         (str = <AND>.image {
308                 if (!type.equals(""))
309                         type = type + " " + str;
310                 else
311                         type = str;
312         })
313         )*
314         {
315                 return type;
316         }
317 }
318
319 void Test() :
320 {
321         String str;     
322         FunctionHeader func;
323 }
324 {
325         /*
326         str = Type()
327         {
328                 System.out.println(str);
329         }
330         */
331         func = FuncDecl() 
332         {
333                 System.out.println(func);
334         }
335         
336 }
337
338 String ParameterizedName() :
339 {
340         String res = "";
341         String str;
342 }
343 {
344         (str = <IDENTIFIER>.image {res = str;})
345         (<LESS_THAN> str = Type() { res = res + "<" + str; }
346         (<COMMA> str = Type() { res = res + ", " + str; })* <GREATER_THAN>
347         { res = res + ">"; }
348         )?
349         {
350                 return res;
351         }
352 }
353
354 FunctionHeader FuncDecl() :
355 {
356         String ret;
357         QualifiedName funcName;
358         ArrayList<VariableDeclaration> args;
359 }
360 {
361         (<STATIC> | <INLINE>)*
362         ret = Type() 
363         funcName = ParseQualifiedName() 
364         args = FormalParamList() 
365         {
366                 FunctionHeader res = new FunctionHeader(ret, funcName, args);
367                 //System.out.println(res);
368                 return res;
369         }
370 }
371
372 QualifiedName ParseQualifiedName() :
373 {
374         String qualifiedName, str;
375 }
376 {
377         { qualifiedName = ""; }
378         (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
379         ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
380         "::" + str; }  ))*
381         {
382                 QualifiedName res = new QualifiedName(qualifiedName);
383                 //System.out.println(res);
384                 return res;
385         }
386 }
387
388 ArrayList<VariableDeclaration> TemplateParamList() :
389 {
390         ArrayList<VariableDeclaration> params;
391         String type;
392         String name;
393 }
394 {
395         {
396                 params = new ArrayList<VariableDeclaration>();
397         }
398         <TEMPLATE>
399         <LESS_THAN>
400         (type = <IDENTIFIER>.image 
401         name = <IDENTIFIER>.image
402         {
403                 params.add(new VariableDeclaration(type, name));
404         }
405         )
406
407         (<COMMA> type = <IDENTIFIER>.image 
408         name = <IDENTIFIER>.image
409         {
410                 params.add(new VariableDeclaration(type, name));
411         }
412         )*
413         <GREATER_THAN>
414         {
415                 //System.out.println(params);
416                 return params;
417         }
418 }
419
420 ArrayList<VariableDeclaration > FormalParamList() :
421 {
422         ArrayList<VariableDeclaration > typeParams;
423         VariableDeclaration varDecl;
424 }
425 {
426         {
427                 typeParams = new ArrayList<VariableDeclaration >();
428         }
429         <OPEN_PAREN>
430         ((varDecl = TypeParam() {typeParams.add(varDecl);})
431         ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
432         <CLOSE_PAREN>
433         {
434                 return typeParams;
435         }
436 }
437
438 VariableDeclaration Declaration() :
439 {
440         String type, param;
441 }
442 {
443         (type = Type()) (param = <IDENTIFIER>.image)  <SEMI_COLON>
444         {
445                 return new VariableDeclaration(type, param);
446         }
447 }
448
449 VariableDeclaration TypeParam() :
450 {
451         String type, param;
452 }
453 {
454         (type = Type()) (param = <IDENTIFIER>.image)
455         {
456                 return new VariableDeclaration(type, param);
457         }
458 }