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