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