Factory 1.5 Grammar
expressionTag
: ( constTag
| varTag
| applyTag
| getTag
)
;
constTag
: CONST_OPEN
( CHAR_LITERAL
| STRING_LITERAL
| NUM_INT
| NUM_FLOAT
| NUM_LONG
| NUM_DOUBLE
| "true"
| "false"
| identifier
)
CONST_CLOSE
;
/** accesses a generator variable **/
varTag
: VAR_OPEN IDENT VAR_CLOSE
;
/** application of methods and factories **/
applyTag
: APPLY_OPEN
( expressionTag methodTag
| classTag
( methodTag
|
)
| factoryTag
)
( argsTag
|
)
APPLY_CLOSE
;
/** read access to fields in green list **/
getTag
: GET_OPEN
( expressionTag
| classTag
)
fieldTag GET_CLOSE
;
identifier
: IDENT ( DOT IDENT )*
;
methodTag
: METHOD_OPEN IDENT METHOD_CLOSE
;
classTag
: CLASS_OPEN identifier CLASS_CLOSE
;
factoryTag
: FACTORY_OPEN factoryIdent FACTORY_CLOSE
;
argsTag
: ARGS_OPEN ( expressionTag )+ ARGS_CLOSE
;
fieldTag
: FIELD_OPEN IDENT FIELD_CLOSE
;
factoryIdent
: IDENT ( DIV IDENT )*
;
factoryDefinition
: ( paramTag )*
( paramListTag
|
)
( packageDefinition
|
)
( importDefinition )* factoryModifiers
( classDefinition
| interfaceDefinition
)
EOF
;
paramTag
: PARAM_OPEN
( boundTag
|
)
varTag PARAM_CLOSE
;
paramListTag
: PARAMLIST_OPEN
( boundTag
|
)
varTag PARAMLIST_CLOSE
;
packageDefinition
: "package" factoryPackage SEMI
;
importDefinition
: "import" identifierStar SEMI
;
factoryModifiers
: ( modifiers
| expressionTag
)
;
classDefinition
: "class" factoryName superClassClause implementsClause classBlock
;
interfaceDefinition
: "interface" factoryName interfaceExtends classBlock
;
boundTag
: BOUND_OPEN identifier BOUND_CLOSE
;
fieldForTag
: FOR_OPEN varTag expressionTag BODY_OPEN fieldTagBody BODY_CLOSE FOR_CLOSE
;
fieldTagBody
: ( field )*
;
statementForTag
: FOR_OPEN varTag expressionTag BODY_OPEN statementTagBody BODY_CLOSE FOR_CLOSE
;
statementTagBody
: ( statement )*
;
fieldIfTag
: IF_OPEN expressionTag fieldThenTag
( fieldElseTag
|
)
IF_CLOSE
;
fieldThenTag
: THEN_OPEN fieldTagBody THEN_CLOSE
;
fieldElseTag
: ELSE_OPEN fieldTagBody ELSE_CLOSE
;
statementIfTag
: IF_OPEN expressionTag statementThenTag
( statementElseTag
|
)
IF_CLOSE
;
statementThenTag
: THEN_OPEN statementTagBody THEN_CLOSE
;
statementElseTag
: ELSE_OPEN statementTagBody ELSE_CLOSE
;
fieldLetTag
: LET_OPEN varTag expressionTag BODY_OPEN fieldTagBody BODY_CLOSE LET_CLOSE
;
statementLetTag
: LET_OPEN varTag expressionTag BODY_OPEN statementTagBody BODY_CLOSE LET_CLOSE
;
field
: "static" compoundStatement
| compoundStatement
| SEMI
| fieldIfTag
| fieldForTag
| fieldLetTag
| factoryMethodHeadDecl
| declarationField
;
statement
: compoundStatement
| declaration SEMI
| expression SEMI
| factoryModifiers classDefinition
| IDENT COLON statement
| "if" LPAREN expression RPAREN statement
( "else" statement
|
)
| "for" LPAREN forInit SEMI forCond SEMI forIter RPAREN statement
| "while" LPAREN expression RPAREN statement
| "do" statement "while" LPAREN expression RPAREN SEMI
| "break"
( IDENT
|
)
SEMI
| "continue"
( IDENT
|
)
SEMI
| "return"
( expression
|
)
SEMI
| "switch" LPAREN expression RPAREN LCURLY ( casesGroup )* RCURLY
| tryBlock
| "throw" expression SEMI
| "synchronized" LPAREN expression RPAREN compoundStatement
| SEMI
| statementForTag
| statementIfTag
| statementLetTag
;
factoryType
: identifier
| expressionTag
;
factoryName
: ( IDENT
| expressionTag
)
;
factoryPackage
: identifier
| expressionTag
;
modifiers
: ( modifier )*
;
factoryLiteral
: ( CHAR_LITERAL
| STRING_LITERAL
| NUM_INT
| NUM_FLOAT
| NUM_LONG
| NUM_DOUBLE
| LITERAL_OPEN expressionTag LITERAL_CLOSE
)
;
/** A factory method-head is always a generated one.
But the grammar allows normal java method-heads, too. **/
factoryMethodHead
: expressionTag
;
factoryMethodParameters
: ( LPAREN parameterDeclarationList RPAREN
| expressionTag
)
;
parameterDeclarationList
: ( parameterDeclaration ( COMMA parameterDeclaration )*
|
)
;
/** A factory argument list is always a generated one.
But the grammar allows normal java argument lists, too. **/
factoryMethodArguments
: expressionTag
;
identifierStar
: IDENT ( DOT IDENT )*
( DOT STAR
|
)
;
/** A declaration is the creation of a reference or primitive-type variable
* Create a separate Type/Var tree for each var in the var list.
*/
declaration
: factoryModifiers typeSpec variableDefinitions
;
typeSpec
: classTypeSpec
| builtInTypeSpec
;
variableDefinitions
: variableDeclarator ( COMMA variableDeclarator )*
;
classTypeSpec
: factoryType ( LBRACK RBRACK )*
;
builtInTypeSpec
: builtInType ( LBRACK RBRACK )*
;
builtInType
: "void"
| "boolean"
| "byte"
| "char"
| "short"
| "int"
| "float"
| "long"
| "double"
;
type
: factoryType
| builtInType
;
modifier
: ( "private"
| "public"
| "protected"
| "static"
| "transient"
| "final"
| "abstract"
| "native"
| "threadsafe"
| "synchronized"
| "volatile"
| "strictfp"
)
;
superClassClause
: ( "extends" factoryType
|
)
;
implementsClause
: ( "implements" factoryType ( COMMA factoryType )*
|
)
;
classBlock
: LCURLY ( field )* RCURLY
;
interfaceExtends
: ( "extends" factoryType ( COMMA factoryType )*
|
)
;
compoundStatement
: LCURLY ( statement )* RCURLY
;
factoryMethodHeadDecl
: factoryMethodHead
( compoundStatement
| SEMI
)
;
/** method, constructor, or variable declaration **/
declarationField
: factoryModifiers
( ctorHead constructorBody
| classDefinition
| interfaceDefinition
| methodOrVarDecl
)
;
ctorHead
: factoryName LPAREN parameterDeclarationList RPAREN
( throwsClause
|
)
;
constructorBody
: LCURLY
( explicitConstructorInvocation
|
)
( statement )* RCURLY
;
methodOrVarDecl
: typeSpec
( ( variableDefinitions SEMI )
| factoryName factoryMethodParameters declaratorBrackets
( throwsClause
|
)
( compoundStatement
| SEMI
)
)
;
declaratorBrackets
: ( LBRACK RBRACK )*
;
throwsClause
: "throws" factoryType ( COMMA factoryType )*
;
/** Declaration of a variable. This can be a class/instance variable,
* or a local variable in a method
* It can also include possible initialization.
*/
variableDeclarator
: factoryName declaratorBrackets varInitializer
;
varInitializer
: ( ASSIGN initializer
|
)
;
initializer
: expression
| arrayInitializer
;
arrayInitializer
: LCURLY
( initializer ( COMMA initializer )*
( COMMA
|
)
|
)
RCURLY
;
expression
: assignmentExpression
;
/** Catch obvious constructor calls, but not the expr.super(...) calls */
explicitConstructorInvocation
: "this" LPAREN argList RPAREN SEMI
| "super" LPAREN argList RPAREN SEMI
;
argList
: ( expressionList
|
)
;
parameterDeclaration
: parameterModifier typeSpec IDENT declaratorBrackets
;
parameterModifier
: ( "final"
|
)
;
forInit
: ( declaration
| expressionList
|
)
;
forCond
: ( expression
|
)
;
forIter
: ( expressionList
|
)
;
casesGroup
: ( aCase )+ caseSList
;
tryBlock
: "try" compoundStatement ( handler )*
( finallyClause
|
)
;
aCase
: ( "case" expression
| "default"
)
COLON
;
caseSList
: ( statement )*
;
expressionList
: expression ( COMMA expression )*
;
handler
: "catch" LPAREN parameterDeclaration RPAREN compoundStatement
;
finallyClause
: "finally" compoundStatement
;
assignmentExpression
: conditionalExpression
( ( ASSIGN
| PLUS_ASSIGN
| MINUS_ASSIGN
| STAR_ASSIGN
| DIV_ASSIGN
| MOD_ASSIGN
| SR_ASSIGN
| BSR_ASSIGN
| SL_ASSIGN
| BAND_ASSIGN
| BXOR_ASSIGN
| BOR_ASSIGN
)
assignmentExpression
|
)
;
conditionalExpression
: logicalOrExpression
( QUESTION assignmentExpression COLON conditionalExpression
|
)
;
logicalOrExpression
: logicalAndExpression ( LOR logicalAndExpression )*
;
logicalAndExpression
: inclusiveOrExpression ( LAND inclusiveOrExpression )*
;
inclusiveOrExpression
: exclusiveOrExpression ( BOR exclusiveOrExpression )*
;
exclusiveOrExpression
: andExpression ( BXOR andExpression )*
;
andExpression
: equalityExpression ( BAND equalityExpression )*
;
equalityExpression
: relationalExpression ( ( NOT_EQUAL
| EQUAL
)
relationalExpression )*
;
relationalExpression
: shiftExpression
( ( ( LT
| GT
| LE
| GE
)
shiftExpression )*
| "instanceof" typeSpec
)
;
shiftExpression
: additiveExpression ( ( SL
| SR
| BSR
)
additiveExpression )*
;
additiveExpression
: multiplicativeExpression ( ( PLUS
| MINUS
)
multiplicativeExpression )*
;
multiplicativeExpression
: unaryExpression ( ( STAR
| DIV
| MOD
)
unaryExpression )*
;
unaryExpression
: INC unaryExpression
| DEC unaryExpression
| MINUS unaryExpression
| PLUS unaryExpression
| unaryExpressionNotPlusMinus
;
unaryExpressionNotPlusMinus
: BNOT unaryExpression
| LNOT unaryExpression
| ( LPAREN builtInTypeSpec RPAREN unaryExpression
| LPAREN classTypeSpec RPAREN unaryExpressionNotPlusMinus
| postfixExpression
)
;
postfixExpression
: primaryExpression
( DOT factoryName
( ( LPAREN argList RPAREN
| factoryMethodArguments
)
|
)
| DOT "this"
| DOT "super"
( LPAREN argList RPAREN
| DOT factoryName
( ( LPAREN argList RPAREN
| factoryMethodArguments
)
|
)
)
| DOT newExpression
| LBRACK expression RBRACK
)*
( INC
| DEC
|
)
;
/** The basic element of an expression
Needs the syntactic predicates
(identPrimary)=>
and
(constant)=>
in order to solve an ambiguity caused by constant
and identPrimary possibly beginning with LT IDENT.
**/
primaryExpression
: identPrimary
( DOT "class"
|
)
| constant
| "true"
| "false"
| "null"
| newExpression
| "this"
| "super"
| LPAREN assignmentExpression RPAREN
| builtInType ( LBRACK RBRACK )* DOT "class"
;
/** object instantiation.
* Trees are built as illustrated by the following input/tree pairs:
*
* new T()
*
* new
* |
* T -- ELIST
* |
* arg1 -- arg2 -- .. -- argn
*
* new int[]
*
* new
* |
* int -- ARRAY_DECLARATOR
*
* new int[] {1,2}
*
* new
* |
* int -- ARRAY_DECLARATOR -- ARRAY_INIT
* |
* EXPR -- EXPR
* | |
* 1 2
*
* new int[3]
* new
* |
* int -- ARRAY_DECLARATOR
* |
* EXPR
* |
* 3
*
* new int[1][2]
*
* new
* |
* int -- ARRAY_DECLARATOR
* |
* ARRAY_DECLARATOR -- EXPR
* | |
* EXPR 1
* |
* 2
*
*/
newExpression
: "new" type
( LPAREN argList RPAREN
( classBlock
|
)
| newArrayDeclarator
( arrayInitializer
|
)
)
;
/** Match a, a.b.c refs, a.b.c(...) refs, a.b.c[], a.b.c[].class,
* and a.b.c.class refs. Also this(...) and super(...). Match
* this or super.
*/
identPrimary
: factoryName ( DOT factoryName )*
( ( LPAREN argList RPAREN
| factoryMethodArguments
)
| ( LBRACK RBRACK )+
|
)
;
constant
: factoryLiteral
;
newArrayDeclarator
: ( LBRACK
( expression
|
)
RBRACK )+
;