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