Merge branch 'charm' of charmgit:charm into charm
[charm.git] / src / langs / jade / java.tree1.g
1 header {
2 //header
3 //Pass1:
4 // build global syntax table
5 // mark if class is a mainchare
6 package jade;
7 }
8
9 {
10 //class preamble
11 import jade.JJ.J;
12 import jade.JJ.ASTJ;
13 }
14
15 /** Java 1.3 AST Recognizer Grammar
16  *
17  * Author: (see java.g preamble)
18  * Author: J. DeSouza
19  *
20  */
21 class JavaTreeParser1 extends TreeParser;
22
23 options {
24         importVocab = Java;
25     ASTLabelType = "ASTJ";
26 }
27
28 compilationUnit
29         :       (p:packageDefinition)?
30                 (importDefinition)*
31                 (typeDefinition[p])*
32         { if (p != null)
33             J.tmp.pop();
34         }
35         ;
36
37 packageDefinition
38         :       #( PACKAGE_DEF i:identifier { J.tmp.push(J.pE(i)); })
39         ;
40
41 importDefinition
42         :       #( IMPORT identifierStar )
43         ;
44
45 typeDefinition[AST parent]
46         :       #(c:CLASS_DEF modifiers IDENT { J.tmp.push(#IDENT.getText()); } extendsClause implementsClause
47             o:objBlock {
48 //                 if ( ((ASTJ)o).hasMain() ) {
49 //                     ((ASTJ)c).status = true; // is a mainchare
50 //                     ((ASTJ)parent).status = true; // is a mainmodule
51 //                 }
52             }
53         ) { J.tmp.pop(); }
54         |       #(INTERFACE_DEF modifiers IDENT extendsClause interfaceBlock )
55         ;
56
57 typeSpec
58         :       #(TYPE typeSpecArray)
59         ;
60
61 typeSpecArray
62         :       #( ARRAY_DECLARATOR typeSpecArray )
63     |   #( TEMPLATE typeSpecArray )
64         |       type
65         ;
66
67 type:   identifier
68         |       builtInType
69         ;
70
71 builtInType
72     :   "void"
73     |   "boolean"
74     |   "byte"
75     |   "char"
76     |   "short"
77     |   "int"
78     |   "float"
79     |   "long"
80     |   "double"
81     ;
82
83 modifiers
84         :       #( MODIFIERS (modifier)* )
85         ;
86
87 modifier
88     :   "private"
89     |   "public"
90     |   "protected"
91     |   "static"
92     |   "transient"
93     |   "final"
94     |   "abstract"
95     |   "native"
96     |   "threadsafe"
97     |   "synchronized"
98 //     |   "const"
99     |   "volatile"
100         |       "strictfp"
101         |       "threaded"
102         |       "blocking"
103         |       "readonly"
104     ;
105
106 extendsClause
107         :       #(EXTENDS_CLAUSE (identifier)* )
108         ;
109
110 implementsClause
111         :       #(IMPLEMENTS_CLAUSE (identifier)* )
112         ;
113
114 interfaceBlock
115         :       #(      OBJBLOCK
116                         (       methodDecl
117                         |       variableDef
118                         )*
119                 )
120         ;
121
122 objBlock
123         :       #(      OBJBLOCK
124                         (       ctorDef
125                         |       methodDef
126                         |       variableDef
127                         |       typeDefinition[null]
128                         |       #(STATIC_INIT slist)
129                         |       #(INSTANCE_INIT slist)
130                         )*
131                 )
132         ;
133
134 ctorDef
135         :       #(CTOR_DEF modifiers methodHead {J.tmp.push("");} ctorSList {J.tmp.pop();})
136         ;
137
138 methodDecl
139         :       #(METHOD_DEF modifiers typeSpec methodHead)
140         ;
141
142 methodDef
143         :       #(METHOD_DEF modifiers typeSpec mh:methodHead
144             {
145                 J.globalStackPush(new String(J.fullName(mh.getText())), #METHOD_DEF);
146                 J.tmp.push(new String(mh.getText()));
147             }
148             (slist)? { J.tmp.pop(); })
149         ;
150
151 variableDef
152         :       #(VARIABLE_DEF m:modifiers typeSpec 
153             v:variableDeclarator {
154                 //System.out.println("pass1: " + v.getText() + J.tmp + "\n");
155                 if (J.tmp.size() == 2) {
156                     J.globalStackPush(new String(J.fullName(v.getText())), #VARIABLE_DEF);
157                 } else {
158                     // should print this only for public static final
159                     if ( ((ASTJ)m).isX("static") )
160                       J.warning(v, "static variable " + v.getText() + " not accessible outside class.");
161                 }
162             }
163             varInitializer)
164         ;
165
166 parameterDef
167         :       #(PARAMETER_DEF modifiers typeSpec IDENT )
168         ;
169
170 objectinitializer
171         :       #(INSTANCE_INIT slist)
172         ;
173
174 variableDeclarator
175         :       IDENT
176         |       LBRACK variableDeclarator
177         ;
178
179 varInitializer
180         :       #(ASSIGN initializer)
181         |
182         ;
183
184 initializer
185         :       expression
186         |       arrayInitializer
187         ;
188
189 arrayInitializer
190         :       #(ARRAY_INIT (initializer)*)
191         ;
192
193 methodHead
194         :       IDENT #( PARAMETERS (parameterDef)* ) (throwsClause)?
195         ;
196
197 throwsClause
198         :       #( "throws" (identifier)* )
199         ;
200
201 templater
202     :  #( TEMPLATE (identifier|constant)+ )
203     ;
204
205 identifier
206         :       #( IDENT (templater)? )
207         |       #( DOT identifier #( IDENT (templater)? ) )
208         ;
209
210 identifierStar
211         :       IDENT
212         |       #( DOT identifier (STAR|IDENT) )
213         ;
214
215 ctorSList
216         :       #( SLIST (ctorCall)? (stat)* )
217         ;
218
219 slist
220         :       #( SLIST (stat)* )
221         ;
222
223 stat:   typeDefinition[null]
224         |       variableDef
225         |       expression
226         |       #(LABELED_STAT IDENT stat)
227         |       #("if" expression stat (stat)? )
228         |       #(      "for"
229                         #(FOR_INIT (variableDef | elist)?)
230                         #(FOR_CONDITION (expression)?)
231                         #(FOR_ITERATOR (elist)?)
232                         stat
233                 )
234         |       #("while" expression stat)
235         |       #("do" stat expression)
236         |       #("break" (IDENT)? )
237         |       #("continue" (IDENT)? )
238         |       #("return" (expression)? )
239         |       #("switch" expression (caseGroup)*)
240         |       #("throw" expression)
241         |       #("synchronized" expression stat)
242         |       tryBlock
243         |       slist // nested SLIST
244         |       EMPTY_STAT
245         ;
246
247 caseGroup
248         :       #(CASE_GROUP (#("case" expression) | "default")+ slist)
249         ;
250
251 tryBlock
252         :       #( "try" slist (handler)* (#("finally" slist))? )
253         ;
254
255 handler
256         :       #( "catch" parameterDef slist )
257         ;
258
259 elist
260         :       #( ELIST (expression)* )
261         ;
262
263 colonExpression
264     :   #(COLON expression expression (expression)?)
265     |   expression
266     ;
267
268 expression
269         :       #(EXPR expr)
270         ;
271
272 expr:   #(QUESTION expr expr expr)      // trinary operator
273         |       #(ASSIGN expr expr)                     // binary operators...
274         |       #(PLUS_ASSIGN expr expr)
275         |       #(MINUS_ASSIGN expr expr)
276         |       #(STAR_ASSIGN expr expr)
277         |       #(DIV_ASSIGN expr expr)
278         |       #(MOD_ASSIGN expr expr)
279         |       #(SR_ASSIGN expr expr)
280         |       #(BSR_ASSIGN expr expr)
281         |       #(SL_ASSIGN expr expr)
282         |       #(BAND_ASSIGN expr expr)
283         |       #(BXOR_ASSIGN expr expr)
284         |       #(BOR_ASSIGN expr expr)
285         |       #(LOR expr expr)
286         |       #(LAND expr expr)
287         |       #(BOR expr expr)
288         |       #(BXOR expr expr)
289         |       #(BAND expr expr)
290         |       #(NOT_EQUAL expr expr)
291         |       #(EQUAL expr expr)
292         |       #(LT expr expr)
293         |       #(GT expr expr)
294         |       #(LE expr expr)
295         |       #(GE expr expr)
296         |       #(SL expr expr)
297         |       #(SR expr expr)
298         |       #(BSR expr expr)
299         |       #(PLUS expr expr)
300         |       #(MINUS expr expr)
301         |       #(DIV expr expr)
302         |       #(MOD expr expr)
303         |       #(STAR expr expr)
304         |       #(INC expr)
305         |       #(DEC expr)
306         |       #(POST_INC expr)
307         |       #(POST_DEC expr)
308         |       #(BNOT expr)
309         |       #(LNOT expr)
310         |       #("instanceof" expr expr)
311         |       #(UNARY_MINUS expr)
312         |       #(UNARY_PLUS expr)
313         |       primaryExpression
314         ;
315
316 primaryExpression
317     :   IDENT
318     |   #(      DOT
319                         (       expr
320                                 (       IDENT
321                                 |       arrayIndex
322                                 |       "this"
323                                 |       "class"
324                                 |       #( "new" IDENT elist )
325                                 |   "super"
326                                 )
327                         |       #(ARRAY_DECLARATOR typeSpecArray)
328                         |       builtInType ("class")?
329                         )
330                 )
331         |       arrayIndex
332         |       #(METHOD_CALL primaryExpression elist)
333         |       #(TYPECAST typeSpec expr)
334         |   newExpression
335         |   constant
336     |   "super"
337     |   "true"
338     |   "false"
339     |   "this"
340     |   "null"
341         |       typeSpec // type name used with instanceof
342         ;
343
344 ctorCall
345         :       #( CTOR_CALL elist )
346         |       #( SUPER_CTOR_CALL
347                         (       elist
348                         |       primaryExpression elist
349                         )
350                  )
351         ;
352
353 arrayIndex
354         :       #(INDEX_OP primaryExpression colonExpression)
355         ;
356
357 constant
358     :   NUM_INT
359     |   CHAR_LITERAL
360     |   STRING_LITERAL
361     |   NUM_FLOAT
362     |   NUM_DOUBLE
363     |   NUM_LONG
364     ;
365
366 newExpression
367         :       #(      "new" type
368                         (       newArrayDeclarator (arrayInitializer)?
369                         |       elist (objBlock)?
370                         )
371                 )
372                         
373         ;
374
375 newArrayDeclarator
376         :       #( ARRAY_DECLARATOR (newArrayDeclarator)? (expression)? )
377         ;