From 09ac2fc5cf0b0e0402846f6c9dc2dc6b6466d130 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Tue, 22 Nov 2022 21:44:13 +0100 Subject: [PATCH] Neue Grammatik-Regeln und daraus resultierende bugfixes --- .gitignore | 1 + .../de/dhbwstuttgart/parser/antlr/Java8.g4 | 114 ++++++++++++++++-- .../StatementGenerator.java | 10 +- 3 files changed, 107 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 2d0d6112..d3e21505 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ bin .project .settings/ /target/ +settings.json # manually/ diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java8.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java8.g4 index 9cce03a6..5188ddf6 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java8.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java8.g4 @@ -109,6 +109,7 @@ classOrInterfaceType | interfaceType_lfno_classOrInterfaceType ) ( classType_lf_classOrInterfaceType + | interfaceType_lf_classOrInterfaceType )* ; @@ -117,6 +118,10 @@ classType | classOrInterfaceType '.' annotation* Identifier typeArguments? ; +classTypeList + : classType (',' classType)* + ; + classType_lf_classOrInterfaceType : '.' annotation* Identifier typeArguments? ; @@ -275,10 +280,11 @@ typeDeclaration classDeclaration : normalClassDeclaration | enumDeclaration + | recordDeclaration ; normalClassDeclaration - : classModifier* 'class' Identifier typeParameters? superclass? superinterfaces? classBody + : classModifier* 'class' Identifier typeParameters? superclass? superinterfaces? permittedsubclasses? classBody ; classModifier @@ -288,7 +294,9 @@ classModifier | 'private' | 'abstract' | 'static' + | 'sealed' | 'final' + | 'non-sealed' | 'strictfp' ; @@ -308,6 +316,10 @@ superinterfaces : 'implements' interfaceTypeList ; +permittedsubclasses + : 'permits' classTypeList + ; + interfaceTypeList : interfaceType (',' interfaceType)* ; @@ -562,6 +574,40 @@ enumBodyDeclarations : ';' 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) */ @@ -745,6 +791,7 @@ statement | ifThenElseStatement | whileStatement | forStatement + | switchExpression ';'? ; statementNoShortIf @@ -763,6 +810,7 @@ statementWithoutTrailingSubstatement | switchStatement | doStatement | breakStatement + | yieldStatement | continueStatement | returnStatement | synchronizedStatement @@ -801,11 +849,11 @@ ifThenStatement ; ifThenElseStatement - : 'if' '(' expression ')' statementNoShortIf 'else' statement + : 'if' parExpression statementNoShortIf 'else' statement ; ifThenElseStatementNoShortIf - : 'if' '(' expression ')' statementNoShortIf 'else' statementNoShortIf + : 'if' parExpression statementNoShortIf 'else' statementNoShortIf ; assertStatement @@ -814,7 +862,7 @@ assertStatement ; switchStatement - : 'switch' '(' expression ')' switchBlock + : 'switch' parExpression switchBlock ; switchBlock @@ -835,20 +883,44 @@ switchLabel | '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 : Identifier ; +enumConstantNameList + : enumConstantName (',' enumConstantName)* + ; + whileStatement - : 'while' '(' expression ')' statement + : 'while' parExpression statement ; whileStatementNoShortIf - : 'while' '(' expression ')' statementNoShortIf + : 'while' parExpression statementNoShortIf ; doStatement - : 'do' statement 'while' '(' expression ')' ';' + : 'do' statement 'while' parExpression ';' ; forStatement @@ -894,6 +966,10 @@ breakStatement : 'break' Identifier? ';' ; +yieldStatement + : 'yield' Identifier? ';' + ; + continueStatement : 'continue' Identifier? ';' ; @@ -907,7 +983,7 @@ throwStatement ; synchronizedStatement - : 'synchronized' '(' expression ')' block + : 'synchronized' parExpression block ; tryStatement @@ -970,7 +1046,7 @@ primaryNoNewArray | 'void' '.' 'class' | 'this' | typeName '.' 'this' - | '(' expression ')' + | parExpression | classInstanceCreationExpression | fieldAccess | arrayAccess @@ -988,7 +1064,7 @@ primaryNoNewArray_lfno_arrayAccess | 'void' '.' 'class' | 'this' | typeName '.' 'this' - | '(' expression ')' + | parExpression | classInstanceCreationExpression | fieldAccess | methodInvocation @@ -1021,7 +1097,7 @@ primaryNoNewArray_lfno_primary | 'void' '.' 'class' | 'this' | typeName '.' 'this' - | '(' expression ')' //done + | parExpression //done | classInstanceCreationExpression_lfno_primary //done | fieldAccess_lfno_primary | arrayAccess_lfno_primary @@ -1040,7 +1116,7 @@ primaryNoNewArray_lfno_primary_lfno_arrayAccess_lfno_primary | 'void' '.' 'class' | 'this' | typeName '.' 'this' - | '(' expression ')' + | parExpression | classInstanceCreationExpression_lfno_primary | fieldAccess_lfno_primary | methodInvocation_lfno_primary @@ -1172,9 +1248,22 @@ constantExpression : expression ; +constantExpressionList + : constantExpression (',' constantExpression)* + ; + expression : lambdaExpression | assignmentExpression + | switchExpression + ; + +expressionList + : expression (',' expression)* + ; + +parExpression + : '(' expression ')' ; lambdaExpression @@ -1738,7 +1827,6 @@ Identifier : JavaLetter JavaLetterOrDigit* ; - fragment JavaLetter : [a-zA-Z$_] // these are the "java letters" below 0x7F diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 2b3c99fd..bcc4f841 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -359,7 +359,7 @@ public class StatementGenerator { } private Statement convert(Java8Parser.IfThenElseStatementContext stmt){ - Expression expr = convert(stmt.expression()); + Expression expr = convert(stmt.parExpression().expression()); Statement thenBlock = convert(stmt.statementNoShortIf()); Statement elseBlock = convert(stmt.statement()); 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){ - Expression expr = convert(stmt.expression()); + Expression expr = convert(stmt.parExpression().expression()); Statement block = convert(stmt.statement()); return new WhileStmt(expr, block,stmt.getStart()); } @@ -403,7 +403,7 @@ public class StatementGenerator { private Statement convert(Java8Parser.DoStatementContext stmt){ Statement block = convert(stmt.statement()); - Expression expr = convert(stmt.expression()); + Expression expr = convert(stmt.parExpression().expression()); return new DoStmt(expr,block,stmt.getStart()); } @@ -818,8 +818,8 @@ public class StatementGenerator { private Expression convert(Java8Parser.PrimaryNoNewArray_lfno_primaryContext expression) { if(expression.literal() != null){ return convert(expression.literal()); - }else if(expression.expression()!=null){ - return convert(expression.expression()); + }else if(expression.parExpression().expression()!=null){ + return convert(expression.parExpression().expression()); }else if(expression.methodInvocation_lfno_primary() != null){ return convert(expression.methodInvocation_lfno_primary()); }else if(expression.classInstanceCreationExpression_lfno_primary() != null) {