Untitled diff

建立於 差異永不過期
6 刪除
751
8 新增
755
/**
/**
* An ANTLRv3 capable Java 1.5 grammar for building ASTs.
* An ANTLRv3 capable Java 1.5 grammar for building ASTs.
*
*
* Note that there's also the tree grammar 'JavaTreeParser.g' that can be fed
* Note that there's also the tree grammar 'JavaTreeParser.g' that can be fed
* with this grammer's output.
* with this grammer's output.
*
*
*
*
* Please report any detected errors or even suggestions regarding this grammar
* Please report any detected errors or even suggestions regarding this grammar
* to
* to
*
*
* dieter [D O T] habelitz [A T] habelitz [D O T] com
* dieter [D O T] habelitz [A T] habelitz [D O T] com
*
*
* with the subject
* with the subject
*
*
* jsom grammar: [your subject note]
* jsom grammar: [your subject note]
*
*
* To generate a parser based on this grammar you'll need ANTLRv3, which you can
* To generate a parser based on this grammar you'll need ANTLRv3, which you can
* get from 'http://www.antlr.org'.
* get from 'http://www.antlr.org'.
*
*
*
*
* This grammar is published under the ...
* This grammar is published under the ...
*
*
* BSD licence
* BSD licence
*
*
* Copyright (c) 2007-2008 by HABELITZ Software Developments
* Copyright (c) 2007-2008 by HABELITZ Software Developments
*
*
* All rights reserved.
* All rights reserved.
*
*
* http://www.habelitz.com
* http://www.habelitz.com
*
*
*
*
* Redistribution and use in source and binary forms, with or without
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* modification, are permitted provided that the following conditions
* are met:
* are met:
*
*
* 1. Redistributions of source code must retain the above copyright
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY HABELITZ SOFTWARE DEVELOPMENTS ('HSD') ``AS IS''
* THIS SOFTWARE IS PROVIDED BY HABELITZ SOFTWARE DEVELOPMENTS ('HSD') ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL 'HSD' BE LIABLE FOR ANY DIRECT, INDIRECT,
* ARE DISCLAIMED. IN NO EVENT SHALL 'HSD' BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
*/
grammar Java;
grammar Java;
options {
options {
backtrack = true;
backtrack = true;
memoize = true;
memoize = true;
output = AST;
output = AST;
ASTLabelType = CommonTree;
ASTLabelType = CommonTree;
}
}
tokens {
tokens {
// operators and other special chars
// operators and other special chars
AND = '&' ;
AND = '&' ;
AND_ASSIGN = '&=' ;
AND_ASSIGN = '&=' ;
ASSIGN = '=' ;
ASSIGN = '=' ;
AT = '@' ;
AT = '@' ;
BIT_SHIFT_RIGHT = '>>>' ;
BIT_SHIFT_RIGHT = '>>>' ;
BIT_SHIFT_RIGHT_ASSIGN = '>>>=' ;
BIT_SHIFT_RIGHT_ASSIGN = '>>>=' ;
COLON = ':' ;
COLON = ':' ;
COMMA = ',' ;
COMMA = ',' ;
DEC = '--' ;
DEC = '--' ;
DIV = '/' ;
DIV = '/' ;
DIV_ASSIGN = '/=' ;
DIV_ASSIGN = '/=' ;
DOT = '.' ;
DOT = '.' ;
DOTSTAR = '.*' ;
DOTSTAR = '.*' ;
ELLIPSIS = '...' ;
ELLIPSIS = '...' ;
EQUAL = '==' ;
EQUAL = '==' ;
GREATER_OR_EQUAL = '>=' ;
GREATER_OR_EQUAL = '>=' ;
GREATER_THAN = '>' ;
GREATER_THAN = '>' ;
INC = '++' ;
INC = '++' ;
LBRACK = '[' ;
LBRACK = '[' ;
LCURLY = '{' ;
LCURLY = '{' ;
LESS_OR_EQUAL = '<=' ;
LESS_OR_EQUAL = '<=' ;
LESS_THAN = '<' ;
LESS_THAN = '<' ;
LOGICAL_AND = '&&' ;
LOGICAL_AND = '&&' ;
LOGICAL_NOT = '!' ;
LOGICAL_NOT = '!' ;
LOGICAL_OR = '||' ;
LOGICAL_OR = '||' ;
LPAREN = '(' ;
LPAREN = '(' ;
MINUS = '-' ;
MINUS = '-' ;
MINUS_ASSIGN = '-=' ;
MINUS_ASSIGN = '-=' ;
MOD = '%' ;
MOD = '%' ;
MOD_ASSIGN = '%=' ;
MOD_ASSIGN = '%=' ;
NOT = '~' ;
NOT = '~' ;
NOT_EQUAL = '!=' ;
NOT_EQUAL = '!=' ;
OR = '|' ;
OR = '|' ;
OR_ASSIGN = '|=' ;
OR_ASSIGN = '|=' ;
PLUS = '+' ;
PLUS = '+' ;
PLUS_ASSIGN = '+=' ;
PLUS_ASSIGN = '+=' ;
QUESTION = '?' ;
QUESTION = '?' ;
RBRACK = ']' ;
RBRACK = ']' ;
RCURLY = '}' ;
RCURLY = '}' ;
RPAREN = ')' ;
RPAREN = ')' ;
SEMI = ';' ;
SEMI = ';' ;
SHIFT_LEFT = '<<' ;
SHIFT_LEFT = '<<' ;
SHIFT_LEFT_ASSIGN = '<<=' ;
SHIFT_LEFT_ASSIGN = '<<=' ;
SHIFT_RIGHT = '>>' ;
SHIFT_RIGHT = '>>' ;
SHIFT_RIGHT_ASSIGN = '>>=' ;
SHIFT_RIGHT_ASSIGN = '>>=' ;
STAR = '*' ;
STAR = '*' ;
STAR_ASSIGN = '*=' ;
STAR_ASSIGN = '*=' ;
XOR = '^' ;
XOR = '^' ;
XOR_ASSIGN = '^=' ;
XOR_ASSIGN = '^=' ;
// keywords
// keywords
ABSTRACT = 'abstract' ;
ABSTRACT = 'abstract' ;
ASSERT = 'assert' ;
ASSERT = 'assert' ;
BOOLEAN = 'boolean' ;
BOOLEAN = 'boolean' ;
BREAK = 'break' ;
BREAK = 'break' ;
BYTE = 'byte' ;
BYTE = 'byte' ;
CASE = 'case' ;
CASE = 'case' ;
CATCH = 'catch' ;
CATCH = 'catch' ;
CHAR = 'char' ;
CHAR = 'char' ;
CLASS = 'class' ;
CLASS = 'class' ;
CONTINUE = 'continue' ;
CONTINUE = 'continue' ;
DEFAULT = 'default' ;
DEFAULT = 'default' ;
DO = 'do' ;
DO = 'do' ;
DOUBLE = 'double' ;
DOUBLE = 'double' ;
ELSE = 'else' ;
ELSE = 'else' ;
ENUM = 'enum' ;
ENUM = 'enum' ;
EXTENDS = 'extends' ;
EXTENDS = 'extends' ;
FALSE = 'false' ;
FALSE = 'false' ;
FINAL = 'final' ;
FINAL = 'final' ;
FINALLY = 'finally' ;
FINALLY = 'finally' ;
FLOAT = 'float' ;
FLOAT = 'float' ;
FOR = 'for' ;
FOR = 'for' ;
IF = 'if' ;
IF = 'if' ;
IMPLEMENTS = 'implements' ;
IMPLEMENTS = 'implements' ;
INSTANCEOF = 'instanceof' ;
INSTANCEOF = 'instanceof' ;
INTERFACE = 'interface' ;
INTERFACE = 'interface' ;
IMPORT = 'import' ;
IMPORT = 'import' ;
INT = 'int' ;
INT = 'int' ;
LONG = 'long' ;
LONG = 'long' ;
NATIVE = 'native' ;
NATIVE = 'native' ;
NEW = 'new' ;
NEW = 'new' ;
NULL = 'null' ;
NULL = 'null' ;
PACKAGE = 'package' ;
PACKAGE = 'package' ;
PRIVATE = 'private' ;
PRIVATE = 'private' ;
PROTECTED = 'protected' ;
PROTECTED = 'protected' ;
PUBLIC = 'public' ;
PUBLIC = 'public' ;
RETURN = 'return' ;
RETURN = 'return' ;
SHORT = 'short' ;
SHORT = 'short' ;
STATIC = 'static' ;
STATIC = 'static' ;
STRICTFP = 'strictfp' ;
STRICTFP = 'strictfp' ;
SUPER = 'super' ;
SUPER = 'super' ;
SWITCH = 'switch' ;
SWITCH = 'switch' ;
SYNCHRONIZED = 'synchronized' ;
SYNCHRONIZED = 'synchronized' ;
THIS = 'this' ;
THIS = 'this' ;
THROW = 'throw' ;
THROW = 'throw' ;
THROWS = 'throws' ;
THROWS = 'throws' ;
TRANSIENT = 'transient' ;
TRANSIENT = 'transient' ;
TRUE = 'true' ;
TRUE = 'true' ;
TRY = 'try' ;
TRY = 'try' ;
VOID = 'void' ;
VOID = 'void' ;
VOLATILE = 'volatile' ;
VOLATILE = 'volatile' ;
WHILE = 'while' ;
WHILE = 'while' ;
// tokens for imaginary nodes
// tokens for imaginary nodes
ANNOTATION_INIT_ARRAY_ELEMENT;
ANNOTATION_INIT_ARRAY_ELEMENT;
ANNOTATION_INIT_BLOCK;
ANNOTATION_INIT_BLOCK;
ANNOTATION_INIT_DEFAULT_KEY;
ANNOTATION_INIT_DEFAULT_KEY;
ANNOTATION_INIT_KEY_LIST;
ANNOTATION_INIT_KEY_LIST;
ANNOTATION_LIST;
ANNOTATION_LIST;
ANNOTATION_METHOD_DECL;
ANNOTATION_METHOD_DECL;
ANNOTATION_SCOPE;
ANNOTATION_SCOPE;
ANNOTATION_TOP_LEVEL_SCOPE;
ANNOTATION_TOP_LEVEL_SCOPE;
ARGUMENT_LIST;
ARGUMENT_LIST;
ARRAY_DECLARATOR;
ARRAY_DECLARATOR;
ARRAY_DECLARATOR_LIST;
ARRAY_DECLARATOR_LIST;
ARRAY_ELEMENT_ACCESS;
ARRAY_ELEMENT_ACCESS;
ARRAY_INITIALIZER;
ARRAY_INITIALIZER;
BLOCK_SCOPE;
BLOCK_SCOPE;
CAST_EXPR;
CAST_EXPR;
CATCH_CLAUSE_LIST;
CATCH_CLAUSE_LIST;
CLASS_CONSTRUCTOR_CALL;
CLASS_CONSTRUCTOR_CALL;
CLASS_INSTANCE_INITIALIZER;
CLASS_INSTANCE_INITIALIZER;
CLASS_STATIC_INITIALIZER;
CLASS_STATIC_INITIALIZER;
CLASS_TOP_LEVEL_SCOPE;
CLASS_TOP_LEVEL_SCOPE;
CONSTRUCTOR_DECL;
CONSTRUCTOR_DECL;
ENUM_TOP_LEVEL_SCOPE;
ENUM_TOP_LEVEL_SCOPE;
EXPR;
EXPR;
EXTENDS_BOUND_LIST;
EXTENDS_BOUND_LIST;
EXTENDS_CLAUSE;
EXTENDS_CLAUSE;
FOR_CONDITION;
FOR_CONDITION;
FOR_EACH;
FOR_EACH;
FOR_INIT;
FOR_INIT;
FOR_UPDATE;
FOR_UPDATE;
FORMAL_PARAM_LIST;
FORMAL_PARAM_LIST;
FORMAL_PARAM_STD_DECL;
FORMAL_PARAM_STD_DECL;
FORMAL_PARAM_VARARG_DECL;
FORMAL_PARAM_VARARG_DECL;
FUNCTION_METHOD_DECL;
FUNCTION_METHOD_DECL;
GENERIC_TYPE_ARG_LIST;
GENERIC_TYPE_ARG_LIST;
GENERIC_TYPE_PARAM_LIST;
GENERIC_TYPE_PARAM_LIST;
INTERFACE_TOP_LEVEL_SCOPE;
INTERFACE_TOP_LEVEL_SCOPE;
IMPLEMENTS_CLAUSE;
IMPLEMENTS_CLAUSE;
LABELED_STATEMENT;
LABELED_STATEMENT;
LOCAL_MODIFIER_LIST;
LOCAL_MODIFIER_LIST;
JAVA_SOURCE;
JAVA_SOURCE;
METHOD_CALL;
METHOD_CALL;
MODIFIER_LIST;
MODIFIER_LIST;
PARENTESIZED_EXPR;
PARENTESIZED_EXPR;
POST_DEC;
POST_DEC;
POST_INC;
POST_INC;
PRE_DEC;
PRE_DEC;
PRE_INC;
PRE_INC;
QUALIFIED_TYPE_IDENT;
QUALIFIED_TYPE_IDENT;
STATIC_ARRAY_CREATOR;
STATIC_ARRAY_CREATOR;
SUPER_CONSTRUCTOR_CALL;
SUPER_CONSTRUCTOR_CALL;
SWITCH_BLOCK_LABEL_LIST;
SWITCH_BLOCK_LABEL_LIST;
THIS_CONSTRUCTOR_CALL;
THIS_CONSTRUCTOR_CALL;
THROWS_CLAUSE;
THROWS_CLAUSE;
TYPE;
TYPE;
UNARY_MINUS;
UNARY_MINUS;
UNARY_PLUS;
UNARY_PLUS;
VAR_DECLARATION;
VAR_DECLARATION;
VAR_DECLARATOR;
VAR_DECLARATOR;
VAR_DECLARATOR_LIST;
VAR_DECLARATOR_LIST;
VOID_METHOD_DECL;
VOID_METHOD_DECL;
}
}
@header {
package com.habelitz.jsobjectizer.unmarshaller.antlrbridge.generated;
}
@members {
@members {
private boolean mMessageCollectionEnabled = false;
private boolean mMessageCollectionEnabled = false;
private boolean mHasErrors = false;
private boolean mHasErrors = false;
private List<String> mMessages;
private List<String> mMessages;
/**
/**
* Switches error message collection on or of.
* Switches error message collection on or of.
*
*
* The standard destination for parser error messages is <code>System.err</code>.
* The standard destination for parser error messages is <code>System.err</code>.
* However, if <code>true</code> gets passed to this method this default
* However, if <code>true</code> gets passed to this method this default
* behaviour will be switched off and all error messages will be collected
* behaviour will be switched off and all error messages will be collected
* instead of written to anywhere.
* instead of written to anywhere.
*
*
* The default value is <code>false</code>.
* The default value is <code>false</code>.
*
*
* @param pNewState <code>true</code> if error messages should be collected.
* @param pNewState <code>true</code> if error messages should be collected.
*/
*/
public void enableErrorMessageCollection(boolean pNewState) {
public void enableErrorMessageCollection(boolean pNewState) {
mMessageCollectionEnabled = pNewState;
mMessageCollectionEnabled = pNewState;
if (mMessages == null && mMessageCollectionEnabled) {
if (mMessages == null && mMessageCollectionEnabled) {
mMessages = new ArrayList<String>();
mMessages = new ArrayList<String>();
}
}
}
}
/**
/**
* Collects an error message or passes the error message to <code>
* Collects an error message or passes the error message to <code>
* super.emitErrorMessage(...)</code>.
* super.emitErrorMessage(...)</code>.
*
*
* The actual behaviour depends on whether collecting error messages
* The actual behaviour depends on whether collecting error messages
* has been enabled or not.
* has been enabled or not.
*
*
* @param pMessage The error message.
* @param pMessage The error message.
*/
*/
@Override
@Override
public void emitErrorMessage(String pMessage) {
public void emitErrorMessage(String pMessage) {
if (mMessageCollectionEnabled) {
if (mMessageCollectionEnabled) {
mMessages.add(pMessage);
mMessages.add(pMessage);
} else {
} else {
super.emitErrorMessage(pMessage);
super.emitErrorMessage(pMessage);
}
}
}
}
/**
/**
* Returns collected error messages.
* Returns collected error messages.
*
*
* @return A list holding collected error messages or <code>null</code> if
* @return A list holding collected error messages or <code>null</code> if
* collecting error messages hasn't been enabled. Of course, this
* collecting error messages hasn't been enabled. Of course, this
* list may be empty if no error message has been emited.
* list may be empty if no error message has been emited.
*/
*/
public List<String> getMessages() {
public List<String> getMessages() {
return mMessages;
return mMessages;
}
}
/**
/**
* Tells if parsing a Java source has caused any error messages.
* Tells if parsing a Java source has caused any error messages.
*
*
* @return <code>true</code> if parsing a Java source has caused at least one error message.
* @return <code>true</code> if parsing a Java source has caused at least one error message.
*/
*/
public boolean hasErrors() {
public boolean hasErrors() {
return mHasErrors;
return mHasErrors;
}
}
}
}
@lexer::header {
package com.habelitz.jsobjectizer.unmarshaller.antlrbridge.generated;
}
@lexer::members {
@lexer::members {
/**
/**
* Determines if whitespaces and comments should be preserved or thrown away.
* Determines if whitespaces and comments should be preserved or thrown away.
*
*
* If <code>true</code> whitespaces and comments will be preserved within the
* If <code>true</code> whitespaces and comments will be preserved within the
* hidden channel, otherwise the appropriate tokens will be skiped. This is
* hidden channel, otherwise the appropriate tokens will be skiped. This is
* a 'little bit' expensive, of course. If only one of the two behaviours is
* a 'little bit' expensive, of course. If only one of the two behaviours is
* needed forever the lexer part of the grammar should be changed by replacing
* needed forever the lexer part of the grammar should be changed by replacing
* the 'if-else' stuff within the approprate lexer grammar actions.
* the 'if-else' stuff within the approprate lexer grammar actions.
*/
*/
public boolean preserveWhitespacesAndComments = false;
public boolean preserveWhitespacesAndComments = false;
}
}
// Starting point for parsing a Java file.
// Starting point for parsing a Java file.
javaSource
javaSource
: compilationUnit
: compilationUnit
-> ^(JAVA_SOURCE compilationUnit)
-> ^(JAVA_SOURCE compilationUnit)
;
;
compilationUnit
compilationUnit
: annotationList
: annotationList
packageDeclaration?
packageDeclaration?
importDeclaration*
importDeclaration*
typeDecls*
typeDecls*
;
;
typeDecls
typeDecls
: typeDeclaration
: typeDeclaration
| SEMI!
| SEMI!
;
;
packageDeclaration
packageDeclaration
: PACKAGE^ qualifiedIdentifier SEMI!
: PACKAGE^ qualifiedIdentifier SEMI!
;
;
importDeclaration
importDeclaration
: IMPORT^ STATIC? qualifiedIdentifier DOTSTAR? SEMI!
: IMPORT^ STATIC? qualifiedIdentifier DOTSTAR? SEMI!
;
;
typeDeclaration
typeDeclaration
: modifierList!
: modifierList!
( classTypeDeclaration[$modifierList.tree]
( classTypeDeclaration[$modifierList.tree]
| interfaceTypeDeclaration[$modifierList.tree]
| interfaceTypeDeclaration[$modifierList.tree]
| enumTypeDeclaration[$modifierList.tree]
| enumTypeDeclaration[$modifierList.tree]
| annotationTypeDeclaration[$modifierList.tree]
| annotationTypeDeclaration[$modifierList.tree]
)
)
;
;
classTypeDeclaration[CommonTree modifiers]
classTypeDeclaration[CommonTree modifiers]
: CLASS IDENT genericTypeParameterList? classExtendsClause? implementsClause? classBody
: CLASS IDENT genericTypeParameterList? classExtendsClause? implementsClause? classBody
-> ^(CLASS {$modifiers} IDENT genericTypeParameterList? classExtendsClause? implementsClause? classBody)
-> ^(CLASS {$modifiers} IDENT genericTypeParameterList? classExtendsClause? implementsClause? classBody)
;
;
classExtendsClause
classExtendsClause
: EXTENDS type
: EXTENDS type
-> ^(EXTENDS_CLAUSE[$EXTENDS, "EXTENDS_CLAUSE"] type)
-> ^(EXTENDS_CLAUSE[$EXTENDS, "EXTENDS_CLAUSE"] type)
;
;
interfaceExtendsClause
interfaceExtendsClause
: EXTENDS typeList
: EXTENDS typeList
-> ^(EXTENDS_CLAUSE[$EXTENDS, "EXTENDS_CLAUSE"] typeList)
-> ^(EXTENDS_CLAUSE[$EXTENDS, "EXTENDS_CLAUSE"] typeList)
;
;
implementsClause
implementsClause
: IMPLEMENTS typeList
: IMPLEMENTS typeList
-> ^(IMPLEMENTS_CLAUSE[$IMPLEMENTS, "IMPLEMENTS_CLAUSE"] typeList)
-> ^(IMPLEMENTS_CLAUSE[$IMPLEMENTS, "IMPLEMENTS_CLAUSE"] typeList)
;
;
genericTypeParameterList
genericTypeParameterList
: LESS_THAN genericTypeParameter (COMMA genericTypeParameter)* genericTypeListClosing
: LESS_THAN genericTypeParameter (COMMA genericTypeParameter)* genericTypeListClosing
-> ^(GENERIC_TYPE_PARAM_LIST[$LESS_THAN, "GENERIC_TYPE_PARAM_LIST"] genericTypeParameter+)
-> ^(GENERIC_TYPE_PARAM_LIST[$LESS_THAN, "GENERIC_TYPE_PARAM_LIST"] genericTypeParameter+)
;
;
genericTypeListClosing // This 'trick' is fairly dirty - if there's some time a better solution should
genericTypeListClosing // This 'trick' is fairly dirty - if there's some time a better solution should
// be found to resolve the problem with nested generic type parameter lists
// be found to resolve the problem with nested generic type parameter lists
// (i.e. <T1 extends AnyType<T2>> for generic type parameters or <T1<T2>> for
// (i.e. <T1 extends AnyType<T2>> for generic type parameters or <T1<T2>> for
// generic type arguments etc).
// generic type arguments etc).
: GREATER_THAN
: GREATER_THAN
| SHIFT_RIGHT
| SHIFT_RIGHT
| BIT_SHIFT_RIGHT
| BIT_SHIFT_RIGHT
| // nothing
| // nothing
;
;
genericTypeParameter
genericTypeParameter
: IDENT bound?
: IDENT bound?
-> ^(IDENT bound?)
-> ^(IDENT bound?)
;
;
bound
bound
: EXTENDS type (AND type)*
: EXTENDS type (AND type)*
-> ^(EXTENDS_BOUND_LIST[$EXTENDS, "EXTENDS_BOUND_LIST"] type+)
-> ^(EXTENDS_BOUND_LIST[$EXTENDS, "EXTENDS_BOUND_LIST"] type+)
;
;
enumTypeDeclaration[CommonTree modifiers]
enumTypeDeclaration[CommonTree modifiers]
: ENUM IDENT implementsClause? enumBody
: ENUM IDENT implementsClause? enumBody
-> ^(ENUM {$modifiers} IDENT implementsClause? enumBody)
-> ^(ENUM {$modifiers} IDENT implementsClause? enumBody)
;
;
enumBody
enumBody
: LCURLY enumScopeDeclarations RCURLY
: LCURLY enumScopeDeclarations RCURLY
-> ^(ENUM_TOP_LEVEL_SCOPE[$LCURLY, "ENUM_TOP_LEVEL_SCOPE"] enumScopeDeclarations)
-> ^(ENUM_TOP_LEVEL_SCOPE[$LCURLY, "ENUM_TOP_LEVEL_SCOPE"] enumScopeDeclarations)
;
;
enumScopeDeclarations
enumScopeDeclarations
: enumConstants (COMMA!)? enumClassScopeDeclarations?
: enumConstants (COMMA!)? enumClassScopeDeclarations?
;
;
enumClassScopeDeclarations
enumClassScopeDeclarations
: SEMI classScopeDeclarations*
: SEMI classScopeDeclarations*
-> ^(CLASS_TOP_LEVEL_SCOPE[$SEMI, "CLASS_TOP_LEVEL_SCOPE"] classScopeDeclarations*)
-> ^(CLASS_TOP_LEVEL_SCOPE[$SEMI, "CLASS_TOP_LEVEL_SCOPE"] classScopeDeclarations*)
;
;
enumConstants
enumConstants
: enumConstant (COMMA! enumConstant)*
: enumConstant (COMMA! enumConstant)*
;
;
enumConstant
enumConstant
: annotationList IDENT^ arguments? classBody?
: annotationList IDENT^ arguments? classBody?
;
;
interfaceTypeDeclaration[CommonTree modifiers]
interfaceTypeDeclaration[CommonTree modifiers]
: INTERFACE IDENT genericTypeParameterList? interfaceExtendsClause? interfaceBody
: INTERFACE IDENT genericTypeParameterList? interfaceExtendsClause? interfaceBody
-> ^(INTERFACE {$modifiers} IDENT genericTypeParameterList? interfaceExtendsClause? interfaceBody)
-> ^(INTERFACE {$modifiers} IDENT genericTypeParameterList? interfaceExtendsClause? interfaceBody)
;
;
typeList
typeList
: type (COMMA! type)*
: type (COMMA! type)*
;
;
classBody
classBody
: LCURLY classScopeDeclarations* RCURLY
: LCURLY classScopeDeclarations* RCURLY
-> ^(CLASS_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] classScopeDeclarations*)
-> ^(CLASS_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] classScopeDeclarations*)
;
;
interfaceBody
interfaceBody
: LCURLY interfaceScopeDeclarations* RCURLY
: LCURLY interfaceScopeDeclarations* RCURLY
-> ^(INTERFACE_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] interfaceScopeDeclarations*)
-> ^(INTERFACE_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] interfaceScopeDeclarations*)
;
;
classScopeDeclarations
classScopeDeclarations
: block -> ^(CLASS_INSTANCE_INITIALIZER block)
: block -> ^(CLASS_INSTANCE_INITIALIZER block)
| STATIC block -> ^(CLASS_STATIC_INITIALIZER[$STATIC, "CLASS_STATIC_INITIALIZER"] block)
| STATIC block -> ^(CLASS_STATIC_INITIALIZER[$STATIC, "CLASS_STATIC_INITIALIZER"] block)
| modifierList
| modifierList
( genericTypeParameterList?
( genericTypeParameterList?
( type IDENT formalParameterList arrayDeclaratorList? throwsClause? (block | SEMI)
( type IDENT formalParameterList arrayDeclaratorList? throwsClause? (block | SEMI)
-> ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause? block?)
-> ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause? block?)
| VOID IDENT formalParameterList throwsClause? (block | SEMI)
| VOID IDENT formalParameterList throwsClause? (block | SEMI)
-> ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause? block?)
-> ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause? block?)
| ident=IDENT formalParameterList throwsClause? block
| ident=IDENT formalParameterList throwsClause? block
-> ^(CONSTRUCTOR_DECL[$ident, "CONSTRUCTOR_DECL"] modifierList genericTypeParameterList? formalParameterList throwsClause? block)
-> ^(CONSTRUCTOR_DECL[$ident, "CONSTRUCTOR_DECL"] modifierList genericTypeParameterList? formalParameterList throwsClause? block)
)
)
| type classFieldDeclaratorList SEMI
| type classFieldDeclaratorList SEMI
-> ^(VAR_DECLARATION modifierList type classFieldDeclaratorList)
-> ^(VAR_DECLARATION modifierList type classFieldDeclaratorList)
)
)
| typeDeclaration
| typeDeclaration
| SEMI!
| SEMI!
;
;
interfaceScopeDeclarations
interfaceScopeDeclarations
: modifierList
: modifierList
( genericTypeParameterList?
( genericTypeParameterList?
( type IDENT formalParameterList arrayDeclaratorList? throwsClause? SEMI
( type IDENT formalParameterList arrayDeclaratorList? throwsClause? SEMI
-> ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause?)
-> ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList? type IDENT formalParameterList arrayDeclaratorList? throwsClause?)
| VOID IDENT formalParameterList throwsClause? SEMI
| VOID IDENT formalParameterList throwsClause? SEMI
-> ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause?)
-> ^(VOID_METHOD_DECL modifierList genericTypeParameterList? IDENT formalParameterList throwsClause?)
)
)
| type interfaceFieldDeclaratorList SEMI
| type interfaceFieldDeclaratorList SEMI
-> ^(VAR_DECLARATION modifierList type interfaceFieldDeclaratorList)
-> ^(VAR_DECLARATION modifierList type interfaceFieldDeclaratorList)
)
)
| typeDeclaration
| typeDeclaration
| SEMI!
| SEMI!
;
;
classFieldDeclaratorList
classFieldDeclaratorList
: classFieldDeclarator (COMMA classFieldDeclarator)*
: classFieldDeclarator (COMMA classFieldDeclarator)*
-> ^(VAR_DECLARATOR_LIST classFieldDeclarator+)
-> ^(VAR_DECLARATOR_LIST classFieldDeclarator+)
;
;
classFieldDeclarator
classFieldDeclarator
: variableDeclaratorId (ASSIGN variableInitializer)?
: variableDeclaratorId (ASSIGN variableInitializer)?
-> ^(VAR_DECLARATOR variableDeclaratorId variableInitializer?)
-> ^(VAR_DECLARATOR variableDeclaratorId variableInitializer?)
;
;
interfaceFieldDeclaratorList
interfaceFieldDeclaratorList
: interfaceFieldDeclarator (COMMA interfaceFieldDeclarator)*
: interfaceFieldDeclarator (COMMA interfaceFieldDeclarator)*
-> ^(VAR_DECLARATOR_LIST interfaceFieldDeclarator+)
-> ^(VAR_DECLARATOR_LIST interfaceFieldDeclarator+)
;
;
interfaceFieldDeclarator
interfaceFieldDeclarator
: variableDeclaratorId ASSIGN variableInitializer
: variableDeclaratorId ASSIGN variableInitializer
-> ^(VAR_DECLARATOR variableDeclaratorId variableInitializer)
-> ^(VAR_DECLARATOR variableDeclaratorId variableInitializer)
;
;
variableDeclaratorId
variableDeclaratorId
: IDENT^ arrayDeclaratorList?
: IDENT^ arrayDeclaratorList?
;
;
variableInitializer
variableInitializer
: arrayInitializer
: arrayInitializer
| expression
| expression
;
;
arrayDeclarator
arrayDeclarator
: LBRACK RBRACK
: LBRACK RBRACK
-> ^(ARRAY_DECLARATOR)
-> ^(ARRAY_DECLARATOR)
;
;
arrayDeclaratorList
arrayDeclaratorList
: arrayDeclarator+
: arrayDeclarator+
-> ^(ARRAY_DECLARATOR_LIST arrayDeclarator+)
-> ^(ARRAY_DECLARATOR_LIST arrayDeclarator+)
;
;
arrayInitializer
arrayInitializer
: LCURLY (variableInitializer (COMMA variableInitializer)* COMMA?)? RCURLY
: LCURLY (variableInitializer (COMMA variableInitializer)* COMMA?)? RCURLY
-> ^(ARRAY_INITIALIZER[$LCURLY, "ARRAY_INITIALIZER"] variableInitializer*)
-> ^(ARRAY_INITIALIZER[$LCURLY, "ARRAY_INITIALIZER"] variableInitializer*)
;
;
throwsClause
throwsClause
: THROWS qualifiedIdentList
: THROWS qualifiedIdentList
-> ^(THROWS_CLAUSE[$THROWS, "THROWS_CLAUSE"] qualifiedIdentList)
-> ^(THROWS_CLAUSE[$THROWS, "THROWS_CLAUSE"] qualifiedIdentList)
;
;
modifierList
modifierList
: modifier*
: modifier*
-> ^(MODIFIER_LIST modifier*)
-> ^(MODIFIER_LIST modifier*)
;
;
modifier
modifier
: PUBLIC
: PUBLIC
| PROTECTED
| PROTECTED
| PRIVATE
| PRIVATE
| STATIC
| STATIC
| ABSTRACT
| ABSTRACT
| NATIVE
| NATIVE
| SYNCHRONIZED
| SYNCHRONIZED
| TRANSIENT
| TRANSIENT
| VOLATILE
| VOLATILE
| STRICTFP
| STRICTFP
| localModifier
| localModifier
;
;
localModifierList
localModifierList
: localModifier*
: localModifier*
-> ^(LOCAL_MODIFIER_LIST localModifier*)
-> ^(LOCAL_MODIFIER_LIST localModifier*)
;
;
localModifier
localModifier
: FINAL
: FINAL
| annotation
| annotation
;
;
type
type
: simpleType
: simpleType
| objectType
| objectType
;
;
simpleType // including static arrays of simple type elements
simpleType // including static arrays of simple type elements
: primitiveType arrayDeclaratorList?
: primitiveType arrayDeclaratorList?
-> ^(TYPE primitiveType arrayDeclaratorList?)
-> ^(TYPE primitiveType arrayDeclaratorList?)
;
;
objectType // including static arrays of object type reference elements
objectType // including static arrays of object type reference elements
: qualifiedTypeIdent arrayDeclaratorList?
: qualifiedTypeIdent arrayDeclaratorList?
-> ^(TYPE qualifiedTypeIdent arrayDeclaratorList?)
-> ^(TYPE qualifiedTypeIdent arrayDeclaratorList?)
;
;
objectTypeSimplified
objectTypeSimplified
: qualifiedTypeIdentSimplified arrayDeclaratorList?
: qualifiedTypeIdentSimplified arrayDeclaratorList?
-> ^(TYPE qualifiedTypeIdentSimplified arrayDeclaratorList?)
-> ^(TYPE qualifiedTypeIdentSimplified arrayDeclaratorList?)
;
;
qualifiedTypeIdent
qualifiedTypeIdent
: typeIdent (DOT typeIdent)*
: typeIdent (DOT typeIdent)*
-> ^(QUALIFIED_TYPE_IDENT typeIdent+)
-> ^(QUALIFIED_TYPE_IDENT typeIdent+)
;
;
qualifiedTypeIdentSimplified
qualifiedTypeIdentSimplified
: typeIdentSimplified (DOT typeIdentSimplified)*
: typeIdentSimplified (DOT typeIdentSimplified)*
-> ^(QUALIFIED_TYPE_IDENT typeIdentSimplified+)
-> ^(QUALIFIED_TYPE_IDENT typeIdentSimplified+)
;
;
typeIdent
typeIdent
: IDENT^ genericTypeArgumentList?
: IDENT^ genericTypeArgumentList?
;
;
typeIdentSimplified
typeIdentSimplified
: IDENT^ genericTypeArgumentListSimplified?
: IDENT^ genericTypeArgumentListSimplified?
;
;
primitiveType
primitiveType
: BOOLEAN
: BOOLEAN
| CHAR
| CHAR
| BYTE
| BYTE
| SHORT
| SHORT
| INT
| INT
| LONG
| LONG
| FLOAT
| FLOAT
| DOUBLE
| DOUBLE
;
;
genericTypeArgumentList
genericTypeArgumentList
: LESS_THAN genericTypeArgument (COMMA genericTypeArgument)* genericTypeListClosing
: LESS_THAN genericTypeArgument (COMMA genericTypeArgument)* genericTypeListClosing
-> ^(GENERIC_TYPE_ARG_LIST[$LESS_THAN, "GENERIC_TYPE_ARG_LIST"] genericTypeArgument+)
-> ^(GENERIC_TYPE_ARG_LIST[$LESS_THAN, "GENERIC_TYPE_ARG_LIST"] genericTypeArgument+)
;
;
genericTypeArgument
genericTypeArgument
: type
: type
| QUESTION genericWildcardBoundType?
| QUESTION genericWildcardBoundType?
-> ^(QUESTION genericWildcardBoundType?)
-> ^(QUESTION genericWildcardBoundType?)
;
;
genericWildcardBoundType
genericWildcardBoundType
: (EXTENDS | SUPER)^ type
: (EXTENDS | SUPER)^ type
;
;
genericTypeArgumentListSimplified
genericTypeArgumentListSimplified
: LESS_THAN genericTypeArgumentSimplified (COMMA genericTypeArgumentSimplified)* genericTypeListClosing
: LESS_THAN genericTypeArgumentSimplified (COMMA genericTypeArgumentSimplified)* genericTypeListClosing
-> ^(GENERIC_TYPE_ARG_LIST[$LESS_THAN, "GENERIC_TYPE_ARG_LIST"] genericTypeArgumentSimplified+)
-> ^(GENERIC_TYPE_ARG_LIST[$LESS_THAN, "GENERIC_TYPE_ARG_LIST"] genericTypeArgumentSimplified+)
;
;
genericTypeArgumentSimplified
genericTypeArgumentSimplified
: type
: type
| QUESTION
| QUESTION
;
;
qualifiedIdentList
qualifiedIdentList
: qualifiedIdentifier (COMMA! qualifiedIdentifier)*
: qualifiedIdentifier (COMMA! qualifiedIdentifier)*
;
;
formalParameterList
formalParameterList
: LPAREN
: LPAREN
( // Contains at least one standard argument declaration and optionally a variable argument declaration.
( // Contains at least one standard argument declaration and optionally a variable argument declaration.
formalParameterStandardDecl (COMMA formalParameterStandardDecl)* (COMMA formalParameterVarArgDecl)?
formalParameterStandardDecl (COMMA formalParameterStandardDecl)* (COMMA formalParameterVarArgDecl)?
-> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"] formalParameterStandardDecl+ formalParameterVarArgDecl?)
-> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"] formalParameterStandardDecl+ formalParameterVarArgDecl?)
// Contains a variable argument declaration only.
// Contains a variable argument declaration only.
| formalParameterVarArgDecl
| formalParameterVarArgDecl
-> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"] formalParameterVarArgDecl)
-> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"] formalParameterVarArgDecl)
// Contains nothing.
// Contains nothing.
| -> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"])
| -> ^(FORMAL_PARAM_LIST[$LPAREN, "FORMAL_PARAM_LIST"])
)
)
RPAREN
RPAREN
;
;
formalParameterStandardDecl
formalParameterStandardDecl
: localModifierList type variableDeclaratorId
: localModifierList type variableDeclaratorId
-> ^(FORMAL_PARAM_STD_DECL localModifierList type variableDeclaratorId)
-> ^(FORMAL_PARAM_STD_DECL localModifierList type variableDeclaratorId)
;
;
formalParameterVarArgDecl
formalParameterVarArgDecl
: localModifierList type ELLIPSIS variableDeclaratorId
: localModifierList type ELLIPSIS variableDeclaratorId
-> ^(FORMAL_PARAM_VARARG_DECL localModifierList type variableDeclaratorId)
-> ^(FORMAL_PARAM_VARARG_DECL localModifierList type variableDeclaratorId)
;
;
qualifiedIdentifier
qualifiedIdentifier
: ( IDENT -> IDENT
: ( IDENT -> IDENT
)
)
( DOT ident=IDENT -> ^(DOT $qualifiedIdentifier $ident)
( DOT ident=IDENT -> ^(DOT $qualifiedIdentifier $ident)
)*
)*
;
;
// ANNOTATIONS
// ANNOTATIONS
annotationList
annotationList
: annotation*
: annotation*
-> ^(ANNOTATION_LIST annotation*)
-> ^(ANNOTATION_LIST annotation*)
;
;
annotation
annotation
: AT^ qualifiedIdentifier annotationInit?
: AT^ qualifiedIdentifier annotationInit?
;
;
annotationInit
annotationInit
: LPAREN annotationInitializers RPAREN
: LPAREN annotationInitializers RPAREN
-> ^(ANNOTATION_INIT_BLOCK[$LPAREN, "ANNOTATION_INIT_BLOCK"] annotationInitializers)
-> ^(ANNOTATION_INIT_BLOCK[$LPAREN, "ANNOTATION_INIT_BLOCK"] annotationInitializers)
;
;
annotationInitializers
annotationInitializers
: annotationInitializer (COMMA annotationInitializer)*
: annotationInitializer (COMMA annotationInitializer)*
-> ^(ANNOTATION_INIT_KEY_LIST annotationInitializer+)
-> ^(ANNOTATION_INIT_KEY_LIST annotationInitializer+)
| annotationElementValue // implicite initialization of the annotation field 'value'
| annotationElementValue // implicite initialization of the annotation field 'value'
-> ^(ANNOTATION_INIT_DEFAULT_KEY annotationElementValue)
-> ^(ANNOTATION_INIT_DEFAULT_KEY annotationElementValue)
;
;
annotationInitializer
annotationInitializer
: IDENT^ ASSIGN! annotationElementValue
: IDENT^ ASSIGN! annotationElementValue
;
;
annotationElementValue
annotationElementValue
: annotationElementValueExpression
: annotationElementValueExpression
| annotation
| annotation
| annotationElementValueArrayInitializer
| annotationElementValueArrayInitializer
;
;
annotationElementValueExpression
annotationElementValueExpression
: conditionalExpression
: conditionalExpression
-> ^(EXPR conditionalExpression)
-> ^(EXPR conditionalExpression)
;
;
annotationElementValueArrayInitializer
annotationElementValueArrayInitializer
: LCURLY (annotationElementValue (COMMA annotationElementValue)*)? (COMMA)? RCURLY
: LCURLY (annotationElementValue (COMMA annotationElementValue)*)? (COMMA)? RCURLY
-> ^(ANNOTATION_INIT_ARRAY_ELEMENT[$LCURLY, "ANNOTATION_ELEM_VALUE_ARRAY_INIT"] annotationElementValue*)
-> ^(ANNOTATION_INIT_ARRAY_ELEMENT[$LCURLY, "ANNOTATION_ELEM_VALUE_ARRAY_INIT"] annotationElementValue*)
;
;
annotationTypeDeclaration[CommonTree modifiers]
annotationTypeDeclaration[CommonTree modifiers]
: AT INTERFACE IDENT annotationBody
: AT INTERFACE IDENT annotationBody
-> ^(AT {$modifiers} IDENT annotationBody)
-> ^(AT {$modifiers} IDENT annotationBody)
;
;
annotationBody
annotationBody
: LCURLY annotationScopeDeclarations* RCURLY
: LCURLY annotationScopeDeclarations* RCURLY
-> ^(ANNOTATION_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] annotationScopeDeclarations*)
-> ^(ANNOTATION_TOP_LEVEL_SCOPE[$LCURLY, "CLASS_TOP_LEVEL_SCOPE"] annotationScopeDeclarations*)
;
;
annotationScopeDeclarations
annotationScopeDeclarations
: modifierList type
: modifierList type
( IDENT LPAREN RPAREN annotationDefaultValue? SEMI
( IDENT LPAREN RPAREN annotationDefaultValue? SEMI
-> ^(ANNOTATION_METHOD_DECL modifierList type IDENT annotationDefaultValue?)
-> ^(ANNOTATION_METHOD_DECL modifierList type IDENT annotationDefaultValue?)
| classFieldDeclaratorList SEMI
| classFieldDeclaratorList SEMI
-> ^(VAR_DECLARATION modifierList type classFieldDeclaratorList)
-> ^(VAR_DECLARATION modifierList type classFieldDeclaratorList)
)
)
| typeDeclaration
| typeDeclaration
;
;
annotationDefaultValue
annotationDefaultValue
: DEFAULT^ annotationElementValue
: DEFAULT^ annotationElementValue
;
;
// STATEMENTS / BLOCKS
// STATEMENTS / BLOCKS
block
block
: LCURLY blockStatement* RCURLY
: LCURLY blockStatement* RCURLY
-> ^(BLOCK_SCOPE[$LCURLY, "BLOCK_SCOPE"] blockStatement*)
-> ^(BLOCK_SCOPE[$LCURLY, "BLOCK_SCOPE"] blockStatement*)
;
;
blockStatement
blockStatement
: localVariableDeclaration SEMI!
: localVariableDeclaration SEMI!
| typeDeclaration
| typeDeclaration
| statement
| statement
;
;
localVariableDeclaration
localVariableDeclaration
: localModifierList type classFieldDeclaratorList
: localModifierList type classFieldDeclaratorList
-> ^(VAR_DECLARATION localModifierList type classFieldDeclaratorList)
;
statement
: block
| ASSERT expr1=expression
(