convert(StatementContext) Implementierung fortgesetzt

This commit is contained in:
luca9913 2023-02-12 07:51:31 +01:00
parent 40d0d6b63e
commit 0e981ce95c
2 changed files with 448 additions and 387 deletions

View File

@ -521,25 +521,25 @@ localTypeDeclaration
;
statement
: blockLabel=block
| ASSERT expression (':' expression)? ';'
| IF parExpression statement (ELSE statement)?
| FOR '(' forControl ')' statement
| WHILE parExpression statement
| DO statement WHILE parExpression ';'
| TRY block (catchClause+ finallyBlock? | finallyBlock)
| TRY resourceSpecification block catchClause* finallyBlock?
| SWITCH parExpression '{' switchBlockStatementGroup* switchLabel* '}'
| SYNCHRONIZED parExpression block
| RETURN expression? ';'
| THROW expression ';'
| BREAK identifier? ';'
| CONTINUE identifier? ';'
| YIELD expression ';' // Java17
| SEMI
| statementExpression=expression ';'
| switchExpression ';'? // Java17
| identifierLabel=identifier ':' statement
: blockLabel=block #blockstmt
| ASSERT expression (':' expression)? ';' #assertstmt
| IF parExpression statement (ELSE statement)? #conditionalstmt
| FOR '(' forControl ')' statement #forloop
| WHILE parExpression statement #whileloop
| DO statement WHILE parExpression ';' #dowhileloop
| TRY block (catchClause+ finallyBlock? | finallyBlock) #trycatchblock
| TRY resourceSpecification block catchClause* finallyBlock? #trycatchresource
| SWITCH parExpression '{' switchBlockStatementGroup* switchLabel* '}' #switchstmt
| SYNCHRONIZED parExpression block #synchronizedstmt
| RETURN expression? ';' #returnstmt
| THROW expression ';' #throstmt
| BREAK identifier? ';' #breakstmt
| CONTINUE identifier? ';' #continuestmt
| YIELD expression ';' #yieldstmt // Java17
| SEMI #semistmt
| statementExpression=expression ';' #stmtexpression
| switchExpression ';'? #switchexpression // Java17
| identifierLabel=identifier ':' statement #identifierlabel
;
catchClause

View File

