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