1 | /** Java 1.2 AST Recognizer Grammar
|
---|
2 | *
|
---|
3 | * Author:
|
---|
4 | * Terence Parr [email protected]
|
---|
5 | *
|
---|
6 | * Version tracking now done with following ID:
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This grammar is in the PUBLIC DOMAIN
|
---|
10 | *
|
---|
11 | * BUGS
|
---|
12 | */
|
---|
13 | class JavaTreeParser extends TreeParser;
|
---|
14 |
|
---|
15 | options {
|
---|
16 | importVocab = Java;
|
---|
17 | }
|
---|
18 |
|
---|
19 | compilationUnit
|
---|
20 | : (packageDefinition)?
|
---|
21 | (importDefinition)*
|
---|
22 | (typeDefinition)*
|
---|
23 | ;
|
---|
24 |
|
---|
25 | packageDefinition
|
---|
26 | : #( PACKAGE_DEF identifier )
|
---|
27 | ;
|
---|
28 |
|
---|
29 | importDefinition
|
---|
30 | : #( IMPORT identifierStar )
|
---|
31 | ;
|
---|
32 |
|
---|
33 | typeDefinition
|
---|
34 | : #(CLASS_DEF modifiers IDENT extendsClause implementsClause objBlock )
|
---|
35 | | #(INTERFACE_DEF modifiers IDENT extendsClause interfaceBlock )
|
---|
36 | ;
|
---|
37 |
|
---|
38 | typeSpec
|
---|
39 | : #(TYPE typeSpecArray)
|
---|
40 | ;
|
---|
41 |
|
---|
42 | typeSpecArray
|
---|
43 | : #( ARRAY_DECLARATOR typeSpecArray )
|
---|
44 | | type
|
---|
45 | ;
|
---|
46 |
|
---|
47 | type: identifier
|
---|
48 | | builtInType
|
---|
49 | ;
|
---|
50 |
|
---|
51 | builtInType
|
---|
52 | : "void"
|
---|
53 | | "boolean"
|
---|
54 | | "byte"
|
---|
55 | | "char"
|
---|
56 | | "short"
|
---|
57 | | "int"
|
---|
58 | | "float"
|
---|
59 | | "long"
|
---|
60 | | "double"
|
---|
61 | ;
|
---|
62 |
|
---|
63 | modifiers
|
---|
64 | : #( MODIFIERS (modifier)* )
|
---|
65 | ;
|
---|
66 |
|
---|
67 | modifier
|
---|
68 | : "private"
|
---|
69 | | "public"
|
---|
70 | | "protected"
|
---|
71 | | "static"
|
---|
72 | | "transient"
|
---|
73 | | "final"
|
---|
74 | | "abstract"
|
---|
75 | | "native"
|
---|
76 | | "threadsafe"
|
---|
77 | | "synchronized"
|
---|
78 | | "const"
|
---|
79 | | "volatile"
|
---|
80 | ;
|
---|
81 |
|
---|
82 | extendsClause
|
---|
83 | : #(EXTENDS_CLAUSE (identifier)* )
|
---|
84 | ;
|
---|
85 |
|
---|
86 | implementsClause
|
---|
87 | : #(IMPLEMENTS_CLAUSE (identifier)* )
|
---|
88 | ;
|
---|
89 |
|
---|
90 |
|
---|
91 | interfaceBlock
|
---|
92 | : #( OBJBLOCK
|
---|
93 | ( methodDecl
|
---|
94 | | variableDef
|
---|
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 | | EMPTY_STAT
|
---|
198 | ;
|
---|
199 |
|
---|
200 | caseGroup
|
---|
201 | : #(CASE_GROUP (#("case" expression) | "default")+ slist)
|
---|
202 | ;
|
---|
203 |
|
---|
204 | tryBlock
|
---|
205 | : #( "try" slist (handler)* (#("finally" slist))? )
|
---|
206 | ;
|
---|
207 |
|
---|
208 | handler
|
---|
209 | : #( "catch" parameterDef slist )
|
---|
210 | ;
|
---|
211 |
|
---|
212 | elist
|
---|
213 | : #( ELIST (expression)* )
|
---|
214 | ;
|
---|
215 |
|
---|
216 | expression
|
---|
217 | : #(EXPR expr)
|
---|
218 | ;
|
---|
219 |
|
---|
220 | expr: #(QUESTION expr expr expr) // trinary operator
|
---|
221 | | #(ASSIGN expr expr) // binary operators...
|
---|
222 | | #(PLUS_ASSIGN expr expr)
|
---|
223 | | #(MINUS_ASSIGN expr expr)
|
---|
224 | | #(STAR_ASSIGN expr expr)
|
---|
225 | | #(DIV_ASSIGN expr expr)
|
---|
226 | | #(MOD_ASSIGN expr expr)
|
---|
227 | | #(SR_ASSIGN expr expr)
|
---|
228 | | #(BSR_ASSIGN expr expr)
|
---|
229 | | #(SL_ASSIGN expr expr)
|
---|
230 | | #(BAND_ASSIGN expr expr)
|
---|
231 | | #(BXOR_ASSIGN expr expr)
|
---|
232 | | #(BOR_ASSIGN expr expr)
|
---|
233 | | #(LOR expr expr)
|
---|
234 | | #(LAND expr expr)
|
---|
235 | | #(BOR expr expr)
|
---|
236 | | #(BXOR expr expr)
|
---|
237 | | #(BAND expr expr)
|
---|
238 | | #(NOT_EQUAL expr expr)
|
---|
239 | | #(EQUAL expr expr)
|
---|
240 | | #(LT expr expr)
|
---|
241 | | #(GT expr expr)
|
---|
242 | | #(LE expr expr)
|
---|
243 | | #(GE expr expr)
|
---|
244 | | #(SL expr expr)
|
---|
245 | | #(SR expr expr)
|
---|
246 | | #(BSR expr expr)
|
---|
247 | | #(PLUS expr expr)
|
---|
248 | | #(MINUS expr expr)
|
---|
249 | | #(DIV expr expr)
|
---|
250 | | #(MOD expr expr)
|
---|
251 | | #(STAR expr expr)
|
---|
252 | | #(INC expr)
|
---|
253 | | #(DEC expr)
|
---|
254 | | #(POST_INC expr)
|
---|
255 | | #(POST_DEC expr)
|
---|
256 | | #(BNOT expr)
|
---|
257 | | #(LNOT expr)
|
---|
258 | | #("instanceof" expr expr)
|
---|
259 | | #(UNARY_MINUS expr)
|
---|
260 | | #(UNARY_PLUS expr)
|
---|
261 | | primaryExpression
|
---|
262 | ;
|
---|
263 |
|
---|
264 | primaryExpression
|
---|
265 | : IDENT
|
---|
266 | | #( DOT
|
---|
267 | ( expr
|
---|
268 | ( IDENT
|
---|
269 | | arrayIndex
|
---|
270 | | "this"
|
---|
271 | | "class"
|
---|
272 | | #( "new" IDENT elist )
|
---|
273 | )
|
---|
274 | | #(ARRAY_DECLARATOR type)
|
---|
275 | | builtInType ("class")?
|
---|
276 | )
|
---|
277 | )
|
---|
278 | | arrayIndex
|
---|
279 | | #(METHOD_CALL primaryExpression elist)
|
---|
280 | | #(TYPECAST typeSpec expr)
|
---|
281 | | newExpression
|
---|
282 | | constant
|
---|
283 | | "super"
|
---|
284 | | "true"
|
---|
285 | | "false"
|
---|
286 | | "this"
|
---|
287 | | "null"
|
---|
288 | | typeSpec // type name used with instanceof
|
---|
289 | ;
|
---|
290 |
|
---|
291 | arrayIndex
|
---|
292 | : #(INDEX_OP primaryExpression expression)
|
---|
293 | ;
|
---|
294 |
|
---|
295 | constant
|
---|
296 | : NUM_INT
|
---|
297 | | CHAR_LITERAL
|
---|
298 | | STRING_LITERAL
|
---|
299 | | NUM_FLOAT
|
---|
300 | ;
|
---|
301 |
|
---|
302 | newExpression
|
---|
303 | : #( "new" type
|
---|
304 | ( newArrayDeclarator (arrayInitializer)?
|
---|
305 | | elist
|
---|
306 | )
|
---|
307 | )
|
---|
308 |
|
---|
309 | ;
|
---|
310 |
|
---|
311 | newArrayDeclarator
|
---|
312 | : #( ARRAY_DECLARATOR (newArrayDeclarator)? (expression)? )
|
---|
313 | ;
|
---|