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
| SYNCHRONIZED parExpression block #synchronizedstmt
| RETURN expression? ';' #returnstmt
| THROW expression ';' #throstmt
| THROW expression ';' #throwstmt
| BREAK identifier? ';' #breakstmt
| CONTINUE identifier? ';' #continuestmt
| YIELD expression ';' #yieldstmt // Java17
| SEMI #semistmt
| statementExpression=expression ';' #stmtexpression
| switchExpression ';'? #switchexpression // Java17
| identifierLabel=identifier ':' statement #identifierlabel
| identifierLabel=identifier ':' statement #labeledstmt
;
catchClause

View File

@ -5,8 +5,25 @@ import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java17Parser;
import de.dhbwstuttgart.parser.antlr.Java17Parser.AssertstmtContext;
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.ContinuestmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ForControlContext;
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.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.*;
@ -77,91 +94,46 @@ public class StatementGenerator {
private Statement convert(Java17Parser.StatementContext stmt) {
switch (stmt) {
case AssertstmtContext assertstmt:
return convert(assertstmt);
break;
case BlockstmtContext blockstmt:
return convert(blockstmt.block(), false);
break;
case ConditionalstmtContext condition:
return convert(condition);
break;
case ForloopContext forloop:
break;
case WhileloopContext whileloop:
return convert(whileloop);
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:
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) {
List<Statement> statements = new ArrayList<>();
if (block.blockStatements() != null)
for (Java17Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()) {
List<Statement> stmt = convert(statementContext);
statements.addAll(stmt);
if (block.blockStatement().size() > 0)
for (Java17Parser.BlockStatementContext statementContext : block.blockStatement()) {
statements.addAll(convert(statementContext));
}
if (addTrailingReturn)
statements = SyntacticSugar.addTrailingReturn(statements);
@ -169,38 +141,52 @@ public class StatementGenerator {
}
private List<Statement> convert(Java17Parser.BlockStatementContext statementContext) {
if (statementContext.localVariableDeclarationStatement() != null) {
return convert(statementContext.localVariableDeclarationStatement());
} else if (statementContext.classDeclaration() != null) {
if (!Objects.isNull(statementContext.localVariableDeclaration())) {
return convert(statementContext.localVariableDeclaration());
} else if (!Objects.isNull(statementContext.localTypeDeclaration())) {
throw new NotImplementedException();
} else {
return Arrays.asList(convert(statementContext.statement()));
}
}
private List<Statement> convert(Java17Parser.LocalVariableDeclarationStatementContext stmt) {
Java17Parser.LocalVariableDeclarationContext declaration = stmt.localVariableDeclaration();
return convert(declaration);
}
private Statement convert(Java17Parser.LabeledStatementContext labeledStatementContext) {
private Statement convert(Java17Parser.LabeledstmtContext labeledStatementContext) {
throw new NotImplementedException();
// return convert(labeledStatementContext.statement());
}
private Statement convert(Java17Parser.LabeledStatementNoShortIfContext stmt) {
throw new NotImplementedException();
// return convert(stmt.statementNoShortIf());
}
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();
}
} else if (!(op = expr.prefix.getText()).isEmpty()) {
private Statement convert(Java17Parser.ExpressionStatementContext stmt) {
return convert(stmt.statementExpression());
}
} else if (!(op = expr.postfix.getText()).isEmpty()) {
private Statement convert(Java17Parser.StatementExpressionContext stmt) {
if (stmt.assignment() != null) {
return convert(stmt.assignment());
} else if (stmt.preIncrementExpression() != null) {
}
if (stmt.preIncrementExpression() != null) {
return convert(stmt.preIncrementExpression());
} else if (stmt.preDecrementExpression() != null) {
return convert(stmt.preDecrementExpression());
@ -216,6 +202,10 @@ public class StatementGenerator {
throw new NotImplementedException();
}
private Statement convert(Java17Parser.AssignmentContext stmt) {
}
public Receiver getReceiver(Expression expr) {
if (expr instanceof StaticClassName) {
return (Receiver) expr;
@ -384,15 +374,8 @@ public class StatementGenerator {
return ret;
}
private Statement convert(Java17Parser.AssignmentContext stmt) {
AssignLeftSide leftHandSide = convert(stmt.leftHandSide());
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());
private AssignLeftSide convert(String leftHandSide, Token start) {
Expression leftSide = generateLocalOrFieldVarOrClassName(leftHandSide, start);
if (leftSide instanceof FieldVar)
return new AssignToField((FieldVar) leftSide);
else if (leftSide instanceof LocalVar)
@ -420,7 +403,17 @@ public class StatementGenerator {
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
throw new NotImplementedException();
}
@ -435,60 +428,27 @@ public class StatementGenerator {
throw new NotImplementedException();
}
private Statement convert(Java17Parser.WhileStatementContext stmt) {
private Statement convert(Java17Parser.WhileloopContext stmt) {
Expression expr = convert(stmt.parExpression().expression());
Statement block = convert(stmt.statement());
return new WhileStmt(expr, block, stmt.getStart());
}
private Statement convert(Java17Parser.WhileStatementNoShortIfContext stmt) {
// TODO
throw new NotImplementedException();
}
private Statement convert(Java17Parser.DoStatementContext stmt) {
private Statement convert(Java17Parser.DowhileloopContext stmt) {
Statement block = convert(stmt.statement());
Expression expr = convert(stmt.parExpression().expression());
return new DoStmt(expr, block, stmt.getStart());
}
private Statement convert(Java17Parser.ForStatementContext stmt) {
if (stmt.basicForStatement() != null) {
return convert(stmt.basicForStatement());
} else if (stmt.enhancedForStatement() != null) {
return convert(stmt.enhancedForStatement());
} else
throw new NotImplementedException();
}
private Statement convert(Java17Parser.ForStatementNoShortIfContext stmt) {
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
private Statement convert(Java17Parser.ForloopContext stmt) {
throw new NotImplementedException();
}
private Statement convert(Java17Parser.BasicForStatementNoShortIfContext stmt) {
private Statement convert(Java17Parser.ExpressionListContext exprlist) {
// 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) {
List<Statement> ret = new ArrayList<>();
if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) {
@ -542,26 +502,7 @@ public class StatementGenerator {
initValue, name.getSymbol()));
}
private Statement convert(Java17Parser.ForUpdateContext 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) {
private Statement convert(Java17Parser.BreakstmtContext stmt) {
// TODO
throw new NotImplementedException();
}
@ -571,7 +512,7 @@ public class StatementGenerator {
throw new NotImplementedException();
}
private Statement convert(Java17Parser.ReturnStatementContext stmt) {
private Statement convert(Java17Parser.ReturnstmtContext stmt) {
if (stmt.expression() != null) {
return new Return(convert(stmt.expression()), stmt.getStart());
} else {
@ -589,7 +530,7 @@ public class StatementGenerator {
throw new NotImplementedException();
}
private Statement convert(Java17Parser.TryStatementContext stmt) {
private Statement convert(Java17Parser.TrycatchblockContext stmt) {
// TODO
throw new NotImplementedException();
}