Fixed parser bug that caused TestComplete to fail

This commit is contained in:
luca9913 2023-06-06 19:43:56 +02:00
parent 03b7f12541
commit 7556b9f590

View File

@ -39,10 +39,10 @@ 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();
if (formalParameterListContext.lastFormalParameter().formalParameter() == null)
throw new NotImplementedException();
if(formalParameterListContext != null && formalParameterListContext.formalParameters() != null
&& formalParameterListContext.formalParameters().formalParameter() != null){
if (formalParameterListContext != null && formalParameterListContext.formalParameters() != null && formalParameterListContext.formalParameters().formalParameter() != null) {
fps = new ArrayList<>(formalParameterListContext.formalParameters().formalParameter());
}
fps.add(formalParameterListContext.lastFormalParameter().formalParameter());
@ -78,7 +78,8 @@ 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(Java8Parser.StatementNoShortIfContext stmt) {
@ -92,7 +93,8 @@ public class StatementGenerator {
return convert(stmt.whileStatementNoShortIf());
} else if (stmt.forStatementNoShortIf() != null) {
return convert(stmt.forStatementNoShortIf());
}else throw new NotImplementedException();
} else
throw new NotImplementedException();
}
private Statement convert(Java8Parser.StatementWithoutTrailingSubstatementContext stmt) {
@ -120,7 +122,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(Java8Parser.BlockContext block, boolean addTrailingReturn) {
@ -130,7 +133,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 +182,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) {
@ -207,36 +212,35 @@ 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> signature = argumentList.getArguments().stream()
.map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart()))
.collect(Collectors.toCollection(ArrayList::new));
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> signature = argumentList.getArguments().stream().map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())).collect(Collectors.toCollection(ArrayList::new));
signature.add(TypePlaceholder.fresh(methodInvocationContext.getStart())); // ReturnType
MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()),
getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()),
signature, methodInvocationContext.getStart());
MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), signature, methodInvocationContext.getStart());
ret.setStatement();
return ret;
}
private ArgumentList convert(Java8Parser.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 (Java8Parser.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);
}
/**
* Der Parser kann nicht zwischen einer lokalen Variable, einem Feldzugriff und
* einer Klassenangabe unterscheiden.
* Der Parser kann nicht zwischen einer lokalen Variable, einem Feldzugriff und einer Klassenangabe unterscheiden.
*
* @param expression
* @param offset
* @return
@ -255,7 +259,8 @@ public class StatementGenerator {
// 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);
}
@ -288,24 +293,23 @@ public class StatementGenerator {
return generateLocalOrFieldVarOrClassName(expressionNameContext.getText(), expressionNameContext.getStart());
}
private Statement convert(Java8Parser.ClassInstanceCreationExpressionContext newExpression) {
Java8Parser.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);
ArgumentList args = convert(newExpression.argumentList());
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> signature = args.getArguments().stream()
.map(x -> TypePlaceholder.fresh(newExpression.getStart()))
.collect(Collectors.toCollection(ArrayList::new));
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> signature = args.getArguments().stream().map(x -> TypePlaceholder.fresh(newExpression.getStart())).collect(Collectors.toCollection(ArrayList::new));
signature.add(TypePlaceholder.fresh(newExpression.getStart())); // ReturnType
Statement ret = new NewClass(newClass, args, null, signature, newExpression.getStart());
ret.setStatement();
@ -321,22 +325,19 @@ public class StatementGenerator {
}
private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) {
Statement ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()),
TypePlaceholder.fresh(stmt.getStart()), stmt.getStart());
Statement ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart());
ret.setStatement();
return ret;
}
private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) {
Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()),
TypePlaceholder.fresh(stmt.getStart()), stmt.getStart());
Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart());
ret.setStatement();
return ret;
}
private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) {
Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()),
TypePlaceholder.fresh(stmt.getStart()), stmt.getStart());
Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart());
ret.setStatement();
return ret;
}
@ -350,9 +351,12 @@ public class StatementGenerator {
private AssignLeftSide convert(Java8Parser.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();
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(Java8Parser.IfThenStatementContext stmt) {
@ -414,7 +418,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(Java8Parser.ForStatementNoShortIfContext stmt) {
@ -422,7 +427,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(Java8Parser.BasicForStatementContext stmt) {
@ -440,7 +446,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(Java8Parser.LocalVariableDeclarationContext declaration) {
@ -473,8 +480,7 @@ 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;
@ -488,10 +494,7 @@ public class StatementGenerator {
} else {
initValue = convert(varDecl.variableInitializer().expression());
}
return (new Assign(new AssignToField(
new FieldVar(new This(varDecl.getStart()), name.getText(),
type, varDecl.getStart())),
initValue, name.getSymbol()));
return (new Assign(new AssignToField(new FieldVar(new This(varDecl.getStart()), name.getText(), type, varDecl.getStart())), initValue, name.getSymbol()));
}
private Statement convert(Java8Parser.ForUpdateContext stmt) {
@ -640,9 +643,7 @@ public class StatementGenerator {
return convert(expression.shiftExpression());
} else {
String operator = expression.getChild(1).getText();
return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()),
convert(expression.relationalExpression()),
convert(expression.shiftExpression()), expression.getStart());
return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), convert(expression.relationalExpression()), convert(expression.shiftExpression()), expression.getStart());
}
// }else throw new NotImplementedException();
}
@ -720,11 +721,9 @@ public class StatementGenerator {
} else if (expression.unaryExpressionNotPlusMinus() != null) {
return convert(expression.unaryExpressionNotPlusMinus());
} else if (expression.getText().startsWith("+")) {
return new UnaryExpr(UnaryExpr.Operation.PLUS, convert(expression.unaryExpression()),
TypePlaceholder.fresh(expression.getStart()), expression.getStart());
return new UnaryExpr(UnaryExpr.Operation.PLUS, convert(expression.unaryExpression()), TypePlaceholder.fresh(expression.getStart()), expression.getStart());
} else if (expression.getText().startsWith("-")) {
return new UnaryExpr(UnaryExpr.Operation.MINUS, convert(expression.unaryExpression()),
TypePlaceholder.fresh(expression.getStart()), expression.getStart());
return new UnaryExpr(UnaryExpr.Operation.MINUS, convert(expression.unaryExpression()), TypePlaceholder.fresh(expression.getStart()), expression.getStart());
} else {
// Diese Exceptions sollte nie geworfen werden.
// Der Code wurde nur noch nicht getestet. Sollte zur Sicherheit drin bleiben.
@ -761,8 +760,7 @@ public class StatementGenerator {
} else {
expr = convert(expression.expressionName());
}
if(expression.postDecrementExpression_lf_postfixExpression() == null &&
expression.postIncrementExpression_lf_postfixExpression() == null){
if (expression.postDecrementExpression_lf_postfixExpression() == null && expression.postIncrementExpression_lf_postfixExpression() == null) {
return expr;
}
@ -805,12 +803,9 @@ public class StatementGenerator {
} else {
Java8Parser.MethodInvocation_lf_primaryContext ctxt = e.methodInvocation_lf_primary();
String methodName = ctxt.Identifier().toString();
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> signature = ctxt.argumentList().expression().stream()
.map(x -> TypePlaceholder.fresh(e.getStart()))
.collect(Collectors.toCollection(ArrayList::new));
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> signature = ctxt.argumentList().expression().stream().map(x -> TypePlaceholder.fresh(e.getStart())).collect(Collectors.toCollection(ArrayList::new));
signature.add(TypePlaceholder.fresh(e.getStart())); // ReturnType
return new MethodCall(TypePlaceholder.fresh(e.getStart()), getReceiver(expr), methodName,
convert(ctxt.argumentList()), TypePlaceholder.fresh(e.getStart()), signature, e.getStart());
return new MethodCall(TypePlaceholder.fresh(e.getStart()), getReceiver(expr), methodName, convert(ctxt.argumentList()), TypePlaceholder.fresh(e.getStart()), signature, e.getStart());
}
}
@ -821,7 +816,7 @@ public class StatementGenerator {
private Expression convert(Java8Parser.PrimaryNoNewArray_lfno_primaryContext expression) {
if (expression.literal() != null) {
return convert(expression.literal());
}else if(expression.parExpression().expression()!=null){
} else if (expression.parExpression() != null) {
return convert(expression.parExpression().expression());
} else if (expression.methodInvocation_lfno_primary() != null) {
return convert(expression.methodInvocation_lfno_primary());
@ -852,21 +847,21 @@ public class StatementGenerator {
private Expression convert(Java8Parser.ClassInstanceCreationExpression_lfno_primaryContext newExpression) {
Java8Parser.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);
ArgumentList args = convert(newExpression.argumentList());
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> signature = args.getArguments().stream()
.map(x -> TypePlaceholder.fresh(newExpression.getStart()))
.collect(Collectors.toCollection(ArrayList::new));
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> signature = args.getArguments().stream().map(x -> TypePlaceholder.fresh(newExpression.getStart())).collect(Collectors.toCollection(ArrayList::new));
signature.add(TypePlaceholder.fresh(newExpression.getStart())); // ReturnType
return new NewClass(newClass, args, null, signature, newExpression.getStart());
}
@ -874,32 +869,24 @@ public class StatementGenerator {
private Expression convert(Java8Parser.LiteralContext literal) {
if (literal.IntegerLiteral() != null) {
Number value = Integer.parseInt(literal.IntegerLiteral().getText());
return new Literal(TypePlaceholder.fresh(literal.getStart()),
value, literal.getStart());
return new Literal(TypePlaceholder.fresh(literal.getStart()), value, literal.getStart());
} else if (literal.FloatingPointLiteral() != null) {
Number value = Double.parseDouble(literal.FloatingPointLiteral().getText());
return new Literal(TypePlaceholder.fresh(literal.getStart()),
value, literal.getStart());
return new Literal(TypePlaceholder.fresh(literal.getStart()), value, literal.getStart());
} else if (literal.BooleanLiteral() != null) {
RefType type = new RefType(reg.getName("java.lang.Boolean"), literal.getStart());
return new Literal(type,
Boolean.parseBoolean(literal.BooleanLiteral().getText()),
literal.getStart());
return new Literal(type, Boolean.parseBoolean(literal.BooleanLiteral().getText()), literal.getStart());
} else if (literal.CharacterLiteral() != null) {
RefType type = new RefType(reg.getName("java.lang.Character"), literal.getStart());
return new Literal(type,
// das gibt immer ' zurück, der Char befindet sich in Position 1
// literal.CharacterLiteral().getText().charAt(0),
literal.CharacterLiteral().getText().charAt(1),
literal.getStart());
literal.CharacterLiteral().getText().charAt(1), literal.getStart());
} else if (literal.StringLiteral() != null) {
RefType type = new RefType(reg.getName("java.lang.String"), literal.getStart());
return new Literal(type,
literal.StringLiteral().getText().substring(1, literal.StringLiteral().getText().length()-1),
literal.getStart());
return new Literal(type, literal.StringLiteral().getText().substring(1, literal.StringLiteral().getText().length() - 1), literal.getStart());
} else if (literal.NullLiteral() != null) {
return new Literal(TypePlaceholder.fresh(literal.getStart()), null,
literal.getStart());
return new Literal(TypePlaceholder.fresh(literal.getStart()), null, literal.getStart());
} else {
throw new NotImplementedException();
}
@ -926,13 +913,9 @@ public class StatementGenerator {
}
ArgumentList argumentList = convert(methodInvocationContext.argumentList());
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> signature = argumentList.getArguments().stream()
.map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart()))
.collect(Collectors.toCollection(ArrayList::new));
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> signature = argumentList.getArguments().stream().map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())).collect(Collectors.toCollection(ArrayList::new));
signature.add(TypePlaceholder.fresh(methodInvocationContext.getStart())); // ReturnType
MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()),
getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()),
signature, methodInvocationContext.getStart());
MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), signature, methodInvocationContext.getStart());
return ret;
}
@ -941,8 +924,7 @@ public class StatementGenerator {
ParameterList params;
if (lambdaParams.Identifier() != null) {
List<FormalParameter> parameterList = new ArrayList<>();
parameterList.add(new FormalParameter(lambdaParams.Identifier().getText(),
TypePlaceholder.fresh(lambdaParams.getStart()), lambdaParams.getStart()));
parameterList.add(new FormalParameter(lambdaParams.Identifier().getText(), TypePlaceholder.fresh(lambdaParams.getStart()), lambdaParams.getStart()));
params = new ParameterList(parameterList, lambdaParams.getStart());
} else if (lambdaParams.formalParameterList() != null) {
params = convert(lambdaParams.formalParameterList());
@ -961,8 +943,7 @@ public class StatementGenerator {
Block block;
if (expression.lambdaBody().expression() != null) {
List<Statement> statements = new ArrayList<>();
statements.add(new Return(lambdaGenerator.convert(expression.lambdaBody().expression()),
expression.lambdaBody().expression().getStart()));
statements.add(new Return(lambdaGenerator.convert(expression.lambdaBody().expression()), expression.lambdaBody().expression().getStart()));
block = new Block(statements, expression.lambdaBody().getStart());
} else {
block = lambdaGenerator.convert(expression.lambdaBody().block(), true);