PageRenderTime 62ms CodeModel.GetById 35ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/main/antlr/java15.tree.g

http://testability-explorer.googlecode.com/
Unknown | 481 lines | 408 code | 73 blank | 0 comment | 0 complexity | b77c4e292179b3c99fb56fce47ba8aaa MD5 | raw file
  1/* Java 1.5 AST Recognizer Grammar
  2 *
  3 * Author: (see java.g preamble)
  4 *
  5 * This grammar is in the PUBLIC DOMAIN
  6 */
  7
  8header {
  9package com.google.test.metric.javasrc;
 10
 11import java.util.List;
 12import java.util.ArrayList;
 13
 14import com.google.test.metric.Type;
 15import com.google.test.metric.JavaType;
 16
 17}
 18
 19class JavaTreeParser extends TreeParser;
 20
 21options {
 22	importVocab = Java;
 23}
 24
 25{
 26	CompilationUnitBuilder builder;
 27	Type ignore;
 28
 29	private String s(AST ast) {
 30	   return ast.getText();
 31	}
 32}
 33
 34compilationUnit
 35	:	(packageDefinition)?
 36		(importDefinition)*
 37		(typeDefinition)*
 38	;
 39
 40packageDefinition
 41	:	#( PACKAGE_DEF annotations pkgIdent:identifier )
 42	;
 43
 44importDefinition
 45	:	#( IMPORT identifierStar)
 46	|	#( STATIC_IMPORT identifierStar )
 47	;
 48
 49typeDefinition
 50{
 51    Type extendsType;
 52    List<Type> impls;
 53}
 54	:	#(CLASS_DEF m:modifiers i:IDENT
 55	    (ignore=typeParameters)? extendsType=extendsClause
 56	    impls=implementsClause {
 57	       builder.startType(i.getLine(), s(i), extendsType, impls);
 58	    }
 59	    objBlock ) {builder.endType();}
 60	|	#(INTERFACE_DEF modifiers IDENT (ignore=typeParameters)? extendsType=extendsClause interfaceBlock )
 61	|	#(ENUM_DEF modifiers IDENT impls=implementsClause enumBlock )
 62	|	#(ANNOTATION_DEF modifiers IDENT annotationBlock )
 63	;
 64
 65typeParameters returns [Type type = null;]
 66	:	#(TYPE_PARAMETERS (type=typeParameter)+)
 67	;
 68
 69typeParameter returns [Type type = null;]
 70	:	#(TYPE_PARAMETER IDENT (type=typeUpperBounds)?)
 71	;
 72
 73typeUpperBounds returns [Type type = null;]
 74	:	#(TYPE_UPPER_BOUNDS (type=classOrInterfaceType)+)
 75	;
 76
 77typeSpec returns [Type type = null;]
 78	:	#(TYPE type=typeSpecArray)
 79	;
 80
 81typeSpecArray returns [Type type = null;]
 82	:	#( ARRAY_DECLARATOR type=typeSpecArray )
 83	|	type=type
 84	;
 85
 86type returns [Type type = null;]
 87	:	type=classOrInterfaceType
 88	|	type=builtInType
 89	;
 90
 91classOrInterfaceType returns [Type info = null;]
 92{info = builder.toType(classOrInterfaceType_AST_in);}
 93	:	IDENT (ignore=typeArguments)?
 94	|	#( DOT ignore=classOrInterfaceType )
 95	;
 96
 97typeArguments returns [Type type = null;]
 98/*  :   #(TYPE_ARGUMENTS (type=type ignore=typeArgument)+) this seams to be wrong */
 99    :   #(TYPE_ARGUMENTS (ignore=typeArgument)+)
