From c118c05bb2475f58e81b5e5718f2fc3a97550eb6 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Sun, 26 Feb 2023 21:04:51 +0100 Subject: [PATCH] Umwandlung von Methodenaufrufen implementiert --- .../parser/antlr/Java17Parser.g4 | 11 +- .../StatementGenerator.java | 161 +++++++++--------- 2 files changed, 89 insertions(+), 83 deletions(-) diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 index d8039c57..588c468c 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -538,7 +538,7 @@ statement | YIELD expression ';' #yieldstmt // Java17 | SEMI #semistmt | statementExpression=expression ';' #stmtexpression - | switchExpression ';'? #switchexpression // Java17 + | switchExpression ';'? #switchexpressionstmt // Java17 | identifierLabel=identifier ':' statement #labeledstmt ; @@ -625,8 +625,7 @@ expression | NEW creator #newinstanceexpression | '(' annotation* typeType ('&' typeType)* ')' expression #castexpression | expression postfix=('++' | '--') #postfixexpression - | prefix=('+'|'-'|'++'|'--') expression #prefixincdecexpression - | prefix=('~'|'!') expression #prefixnegateexpression + | prefix=('+'|'-'|'++'|'--'|'~'|'!') expression #prefixexpression | expression bop=('*'|'/'|'%') expression #mathmuldivmodexpression | expression bop=('+'|'-') expression #mathaddsubexpression | expression ('<' '<' | '>' '>' '>' | '>' '>') expression #shifexpression @@ -646,9 +645,9 @@ expression | switchExpression #switchexpression // Java17 // Java 8 methodReference - | expression '::' typeArguments? identifier - | typeType '::' (typeArguments? identifier | NEW) - | classType '::' typeArguments? NEW + | expression '::' typeArguments? identifier #methodreferenceexpression + | typeType '::' (typeArguments? identifier | NEW) #methodorcreatorreferenceexpression + | classType '::' typeArguments? NEW #creatorreferenceexpression ; // Java17 diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 882e6215..1fb0c6a6 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -4,18 +4,26 @@ 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.AssignexpressionContext; 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.DottedexpressionContext; 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.MethodcallexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.MethodCallContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.NewinstanceexpressionContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.PostfixexpressionContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.PrefixexpressionContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryexpressionContext; 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.SwitchexpressionstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ThrowstmtContext; @@ -105,7 +113,7 @@ public class StatementGenerator { return convert(dowhileloop); case SwitchstmtContext switchstmt: return convert(switchstmt); - case SwitchexpressionContext switchexpression: + case SwitchexpressionstmtContext switchexpression: return convert(switchexpression); case ReturnstmtContext returnstmt: return convert(returnstmt); @@ -159,32 +167,27 @@ public class StatementGenerator { private Statement convert(Java17Parser.StmtexpressionContext stmt) { ExpressionContext expr = stmt.statementExpression; Token offset = stmt.getStart(); - String op = expr.bop.getText(); - if(!Objects.isNull(expr.methodCall())){ - return convertMethodCall(expr, offset); - }else if(!Objects.isNull(expr.innerCreator())){ + switch (expr) { + case DottedexpressionContext dotted: + return convertDottedExpression(dotted, offset); + case MethodcallexpressionContext methodc: + return convertMethodCall(methodc.methodCall(), offset); + case NewinstanceexpressionContext newinstance: - }else if() - else if (!op.isEmpty()) { - return convertAssignment(expr, op); - } - - if (!(op = expr.prefix.getText()).isEmpty()) { - return convertPrefixStatementExpression(expr, offset); - } else if (!(op = expr.postfix.getText()).isEmpty()) { - return convertPostfixStatementExpression(expr, offset); - } else if (!(Objects.isNull(expr.methodCall()))) { - return convertMethodCall(expr, offset); - } else - throw new NotImplementedException(); - - if (stmt.classInstanceCreationExpression() != null) { - return convert(stmt.classInstanceCreationExpression()); + break; + case AssignexpressionContext assignment: + return convertAssignment(assignment); + case PostfixexpressionContext postfix: + return convertPostfixStatementExpression(postfix.expression(), postfix.postfix.getText(), offset); + case PrefixexpressionContext prefix: + return convertPrefixStatementExpression(prefix.expression(), prefix.prefix.getText(), offset); + default: + throw new NotImplementedException(); } } - private Statement convertAssignment(ExpressionContext expr, String operator) { - switch (operator) { + private Statement convertAssignment(AssignexpressionContext expr) { + switch (expr.bop.getText()) { case "=": ExpressionContext leftside = expr.expression(0); AssignLeftSide leftHandSide = convert(leftside.getText(), leftside.getStart()); @@ -207,8 +210,7 @@ public class StatementGenerator { } } - private Statement convertPrefixStatementExpression(ExpressionContext expr, Token offset) { - String op = expr.bop.getText(); + private Statement convertPrefixStatementExpression(ExpressionContext expr, String op, Token offset) { Expression argument = convert(expr); Statement ret; if (op.equals("++")) { @@ -224,8 +226,7 @@ public class StatementGenerator { } } - private Statement convertPostfixStatementExpression(ExpressionContext expr, Token offset) { - String op = expr.bop.getText(); + private Statement convertPostfixStatementExpression(ExpressionContext expr, String op, Token offset) { Expression argument = convert(expr); Statement ret; if (op.equals("++")) { @@ -241,48 +242,69 @@ public class StatementGenerator { } } - private Statement convertMethodCall(ExpressionContext expr, Token offset) { - MethodCallContext methodc = expr.methodCall(); - String name; - Expression receiver; - if (methodc.identifier() != null) { - name = methodc.identifier().getText(); - receiver = null; - } else if (!Objects.isNull(methodc.SUPER())) { + private Statement convertDottedExpression(DottedexpressionContext expr, Token offset) { + if (!Objects.isNull(expr.methodCall())) { + return convertMethodCall(expr.methodCall(), expr.expression(), offset); + } else if (!Objects.isNull(expr.NEW())) { + return null; + } else { + throw new NotImplementedException(); + } + } + + private MethodCall convertMethodCall(MethodCallContext expr, Token offset) { + String name = "this"; + Expression receiver = new This(offset); + if (expr.identifier() != null) { + name = expr.identifier().getText(); + } else if (!Objects.isNull(expr.SUPER())) { // if(methodInvocationContext.Identifier() != null){ - name = methodc.SUPER().getText(); + name = expr.SUPER().getText(); receiver = new Super(offset); - } else if (!Objects.isNull(methodc.THIS())) { - name = methodc.THIS().getText(); - receiver = new This(offset); } - if (methodInvocationContext.typeName() != null) { - receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), - methodInvocationContext.typeName().getStart()); - } else if (methodInvocationContext.expressionName() != null) { - receiver = convert(methodInvocationContext.expressionName()); - } else if (methodInvocationContext.primary() != null) { - receiver = convert(methodInvocationContext.primary()); - } else if (methodInvocationContext.toString().startsWith("super")) { - receiver = new Super(methodInvocationContext.getStart()); - } else if (methodInvocationContext.methodName() != null) { - receiver = new This(methodInvocationContext.getStart()); - } else - throw new NotImplementedException(); - - ArgumentList argumentList = convertArguments(methodc.expressionList()); + ArgumentList argumentList = convertArguments(expr.expressionList()); ArrayList argTypes = argumentList.getArguments().stream() .map(x -> (RefTypeOrTPHOrWildcardOrGeneric) TypePlaceholder.fresh(offset)) .collect(Collectors.toCollection(ArrayList::new)); MethodCall ret = new MethodCall(TypePlaceholder.fresh(offset), - getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), - argTypes, methodInvocationContext.getStart()); + getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), + argTypes, offset); ret.setStatement(); return ret; } + private MethodCall convertMethodCall(MethodCallContext expr, ExpressionContext receiver, Token offset) { + String name = "this"; + if (expr.identifier() != null) { + name = expr.identifier().getText(); + } else if (!Objects.isNull(expr.SUPER())) { + // if(methodInvocationContext.Identifier() != null){ + name = expr.SUPER().getText(); + } + ArgumentList argumentList = convertArguments(expr.expressionList()); + ArrayList argTypes = argumentList.getArguments().stream() + .map(x -> (RefTypeOrTPHOrWildcardOrGeneric) TypePlaceholder.fresh(offset)) + .collect(Collectors.toCollection(ArrayList::new)); + + MethodCall ret = new MethodCall(TypePlaceholder.fresh(offset), + getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), + argTypes, offset); + ret.setStatement(); + return ret; + } + + public Receiver getReceiver(ExpressionContext expr) { + Expression expression; + if (expr instanceof PrimaryexpressionContext pc) { + expression = convert(pc.primary()); + } else { + expression = generateLocalOrFieldVarOrClassName(expr.getText(), expr.getStart()); + } + return getReceiver(expression); + } + public Receiver getReceiver(Expression expr) { if (expr instanceof StaticClassName) { return (Receiver) expr; @@ -291,21 +313,6 @@ public class StatementGenerator { } } - private ArgumentList convert(Java17Parser.ArgumentListContext argumentListContext) { - if (argumentListContext == null) - return new ArgumentList(new ArrayList<>(), new NullToken()); - List 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(); - - return new ArgumentList(args, offset); - } - /** * Der Parser kann nicht zwischen einer lokalen Variable, einem Feldzugriff und * einer Klassenangabe unterscheiden. @@ -359,11 +366,7 @@ public class StatementGenerator { return new FieldVar(receiver, parts[parts.length - 1], TypePlaceholder.fresh(offset), offset); } - private Expression convert(Java17Parser.ExpressionNameContext expressionNameContext) { - return generateLocalOrFieldVarOrClassName(expressionNameContext.getText(), expressionNameContext.getStart()); - } - - private Statement convert(Java17Parser.ClassInstanceCreationExpressionContext newExpression) { + private Statement convert(Java17Parser.NewinstanceexpressionContext newExpression) { Java17Parser.TypeArgumentsContext genericArgs = null; if (newExpression.expressionName() != null) throw new NotImplementedException(); @@ -464,11 +467,15 @@ public class StatementGenerator { } private ArgumentList convertArguments(Java17Parser.ExpressionListContext arglist) { - Token offset = arglist.getStart(); + if (arglist == null) + return new ArgumentList(new ArrayList<>(), new NullToken()); + Token offset = new NullToken(); List arguments = new ArrayList(); for (ExpressionContext arg : arglist.expression()) { arguments.add(convert(arg)); } + if (arguments.size() > 0) + offset = arguments.get(0).getOffset(); return new ArgumentList(arguments, offset); }