📄 java.g
字号:
/**
* An ANTLRv3 capable Java 1.5 grammar for building ASTs.
*
* Note that there's also the tree grammar 'JavaTreeParser.g' that can be fed
* with this grammer's output.
*
*
* Please report any detected errors or even suggestions regarding this grammar
* to
*
* dieter [D O T] habelitz [A T] habelitz [D O T] com
*
* with the subject
*
* jsom grammar: [your subject note]
*
* To generate a parser based on this grammar you'll need ANTLRv3, which you can
* get from 'http://www.antlr.org'.
*
*
* This grammar is published under the ...
*
* BSD licence
*
* Copyright (c) 2007-2008 by HABELITZ Software Developments
*
* All rights reserved.
*
* http://www.habelitz.com
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY HABELITZ SOFTWARE DEVELOPMENTS ('HSD') ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL 'HSD' BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
grammar java;
options {
backtrack = true;
memoize = true;
output = AST;
ASTLabelType = CommonTree;
}
tokens {
// operators and other special chars
AND = '&' ;
AND_ASSIGN = '&=' ;
ASSIGN = '=' ;
AT = '@' ;
BIT_SHIFT_RIGHT = '>>>' ;
BIT_SHIFT_RIGHT_ASSIGN = '>>>=' ;
COLON = ':' ;
COMMA = ',' ;
DEC = '--' ;
DIV = '/' ;
DIV_ASSIGN = '/=' ;
DOT = '.' ;
DOTSTAR = '.*' ;
ELLIPSIS = '...' ;
EQUAL = '==' ;
GREATER_OR_EQUAL = '>=' ;
GREATER_THAN = '>' ;
INC = '++' ;
LBRACK = '[' ;
LCURLY = '{' ;
LESS_OR_EQUAL = '<=' ;
LESS_THAN = '<' ;
LOGICAL_AND = '&&' ;
LOGICAL_NOT = '!' ;
LOGICAL_OR = '||' ;
LPAREN = '(' ;
MINUS = '-' ;
MINUS_ASSIGN = '-=' ;
MOD = '%' ;
MOD_ASSIGN = '%=' ;
NOT = '~' ;
NOT_EQUAL = '!=' ;
OR = '|' ;
OR_ASSIGN = '|=' ;
PLUS = '+' ;
PLUS_ASSIGN = '+=' ;
QUESTION = '?' ;
RBRACK = ']' ;
RCURLY = '}' ;
RPAREN = ')' ;
SEMI = ';' ;
SHIFT_LEFT = '<<' ;
SHIFT_LEFT_ASSIGN = '<<=' ;
SHIFT_RIGHT = '>>' ;
SHIFT_RIGHT_ASSIGN = '>>=' ;
STAR = '*' ;
STAR_ASSIGN = '*=' ;
XOR = '^' ;
XOR_ASSIGN = '^=' ;
// keywords
ABSTRACT = 'abstract' ;
ASSERT = 'assert' ;
BOOLEAN = 'boolean' ;
BREAK = 'break' ;
BYTE = 'byte' ;
CASE = 'case' ;
CATCH = 'catch' ;
CHAR = 'char' ;
CLASS = 'class' ;
CONTINUE = 'continue' ;
DEFAULT = 'default' ;
DO = 'do' ;
DOUBLE = 'double' ;
ELSE = 'else' ;
ENUM = 'enum' ;
EXTENDS = 'extends' ;
FALSE = 'false' ;
FINAL = 'final' ;
FINALLY = 'finally' ;
FLOAT = 'float' ;
FOR = 'for' ;
IF = 'if' ;
IMPLEMENTS = 'implements' ;
INSTANCEOF = 'instanceof' ;
INTERFACE = 'interface' ;
IMPORT = 'import' ;
INT = 'int' ;
LONG = 'long' ;
NATIVE = 'native' ;
NEW = 'new' ;
NULL = 'null' ;
PACKAGE = 'package' ;
PRIVATE = 'private' ;
PROTECTED = 'protected' ;
PUBLIC = 'public' ;
RETURN = 'return' ;
SHORT = 'short' ;
STATIC = 'static' ;
STRICTFP = 'strictfp' ;
SUPER = 'super' ;
SWITCH = 'switch' ;
SYNCHRONIZED = 'synchronized' ;
THIS = 'this' ;
THROW = 'throw' ;
THROWS = 'throws' ;
TRANSIENT = 'transient' ;
TRUE = 'true' ;
TRY = 'try' ;
VOID = 'void' ;
VOLATILE = 'volatile' ;
WHILE = 'while' ;
// tokens for imaginary nodes
ANNOTATION_INIT_ARRAY_ELEMENT;
ANNOTATION_INIT_BLOCK;
ANNOTATION_INIT_DEFAULT_KEY;
ANNOTATION_INIT_KEY_LIST;
ANNOTATION_LIST;
ANNOTATION_METHOD_DECL;
ANNOTATION_SCOPE;
ANNOTATION_TOP_LEVEL_SCOPE;
ARGUMENT_LIST;
ARRAY_DECLARATOR;
ARRAY_DECLARATOR_LIST;
ARRAY_ELEMENT_ACCESS;
ARRAY_INITIALIZER;
BLOCK_SCOPE;
CAST_EXPR;
CATCH_CLAUSE_LIST;
CLASS_CONSTRUCTOR_CALL;
CLASS_INSTANCE_INITIALIZER;
CLASS_STATIC_INITIALIZER;
CLASS_TOP_LEVEL_SCOPE;
CONSTRUCTOR_DECL;
ENUM_TOP_LEVEL_SCOPE;
EXPR;
EXTENDS_BOUND_LIST;
EXTENDS_CLAUSE;
FOR_CONDITION;
FOR_EACH;
FOR_INIT;
FOR_UPDATE;
FORMAL_PARAM_LIST;
FORMAL_PARAM_STD_DECL;
FORMAL_PARAM_VARARG_DECL;
FUNCTION_METHOD_DECL;
GENERIC_TYPE_ARG_LIST;
GENERIC_TYPE_PARAM_LIST;
INTERFACE_TOP_LEVEL_SCOPE;
IMPLEMENTS_CLAUSE;
LABELED_STATEMENT;
LOCAL_MODIFIER_LIST;
JAVA_SOURCE;
METHOD_CALL;
MODIFIER_LIST;
PARENTESIZED_EXPR;
POST_DEC;
POST_INC;
PRE_DEC;
PRE_INC;
QUALIFIED_TYPE_IDENT;
STATIC_ARRAY_CREATOR;
SUPER_CONSTRUCTOR_CALL;
SWITCH_BLOCK_LABEL_LIST;
THIS_CONSTRUCTOR_CALL;
THROWS_CLAUSE;
TYPE;
UNARY_MINUS;
UNARY_PLUS;
VAR_DECLARATION;
VAR_DECLARATOR;
VAR_DECLARATOR_LIST;
VOID_METHOD_DECL;
}
@header {
package com.habelitz.jsobjectizer.unmarshaller.antlrbridge.generated;
}
@members {
private boolean mMessageCollectionEnabled = false;
private boolean mHasErrors = false;
private List<String> mMessages;
/**
* Switches error message collection on or of.
*
* The standard destination for parser error messages is <code>System.err</code>.
* However, if <code>true</code> gets passed to this method this default
* behaviour will be switched off and all error messages will be collected
* instead of written to anywhere.
*
* The default value is <code>false</code>.
*
* @param pNewState <code>true</code> if error messages should be collected.
*/
public void enableErrorMessageCollection(boolean pNewState) {
mMessageCollectionEnabled = pNewState;
if (mMessages == null && mMessageCollectionEnabled) {
mMessages = new ArrayList<String>();
}
}
/**
* Collects an error message or passes the error message to <code>
* super.emitErrorMessage(...)</code>.
*
* The actual behaviour depends on whether collecting error messages
* has been enabled or not.
*
* @param pMessage The error message.
*/
@Override
public void emitErrorMessage(String pMessage) {
if (mMessageCollectionEnabled) {
mMessages.add(pMessage);
} else {
super.emitErrorMessage(pMessage);
}
}
/**
* Returns collected error messages.
*
* @return A list holding collected error messages or <code>null</code> if
* collecting error messages hasn't been enabled. Of course, this
* list may be empty if no error message has been emited.
*/
public List<String> getMessages() {
return mMessages;
}
/**
* 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.
*/
public boolean hasErrors() {
return mHasErrors;
}
}
@lexer::header {
package com.habelitz.jsobjectizer.unmarshaller.antlrbridge.generated;
}
@lexer::members {
/**
* Determines if whitespaces and comments should be preserved or thrown away.
*
* If <code>true</code> whitespaces and comments will be preserved within the
* 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
* needed forever the lexer part of the grammar should be changed by replacing
* the 'if-else' stuff within the approprate lexer grammar actions.
*/
public boolean preserveWhitespacesAndComments = false;
}
// Starting point for parsing a Java file.
javaSource
: compilationUnit
-> ^(JAVA_SOURCE compilationUnit)
;
compilationUnit
: annotationList
packageDeclaration?
importDeclaration*
typeDecls*
;
typeDecls
: typeDeclaration
| SEMI!
;
packageDeclaration
: PACKAGE^ qualifiedIdentifier SEMI!
;
importDeclaration
: IMPORT^ STATIC? qualifiedIdentifier DOTSTAR? SEMI!
;
typeDeclaration
: modifierList!
( classTypeDeclaration[$modifierList.tree]
| interfaceTypeDeclaration[$modifierList.tree]
| enumTypeDeclaration[$modifierList.tree]
| annotationTypeDeclaration[$modifierList.tree]
)
;
classTypeDeclaration[CommonTree modifiers]
: CLASS IDENT genericTypeParameterList? classExtendsClause? implementsClause? classBody
-> ^(CLASS {$modifiers} IDENT genericTypeParameterList? classExtendsClause? implementsClause? classBody)
;
classExtendsClause
: EXTENDS type
-> ^(EXTENDS_CLAUSE[$EXTENDS, "EXTENDS_CLAUSE"] type)
;
interfaceExtendsClause
: EXTENDS typeList
-> ^(EXTENDS_CLAUSE[$EXTENDS, "EXTENDS_CLAUSE"] typeList)
;
implementsClause
: IMPLEMENTS typeList
-> ^(IMPLEMENTS_CLAUSE[$IMPLEMENTS, "IMPLEMENTS_CLAUSE"] typeList)
;
genericTypeParameterList
: LESS_THAN genericTypeParameter (COMMA genericTypeParameter)* genericTypeListClosing
-> ^(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
// 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
// generic type arguments etc).
: GREATER_THAN
| SHIFT_RIGHT
| BIT_SHIFT_RIGHT
| // nothing
;
genericTypeParameter
: IDENT bound?
-> ^(IDENT bound?)
;
bound
: EXTENDS type (AND type)*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -