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