Anpassung Statementexpression an Java17-Grammatik begonnen

This commit is contained in:
luca9913 2023-02-13 08:16:16 +01:00
parent 0e981ce95c
commit c38bf658fc
2 changed files with 106 additions and 165 deletions

View File

@ -532,14 +532,14 @@ statement
| SWITCH parExpression '{' switchBlockStatementGroup* switchLabel* '}' #switchstmt | SWITCH parExpression '{' switchBlockStatementGroup* switchLabel* '}' #switchstmt
| SYNCHRONIZED parExpression block #synchronizedstmt | SYNCHRONIZED parExpression block #synchronizedstmt
| RETURN expression? ';' #returnstmt | RETURN expression? ';' #returnstmt
| THROW expression ';' #throstmt | THROW expression ';' #throwstmt
| BREAK identifier? ';' #breakstmt | BREAK identifier? ';' #breakstmt
| CONTINUE identifier? ';' #continuestmt | CONTINUE identifier? ';' #continuestmt
| YIELD expression ';' #yieldstmt // Java17 | YIELD expression ';' #yieldstmt // Java17
| SEMI #semistmt | SEMI #semistmt
| statementExpression=expression ';' #stmtexpression | statementExpression=expression ';' #stmtexpression
| switchExpression ';'? #switchexpression // Java17 | switchExpression ';'? #switchexpression // Java17
| identifierLabel=identifier ':' statement #identifierlabel | identifierLabel=identifier ':' statement #labeledstmt
; ;
catchClause catchClause

View File

