diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 index 083cdacc..d8039c57 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -610,7 +610,7 @@ methodCall ; expression - : primary + : primary #primaryexpression | expression bop='.' ( identifier @@ -619,31 +619,31 @@ expression | NEW nonWildcardTypeArguments? innerCreator | SUPER superSuffix | explicitGenericInvocation - ) - | expression '[' expression ']' - | methodCall - | NEW creator - | '(' annotation* typeType ('&' typeType)* ')' expression - | expression postfix=('++' | '--') - | prefix=('+'|'-'|'++'|'--') expression - | prefix=('~'|'!') expression - | expression bop=('*'|'/'|'%') expression - | expression bop=('+'|'-') expression - | expression ('<' '<' | '>' '>' '>' | '>' '>') expression - | expression bop=('<=' | '>=' | '>' | '<') expression - | expression bop=INSTANCEOF (typeType | pattern) - | expression bop=('==' | '!=') expression - | expression bop='&' expression - | expression bop='^' expression - | expression bop='|' expression - | expression bop='&&' expression - | expression bop='||' expression - | expression bop='?' expression ':' expression + ) # dottedexpression + | expression '[' expression ']' #arrayexpression + | methodCall #methodcallexpression + | NEW creator #newinstanceexpression + | '(' annotation* typeType ('&' typeType)* ')' expression #castexpression + | expression postfix=('++' | '--') #postfixexpression + | prefix=('+'|'-'|'++'|'--') expression #prefixincdecexpression + | prefix=('~'|'!') expression #prefixnegateexpression + | expression bop=('*'|'/'|'%') expression #mathmuldivmodexpression + | expression bop=('+'|'-') expression #mathaddsubexpression + | expression ('<' '<' | '>' '>' '>' | '>' '>') expression #shifexpression + | expression bop=('<=' | '>=' | '>' | '<') expression #compareexpression + | expression bop=INSTANCEOF (typeType | pattern) #instanceofexpression + | expression bop=('==' | '!=') expression #compareequalityexpression + | expression bop='&' expression #bitwiseandexpression + | expression bop='^' expression #bitwisexorexpression + | expression bop='|' expression #bitwiseorexpression + | expression bop='&&' expression #andexpression + | expression bop='||' expression #orexpression + | expression bop='?' expression ':' expression #conditionalassignexpression | expression bop=('=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '>>=' | '>>>=' | '<<=' | '%=') - expression - | lambdaExpression // Java8 - | switchExpression // Java17 + expression #assignexpression + | lambdaExpression #lambdaexpression // Java8 + | switchExpression #switchexpression // Java17 // Java 8 methodReference | expression '::' typeArguments? identifier diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/ASTGen.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/ASTGen.java index 37da5728..5ba6bc72 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/ASTGen.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/ASTGen.java @@ -298,10 +298,11 @@ public class ASTGen { RefType superClass, GenericsRegistry generics) { GenericsRegistry localgenerics = generics; MethodDeclarationContext methoddeclaration; - GenericDeclarationListContext genericdeclarations;GenericDeclarationList gtvDeclarations; + GenericDeclarationListContext genericdeclarations; + GenericDeclarationList gtvDeclarations; MethodHeaderContext header; String name; - if(methodContext instanceof GenericmethodContext){ + if (methodContext instanceof GenericmethodContext) { GenericmethodContext gmc = new GenericmethodContext(methodContext); genericdeclarations = gmc.genericMethodDeclaration().genericDeclarationList(); methoddeclaration = gmc.genericMethodDeclaration().methodDeclaration(); @@ -309,7 +310,7 @@ public class ASTGen { name = header.identifier().getText(); localgenerics.putAll(createGenerics(genericdeclarations, parentClass, name, reg, generics)); gtvDeclarations = TypeGenerator.convert(genericdeclarations, parentClass, name, reg, localgenerics); - }else{ + } else { MethoddeclContext mdc = new MethoddeclContext(methodContext); methoddeclaration = mdc.methodDeclaration(); header = methoddeclaration.methodHeader(); @@ -317,10 +318,9 @@ public class ASTGen { } RefTypeOrTPHOrWildcardOrGeneric retType; - if(header.refType() != null){ - retType = TypeGenerator.convert(header.refType()) + if (header.refType() != null) { + retType = TypeGenerator.convert(header.refType()); } - return null; } diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index ade1028c..882e6215 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -11,6 +11,7 @@ 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.MethodCallContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ReturnstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SemistmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.StmtexpressionContext; @@ -157,71 +158,105 @@ public class StatementGenerator { 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()) { - - } else if (!(op = expr.postfix.getText()).isEmpty()) { + Token offset = stmt.getStart(); + String op = expr.bop.getText(); + if(!Objects.isNull(expr.methodCall())){ + return convertMethodCall(expr, offset); + }else if(!Objects.isNull(expr.innerCreator())){ + }else if() + else if (!op.isEmpty()) { + return convertAssignment(expr, op); } - if (stmt.preIncrementExpression() != null) { - return convert(stmt.preIncrementExpression()); - } else if (stmt.preDecrementExpression() != null) { - return convert(stmt.preDecrementExpression()); - } else if (stmt.postIncrementExpression() != null) { - return convert(stmt.postIncrementExpression()); - } else if (stmt.postDecrementExpression() != null) { - return convert(stmt.postDecrementExpression()); - } else if (stmt.methodInvocation() != null) { - return convert(stmt.methodInvocation()); - } else if (stmt.classInstanceCreationExpression() != null) { - return convert(stmt.classInstanceCreationExpression()); + 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()); + } } - private Statement convert(Java17Parser.AssignmentContext stmt) { - + private Statement convertAssignment(ExpressionContext expr, String operator) { + switch (operator) { + 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; + case "+=": + case "-=": + case "*=": + case "/=": + case "&=": + case "|=": + case "^=": + case ">>=": + case ">>>=": + case "<<=": + case "%=": + default: + throw new NotImplementedException(); + } } - public Receiver getReceiver(Expression expr) { - if (expr instanceof StaticClassName) { - return (Receiver) expr; + private Statement convertPrefixStatementExpression(ExpressionContext expr, Token offset) { + String op = expr.bop.getText(); + Expression argument = convert(expr); + Statement ret; + if (op.equals("++")) { + ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset); + ret.setStatement(); + return ret; + } else if (op.equals("--")) { + ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, argument, TypePlaceholder.fresh(offset), offset); + ret.setStatement(); + return ret; } else { - return new ExpressionReceiver(expr); + throw new NotImplementedException(); } } - private Statement convert(Java17Parser.MethodInvocationContext methodInvocationContext) { - String name; - if (methodInvocationContext.methodName() != null) { - name = methodInvocationContext.methodName().Identifier().getText(); - } else {// if(methodInvocationContext.Identifier() != null){ - name = methodInvocationContext.Identifier().getText(); + private Statement convertPostfixStatementExpression(ExpressionContext expr, Token offset) { + String op = expr.bop.getText(); + Expression argument = convert(expr); + Statement ret; + if (op.equals("++")) { + ret = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, argument, TypePlaceholder.fresh(offset), offset); + ret.setStatement(); + return ret; + } else if (op.equals("--")) { + ret = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, argument, TypePlaceholder.fresh(offset), offset); + ret.setStatement(); + return ret; + } else { + throw new NotImplementedException(); } + } + + 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())) { + // if(methodInvocationContext.Identifier() != null){ + name = methodc.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()); @@ -236,17 +271,26 @@ public class StatementGenerator { } else throw new NotImplementedException(); - ArgumentList argumentList = convert(methodInvocationContext.argumentList()); + ArgumentList argumentList = convertArguments(methodc.expressionList()); ArrayList argTypes = argumentList.getArguments().stream() - .map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())) + .map(x -> (RefTypeOrTPHOrWildcardOrGeneric) TypePlaceholder.fresh(offset)) .collect(Collectors.toCollection(ArrayList::new)); - MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), + + MethodCall ret = new MethodCall(TypePlaceholder.fresh(offset), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), argTypes, methodInvocationContext.getStart()); ret.setStatement(); return ret; } + public Receiver getReceiver(Expression expr) { + if (expr instanceof StaticClassName) { + return (Receiver) expr; + } else { + return new ExpressionReceiver(expr); + } + } + private ArgumentList convert(Java17Parser.ArgumentListContext argumentListContext) { if (argumentListContext == null) return new ArgumentList(new ArrayList<>(), new NullToken()); @@ -344,36 +388,6 @@ public class StatementGenerator { return ret; } - 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); - ret.setStatement(); - return ret; - } - - private Statement convert(Java17Parser.PreDecrementExpressionContext stmt) { - Statement ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()), - TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); - ret.setStatement(); - return ret; - } - - private Statement convert(Java17Parser.PostIncrementExpressionContext stmt) { - Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), - TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); - ret.setStatement(); - return ret; - } - - private Statement convert(Java17Parser.PostDecrementExpressionContext stmt) { - Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()), - TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); - ret.setStatement(); - return ret; - } - private AssignLeftSide convert(String leftHandSide, Token start) { Expression leftSide = generateLocalOrFieldVarOrClassName(leftHandSide, start); if (leftSide instanceof FieldVar) @@ -449,6 +463,15 @@ public class StatementGenerator { throw new NotImplementedException(); } + private ArgumentList convertArguments(Java17Parser.ExpressionListContext arglist) { + Token offset = arglist.getStart(); + List arguments = new ArrayList(); + for (ExpressionContext arg : arglist.expression()) { + arguments.add(convert(arg)); + } + return new ArgumentList(arguments, offset); + } + private List convert(Java17Parser.LocalVariableDeclarationContext declaration) { List ret = new ArrayList<>(); if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) {