Neue Grammatik-Regeln und daraus resultierende bugfixes

This commit is contained in:
luca9913 2022-11-22 21:44:13 +01:00
parent 291fee12db
commit 09ac2fc5cf
3 changed files with 107 additions and 18 deletions

1
.gitignore vendored
View File

@ -20,6 +20,7 @@ bin
.project .project
.settings/ .settings/
/target/ /target/
settings.json
# #
manually/ manually/

View File

@ -109,6 +109,7 @@ classOrInterfaceType
| interfaceType_lfno_classOrInterfaceType | interfaceType_lfno_classOrInterfaceType
) )
( classType_lf_classOrInterfaceType ( classType_lf_classOrInterfaceType
| interfaceType_lf_classOrInterfaceType
)* )*
; ;
@ -117,6 +118,10 @@ classType
| classOrInterfaceType '.' annotation* Identifier typeArguments? | classOrInterfaceType '.' annotation* Identifier typeArguments?
; ;
classTypeList
: classType (',' classType)*
;
classType_lf_classOrInterfaceType classType_lf_classOrInterfaceType
: '.' annotation* Identifier typeArguments? : '.' annotation* Identifier typeArguments?
; ;
@ -275,10 +280,11 @@ typeDeclaration
classDeclaration classDeclaration
: normalClassDeclaration : normalClassDeclaration
| enumDeclaration | enumDeclaration
| recordDeclaration
; ;
normalClassDeclaration normalClassDeclaration
: classModifier* 'class' Identifier typeParameters? superclass? superinterfaces? classBody : classModifier* 'class' Identifier typeParameters? superclass? superinterfaces? permittedsubclasses? classBody
; ;
classModifier classModifier
@ -288,7 +294,9 @@ classModifier
| 'private' | 'private'
| 'abstract' | 'abstract'
| 'static' | 'static'
| 'sealed'
| 'final' | 'final'
| 'non-sealed'
| 'strictfp' | 'strictfp'
; ;
@ -308,6 +316,10 @@ superinterfaces
: 'implements' interfaceTypeList : 'implements' interfaceTypeList
; ;
permittedsubclasses
: 'permits' classTypeList
;
interfaceTypeList interfaceTypeList
: interfaceType (',' interfaceType)* : interfaceType (',' interfaceType)*
; ;
@ -562,6 +574,40 @@ enumBodyDeclarations
: ';' classBodyDeclaration* : ';' classBodyDeclaration*
; ;
recordDeclaration
: classModifier* 'record' Identifier typeParameters? recordHeader superinterfaces? recordBody
;
recordHeader
: '(' recordComponentList? ')'
;
recordComponentList
: recordComponent (',' recordComponent)*
;
recordComponent
: annotation* unannType Identifier
| variableArityRecordComponent
;
variableArityRecordComponent
: annotation* unannType annotation* '...' Identifier
;
recordBody
: '{' recordBodyDeclaration* '}'
;
recordBodyDeclaration
: classBodyDeclaration
| compactConstructorDeclaration
;
compactConstructorDeclaration
: constructorModifier* simpleTypeName constructorBody
;
/* /*
* Productions from §9 (Interfaces) * Productions from §9 (Interfaces)
*/ */
@ -745,6 +791,7 @@ statement
| ifThenElseStatement | ifThenElseStatement
| whileStatement | whileStatement
| forStatement | forStatement
| switchExpression ';'?
; ;
statementNoShortIf statementNoShortIf
@ -763,6 +810,7 @@ statementWithoutTrailingSubstatement
| switchStatement | switchStatement
| doStatement | doStatement
| breakStatement | breakStatement
| yieldStatement
| continueStatement | continueStatement
| returnStatement | returnStatement
| synchronizedStatement | synchronizedStatement
@ -801,11 +849,11 @@ ifThenStatement
; ;
ifThenElseStatement ifThenElseStatement
: 'if' '(' expression ')' statementNoShortIf 'else' statement : 'if' parExpression statementNoShortIf 'else' statement
; ;
ifThenElseStatementNoShortIf ifThenElseStatementNoShortIf
: 'if' '(' expression ')' statementNoShortIf 'else' statementNoShortIf : 'if' parExpression statementNoShortIf 'else' statementNoShortIf
; ;
assertStatement assertStatement
@ -814,7 +862,7 @@ assertStatement
; ;
switchStatement switchStatement
: 'switch' '(' expression ')' switchBlock : 'switch' parExpression switchBlock
; ;
switchBlock switchBlock
@ -835,20 +883,44 @@ switchLabel
| 'default' ':' | 'default' ':'
; ;
switchExpression
: 'switch' parExpression '{' switchLabeledRule* '}'
;
switchLabeledRule
: CASE (expressionList | NullLiteral | guardedPattern) (ARROW|COLON) switchRuleOutcome
| DEFAULT (ARROW|COLON) switchRuleOutcome
;
guardedPattern
: '(' guardedPattern ')'
| variableModifier* annotation* Identifier ('&&' expression)*
| guardedPattern '&&' expression
;
switchRuleOutcome
: block
| blockStatement*
;
enumConstantName enumConstantName
: Identifier : Identifier
; ;
enumConstantNameList
: enumConstantName (',' enumConstantName)*
;
whileStatement whileStatement
: 'while' '(' expression ')' statement : 'while' parExpression statement
; ;
whileStatementNoShortIf whileStatementNoShortIf
: 'while' '(' expression ')' statementNoShortIf : 'while' parExpression statementNoShortIf
; ;
doStatement doStatement
: 'do' statement 'while' '(' expression ')' ';' : 'do' statement 'while' parExpression ';'
; ;
forStatement forStatement
@ -894,6 +966,10 @@ breakStatement
: 'break' Identifier? ';' : 'break' Identifier? ';'
; ;
yieldStatement
: 'yield' Identifier? ';'
;
continueStatement continueStatement
: 'continue' Identifier? ';' : 'continue' Identifier? ';'
; ;
@ -907,7 +983,7 @@ throwStatement
; ;
synchronizedStatement synchronizedStatement
: 'synchronized' '(' expression ')' block : 'synchronized' parExpression block
; ;
tryStatement tryStatement
@ -970,7 +1046,7 @@ primaryNoNewArray
| 'void' '.' 'class' | 'void' '.' 'class'
| 'this' | 'this'
| typeName '.' 'this' | typeName '.' 'this'
| '(' expression ')' | parExpression
| classInstanceCreationExpression | classInstanceCreationExpression
| fieldAccess | fieldAccess
| arrayAccess | arrayAccess
@ -988,7 +1064,7 @@ primaryNoNewArray_lfno_arrayAccess
| 'void' '.' 'class' | 'void' '.' 'class'
| 'this' | 'this'
| typeName '.' 'this' | typeName '.' 'this'
| '(' expression ')' | parExpression
| classInstanceCreationExpression | classInstanceCreationExpression
| fieldAccess | fieldAccess
| methodInvocation | methodInvocation
@ -1021,7 +1097,7 @@ primaryNoNewArray_lfno_primary
| 'void' '.' 'class' | 'void' '.' 'class'
| 'this' | 'this'
| typeName '.' 'this' | typeName '.' 'this'
| '(' expression ')' //done | parExpression //done
| classInstanceCreationExpression_lfno_primary //done | classInstanceCreationExpression_lfno_primary //done
| fieldAccess_lfno_primary | fieldAccess_lfno_primary
| arrayAccess_lfno_primary | arrayAccess_lfno_primary
@ -1040,7 +1116,7 @@ primaryNoNewArray_lfno_primary_lfno_arrayAccess_lfno_primary
| 'void' '.' 'class' | 'void' '.' 'class'
| 'this' | 'this'
| typeName '.' 'this' | typeName '.' 'this'
| '(' expression ')' | parExpression
| classInstanceCreationExpression_lfno_primary | classInstanceCreationExpression_lfno_primary
| fieldAccess_lfno_primary | fieldAccess_lfno_primary
| methodInvocation_lfno_primary | methodInvocation_lfno_primary
@ -1172,9 +1248,22 @@ constantExpression
: expression : expression
; ;
constantExpressionList
: constantExpression (',' constantExpression)*
;
expression expression
: lambdaExpression : lambdaExpression
| assignmentExpression | assignmentExpression
| switchExpression
;
expressionList
: expression (',' expression)*
;
parExpression
: '(' expression ')'
; ;
lambdaExpression lambdaExpression
@ -1738,7 +1827,6 @@ Identifier
: JavaLetter JavaLetterOrDigit* : JavaLetter JavaLetterOrDigit*
; ;
fragment fragment
JavaLetter JavaLetter
: [a-zA-Z$_] // these are the "java letters" below 0x7F : [a-zA-Z$_] // these are the "java letters" below 0x7F