@ -3,6 +3,10 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
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.ConditionalstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ForloopContext;
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.*;
@ -22,11 +26,14 @@ import java.util.stream.Collectors;
public class StatementGenerator {
private JavaClassRegistry reg;
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> fields; //PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH bekommen
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> fields; // PL 2018-11-01 fields eingefuegt, damit die fields
// immer die gleiche TPH bekommen
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> localVars;
private GenericsRegistry generics;
public StatementGenerator(JavaClassRegistry reg, GenericsRegistry generics, Map<String, RefTypeOrTPHOrWildcardOrGeneric> fields, Map<String, RefTypeOrTPHOrWildcardOrGeneric> localVars){
public StatementGenerator(JavaClassRegistry reg, GenericsRegistry generics,
Map<String, RefTypeOrTPHOrWildcardOrGeneric> fields,
Map<String, RefTypeOrTPHOrWildcardOrGeneric> localVars) {
this.reg = reg;
this.generics = generics;
this.fields = fields;
@ -39,19 +46,22 @@ public class StatementGenerator {
if (formalParameterListContext == null || formalParameterListContext.lastFormalParameter() == null)
return new ParameterList(ret, new NullToken()); // Dann ist die Parameterliste leer
if(formalParameterListContext.lastFormalParameter().formalParameter() == null)throw new NotImplementedException();
/*
* Restrukturierung Java17-Grammatik
* Zeile wird nicht mehr benötigt, da Regel für Parameter-Liste nicht mehr
* rekursiv ist. "lastFormalParameter" hat kein Kind "formalParameter" mehr.
*
* if(formalParameterListContext.lastFormalParameter().formalParameter() ==
* null) throw new NotImplementedException();
*/
if(formalParameterListContext != null && formalParameterListContext.formalParameters() != null
&& formalParameterListContext.formalParameters().formalParameter() != null){
fps = new ArrayList<>(formalParameterListContext.formalParameters().formalParameter());
}
fps.add(formalParameterListContext.lastFormalParameter().formalParameter());
fps = formalParameterListContext.formalParameter();
for (Java17Parser.FormalParameterContext fp : fps) {
String paramName = SyntaxTreeGenerator.convert(fp.variableDeclaratorId());
RefTypeOrTPHOrWildcardOrGeneric type;
if(fp.unannType() != null){
type = TypeGenerator.convert(fp.unannType(), reg, generics);
if (fp.typeType() != null) {
type = TypeGenerator.convert(fp.typeType(), reg, generics);
} else {
type = TypePlaceholder.fresh(fp.getStart());
}
@ -66,6 +76,22 @@ 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;
default:
throw new NotImplementedException();
}
if (stmt.statementWithoutTrailingSubstatement() != null) {
return convert(stmt.statementWithoutTrailingSubstatement());
} else if (stmt.whileStatement() != null) {
@ -78,22 +104,28 @@ public class StatementGenerator {
return convert(stmt.ifThenStatement());
} else if (stmt.labeledStatement() != null) {
return convert(stmt.labeledStatement());
}else throw new NotImplementedException();
} else
throw new NotImplementedException();
}
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();
}
/*
* 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) {
@ -120,7 +152,8 @@ public class StatementGenerator {
return convert(stmt.throwStatement());
} else if (stmt.tryStatement() != null) {
return convert(stmt.tryStatement());
}else throw new NotImplementedException();
} else
throw new NotImplementedException();
}
public Block convert(Java17Parser.BlockContext block, boolean addTrailingReturn) {
@ -130,7 +163,8 @@ public class StatementGenerator {
List<Statement> stmt = convert(statementContext);
statements.addAll(stmt);
}
if(addTrailingReturn)statements = SyntacticSugar.addTrailingReturn(statements);
if (addTrailingReturn)
statements = SyntacticSugar.addTrailingReturn(statements);
return new Block(statements, block.getStart());
}
@ -178,7 +212,8 @@ public class StatementGenerator {
return convert(stmt.methodInvocation());
} else if (stmt.classInstanceCreationExpression() != null) {
return convert(stmt.classInstanceCreationExpression());
}else throw new NotImplementedException();
} else
throw new NotImplementedException();
}
public Receiver getReceiver(Expression expr) {
@ -198,7 +233,8 @@ public class StatementGenerator {
}
Expression receiver;
if (methodInvocationContext.typeName() != null) {
receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart());
receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(),
methodInvocationContext.typeName().getStart());
} else if (methodInvocationContext.expressionName() != null) {
receiver = convert(methodInvocationContext.expressionName());
} else if (methodInvocationContext.primary() != null) {
@ -207,7 +243,8 @@ public class StatementGenerator {
receiver = new Super(methodInvocationContext.getStart());
} else if (methodInvocationContext.methodName() != null) {
receiver = new This(methodInvocationContext.getStart());
}else throw new NotImplementedException();
} else
throw new NotImplementedException();
ArgumentList argumentList = convert(methodInvocationContext.argumentList());
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> argTypes = argumentList.getArguments().stream()
@ -221,14 +258,16 @@ public class StatementGenerator {
}
private ArgumentList convert(Java17Parser.ArgumentListContext argumentListContext) {
if(argumentListContext == null)return new ArgumentList(new ArrayList<>(), new NullToken());
if (argumentListContext == null)
return new ArgumentList(new ArrayList<>(), new NullToken());
List<Expression> args = new ArrayList<>();
Token offset = new NullToken();
for (Java17Parser.ExpressionContext expr : argumentListContext.expression()) {
args.add(convert(expr));
}
if(args.size()>0)offset = args.get(0).getOffset();
if (args.size() > 0)
offset = args.get(0).getOffset();
return new ArgumentList(args, offset);
}
@ -236,6 +275,7 @@ public class StatementGenerator {
/**
* Der Parser kann nicht zwischen einer lokalen Variable, einem Feldzugriff und
* einer Klassenangabe unterscheiden.
*
* @param expression
* @param offset
* @return
@ -247,14 +287,16 @@ public class StatementGenerator {
if (localVars.get(expression) != null) {
return new LocalVar(expression, localVars.get(expression), offset);
} else {
if(fields.get(expression) != null){//PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH bekommen
if (fields.get(expression) != null) {// PL 2018-11-01 fields eingefuegt, damit die fields immer die
// gleiche TPH bekommen
return new FieldVar(new This(offset), expression, fields.get(expression), offset);
} else {
// kann eigentlich nicht vorkommen
// Dann Muss es ein Feld sein!
return new FieldVar(new This(offset), expression, TypePlaceholder.fresh(offset), offset);
}}
}
}
}
return generateFieldVarOrClassname(expression, offset);
}
@ -287,19 +329,21 @@ public class StatementGenerator {
return generateLocalOrFieldVarOrClassName(expressionNameContext.getText(), expressionNameContext.getStart());
}
private Statement convert(Java17Parser.ClassInstanceCreationExpressionContext newExpression) {
Java17Parser.TypeArgumentsContext genericArgs = null;
if(newExpression.expressionName()!= null)throw new NotImplementedException();
if (newExpression.expressionName() != null)
throw new NotImplementedException();
if (newExpression.typeArgumentsOrDiamond() != null) {
if (newExpression.typeArgumentsOrDiamond().typeArguments() != null) {
genericArgs = newExpression.typeArgumentsOrDiamond().typeArguments();
}
}
if(newExpression.typeArguments()!= null)throw new NotImplementedException();
if (newExpression.typeArguments() != null)
throw new NotImplementedException();
TerminalNode identifier = newExpression.Identifier(0);
RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(),genericArgs,identifier.getSymbol(),reg,generics);
RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(), genericArgs,
identifier.getSymbol(), reg, generics);
ArgumentList args = convert(newExpression.argumentList());
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> argTypes = args.getArguments().stream()
@ -313,7 +357,8 @@ public class StatementGenerator {
private Statement convert(Java17Parser.PreIncrementExpressionContext stmt) {
Expression argument = convert(stmt.unaryExpression());
Token offset = stmt.getStart();
Statement ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset);
Statement ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset),
offset);
ret.setStatement();
return ret;
}
@ -348,17 +393,17 @@ public class StatementGenerator {
private AssignLeftSide convert(Java17Parser.LeftHandSideContext leftHandSide) {
Expression leftSide = generateLocalOrFieldVarOrClassName(leftHandSide.getText(), leftHandSide.getStart());
if(leftSide instanceof FieldVar)return new AssignToField((FieldVar) leftSide);
else if (leftSide instanceof LocalVar)return new AssignToLocal((LocalVar) leftSide);
else throw new NotImplementedException();
}
private Statement convert(Java17Parser.IfThenStatementContext stmt){
//TODO
if (leftSide instanceof FieldVar)
return new AssignToField((FieldVar) leftSide);
else if (leftSide instanceof LocalVar)
return new AssignToLocal((LocalVar) leftSide);
else
throw new NotImplementedException();
}
private Statement convert(Java17Parser.IfThenElseStatementContext stmt){
private Statement convert(Java17Parser.ConditionalstmtContext stmt) {
if (Objects.isNull(stmt.ELSE()))
throw new NotImplementedException();
Expression expr = convert(stmt.parExpression().expression());
Statement thenBlock = convert(stmt.statementNoShortIf());
Statement elseBlock = convert(stmt.statement());
@ -370,7 +415,7 @@ public class StatementGenerator {
throw new NotImplementedException();
}
private Statement convert(Java17Parser.AssertStatementContext stmt){
private Statement convert(Java17Parser.AssertstmtContext stmt) {
// TODO
throw new NotImplementedException();
}
@ -412,7 +457,8 @@ public class StatementGenerator {
return convert(stmt.basicForStatement());
} else if (stmt.enhancedForStatement() != null) {
return convert(stmt.enhancedForStatement());
}else throw new NotImplementedException();
} else
throw new NotImplementedException();
}
private Statement convert(Java17Parser.ForStatementNoShortIfContext stmt) {
@ -420,7 +466,8 @@ public class StatementGenerator {
return convert(stmt.basicForStatementNoShortIf());
} else if (stmt.enhancedForStatementNoShortIf() != null) {
return convert(stmt.enhancedForStatementNoShortIf());
}else throw new NotImplementedException();
} else
throw new NotImplementedException();
}
private Statement convert(Java17Parser.BasicForStatementContext stmt) {
@ -438,7 +485,8 @@ public class StatementGenerator {
return Arrays.asList(convert(stmt.statementExpressionList()));
} else if (stmt.localVariableDeclaration() != null) {
return convert(stmt.localVariableDeclaration());
}else throw new NotImplementedException();
} else
throw new NotImplementedException();
}
private List<Statement> convert(Java17Parser.LocalVariableDeclarationContext declaration) {
@ -457,7 +505,8 @@ public class StatementGenerator {
return ret;
}
private List<Statement> generateLocalVariableAssignments(List<Java17Parser.VariableDeclaratorContext> varDeclarators, RefTypeOrTPHOrWildcardOrGeneric type){
private List<Statement> generateLocalVariableAssignments(
List<Java17Parser.VariableDeclaratorContext> varDeclarators, RefTypeOrTPHOrWildcardOrGeneric type) {
List<Statement> ret = new ArrayList<>();
for (Java17Parser.VariableDeclaratorContext varDecl : varDeclarators) {
TerminalNode name = varDecl.variableDeclaratorId().Identifier();
@ -471,14 +520,15 @@ public class StatementGenerator {
} else {
initValue = convert(varDecl.variableInitializer().expression());
}
ret.add(new Assign(new AssignToLocal(new LocalVar(name.getText(), type, name.getSymbol()))
, initValue, name.getSymbol()));
ret.add(new Assign(new AssignToLocal(new LocalVar(name.getText(), type, name.getSymbol())), initValue,
name.getSymbol()));
}
}
return ret;
}
public Statement generateFieldAssignment(Java17Parser.VariableDeclaratorContext varDecl, RefTypeOrTPHOrWildcardOrGeneric type){
public Statement generateFieldAssignment(Java17Parser.VariableDeclaratorContext varDecl,
RefTypeOrTPHOrWildcardOrGeneric type) {
TerminalNode name = varDecl.variableDeclaratorId().Identifier();
Expression initValue;
if (varDecl.variableInitializer().arrayInitializer() != null) {
@ -629,7 +679,8 @@ public class StatementGenerator {
String operator = expression.getChild(1).getText();
Expression leftSide = convert(expression.equalityExpression());
Expression rightSide = convert(expression.relationalExpression());
return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), leftSide, rightSide, expression.getStart());
return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()),
leftSide, rightSide, expression.getStart());
}
}
@ -749,7 +800,8 @@ public class StatementGenerator {
} else if (expressionContext.lambdaExpression() != null) {
expr = convert(expressionContext.lambdaExpression());
}
return new CastExpr(TypeGenerator.convert(expressionContext.referenceType(), reg, generics),expr, expressionContext.getStart());
return new CastExpr(TypeGenerator.convert(expressionContext.referenceType(), reg, generics), expr,
expressionContext.getStart());
}
private Expression convert(Java17Parser.PostfixExpressionContext expression) {
@ -764,11 +816,15 @@ public class StatementGenerator {
return expr;
}
for(Java17Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression.postIncrementExpression_lf_postfixExpression()){
expr = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, expr, TypePlaceholder.fresh(inc.getStart()), inc.getStart());
for (Java17Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression
.postIncrementExpression_lf_postfixExpression()) {
expr = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, expr, TypePlaceholder.fresh(inc.getStart()),
inc.getStart());
}
for(Java17Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression.postDecrementExpression_lf_postfixExpression()){
expr = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, expr, TypePlaceholder.fresh(dec.getStart()), dec.getStart());
for (Java17Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression
.postDecrementExpression_lf_postfixExpression()) {
expr = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, expr, TypePlaceholder.fresh(dec.getStart()),
dec.getStart());
}
return expr;
@ -849,16 +905,19 @@ public class StatementGenerator {
private Expression convert(Java17Parser.ClassInstanceCreationExpression_lfno_primaryContext newExpression) {
Java17Parser.TypeArgumentsContext genericArgs = null;
if(newExpression.expressionName()!= null)throw new NotImplementedException();
if (newExpression.expressionName() != null)
throw new NotImplementedException();
if (newExpression.typeArgumentsOrDiamond() != null) {
if (newExpression.typeArgumentsOrDiamond().typeArguments() != null) {
genericArgs = newExpression.typeArgumentsOrDiamond().typeArguments();
}
}
if(newExpression.typeArguments()!= null)throw new NotImplementedException();
if (newExpression.typeArguments() != null)
throw new NotImplementedException();
TerminalNode identifier = newExpression.Identifier(0);
RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(),genericArgs,identifier.getSymbol(),reg,generics);
RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(), genericArgs,
identifier.getSymbol(), reg, generics);
ArgumentList args = convert(newExpression.argumentList());
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> argTypes = args.getArguments().stream()
@ -910,7 +969,8 @@ public class StatementGenerator {
}
Expression receiver;
if (methodInvocationContext.typeName() != null) {
receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart());
receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(),
methodInvocationContext.typeName().getStart());
} else if (methodInvocationContext.expressionName() != null) {
receiver = convert(methodInvocationContext.expressionName());
} else if (methodInvocationContext.toString().startsWith("super")) {
@ -967,7 +1027,8 @@ public class StatementGenerator {
params.getFormalparalist().forEach(formalParameter -> // Für jeden Parameter einen TPH anfügen:
funNParams.add(TypePlaceholder.fresh(expression.getStart())));
RefTypeOrTPHOrWildcardOrGeneric lambdaType = TypePlaceholder.fresh(expression.getStart());
//RefType lambdaType = new RefType(reg.getName("Fun"+params.getFormalparalist().size()),
// RefType lambdaType = new
// RefType(reg.getName("Fun"+params.getFormalparalist().size()),
// funNParams, name.getStart());
return new LambdaExpression(lambdaType, params, block, expression.getStart());
}