- options {
- language="Cpp";
- }
- /** Java 1.1 AST Recognizer Grammar
- *
- * Author:
- * Terence Parr parrt@magelang.com
- *
- * This grammar is in the PUBLIC DOMAIN
- *
- * BUGS
- */
- class JavaTreeParser extends TreeParser;
- options {
- importVocab=Java;
- }
- compilationUnit
- : (packageDefinition)?
- (importDefinition)*
- (typeDefinition)*
- ;
- packageDefinition
- : #( PACKAGE_DEF identifier )
- ;
- importDefinition
- : #( IMPORT identifierStar )
- ;
- typeDefinition
- : #(CLASS_DEF modifiers IDENT extendsClause implementsClause objBlock )
- | #(INTERFACE_DEF modifiers )
- ;
- typeSpec
- : #(TYPE typeSpecArray)
- ;
- typeSpecArray
- : #( ARRAY_DECLARATOR typeSpecArray )
- | type
- ;
- type: identifier
- | builtInType
- ;
- builtInType
- : LITERAL_void
- | LITERAL_boolean
- | LITERAL_byte
- | LITERAL_char
- | LITERAL_short
- | LITERAL_int
- | LITERAL_float
- | LITERAL_long
- | LITERAL_double
- ;
- modifiers
- : #( MODIFIERS (modifier)* )
- ;
- modifier
- : LITERAL_private
- | LITERAL_public
- | LITERAL_protected
- | LITERAL_static
- | LITERAL_transient
- | LITERAL_final
- | LITERAL_abstract
- | LITERAL_native
- | LITERAL_threadsafe
- | LITERAL_synchronized
- | LITERAL_const
- | LITERAL_volatile
- ;
- extendsClause
- : #(EXTENDS_CLAUSE (identifier)* )
- ;
- implementsClause
- : #(IMPLEMENTS_CLAUSE (identifier)* )
- ;
- objBlock
- : #( OBJBLOCK
- ( ctorDef
- | methodDef
- | variableDef
- | typeDefinition
- | #(STATIC_INIT slist)
- | #(INSTANCE_INIT slist)
- )*
- )
- ;
- ctorDef
- : #(CTOR_DEF modifiers methodHead slist)
- ;
- methodDef
- : #(METHOD_DEF modifiers typeSpec methodHead (slist)?)
- ;
- variableDef
- : #(VARIABLE_DEF modifiers typeSpec variableDeclarator varInitializer)
- ;
- parameterDef
- : #(PARAMETER_DEF modifiers typeSpec IDENT )
- ;
- objectinitializer
- : #(INSTANCE_INIT slist)
- ;
- variableDeclarator
- : IDENT
- | LBRACK variableDeclarator
- ;
- varInitializer
- : #(ASSIGN initializer)
- |
- ;
- initializer
- : expression
- | arrayInitializer
- ;
- arrayInitializer
- : #(ARRAY_INIT (initializer)+)
- ;
- methodHead
- : IDENT #( PARAMETERS (parameterDef)* ) (throwsClause)?
- ;
- throwsClause
- : #( LITERAL_throws (identifier)* )
- ;
- identifier
- : IDENT
- | #( DOT identifier IDENT )
- ;
- identifierStar
- : IDENT
- | #( DOT identifier (STAR|IDENT) )
- ;
- slist
- : #( SLIST (stat)* )
- ;
- stat: typeDefinition
- | variableDef
- | expression
- | #(LABELED_STAT IDENT stat)
- | #("if" expression stat)
- | #( "for"
- #(FOR_INIT (variableDef | elist)?)
- #(FOR_CONDITION (expression)?)
- #(FOR_ITERATOR (elist)?)
- stat
- )
- | #("while" expression stat)
- | #("do" stat expression)
- | #("break" (IDENT)? )
- | #("continue" (IDENT)? )
- | #("return" (expression)? )
- | #("switch" expression (caseGroup)*)
- | #("throw" expression)
- | #("synchronized" stat)
- | tryBlock
- | slist // nested SLIST
- | EMPTY_STAT
- ;
- caseGroup
- : #(CASE_GROUP (#("case" expression) | "default")+ slist)
- ;
- tryBlock
- : #( "try" slist (handler)* (#("finally" slist))? )
- ;
- handler
- : #( "catch" parameterDef slist )
- ;
- elist
- : #( ELIST (expression)* )
- ;
- expression
- : #(EXPR expr)
- ;
- expr: #(QUESTION expr expr expr) // trinary operator
- | #(ASSIGN expr expr) // binary operators...
- | #(PLUS_ASSIGN expr expr)
- | #(MINUS_ASSIGN expr expr)
- | #(STAR_ASSIGN expr expr)
- | #(DIV_ASSIGN expr expr)
- | #(MOD_ASSIGN expr expr)
- | #(SR_ASSIGN expr expr)
- | #(BSR_ASSIGN expr expr)
- | #(SL_ASSIGN expr expr)
- | #(BAND_ASSIGN expr expr)
- | #(BXOR_ASSIGN expr expr)
- | #(BOR_ASSIGN expr expr)
- | #(LOR expr expr)
- | #(LAND expr expr)
- | #(BOR expr expr)
- | #(BXOR expr expr)
- | #(BAND expr expr)
- | #(NOT_EQUAL expr expr)
- | #(EQUAL expr expr)
- | #(LT_ expr expr)
- | #(GT expr expr)
- | #(LE expr expr)
- | #(GE expr expr)
- | #(SL expr expr)
- | #(SR expr expr)
- | #(BSR expr expr)
- | #(PLUS expr expr)
- | #(MINUS expr expr)
- | #(DIV expr expr)
- | #(MOD expr expr)
- | #(STAR expr expr)
- | #(INC expr)
- | #(DEC expr)
- | #(POST_INC expr)
- | #(POST_DEC expr)
- | #(BNOT expr)
- | #(LNOT expr)
- | #(LITERAL_instanceof expr expr)
- | #(UNARY_MINUS expr)
- | #(UNARY_PLUS expr)
- | primaryExpression
- ;
- primaryExpression
- : IDENT
- | #( DOT
- ( primaryExpression
- ( IDENT
- | arrayIndex
- | LITERAL_this
- | LITERAL_class
- | #( "new" IDENT elist )
- )
- | #(ARRAY_DECLARATOR builtInType)
- | builtInType ("class")?
- )
- )
- | arrayIndex
- | #(METHOD_CALL primaryExpression elist)
- | #(TYPECAST typeSpec expr)
- | newExpression
- | constant
- | LITERAL_super
- | LITERAL_true
- | LITERAL_false
- | LITERAL_this
- | LITERAL_null
- | typeSpec // type name used with instanceof
- ;
- arrayIndex
- : #(INDEX_OP primaryExpression expression)
- ;
- constant
- : NUM_INT
- | CHAR_LITERAL
- | STRING_LITERAL
- | NUM_FLOAT
- ;
- newExpression
- : #( "new" type
- ( newArrayDeclarator (arrayInitializer)?
- | elist
- )
- )
- ;
- newArrayDeclarator
- : #( ARRAY_DECLARATOR (newArrayDeclarator)? (expression)? )
- ;