a006e7bca3e5a463650b80f917e11477270df1d0
[cdsspec-compiler.git] / grammer / spec-compiler.jj
1 /* spec-compiler.jj Grammer definition for the specification */
2
3
4 /*
5         SPEC constructs:
6         Each construct should be embraced by /DOUBLE_STAR ... STAR/ annotation.
7         Within there, any line beginning with a "#" is a comment of the annotation.
8         Each constrcut should begin with @Begin and end with @End. Otherwise, the
9         annotation would be considered as normal comments of the source.
10         
11         a) Global construct
12         @Begin
13         @Global_define:
14                 ...
15         @Interface_cluster:
16                 ...
17         @Happens-before:
18                 ...
19         @End
20         
21         b) Interface construct
22         @Begin
23         @Interface: ...
24         @Commit_point_set:
25                 IDENTIFIER | IDENTIFIER ...
26         @Condition: ... (Optional)
27         @HB_Condition:
28                 IDENTIFIER :: <C_CPP_Condition>
29         @HB_Condition: ...
30         @ID: ... (Optional, use default ID)
31         @Check: (Optional)
32                 ...
33         @Action: (Optional)
34                 ...
35         @Post_action: (Optional)
36         @Post_check: (Optional)
37         @End
38
39         c) Potential commit construct
40         @Begin
41         @Potential_commit_point_define: ...
42         @Label: ...
43         @End
44
45         d) Commit point define construct
46         @Begin
47         @Commit_point_define_check: ...
48         @Label: ...
49         @End
50         
51                 OR
52
53         @Begin
54         @Commit_point_define: ...
55         @Potential_commit_point_label: ...
56         @Label: ...
57         @End
58 */
59
60
61
62 options {
63         STATIC = false;
64         JAVA_UNICODE_ESCAPE = true;
65 }
66
67 PARSER_BEGIN(SpecParser)
68 package edu.uci.eecs.specCompiler.grammerParser;
69
70 import java.io.FileInputStream;
71 import java.io.FileNotFoundException;
72
73 import edu.uci.eecs.specCompiler.specExtraction.Construct;
74 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
75 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
76 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
77 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
78 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
79 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
80
81         public class SpecParser {
82                 public static void main(String[] argvs)
83                 throws ParseException, TokenMgrError {
84                         try {
85                                 FileInputStream fis = new FileInputStream("./grammer/spec.txt");
86                                 SpecParser parser = new SpecParser(fis);
87                                 parser.Start();
88                                 System.out.println("Parsing finished!");
89                         } catch (FileNotFoundException e) {
90                                 e.printStackTrace();
91                         }
92                 }
93         }
94 PARSER_END(SpecParser)
95
96 SKIP :
97 {
98         " "
99 |
100         "\n"
101 |
102         "\r"
103 |
104         "\r\n"
105 |
106         "\t"
107 |
108         // "#" comment for the specification
109         <"#" (~["\n", "\r"])* (["\n", "\r"])>
110 |
111         // "//" comment for the specification
112         <"//" (~["\n", "\r"])* (["\n", "\r"])>
113 }
114
115 TOKEN :
116 {
117 /*   Above are specification-only tokens   */
118         <HEAD: "/**">
119 |
120         <TAIL: "*/">
121 |
122         <BEGIN: "@Begin">
123 |
124         <END: "@End">
125 |
126         <GLOBAL_DEFINE: "@Global_define:">
127 |
128         <INTERFACE_CLUSTER: "@Interface_cluster:">
129 |
130         <HAPPENS_BEFORE: "@Happens_before:">
131 |
132         <INTERFACE: "@Interface:">
133 |
134         <COMMIT_POINT_SET: "@Commit_point_set:">
135 |
136         <CONDITION: "@Condition:">
137 |
138         <HB_CONDITION: "@HB_condition:">
139 |
140         <ID: "@ID:">
141 |
142         <CHECK: "@Check:">
143 |
144         <ACTION: "@Action:">
145 |
146         <POST_ACTION: "@Post_action:">
147 |
148         <POST_CHECK: "@Post_check:">
149 |
150         <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
151 |
152         <LABEL: "@Label:">
153 |
154         <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
155 |
156         <COMMIT_POINT_DEFINE: "@Commit_point_define:">
157 |
158         <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
159
160
161 /*   Specification & C/C++ shared tokens   */
162 |
163         <#DIGIT: ["0"-"9"]>
164 |
165         <#LETTER: ["a"-"z", "A"-"Z"]>
166 |
167         <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
168 |
169         <EQUALS: "=">
170 |
171         <OPEN_PAREN: "{">
172 |
173         <CLOSE_PAREN: "}">
174 |
175         <OPEN_BRACKET: "(">
176 |
177         <CLOSE_BRACKET: ")">
178 |
179         <HB_SYMBOL: "->">
180 |
181         <COMMA: ",">
182
183 |
184 /*   C/C++ only token*/
185         <DOT: ".">
186 |
187         <STAR: "*">
188 |
189         <NEGATE: "~">
190 |
191         <AND: "&">
192 |
193         <OR: "|">
194 |
195         <MOD: "%">
196 |
197         <PLUS: "+">
198 |
199         <PLUSPLUS: "++">
200 |
201         <MINUS: "-">
202 |
203         <MINUSMINUS: "--">
204 |
205         <DIVIDE: "/">
206 |
207         <BACKSLASH: "\\">
208 |
209         <LESS_THAN: "<">
210 |
211         <GREATER_THAN: ">">
212 |
213         <GREATER_EQUALS: ">=">
214 |
215         <LESS_EQUALS: "<=">
216 |
217         <LOGICAL_EQUALS: "==">
218 |
219         <NOT_EQUALS: "!=">
220 |
221         <LOGICAL_AND: "&&">
222 |
223         <LOGICAL_OR: "||">
224 |
225         <XOR: "^">
226 |
227         <QUESTION_MARK: "?">
228 |
229         <COLON: ":">
230 |
231         <DOUBLECOLON: "::">
232 |
233         <SEMI_COLON: ";">
234 |
235         <STRING_LITERAL:
236         "\""
237         ((~["\"","\\","\n","\r"])
238         | ("\\"
239                 ( ["n","t","b","r","f","\\","'","\""]
240                 | ["0"-"7"] ( ["0"-"7"] )?
241                 | ["0"-"3"] ["0"-"7"]
242                         ["0"-"7"]
243                 )
244                 )
245         )*
246         "\"">
247 |
248         <CHARACTER_LITERAL:
249         "'"
250         ((~["'","\\","\n","\r"])
251         | ("\\"
252                 (["n","t","b","r","f","\\","'","\""]
253                 | ["0"-"7"] ( ["0"-"7"] )?
254                 | ["0"-"3"] ["0"-"7"]
255                 ["0"-"7"]
256                 )
257                 )
258         )
259         "'">
260 |
261         < INTEGER_LITERAL:
262         <DECIMAL_LITERAL> (["l","L"])?
263       | <HEX_LITERAL> (["l","L"])?
264       | <OCTAL_LITERAL> (["l","L"])?>
265 |
266         < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
267 |
268         < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
269 |
270         < #OCTAL_LITERAL: "0" (["0"-"7"])* >
271 |
272         < FLOATING_POINT_LITERAL:
273         <DECIMAL_FLOATING_POINT_LITERAL>
274       | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
275 |
276         < #DECIMAL_FLOATING_POINT_LITERAL:
277         (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
278       | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
279       | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
280       | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
281 |
282         < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
283 |
284         < #HEXADECIMAL_FLOATING_POINT_LITERAL:
285         "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
286       | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
287 |
288         < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
289 }
290
291 Construct Start() :
292 {
293         Construct res;  
294 }
295 {
296         (
297         LOOKAHEAD(3) res = Global_construct() |
298         LOOKAHEAD(3) res = Interface() |
299         LOOKAHEAD(3) res = Potential_commit_point_define() |
300         LOOKAHEAD(3) res = Commit_point_define() |
301         LOOKAHEAD(3) res = Commit_point_define_check()
302         )
303         <EOF>
304         { return res; }
305 }
306
307 GlobalConstruct Global_construct() :
308 {
309         GlobalConstruct res;
310         String code;
311 }
312 {
313         { res = null; }
314         <HEAD>
315                 <BEGIN> 
316                         (code = Global_define())
317                         { res = new GlobalConstruct(code); }
318                         (Interface_clusters(res))?
319                         (Happens_before(res))?
320                 <END>
321         <TAIL>
322         {
323                 res.unfoldInterfaceCluster();
324                 System.out.println(res);
325                 return res;
326         }
327 }
328
329 String C_CPP_CODE() :
330 {
331         StringBuilder text;
332         Token t;
333 }
334 {
335         {
336                 text = new StringBuilder();
337                 t = new Token();
338         }
339         ((
340         t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
341         t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
342         t = <DOT> | t = <STAR> | t = <NEGATE> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
343         t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
344         t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS>     | t = <LESS_EQUALS> |
345         t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR>
346         t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
347         t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
348         t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
349         )
350         {
351                 text.append(t.image);
352                 if (t.image.equals(";") || t.image.equals("\\")
353                         || t.image.equals("{") || t.image.equals("}"))
354                         text.append("\n");
355                 else
356                         text.append(" ");
357         }
358         )+
359         {
360                 //System.out.println(text);
361                 return text.toString();
362         }
363 }
364
365 String Global_define() :
366 {
367         String code;    
368 }
369 {
370         <GLOBAL_DEFINE> (code = C_CPP_CODE())
371         {
372                 return code;
373         }
374 }
375
376 ConditionalInterface Conditional_interface() :
377 {
378         String interfaceName, hbConditionLabel;
379 }
380 {
381         {
382                 hbConditionLabel = "";
383         }
384         interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
385         <IDENTIFIER>.image <CLOSE_BRACKET>)?
386         {
387                 return new ConditionalInterface(interfaceName, hbConditionLabel);
388         }
389 }
390
391 void Interface_cluster(GlobalConstruct inst) :
392 {
393         String clusterName;
394         ConditionalInterface condInterface;
395 }
396 {
397         (clusterName= <IDENTIFIER>.image)
398         <EQUALS> <OPEN_PAREN>
399                 (condInterface = Conditional_interface()
400                 { inst.addInterface2Cluster(clusterName, condInterface); } 
401                 )
402                 (<COMMA> condInterface = Conditional_interface()
403                 { inst.addInterface2Cluster(clusterName, condInterface); } 
404                 )*
405         <CLOSE_PAREN>
406 }
407
408 void Interface_clusters(GlobalConstruct inst) :
409 {}
410 {
411         <INTERFACE_CLUSTER> (Interface_cluster(inst))+
412 }
413
414 void Happens_before(GlobalConstruct inst) :
415 {
416         ConditionalInterface left, right;       
417 }
418 {
419         <HAPPENS_BEFORE> 
420         (
421         left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
422         { inst.addHBCondition(left, right); }
423         )+
424 }
425
426 InterfaceConstruct Interface() :
427 {
428         InterfaceConstruct res; 
429 }
430 {
431         { res = null; }
432         <HEAD> 
433                 <BEGIN>
434                         <INTERFACE> <IDENTIFIER>
435                         <COMMIT_POINT_SET> <IDENTIFIER> (<OR> <IDENTIFIER>)*
436                         (<CONDITION> C_CPP_CODE())?
437                         (<HB_CONDITION> <IDENTIFIER> C_CPP_CODE())*
438                         (<ID> C_CPP_CODE())?
439                         (<CHECK> C_CPP_CODE())?
440                         (<ACTION> C_CPP_CODE())?
441                         (<POST_ACTION> C_CPP_CODE())?
442                         (<POST_CHECK> C_CPP_CODE())?
443                 <END>
444         <TAIL>
445         { return res; }
446 }
447
448 PotentialCPDefineConstruct Potential_commit_point_define() :
449 {
450         PotentialCPDefineConstruct res; 
451 }
452 {
453
454         { res = null; }
455         <HEAD> 
456                 <BEGIN>
457                         <POTENTIAL_COMMIT_POINT_DEFINE> C_CPP_CODE()
458                         <LABEL> <IDENTIFIER>
459                 <END>
460         <TAIL>
461         { return res; }
462 }
463
464
465 CPDefineConstruct Commit_point_define() :
466 {
467         CPDefineConstruct res;  
468 }
469 {
470
471         { res = null; }
472         <HEAD> 
473                 <BEGIN>
474                         <COMMIT_POINT_DEFINE> C_CPP_CODE()
475                         <POTENTIAL_COMMIT_POINT_LABEL> <IDENTIFIER>
476                         <LABEL> <IDENTIFIER>
477                 <END>
478         <TAIL>
479         { return res; }
480 }
481
482
483 CPDefineCheckConstruct Commit_point_define_check() :
484 {
485         CPDefineCheckConstruct res;     
486 }
487 {
488
489         { res = null; }
490         <HEAD> 
491                 <BEGIN> 
492                         <COMMIT_POINT_DEFINE_CHECK> C_CPP_CODE()
493                         <LABEL> <IDENTIFIER>
494                 <END>
495         <TAIL>
496         { return res; }
497 }