View File

@ -359,7 +359,7 @@ public class StatementGenerator {
} }
private Statement convert(Java8Parser.IfThenElseStatementContext stmt){ private Statement convert(Java8Parser.IfThenElseStatementContext stmt){
Expression expr = convert(stmt.expression()); Expression expr = convert(stmt.parExpression().expression());
Statement thenBlock = convert(stmt.statementNoShortIf()); Statement thenBlock = convert(stmt.statementNoShortIf());
Statement elseBlock = convert(stmt.statement()); Statement elseBlock = convert(stmt.statement());
return new IfStmt(TypePlaceholder.fresh(stmt.getStart()), expr, thenBlock, elseBlock, stmt.getStart()); return new IfStmt(TypePlaceholder.fresh(stmt.getStart()), expr, thenBlock, elseBlock, stmt.getStart());
@ -391,7 +391,7 @@ public class StatementGenerator {
} }
private Statement convert(Java8Parser.WhileStatementContext stmt){ private Statement convert(Java8Parser.WhileStatementContext stmt){
Expression expr = convert(stmt.expression()); Expression expr = convert(stmt.parExpression().expression());
Statement block = convert(stmt.statement()); Statement block = convert(stmt.statement());
return new WhileStmt(expr, block,stmt.getStart()); return new WhileStmt(expr, block,stmt.getStart());
} }
@ -403,7 +403,7 @@ public class StatementGenerator {
private Statement convert(Java8Parser.DoStatementContext stmt){ private Statement convert(Java8Parser.DoStatementContext stmt){
Statement block = convert(stmt.statement()); Statement block = convert(stmt.statement());
Expression expr = convert(stmt.expression()); Expression expr = convert(stmt.parExpression().expression());
return new DoStmt(expr,block,stmt.getStart()); return new DoStmt(expr,block,stmt.getStart());
} }
@ -818,8 +818,8 @@ public class StatementGenerator {
private Expression convert(Java8Parser.PrimaryNoNewArray_lfno_primaryContext expression) { private Expression convert(Java8Parser.PrimaryNoNewArray_lfno_primaryContext expression) {
if(expression.literal() != null){ if(expression.literal() != null){
return convert(expression.literal()); return convert(expression.literal());
}else if(expression.expression()!=null){ }else if(expression.parExpression().expression()!=null){
return convert(expression.expression()); return convert(expression.parExpression().expression());
}else if(expression.methodInvocation_lfno_primary() != null){ }else if(expression.methodInvocation_lfno_primary() != null){
return convert(expression.methodInvocation_lfno_primary()); return convert(expression.methodInvocation_lfno_primary());
}else if(expression.classInstanceCreationExpression_lfno_primary() != null) { }else if(expression.classInstanceCreationExpression_lfno_primary() != null) {