4a7d11a871df83c963082c303ce71424f3ec624e
[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         class SpecParser {
74                 public static void main(String[] argvs)
75                 throws ParseException, TokenMgrError {
76                         try {
77                                 FileInputStream fis = new FileInputStream("./grammer/spec.txt");
78                                 SpecParser parser = new SpecParser(fis);
79                                 parser.Start();
80                                 System.out.println("Parsing finished!");
81                         } catch (FileNotFoundException e) {
82                                 e.printStackTrace();
83                         }
84                 }
85         }
86 PARSER_END(SpecParser)
87
88 SKIP :
89 {
90         " "
91 |
92         "\n"
93 |
94         "\r"
95 |
96         "\r\n"
97 |
98         "\t"
99 |
100         // "#" comment for the specification
101         <"#" (~["\n", "\r"])* (["\n", "\r"])>
102 |
103         // "//" comment for the specification
104         <"//" (~["\n", "\r"])* (["\n", "\r"])>
105 }
106
107 TOKEN :
108 {
109 /*   Above are specification-only tokens   */
110         <HEAD: "/**">
111 |
112         <TAIL: "*/">
113 |
114         <BEGIN: "@Begin">
115 |
116         <END: "@End">
117 |
118         <GLOBAL_DEFINE: "@Global_define:">
119 |
120         <INTERFACE_CLUSTER: "@Interface_cluster:">
121 |
122         <HAPPENS_BEFORE: "@Happens_before:">
123 |
124         <INTERFACE: "@Interface:">
125 |
126         <COMMIT_POINT_SET: "@Commit_point_set:">
127 |
128         <CONDITION: "@Condition:">
129 |
130         <HB_CONDITION: "@HB_condition:">
131 |
132         <ID: "@ID:">
133 |
134         <CHECK: "@Check:">
135 |
136         <ACTION: "@Action:">
137 |
138         <POST_ACTION: "@Post_action:">
139 |
140         <POST_CHECK: "@Post_check:">
141 |
142         <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
143 |
144         <LABEL: "@Label:">
145 |
146         <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
147 |
148         <COMMIT_POINT_DEFINE: "@Commit_point_define:">
149 |
150         <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
151
152
153 /*   Specification & C/C++ shared tokens   */
154 |
155         <#DIGIT: ["0"-"9"]>
156 |
157         <#LETTER: ["a"-"z", "A"-"Z"]>
158 |
159         <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
160 |
161         <EQUALS: "=">
162 |
163         <OPEN_PAREN: "{">
164 |
165         <CLOSE_PAREN: "}">
166 |
167         <OPEN_BRACKET: "(">
168 |
169         <CLOSE_BRACKET: ")">
170 |
171         <HB_SYMBOL: "->">
172 |
173         <COMMA: ",">
174
175 |
176 /*   C/C++ only token*/
177         <DOT: ".">
178 |
179         <STAR: "*">
180 |
181         <NEGATE: "~">
182 |
183         <AND: "&">
184 |
185         <OR: "|">
186 |
187         <MOD: "%">
188 |
189         <PLUS: "+">
190 |
191         <PLUSPLUS: "++">
192 |
193         <MINUS: "-">
194 |
195         <MINUSMINUS: "--">
196 |
197         <DIVIDE: "/">
198 |
199         <BACKSLASH: "\\">
200 |
201         <LESS_THAN: "<">
202 |
203         <GREATER_THAN: ">">
204 |
205         <GREATER_EQUALS: ">=">
206 |
207         <LESS_EQUALS: "<=">
208 |
209         <LOGICAL_EQUALS: "==">
210 |
211         <NOT_EQUALS: "!=">
212 |
213         <LOGICAL_AND: "&&">
214 |
215         <LOGICAL_OR: "||">
216 |
217         <XOR: "^">
218 |
219         <QUESTION_MARK: "?">
220 |
221         <COLON: ":">
222 |
223         <DOUBLECOLON: "::">
224 |
225         <SEMI_COLON: ";">
226 |
227         <STRING_LITERAL:
228         "\""
229         ((~["\"","\\","\n","\r"])
230         | ("\\"
231                 ( ["n","t","b","r","f","\\","'","\""]
232                 | ["0"-"7"] ( ["0"-"7"] )?
233                 | ["0"-"3"] ["0"-"7"]
234                         ["0"-"7"]
235                 )
236                 )
237         )*
238         "\"">
239 |
240         <CHARACTER_LITERAL:
241         "'"
242         ((~["'","\\","\n","\r"])
243         | ("\\"
244                 (["n","t","b","r","f","\\","'","\""]
245                 | ["0"-"7"] ( ["0"-"7"] )?
246                 | ["0"-"3"] ["0"-"7"]
247                 ["0"-"7"]
248                 )
249                 )
250         )
251         "'">
252 |
253         < INTEGER_LITERAL:
254         <DECIMAL_LITERAL> (["l","L"])?
255       | <HEX_LITERAL> (["l","L"])?
256       | <OCTAL_LITERAL> (["l","L"])?>
257 |
258         < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
259 |
260         < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
261 |
262         < #OCTAL_LITERAL: "0" (["0"-"7"])* >
263 |
264         < FLOATING_POINT_LITERAL:
265         <DECIMAL_FLOATING_POINT_LITERAL>
266       | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
267 |
268         < #DECIMAL_FLOATING_POINT_LITERAL:
269         (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
270       | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
271       | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
272       | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
273 |
274         < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
275 |
276         < #HEXADECIMAL_FLOATING_POINT_LITERAL:
277         "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
278       | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
279 |
280         < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
281 }
282
283 void Start() :
284 {}
285 {
286         (
287         LOOKAHEAD(3) Global_construct() |
288         LOOKAHEAD(3) Interface() |
289         LOOKAHEAD(3) Potential_commit_point_define() |
290         LOOKAHEAD(3) Commit_point_define() |
291         LOOKAHEAD(3) Commit_point_define_check()
292         )
293         <EOF>
294 }
295
296 void Global_construct() :
297 {
298 }
299 {
300         <HEAD>
301                 <BEGIN> 
302                         Global_define() (Interface_clusters())? (Happens_before())?
303                 <END>
304         <TAIL>
305 }
306
307 String C_CPP_CODE() :
308 {
309         StringBuilder text;
310         Token t;
311 }
312 {
313         {
314                 text = new StringBuilder();
315                 t = new Token();
316         }
317         ((
318         t = <IDENTIFIER> | t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
319         t = <OPEN_BRACKET> | t = <CLOSE_BRACKET> | t = <HB_SYMBOL> | t = <COMMA> |
320         t = <DOT> | t = <STAR> | t = <NEGATE> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
321         t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
322         t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS>     | t = <LESS_EQUALS> |
323         t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR>
324         t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
325         t = <SEMI_COLON> | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
326         t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL>
327         )
328         {
329                 text.append(t.image);
330                 if (t.image.equals(";") || t.image.equals("\\")
331                         || t.image.equals("{") || t.image.equals("}"))
332                         text.append("\n");
333                 else
334                         text.append(" ");
335         }
336         )+
337         {
338                 System.out.println(text);
339                 return text.toString();
340         }
341 }
342
343 void Global_define() :
344 {}
345 {
346         <GLOBAL_DEFINE> C_CPP_CODE()
347 }
348
349 void Conditional_interface() :
350 {}
351 {
352         <IDENTIFIER> (<OPEN_BRACKET> <IDENTIFIER> <CLOSE_BRACKET>)*
353 }
354
355 void Interface_cluster() :
356 {}
357 {
358         <IDENTIFIER> <EQUALS> <OPEN_PAREN>
359                 Conditional_interface() (<COMMA> Conditional_interface())*
360         <CLOSE_PAREN>
361 }
362
363 void Interface_clusters() :
364 {}
365 {
366         <INTERFACE_CLUSTER> (Interface_cluster())+
367 }
368
369 void Happens_before() :
370 {}
371 {
372         <HAPPENS_BEFORE> (Conditional_interface() <HB_SYMBOL> Conditional_interface())+
373 }
374
375 void Interface() :
376 {}
377 {
378         <HEAD> 
379                 <BEGIN>
380                         <INTERFACE> <IDENTIFIER>
381                         <COMMIT_POINT_SET> <IDENTIFIER> (<OR> <IDENTIFIER>)*
382                         (<CONDITION> C_CPP_CODE())?
383                         (<HB_CONDITION> <IDENTIFIER> C_CPP_CODE())*
384                         (<ID> C_CPP_CODE())?
385                         (<CHECK> C_CPP_CODE())?
386                         (<ACTION> C_CPP_CODE())?
387                         (<POST_ACTION> C_CPP_CODE())?
388                         (<POST_CHECK> C_CPP_CODE())?
389                 <END>
390         <TAIL>
391 }
392
393 void Potential_commit_point_define() :
394 {}
395 {
396         <HEAD> 
397                 <BEGIN>
398                         <POTENTIAL_COMMIT_POINT_DEFINE> C_CPP_CODE()
399                         <LABEL> <IDENTIFIER>
400                 <END>
401         <TAIL>
402 }
403
404
405 void Commit_point_define() :
406 {}
407 {
408         <HEAD> 
409                 <BEGIN>
410                         <COMMIT_POINT_DEFINE> C_CPP_CODE()
411                         <POTENTIAL_COMMIT_POINT_LABEL> <IDENTIFIER>
412                         <LABEL> <IDENTIFIER>
413                 <END>
414         <TAIL>
415 }
416
417
418 void Commit_point_define_check() :
419 {}
420 {
421         <HEAD> 
422                 <BEGIN> 
423                         <COMMIT_POINT_DEFINE_CHECK> C_CPP_CODE()
424                         <LABEL> <IDENTIFIER>
425                 <END>
426         <TAIL>
427 }