@ -5,8 +5,25 @@ import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java17Parser; import de.dhbwstuttgart.parser.antlr.Java17Parser;
import de.dhbwstuttgart.parser.antlr.Java17Parser.AssertstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.AssertstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.BlockstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.BlockstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.BreakstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ConditionalstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ConditionalstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ContinuestmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ForControlContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ForloopContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ForloopContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.LabeledstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ReturnstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SemistmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.StmtexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ThrowstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.WhileloopContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.YieldstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.DowhileloopContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ExpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchblockContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchresourceContext;
import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.*;
@ -77,91 +94,46 @@ public class StatementGenerator {
private Statement convert(Java17Parser.StatementContext stmt) { private Statement convert(Java17Parser.StatementContext stmt) {
switch (stmt) { switch (stmt) {
case AssertstmtContext assertstmt:
return convert(assertstmt);
break;
case BlockstmtContext blockstmt: case BlockstmtContext blockstmt:
return convert(blockstmt.block(), false); return convert(blockstmt.block(), false);
break;
case ConditionalstmtContext condition: case ConditionalstmtContext condition:
return convert(condition); return convert(condition);
break; case WhileloopContext whileloop:
case ForloopContext forloop: return convert(whileloop);
break; case DowhileloopContext dowhileloop:
return convert(dowhileloop);
case SwitchstmtContext switchstmt:
return convert(switchstmt);
case SwitchexpressionContext switchexpression:
return convert(switchexpression);
case ReturnstmtContext returnstmt:
return convert(returnstmt);
case YieldstmtContext yieldstmt:
return convert(yieldstmt);
case StmtexpressionContext stmtexpression:
return convert(stmtexpression);
case AssertstmtContext assertstmt:
/*
* case ForloopContext forloop:
* case TrycatchblockContext trycatchblock:
* case TrycatchresourceContext trycatchresource:
* case SynchronizedstmtContext synchronizedstmt:
* case ThrowstmtContext throwstmt:
* case BreakstmtContext breakstmt:
* case ContinuestmtContext continuestmt:
* case SemistmtContext semistmt:
* case LabeledstmtContext labeledstmt:
*/
default: default:
throw new NotImplementedException(); throw new NotImplementedException();
} }
if (stmt.statementWithoutTrailingSubstatement() != null) {
return convert(stmt.statementWithoutTrailingSubstatement());
} else if (stmt.whileStatement() != null) {
return convert(stmt.whileStatement());
} else if (stmt.forStatement() != null) {
return convert(stmt.forStatement());
} else if (stmt.ifThenElseStatement() != null) {
return convert(stmt.ifThenElseStatement());
} else if (stmt.ifThenStatement() != null) {
return convert(stmt.ifThenStatement());
} else if (stmt.labeledStatement() != null) {
return convert(stmt.labeledStatement());
} else
throw new NotImplementedException();
}
/*
* Restrukturierung Java17-Grammatik
* Fällt weg
* private Statement convert(Java17Parser.StatementNoShortIfContext stmt) {
* if (stmt.statementWithoutTrailingSubstatement() != null) {
* return convert(stmt.statementWithoutTrailingSubstatement());
* } else if (stmt.labeledStatementNoShortIf() != null) {
* return convert(stmt.labeledStatementNoShortIf());
* } else if (stmt.ifThenElseStatementNoShortIf() != null) {
* return convert(stmt.ifThenElseStatementNoShortIf());
* } else if (stmt.whileStatementNoShortIf() != null) {
* return convert(stmt.whileStatementNoShortIf());
* } else if (stmt.forStatementNoShortIf() != null) {
* return convert(stmt.forStatementNoShortIf());
* } else
* throw new NotImplementedException();
* }
*/
private Statement convert(Java17Parser.StatementWithoutTrailingSubstatementContext stmt) {
if (stmt.block() != null) {
return convert(stmt.block(), false);
} else if (stmt.emptyStatement() != null) {
return new EmptyStmt(stmt.getStart());
} else if (stmt.expressionStatement() != null) {
return convert(stmt.expressionStatement());
} else if (stmt.assertStatement() != null) {
return convert(stmt.assertStatement());
} else if (stmt.switchStatement() != null) {
return convert(stmt.switchStatement());
} else if (stmt.doStatement() != null) {
return convert(stmt.doStatement());
} else if (stmt.breakStatement() != null) {
return convert(stmt.breakStatement());
} else if (stmt.continueStatement() != null) {
return convert(stmt.continueStatement());
} else if (stmt.returnStatement() != null) {
return convert(stmt.returnStatement());
} else if (stmt.synchronizedStatement() != null) {
return convert(stmt.synchronizedStatement());
} else if (stmt.throwStatement() != null) {
return convert(stmt.throwStatement());
} else if (stmt.tryStatement() != null) {
return convert(stmt.tryStatement());
} else
throw new NotImplementedException();
} }
public Block convert(Java17Parser.BlockContext block, boolean addTrailingReturn) { public Block convert(Java17Parser.BlockContext block, boolean addTrailingReturn) {
List<Statement> statements = new ArrayList<>(); List<Statement> statements = new ArrayList<>();
if (block.blockStatements() != null) if (block.blockStatement().size() > 0)
for (Java17Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()) { for (Java17Parser.BlockStatementContext statementContext : block.blockStatement()) {
List<Statement> stmt = convert(statementContext); statements.addAll(convert(statementContext));
statements.addAll(stmt);
} }
if (addTrailingReturn) if (addTrailingReturn)
statements = SyntacticSugar.addTrailingReturn(statements); statements = SyntacticSugar.addTrailingReturn(statements);
@ -169,38 +141,52 @@ public class StatementGenerator {
} }
private List<Statement> convert(Java17Parser.BlockStatementContext statementContext) { private List<Statement> convert(Java17Parser.BlockStatementContext statementContext) {
if (statementContext.localVariableDeclarationStatement() != null) { if (!Objects.isNull(statementContext.localVariableDeclaration())) {
return convert(statementContext.localVariableDeclarationStatement()); return convert(statementContext.localVariableDeclaration());
} else if (statementContext.classDeclaration() != null) { } else if (!Objects.isNull(statementContext.localTypeDeclaration())) {
throw new NotImplementedException(); throw new NotImplementedException();
} else { } else {
return Arrays.asList(convert(statementContext.statement())); return Arrays.asList(convert(statementContext.statement()));
} }
} }
private List<Statement> convert(Java17Parser.LocalVariableDeclarationStatementContext stmt) { private Statement convert(Java17Parser.LabeledstmtContext labeledStatementContext) {
Java17Parser.LocalVariableDeclarationContext declaration = stmt.localVariableDeclaration();
return convert(declaration);
}
private Statement convert(Java17Parser.LabeledStatementContext labeledStatementContext) {
throw new NotImplementedException(); throw new NotImplementedException();
// return convert(labeledStatementContext.statement()); // return convert(labeledStatementContext.statement());
} }
private Statement convert(Java17Parser.LabeledStatementNoShortIfContext stmt) { private Statement convert(Java17Parser.StmtexpressionContext stmt) {
ExpressionContext expr = stmt.statementExpression;
String op;
if (!(op = expr.bop.getText()).isEmpty()) {
switch (op) {
case "=":
case "+=":
case "-=":
case "*=":
case "/=":
case "&=":
case "|=":
case "^=":
case ">>=":
case ">>>=":
case "<<=":
case "%=":
ExpressionContext leftside = expr.expression(0);
AssignLeftSide leftHandSide = convert(leftside.getText(), leftside.getStart());
Statement ret = new Assign(leftHandSide, convert(expr.expression(1)), expr.getStart());
ret.setStatement();
return ret;
default:
throw new NotImplementedException(); throw new NotImplementedException();
// return convert(stmt.statementNoShortIf()); }
} else if (!(op = expr.prefix.getText()).isEmpty()) {
} else if (!(op = expr.postfix.getText()).isEmpty()) {
} }
private Statement convert(Java17Parser.ExpressionStatementContext stmt) { if (stmt.preIncrementExpression() != null) {
return convert(stmt.statementExpression());
}
private Statement convert(Java17Parser.StatementExpressionContext stmt) {
if (stmt.assignment() != null) {
return convert(stmt.assignment());
} else if (stmt.preIncrementExpression() != null) {
return convert(stmt.preIncrementExpression()); return convert(stmt.preIncrementExpression());
} else if (stmt.preDecrementExpression() != null) { } else if (stmt.preDecrementExpression() != null) {
return convert(stmt.preDecrementExpression()); return convert(stmt.preDecrementExpression());
@ -216,6 +202,10 @@ public class StatementGenerator {
throw new NotImplementedException(); throw new NotImplementedException();
} }
private Statement convert(Java17Parser.AssignmentContext stmt) {
}
public Receiver getReceiver(Expression expr) { public Receiver getReceiver(Expression expr) {
if (expr instanceof StaticClassName) { if (expr instanceof StaticClassName) {
return (Receiver) expr; return (Receiver) expr;
@ -384,15 +374,8 @@ public class StatementGenerator {
return ret; return ret;
} }
private Statement convert(Java17Parser.AssignmentContext stmt) { private AssignLeftSide convert(String leftHandSide, Token start) {
AssignLeftSide leftHandSide = convert(stmt.leftHandSide()); Expression leftSide = generateLocalOrFieldVarOrClassName(leftHandSide, start);
Statement ret = new Assign(leftHandSide, convert(stmt.expression()), stmt.getStart());
ret.setStatement();
return ret;
}
private AssignLeftSide convert(Java17Parser.LeftHandSideContext leftHandSide) {
Expression leftSide = generateLocalOrFieldVarOrClassName(leftHandSide.getText(), leftHandSide.getStart());
if (leftSide instanceof FieldVar) if (leftSide instanceof FieldVar)
return new AssignToField((FieldVar) leftSide); return new AssignToField((FieldVar) leftSide);
else if (leftSide instanceof LocalVar) else if (leftSide instanceof LocalVar)
@ -420,7 +403,17 @@ public class StatementGenerator {
throw new NotImplementedException(); throw new NotImplementedException();
} }
private Statement convert(Java17Parser.SwitchStatementContext stmt) { private Statement convert(Java17Parser.SwitchstmtContext stmt) {
// TODO
throw new NotImplementedException();
}
private Expression convert(Java17Parser.SwitchExpressionContext switchexpression) {
// TODO
throw new NotImplementedException();
}
private Statement convert(Java17Parser.YieldstmtContext yieldstmt) {
// TODO // TODO
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -435,60 +428,27 @@ public class StatementGenerator {
throw new NotImplementedException(); throw new NotImplementedException();
} }
private Statement convert(Java17Parser.WhileStatementContext stmt) { private Statement convert(Java17Parser.WhileloopContext stmt) {
Expression expr = convert(stmt.parExpression().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());
} }
private Statement convert(Java17Parser.WhileStatementNoShortIfContext stmt) { private Statement convert(Java17Parser.DowhileloopContext stmt) {
// TODO
throw new NotImplementedException();
}
private Statement convert(Java17Parser.DoStatementContext stmt) {
Statement block = convert(stmt.statement()); Statement block = convert(stmt.statement());
Expression expr = convert(stmt.parExpression().expression()); Expression expr = convert(stmt.parExpression().expression());
return new DoStmt(expr, block, stmt.getStart()); return new DoStmt(expr, block, stmt.getStart());
} }
private Statement convert(Java17Parser.ForStatementContext stmt) { private Statement convert(Java17Parser.ForloopContext stmt) {
if (stmt.basicForStatement() != null) {
return convert(stmt.basicForStatement());
} else if (stmt.enhancedForStatement() != null) {
return convert(stmt.enhancedForStatement());
} else
throw new NotImplementedException(); throw new NotImplementedException();
} }
private Statement convert(Java17Parser.ForStatementNoShortIfContext stmt) { private Statement convert(Java17Parser.ExpressionListContext exprlist) {
if (stmt.basicForStatementNoShortIf() != null) {
return convert(stmt.basicForStatementNoShortIf());
} else if (stmt.enhancedForStatementNoShortIf() != null) {
return convert(stmt.enhancedForStatementNoShortIf());
} else
throw new NotImplementedException();
}
private Statement convert(Java17Parser.BasicForStatementContext stmt) {
// TODO // TODO
throw new NotImplementedException(); throw new NotImplementedException();
} }
private Statement convert(Java17Parser.BasicForStatementNoShortIfContext stmt) {
// TODO
throw new NotImplementedException();
}
private List<Statement> convert(Java17Parser.ForInitContext stmt) {
if (stmt.statementExpressionList() != null) {
return Arrays.asList(convert(stmt.statementExpressionList()));
} else if (stmt.localVariableDeclaration() != null) {
return convert(stmt.localVariableDeclaration());
} else
throw new NotImplementedException();
}
private List<Statement> convert(Java17Parser.LocalVariableDeclarationContext declaration) { private List<Statement> convert(Java17Parser.LocalVariableDeclarationContext declaration) {
List<Statement> ret = new ArrayList<>(); List<Statement> ret = new ArrayList<>();
if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) { if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) {
@ -542,26 +502,7 @@ public class StatementGenerator {
initValue, name.getSymbol())); initValue, name.getSymbol()));
} }
private Statement convert(Java17Parser.ForUpdateContext stmt) { private Statement convert(Java17Parser.BreakstmtContext stmt) {
return convert(stmt.statementExpressionList());
}
private Statement convert(Java17Parser.StatementExpressionListContext stmt) {
// TODO
throw new NotImplementedException();
}
private Statement convert(Java17Parser.EnhancedForStatementContext stmt) {
// TODO
throw new NotImplementedException();
}
private Statement convert(Java17Parser.EnhancedForStatementNoShortIfContext stmt) {
// TODO
throw new NotImplementedException();
}
private Statement convert(Java17Parser.BreakStatementContext stmt) {
// TODO // TODO
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -571,7 +512,7 @@ public class StatementGenerator {
throw new NotImplementedException(); throw new NotImplementedException();
} }
private Statement convert(Java17Parser.ReturnStatementContext stmt) { private Statement convert(Java17Parser.ReturnstmtContext stmt) {
if (stmt.expression() != null) { if (stmt.expression() != null) {
return new Return(convert(stmt.expression()), stmt.getStart()); return new Return(convert(stmt.expression()), stmt.getStart());
} else { } else {
@ -589,7 +530,7 @@ public class StatementGenerator {
throw new NotImplementedException(); throw new NotImplementedException();
} }
private Statement convert(Java17Parser.TryStatementContext stmt) { private Statement convert(Java17Parser.TrycatchblockContext stmt) {
// TODO // TODO
throw new NotImplementedException(); throw new NotImplementedException();
} }