Antlr-Grammatik angepasst, um syntaxtree-Klassen zu matchen

This commit is contained in:
luca9913 2022-12-04 21:17:39 +01:00
parent 1145f010c6
commit 4b6d69d748

View File

@ -33,8 +33,8 @@ parser grammar Java17Parser;
options { tokenVocab=Java17Lexer; }
compilationUnit
: packageDeclaration? importDeclaration* typeDeclaration*
sourceFile
: packageDeclaration? importDeclaration* classOrInterface*
| moduleDeclaration EOF
;
@ -46,7 +46,7 @@ importDeclaration
: IMPORT STATIC? qualifiedName ('.' '*')? ';'
;
typeDeclaration
classOrInterface
: classOrInterfaceModifier*
(classDeclaration | enumDeclaration | interfaceDeclaration | annotationTypeDeclaration | recordDeclaration)
| ';'
@ -79,19 +79,19 @@ variableModifier
;
classDeclaration
: CLASS identifier typeParameters?
: CLASS identifier genericDeclarationList?
(EXTENDS typeType)?
(IMPLEMENTS typeList)?
(PERMITS typeList)? // Java17
classBody
;
typeParameters
: '<' typeParameter (',' typeParameter)* '>'
genericDeclarationList
: '<' genericTypeVar (',' genericTypeVar)* '>'
;
typeParameter
: annotation* identifier (EXTENDS annotation* typeBound)?
genericTypeVar
: annotation* identifier ((EXTENDS | IMPLEMENTS) annotation* typeBound)?
;
typeBound
@ -115,7 +115,7 @@ enumBodyDeclarations
;
interfaceDeclaration
: INTERFACE identifier typeParameters? (EXTENDS typeList)? (PERMITS typeList)? interfaceBody
: INTERFACE identifier genericDeclarationList? (EXTENDS typeList)? (PERMITS typeList)? interfaceBody
;
classBody
@ -133,16 +133,15 @@ classBodyDeclaration
;
memberDeclaration
: recordDeclaration //Java17
| methodDeclaration
| genericMethodDeclaration
: classOrInterface
| fieldDeclaration
| constructorDeclaration
| genericConstructorDeclaration
| interfaceDeclaration
| annotationTypeDeclaration
| classDeclaration
| enumDeclaration
| method
| constructor
;
method
: methodDeclaration
| genericMethodDeclaration
;
/* We use rule this even for void methods which cannot have [] after parameters.
@ -151,8 +150,8 @@ memberDeclaration
for invalid return type after parsing.
*/
methodDeclaration
: typeTypeOrVoid identifier formalParameters ('[' ']')*
(THROWS qualifiedNameList)?
: refType? identifier formalParameters ('[' ']')*
(THROWS exceptionList)?
methodBody
;
@ -161,25 +160,30 @@ methodBody
| ';'
;
typeTypeOrVoid
refType
: typeType
| VOID
;
genericMethodDeclaration
: typeParameters methodDeclaration
: genericDeclarationList methodDeclaration
;
constructor
: genericConstructorDeclaration
| constructorDeclaration
;
genericConstructorDeclaration
: typeParameters constructorDeclaration
: genericDeclarationList constructorDeclaration
;
constructorDeclaration
: identifier formalParameters (THROWS qualifiedNameList)? constructorBody=block
: identifier formalParameters (THROWS exceptionList)? constructorBody=block
;
fieldDeclaration
: typeType variableDeclarators ';'
: typeType? variableDeclarators ';'
;
interfaceBodyDeclaration
@ -199,7 +203,7 @@ interfaceMemberDeclaration
;
constDeclaration
: typeType constantDeclarator (',' constantDeclarator)* ';'
: typeType? constantDeclarator (',' constantDeclarator)* ';'
;
constantDeclarator
@ -225,11 +229,11 @@ interfaceMethodModifier
;
genericInterfaceMethodDeclaration
: interfaceMethodModifier* typeParameters interfaceCommonBodyDeclaration
: interfaceMethodModifier* genericDeclarationList interfaceCommonBodyDeclaration
;
interfaceCommonBodyDeclaration
: annotation* typeTypeOrVoid identifier formalParameters ('[' ']')* (THROWS qualifiedNameList)? methodBody
: annotation* refType? identifier formalParameters ('[' ']')* (THROWS exceptionList)? methodBody
;
variableDeclarators
@ -259,13 +263,29 @@ classOrInterfaceType
typeArgument
: typeType
| annotation* '?' ((EXTENDS | SUPER) typeType)?
| wildcardType
;
wildcardType
: annotation* '?' (extendsWildcardType | superWildcardType)?
;
extendsWildcardType
: EXTENDS typeType
;
superWildcardType
: SUPER typeType
;
qualifiedNameList
: qualifiedName (',' qualifiedName)*
;
exceptionList
: qualifiedNameList
;
formalParameters
: '(' ( receiverParameter?
| receiverParameter (',' formalParameterList)?
@ -274,7 +294,7 @@ formalParameters
;
receiverParameter
: typeType (identifier '.')* THIS
: typeType? (identifier '.')* THIS
;
formalParameterList
@ -283,11 +303,11 @@ formalParameterList
;
formalParameter
: variableModifier* typeType variableDeclaratorId
: variableModifier* typeType? variableDeclaratorId
;
lastFormalParameter
: variableModifier* typeType annotation* '...' variableDeclaratorId
: variableModifier* typeType? annotation* '...' variableDeclaratorId
;
// local variable type inference
@ -417,7 +437,7 @@ requiresModifier
// RECORDS - Java 17
recordDeclaration
: RECORD identifier typeParameters? recordHeader
: RECORD identifier genericDeclarationList? recordHeader
(IMPLEMENTS typeList)?
recordBody
;
@ -431,7 +451,7 @@ recordComponentList
;
recordComponent
: typeType identifier
: typeType? identifier
;
recordBody
@ -451,7 +471,7 @@ blockStatement
;
localVariableDeclaration
: variableModifier* (VAR identifier '=' expression | typeType variableDeclarators)
: variableModifier* (VAR identifier '=' expression | typeType? variableDeclarators)
;
identifier
@ -539,7 +559,7 @@ resources
;
resource
: variableModifier* ( classOrInterfaceType variableDeclaratorId | VAR identifier ) '=' expression
: variableModifier* ( classOrInterfaceType? variableDeclaratorId | VAR identifier ) '=' expression
| identifier
;
@ -566,7 +586,7 @@ forInit
;
enhancedForControl
: variableModifier* (typeType | VAR) variableDeclaratorId ':' expression
: variableModifier* (typeType? | VAR) variableDeclaratorId ':' expression
;
// EXPRESSIONS
@ -657,7 +677,7 @@ primary
| SUPER
| literal
| identifier
| typeTypeOrVoid '.' CLASS
| refType '.' CLASS
| nonWildcardTypeArguments (explicitGenericInvocationSuffix | THIS arguments)
;