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         (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })? 
284         (
285         name = ParseQualifiedName() {
286                 if (!type.equals(""))
287                         type = type + " " + name.fullName;
288                 else
289                         type = name.fullName;
290         })
291         )
292         ((str = <CONST>.image {
293                 if (!type.equals(""))
294                         type = type + " " + str;
295                 else
296                         type = str;
297         }) |
298         (str = <STAR>.image {
299                 if (!type.equals(""))
300                         type = type + " " + str;
301                 else
302                         type = str;
303         }) |
304         (str = <AND>.image {
305                 if (!type.equals(""))
306                         type = type + " " + str;
307                 else
308                         type = str;
309         })
310         )*
311         {
312                 return type;
313         }
314 }
315
316 void Test() :
317 {
318         String str;     
319         FunctionHeader func;
320 }
321 {
322         /*
323         str = Type()
324         {
325                 System.out.println(str);
326         }
327         */
328         func = FuncDecl() 
329         {
330                 System.out.println(func);
331         }
332         
333 }
334
335 String ParameterizedName() :
336 {
337         String res = "";
338         String str;
339 }
340 {
341         (str = <IDENTIFIER>.image {res = str;})
342         (<LESS_THAN> str = Type() { res = res + "<" + str; }
343         (<COMMA> str = Type() { res = res + ", " + str; })* <GREATER_THAN>
344         { res = res + ">"; }
345         )?
346         {
347                 return res;
348         }
349 }
350
351 FunctionHeader FuncDecl() :
352 {
353         String ret;
354         QualifiedName funcName;
355         ArrayList<VariableDeclaration> args;
356 }
357 {
358         (<STATIC> | <INLINE>)*
359         ret = Type() 
360         funcName = ParseQualifiedName() 
361         args = FormalParamList() 
362         {
363                 FunctionHeader res = new FunctionHeader(ret, funcName, args);
364                 //System.out.println(res);
365                 return res;
366         }
367 }
368
369 QualifiedName ParseQualifiedName() :
370 {
371         String qualifiedName, str;
372 }
373 {
374         { qualifiedName = ""; }
375         (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
376         ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
377         "::" + str; }  ))*
378         {
379                 QualifiedName res = new QualifiedName(qualifiedName);
380                 //System.out.println(res);
381                 return res;
382         }
383 }
384
385 ArrayList<VariableDeclaration> TemplateParamList() :
386 {
387         ArrayList<VariableDeclaration> params;
388         String type;
389         String name;
390 }
391 {
392         {
393                 params = new ArrayList<VariableDeclaration>();
394         }
395         <TEMPLATE>
396         <LESS_THAN>
397         (type = <IDENTIFIER>.image 
398         name = <IDENTIFIER>.image
399         {
400                 params.add(new VariableDeclaration(type, name));
401         }
402         )
403
404         (<COMMA> type = <IDENTIFIER>.image 
405         name = <IDENTIFIER>.image
406         {
407                 params.add(new VariableDeclaration(type, name));
408         }
409         )*
410         <GREATER_THAN>
411         {
412                 //System.out.println(params);
413                 return params;
414         }
415 }
416
417 ArrayList<VariableDeclaration > FormalParamList() :
418 {
419         ArrayList<VariableDeclaration > typeParams;
420         VariableDeclaration varDecl;
421 }
422 {
423         {
424                 typeParams = new ArrayList<VariableDeclaration >();
425         }
426         <OPEN_PAREN>
427         ((varDecl = TypeParam() {typeParams.add(varDecl);})
428         ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
429         <CLOSE_PAREN>
430         {
431                 return typeParams;
432         }
433 }
434
435 VariableDeclaration Declaration() :
436 {
437         String type, param;
438 }
439 {
440         (type = Type()) (param = <IDENTIFIER>.image)  <SEMI_COLON>
441         {
442                 return new VariableDeclaration(type, param);
443         }
444 }
445
446 VariableDeclaration TypeParam() :
447 {
448         String type, param;
449 }
450 {
451         (type = Type()) (param = <IDENTIFIER>.image)
452         {
453                 return new VariableDeclaration(type, param);
454         }
455 }