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