100	;
101
102typeArgument returns [Type type = null;]
103	:	#(	TYPE_ARGUMENT
104			(	type=typeSpec
105			|	type=wildcardType
106			)
107		)
108	;
109
110wildcardType returns [Type type = null;]
111	:	#(WILDCARD_TYPE (type=typeArgumentBounds)?)
112	;
113
114typeArgumentBounds returns [Type type = null;]
115	:	#(TYPE_UPPER_BOUNDS (type=classOrInterfaceType)+)
116	|	#(TYPE_LOWER_BOUNDS (type=classOrInterfaceType)+)
117	;
118
119builtInType returns [Type type = null;]
120	:	"void" {type = JavaType.VOID;}
121	|	"boolean" {type = JavaType.BOOLEAN;}
122	|	"byte" {type = JavaType.BYTE;}
123	|	"char" {type = JavaType.CHAR;}
124	|	"short" {type = JavaType.SHORT;}
125	|	"int" {type = JavaType.INT;}
126	|	"float" {type = JavaType.FLOAT;}
127	|	"long" {type = JavaType.LONG;}
128	|	"double" {type = JavaType.DOUBLE;}
129	;
130
131modifiers
132	:	#( MODIFIERS (modifier)* )
133	;
134
135modifier
136	:	"private"
137	|	"public"
138	|	"protected"
139	|	"static"
140	|	"transient"
141	|	"final"
142	|	"abstract"
143	|	"native"
144	|	"threadsafe"
145	|	"synchronized"
146	|	"const"
147	|	"volatile"
148	|	"strictfp"
149	|	annotation
150	;
151
152annotations
153	:	#(ANNOTATIONS (annotation)* )
154	;
155
156annotation
157	:	#(ANNOTATION identifier (annotationMemberValueInitializer | (anntotationMemberValuePair)+)? )
158	;
159
160annotationMemberValueInitializer
161	:	conditionalExpr | annotation | annotationMemberArrayInitializer
162	;
163
164anntotationMemberValuePair
165	:	#(ANNOTATION_MEMBER_VALUE_PAIR IDENT annotationMemberValueInitializer)
166	;
167
168annotationMemberArrayInitializer
169	:	#(ANNOTATION_ARRAY_INIT (annotationMemberArrayValueInitializer)* )
170	;
171
172annotationMemberArrayValueInitializer
173	:	conditionalExpr | annotation
174	;
175
176extendsClause returns [Type type=null]
177	:	#(EXTENDS_CLAUSE (type=classOrInterfaceType)* )
178	;
179
180implementsClause returns [List<Type> impls=new ArrayList<Type>();]
181{Type c;}
182	:	#(IMPLEMENTS_CLAUSE (c=classOrInterfaceType{impls.add(c);})* )
183	;
184
185
186interfaceBlock
187	:	#(	OBJBLOCK
188			(	methodDecl
189			|	variableDef
190			|	typeDefinition
191			)*
192		)
193	;
194
195objBlock
196	:	#(	OBJBLOCK
197			(	ctorDef
198			|	methodDef
199			|	variableDef
200			|	typeDefinition
201			|	#(STATIC_INIT slist)
202			|	#(INSTANCE_INIT slist)
203			)*
204		)
205	;
206
207annotationBlock
208	:	#(	OBJBLOCK
209			(	annotationFieldDecl
210			|	variableDef
211			|	typeDefinition
212			)*
213		)
214	;
215
216enumBlock
217	:	#(	OBJBLOCK
218			(
219				enumConstantDef
220			)*
221			(	ctorDef
222			|	methodDef
223			|	variableDef
224			|	typeDefinition
225			|	#(STATIC_INIT slist)
226			|	#(INSTANCE_INIT slist)
227			)*
228		)
229	;
230
231ctorDef
232	:	#(CTOR_DEF modifiers (ignore=typeParameters)? methodHead (slist)?)
233	;
234
235methodDecl
236	:	#(METHOD_DEF modifiers (ignore=typeParameters)? ignore=typeSpec methodHead)
237	;
238
239methodDef
240	:	#(METHOD_DEF modifiers (ignore=typeParameters)? ignore=typeSpec methodHead (slist)?)
241	;
242
243variableDef
244{Type type;}
245	:	#(VARIABLE_DEF m:modifiers type=typeSpec v:variableDeclarator i:varInitializer)
246	    {builder.type.addField(s(v), type, builder.visibility(m), builder.isStatic(m), builder.isFinal(m));}
247	;
248
249parameterDef
250	:	#(PARAMETER_DEF modifiers ignore=typeSpec IDENT )
251	;
252
253variableLengthParameterDef
254	:	#(VARIABLE_PARAMETER_DEF modifiers ignore=typeSpec IDENT )
255	;
256
257annotationFieldDecl
258	:	#(ANNOTATION_FIELD_DEF modifiers ignore=typeSpec IDENT (annotationMemberValueInitializer)?)
259	;
260
261enumConstantDef
262	:	#(ENUM_CONSTANT_DEF annotations IDENT (elist)? (enumConstantBlock)?)
263	;
264
265enumConstantBlock
266	:	#(	OBJBLOCK
267			(	methodDef
268			|	variableDef
269			|	typeDefinition
270			|	#(INSTANCE_INIT slist)
271			)*
272		)
273	;
274
275objectinitializer
276	:	#(INSTANCE_INIT slist)
277	;
278
279variableDeclarator
280	:	IDENT
281	|	LBRACK variableDeclarator
282	;
283
284varInitializer
285	:	#(ASSIGN initializer)
286	|
287	;
288
289initializer
290	:	expression
291	|	arrayInitializer
292	;
293
294arrayInitializer
295	:	#(ARRAY_INIT (initializer)*)
296	;
297
298methodHead
299	:	IDENT #( PARAMETERS (parameterDef)* ) (throwsClause)?
300	;
301
302throwsClause
303	:	#( "throws" (ignore=classOrInterfaceType)* )
304	;
305
306identifier
307	:	IDENT
308	|	#( DOT identifier IDENT )
309	;
310
311identifierStar
312	:	IDENT
313	|	#( DOT identifier (STAR|IDENT) )
314	;
315
316slist
317	:	#( SLIST (stat)* )
318	;
319
320stat:	typeDefinition
321	|	variableDef
322	|	expression
323	|	#(LABELED_STAT IDENT stat)
324	|	#("if" expression stat (stat)? )
325	|	#(	"for"
326			(
327				#(FOR_INIT ((variableDef)+ | elist)?)
328				#(FOR_CONDITION (expression)?)
329				#(FOR_ITERATOR (elist)?)
330			|
331				#(FOR_EACH_CLAUSE parameterDef expression)
332			)
333			stat
334		)
335	|	#("while" expression stat)
336	|	#("do" stat expression)
337	|	#("break" (IDENT)? )
338	|	#("continue" (IDENT)? )
339	|	#("return" (expression)? )
340	|	#("switch" expression (caseGroup)*)
341	|	#("throw" expression)
342	|	#("synchronized" expression stat)
343	|	tryBlock
344	|	slist // nested SLIST
345	|	#("assert" expression (expression)?)
346	|	EMPTY_STAT
347	;
348
349caseGroup
350	:	#(CASE_GROUP (#("case" expression) | "default")+ slist)
351	;
352
353tryBlock
354	:	#( "try" slist (handler)* (#("finally" slist))? )
355	;
356
357handler
358	:	#( "catch" parameterDef slist )
359	;
360
361elist
362	:	#( ELIST (expression)* )
363	;
364
365expression
366	:	#(EXPR expr)
367	;
368
369expr
370	:	conditionalExpr
371	|	#(ASSIGN expr expr)			// binary operators...
372	|	#(PLUS_ASSIGN expr expr)
373	|	#(MINUS_ASSIGN expr expr)
374	|	#(STAR_ASSIGN expr expr)
375	|	#(DIV_ASSIGN expr expr)
376	|	#(MOD_ASSIGN expr expr)
377	|	#(SR_ASSIGN expr expr)
378	|	#(BSR_ASSIGN expr expr)
379	|	#(SL_ASSIGN expr expr)
380	|	#(BAND_ASSIGN expr expr)
381	|	#(BXOR_ASSIGN expr expr)
382	|	#(BOR_ASSIGN expr expr)
383	;
384
385conditionalExpr
386	:	#(QUESTION expr expr expr)	// trinary operator
387	|	#(LOR expr expr)
388	|	#(LAND expr expr)
389	|	#(BOR expr expr)
390	|	#(BXOR expr expr)
391	|	#(BAND expr expr)
392	|	#(NOT_EQUAL expr expr)
393	|	#(EQUAL expr expr)
394	|	#(LT expr expr)
395	|	#(GT expr expr)
396	|	#(LE expr expr)
397	|	#(GE expr expr)
398	|	#(SL expr expr)
399	|	#(SR expr expr)
400	|	#(BSR expr expr)
401	|	#(PLUS expr expr)
402	|	#(MINUS expr expr)
403	|	#(DIV expr expr)
404	|	#(MOD expr expr)
405	|	#(STAR expr expr)
406	|	#(INC expr)
407	|	#(DEC expr)
408	|	#(POST_INC expr)
409	|	#(POST_DEC expr)
410	|	#(BNOT expr)
411	|	#(LNOT expr)
412	|	#("instanceof" expr expr)
413	|	#(UNARY_MINUS expr)
414	|	#(UNARY_PLUS expr)
415	|	primaryExpression
416	;
417
418primaryExpression
419	:	IDENT
420	|	#(	DOT
421			(	expr
422				(	IDENT
423				|	arrayIndex
424				|	"this"
425				|	"class"
426				|	newExpression
427				|	"super"
428				|	(ignore=typeArguments)? // for generic methods calls
429				)
430			|	#(ARRAY_DECLARATOR ignore=typeSpecArray)
431			|	ignore=builtInType ("class")?
432			)
433		)
434	|	arrayIndex
435	|	#(METHOD_CALL primaryExpression (ignore=typeArguments)? elist)
436	|	ctorCall
437	|	#(TYPECAST ignore=typeSpec expr)
438	|	newExpression
439	|	constant
440	|	"super"
441	|	"true"
442	|	"false"
443	|	"this"
444	|	"null"
445	|	ignore=typeSpec // type name used with instanceof
446	;
447
448ctorCall
449	:	#( CTOR_CALL elist )
450	|	#( SUPER_CTOR_CALL
451			(	elist
452			|	primaryExpression elist
453			)
454		 )
455	;
456
457arrayIndex
458	:	#(INDEX_OP expr expression)
459	;
460
461constant
462	:	NUM_INT
463	|	CHAR_LITERAL
464	|	STRING_LITERAL
465	|	NUM_FLOAT
466	|	NUM_DOUBLE
467	|	NUM_LONG
468	;
469
470newExpression
471	:	#(	"new" (ignore=typeArguments)? ignore=type
472			(	newArrayDeclarator (arrayInitializer)?
473			|	elist (objBlock)?
474			)
475		)
476
477	;
478
479newArrayDeclarator
480	:	#( ARRAY_DECLARATOR (newArrayDeclarator)? (expression)? )
481	;