📄 java.g
字号:
/** A Java 1.5 grammar for ANTLR v3 derived from the spec * * This is a very close representation of the spec; the changes * are comestic (remove left recursion) and also fixes (the spec * isn't exactly perfect). I have run this on the 1.4.2 source * and some nasty looking enums from 1.5, but have not really * tested for 1.5 compatibility. * * I built this with: java -Xmx100M org.antlr.Tool java.g * and got two errors that are ok (for now): * java.g:691:9: Decision can match input such as * "'0'..'9'{'E', 'e'}{'+', '-'}'0'..'9'{'D', 'F', 'd', 'f'}" * using multiple alternatives: 3, 4 * As a result, alternative(s) 4 were disabled for that input * java.g:734:35: Decision can match input such as "{'$', 'A'..'Z', * '_', 'a'..'z', '\u00C0'..'\u00D6', '\u00D8'..'\u00F6', * '\u00F8'..'\u1FFF', '\u3040'..'\u318F', '\u3300'..'\u337F', * '\u3400'..'\u3D2D', '\u4E00'..'\u9FFF', '\uF900'..'\uFAFF'}" * using multiple alternatives: 1, 2 * As a result, alternative(s) 2 were disabled for that input * * You can turn enum on/off as a keyword :) * * Version 1.0 -- initial release July 5, 2006 (requires 3.0b2 or higher) * * Primary author: Terence Parr, July 2006 * * Version 1.0.1 -- corrections by Koen Vanderkimpen & Marko van Dooren, * October 25, 2006; * fixed normalInterfaceDeclaration: now uses typeParameters instead * of typeParameter (according to JLS, 3rd edition) * fixed castExpression: no longer allows expression next to type * (according to semantics in JLS, in contrast with syntax in JLS) * * Version 1.0.2 -- Terence Parr, Nov 27, 2006 * java spec I built this from had some bizarre for-loop control. * Looked weird and so I looked elsewhere...Yep, it's messed up. * simplified. */grammar Java;options { language=Python; k=2; backtrack=true; memoize=true;}@lexer::init {self.enumIsKeyword = False}// starting point for parsing a java filecompilationUnit : annotations? packageDeclaration? importDeclaration* typeDeclaration* ;packageDeclaration : 'package' qualifiedName ';' ; importDeclaration : 'import' 'static'? Identifier ('.' Identifier)* ('.' '*')? ';' ; typeDeclaration : classOrInterfaceDeclaration | ';' ; classOrInterfaceDeclaration : modifier* (classDeclaration | interfaceDeclaration) ; classDeclaration : normalClassDeclaration | enumDeclaration ; normalClassDeclaration : 'class' Identifier (typeParameters)? ('extends' type)? ('implements' typeList)? classBody ; typeParameters : '<' typeParameter (',' typeParameter)* '>' ;typeParameter : Identifier ('extends' bound)? ; bound : type ('&' type)* ;enumDeclaration : ENUM Identifier ('implements' typeList)? enumBody ; enumBody : '{' enumConstants? ','? enumBodyDeclarations? '}' ;enumConstants : enumConstant (',' enumConstant)* ; enumConstant : annotations? Identifier (arguments)? (classBody)? ; enumBodyDeclarations : ';' (classBodyDeclaration)* ; interfaceDeclaration : normalInterfaceDeclaration | annotationTypeDeclaration ; normalInterfaceDeclaration : 'interface' Identifier typeParameters? ('extends' typeList)? interfaceBody ; typeList : type (',' type)* ; classBody : '{' classBodyDeclaration* '}' ; interfaceBody : '{' interfaceBodyDeclaration* '}' ;classBodyDeclaration : ';' | 'static'? block | modifier* memberDecl ; memberDecl : genericMethodOrConstructorDecl | methodDeclaration | fieldDeclaration | 'void' Identifier voidMethodDeclaratorRest | Identifier constructorDeclaratorRest | interfaceDeclaration | classDeclaration ; genericMethodOrConstructorDecl : typeParameters genericMethodOrConstructorRest ; genericMethodOrConstructorRest : (type | 'void') Identifier methodDeclaratorRest | Identifier constructorDeclaratorRest ;methodDeclaration : type Identifier methodDeclaratorRest ;fieldDeclaration : type variableDeclarators ';' ; interfaceBodyDeclaration : modifier* interfaceMemberDecl | ';' ;interfaceMemberDecl : interfaceMethodOrFieldDecl | interfaceGenericMethodDecl | 'void' Identifier voidInterfaceMethodDeclaratorRest | interfaceDeclaration | classDeclaration ; interfaceMethodOrFieldDecl : type Identifier interfaceMethodOrFieldRest ; interfaceMethodOrFieldRest : constantDeclaratorsRest ';' | interfaceMethodDeclaratorRest ; methodDeclaratorRest : formalParameters ('[' ']')* ('throws' qualifiedNameList)? ( methodBody | ';' ) ; voidMethodDeclaratorRest : formalParameters ('throws' qualifiedNameList)? ( methodBody | ';' ) ; interfaceMethodDeclaratorRest : formalParameters ('[' ']')* ('throws' qualifiedNameList)? ';' ; interfaceGenericMethodDecl : typeParameters (type | 'void') Identifier interfaceMethodDeclaratorRest ; voidInterfaceMethodDeclaratorRest : formalParameters ('throws' qualifiedNameList)? ';' ; constructorDeclaratorRest : formalParameters ('throws' qualifiedNameList)? methodBody ;constantDeclarator : Identifier constantDeclaratorRest ; variableDeclarators : variableDeclarator (',' variableDeclarator)* ;variableDeclarator : Identifier variableDeclaratorRest ; variableDeclaratorRest : ('[' ']')+ ('=' variableInitializer)? | '=' variableInitializer | {pass} ; constantDeclaratorsRest : constantDeclaratorRest (',' constantDeclarator)* ;constantDeclaratorRest : ('[' ']')* '=' variableInitializer ; variableDeclaratorId : Identifier ('[' ']')* ;variableInitializer : arrayInitializer | expression ; arrayInitializer : '{' (variableInitializer (',' variableInitializer)* (',')? )? '}' ;modifier : annotation | 'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native' | 'synchronized' | 'transient' | 'volatile' | 'strictfp' ;packageOrTypeName : Identifier ('.' Identifier)* ;enumConstantName : Identifier ;typeName : Identifier | packageOrTypeName '.' Identifier ;type : Identifier (typeArguments)? ('.' Identifier (typeArguments)? )* ('[' ']')* | primitiveType ('[' ']')* ;primitiveType : 'boolean' | 'char' | 'byte' | 'short' | 'int' | 'long' | 'float' | 'double' ;variableModifier : 'final' | annotation ;typeArguments : '<' typeArgument (',' typeArgument)* '>' ; typeArgument : type | '?' (('extends' | 'super') type)? ; qualifiedNameList : qualifiedName (',' qualifiedName)* ; formalParameters : '(' formalParameterDecls? ')' ; formalParameterDecls : 'final'? annotations? type formalParameterDeclsRest? ; formalParameterDeclsRest : variableDeclaratorId (',' formalParameterDecls)? | '...' variableDeclaratorId ; methodBody : block ;qualifiedName : Identifier ('.' Identifier)* ; literal : integerLiteral | FloatingPointLiteral | CharacterLiteral | StringLiteral | booleanLiteral | 'null' ;integerLiteral : HexLiteral | OctalLiteral | DecimalLiteral ;booleanLiteral : 'true' | 'false' ;// ANNOTATIONSannotations : annotation+ ;annotation : '@' typeName ('(' (Identifier '=')? elementValue ')')? ; elementValue : conditionalExpression | annotation | elementValueArrayInitializer ; elementValueArrayInitializer : '{' (elementValue)? (',')? '}' ; annotationTypeDeclaration : '@' 'interface' Identifier annotationTypeBody ; annotationTypeBody : '{' (annotationTypeElementDeclarations)? '}' ; annotationTypeElementDeclarations : (annotationTypeElementDeclaration) (annotationTypeElementDeclaration)* ; annotationTypeElementDeclaration : (modifier)* annotationTypeElementRest ; annotationTypeElementRest : type Identifier annotationMethodOrConstantRest ';' | classDeclaration | interfaceDeclaration | enumDeclaration | annotationTypeDeclaration ; annotationMethodOrConstantRest
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -