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