Umwandlung von Methodenaufrufen implementiert

This commit is contained in:
luca9913 2023-02-26 21:04:51 +01:00
parent aa5f79257e
commit c118c05bb2
2 changed files with 89 additions and 83 deletions

View File

@ -538,7 +538,7 @@ statement
| YIELD expression ';' #yieldstmt // Java17 | YIELD expression ';' #yieldstmt // Java17
| SEMI #semistmt | SEMI #semistmt
| statementExpression=expression ';' #stmtexpression | statementExpression=expression ';' #stmtexpression
| switchExpression ';'? #switchexpression // Java17 | switchExpression ';'? #switchexpressionstmt // Java17
| identifierLabel=identifier ':' statement #labeledstmt | identifierLabel=identifier ':' statement #labeledstmt
; ;
@ -625,8 +625,7 @@ expression
| NEW creator #newinstanceexpression | NEW creator #newinstanceexpression
| '(' annotation* typeType ('&' typeType)* ')' expression #castexpression | '(' annotation* typeType ('&' typeType)* ')' expression #castexpression
| expression postfix=('++' | '--') #postfixexpression | expression postfix=('++' | '--') #postfixexpression
| prefix=('+'|'-'|'++'|'--') expression #prefixincdecexpression | prefix=('+'|'-'|'++'|'--'|'~'|'!') expression #prefixexpression
| prefix=('~'|'!') expression #prefixnegateexpression
| expression bop=('*'|'/'|'%') expression #mathmuldivmodexpression | expression bop=('*'|'/'|'%') expression #mathmuldivmodexpression
| expression bop=('+'|'-') expression #mathaddsubexpression | expression bop=('+'|'-') expression #mathaddsubexpression
| expression ('<' '<' | '>' '>' '>' | '>' '>') expression #shifexpression | expression ('<' '<' | '>' '>' '>' | '>' '>') expression #shifexpression
@ -646,9 +645,9 @@ expression
| switchExpression #switchexpression // Java17 | switchExpression #switchexpression // Java17
// Java 8 methodReference // Java 8 methodReference
| expression '::' typeArguments? identifier | expression '::' typeArguments? identifier #methodreferenceexpression
| typeType '::' (typeArguments? identifier | NEW) | typeType '::' (typeArguments? identifier | NEW) #methodorcreatorreferenceexpression
| classType '::' typeArguments? NEW | classType '::' typeArguments? NEW #creatorreferenceexpression
; ;
// Java17 // Java17

View File

@ -4,18 +4,26 @@ import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java17Parser; import de.dhbwstuttgart.parser.antlr.Java17Parser;
import de.dhbwstuttgart.parser.antlr.Java17Parser.AssertstmtContext; 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.BlockstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.BreakstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.BreakstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ConditionalstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ConditionalstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ContinuestmtContext; 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.ForControlContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ForloopContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ForloopContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.LabeledstmtContext; 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.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.ReturnstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SemistmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SemistmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.StmtexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.StmtexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionContext; 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.SwitchstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ThrowstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ThrowstmtContext;
@ -105,7 +113,7 @@ public class StatementGenerator {
return convert(dowhileloop); return convert(dowhileloop);
case SwitchstmtContext switchstmt: case SwitchstmtContext switchstmt:
return convert(switchstmt); return convert(switchstmt);
case SwitchexpressionContext switchexpression: case SwitchexpressionstmtContext switchexpression:
return convert(switchexpression); return convert(switchexpression);
case ReturnstmtContext returnstmt: case ReturnstmtContext returnstmt:
return convert(returnstmt); return convert(returnstmt);
@ -159,32 +167,27 @@ public class StatementGenerator {
private Statement convert(Java17Parser.StmtexpressionContext stmt) { private Statement convert(Java17Parser.StmtexpressionContext stmt) {
ExpressionContext expr = stmt.statementExpression; ExpressionContext expr = stmt.statementExpression;
Token offset = stmt.getStart(); Token offset = stmt.getStart();
String op = expr.bop.getText(); switch (expr) {
if(!Objects.isNull(expr.methodCall())){ case DottedexpressionContext dotted:
return convertMethodCall(expr, offset); return convertDottedExpression(dotted, offset);
}else if(!Objects.isNull(expr.innerCreator())){ case MethodcallexpressionContext methodc:
return convertMethodCall(methodc.methodCall(), offset);
case NewinstanceexpressionContext newinstance:
}else if() break;
else if (!op.isEmpty()) { case AssignexpressionContext assignment:
return convertAssignment(expr, op); return convertAssignment(assignment);
} case PostfixexpressionContext postfix:
return convertPostfixStatementExpression(postfix.expression(), postfix.postfix.getText(), offset);
if (!(op = expr.prefix.getText()).isEmpty()) { case PrefixexpressionContext prefix:
return convertPrefixStatementExpression(expr, offset); return convertPrefixStatementExpression(prefix.expression(), prefix.prefix.getText(), offset);
} else if (!(op = expr.postfix.getText()).isEmpty()) { default:
return convertPostfixStatementExpression(expr, offset);
} else if (!(Objects.isNull(expr.methodCall()))) {
return convertMethodCall(expr, offset);
} else
throw new NotImplementedException(); throw new NotImplementedException();
if (stmt.classInstanceCreationExpression() != null) {
return convert(stmt.classInstanceCreationExpression());
} }
} }
private Statement convertAssignment(ExpressionContext expr, String operator) { private Statement convertAssignment(AssignexpressionContext expr) {
switch (operator) { switch (expr.bop.getText()) {
case "=": case "=":
ExpressionContext leftside = expr.expression(0); ExpressionContext leftside = expr.expression(0);
AssignLeftSide leftHandSide = convert(leftside.getText(), leftside.getStart()); AssignLeftSide leftHandSide = convert(leftside.getText(), leftside.getStart());
@ -207,8 +210,7 @@ public class StatementGenerator {
} }
} }
private Statement convertPrefixStatementExpression(ExpressionContext expr, Token offset) { private Statement convertPrefixStatementExpression(ExpressionContext expr, String op, Token offset) {
String op = expr.bop.getText();
Expression argument = convert(expr); Expression argument = convert(expr);
Statement ret; Statement ret;
if (op.equals("++")) { if (op.equals("++")) {
@ -224,8 +226,7 @@ public class StatementGenerator {
} }
} }
private Statement convertPostfixStatementExpression(ExpressionContext expr, Token offset) { private Statement convertPostfixStatementExpression(ExpressionContext expr, String op, Token offset) {
String op = expr.bop.getText();
Expression argument = convert(expr); Expression argument = convert(expr);
Statement ret; Statement ret;
if (op.equals("++")) { if (op.equals("++")) {
@ -241,48 +242,69 @@ public class StatementGenerator {
} }
} }
private Statement convertMethodCall(ExpressionContext expr, Token offset) { private Statement convertDottedExpression(DottedexpressionContext expr, Token offset) {
MethodCallContext methodc = expr.methodCall(); if (!Objects.isNull(expr.methodCall())) {
String name; return convertMethodCall(expr.methodCall(), expr.expression(), offset);
Expression receiver; } else if (!Objects.isNull(expr.NEW())) {
if (methodc.identifier() != null) { return null;
name = methodc.identifier().getText(); } else {
receiver = null; throw new NotImplementedException();
} 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) { private MethodCall convertMethodCall(MethodCallContext expr, Token offset) {
receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), String name = "this";
methodInvocationContext.typeName().getStart()); Expression receiver = new This(offset);
} else if (methodInvocationContext.expressionName() != null) { if (expr.identifier() != null) {
receiver = convert(methodInvocationContext.expressionName()); name = expr.identifier().getText();
} else if (methodInvocationContext.primary() != null) { } else if (!Objects.isNull(expr.SUPER())) {
receiver = convert(methodInvocationContext.primary()); // if(methodInvocationContext.Identifier() != null){
} else if (methodInvocationContext.toString().startsWith("super")) { name = expr.SUPER().getText();
receiver = new Super(methodInvocationContext.getStart()); receiver = new Super(offset);
} 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<RefTypeOrTPHOrWildcardOrGeneric> argTypes = argumentList.getArguments().stream() ArrayList<RefTypeOrTPHOrWildcardOrGeneric> argTypes = argumentList.getArguments().stream()
.map(x -> (RefTypeOrTPHOrWildcardOrGeneric) TypePlaceholder.fresh(offset)) .map(x -> (RefTypeOrTPHOrWildcardOrGeneric) TypePlaceholder.fresh(offset))
.collect(Collectors.toCollection(ArrayList::new)); .collect(Collectors.toCollection(ArrayList::new));
MethodCall ret = new MethodCall(TypePlaceholder.fresh(offset), MethodCall ret = new MethodCall(TypePlaceholder.fresh(offset),
getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset),
argTypes, methodInvocationContext.getStart()); argTypes, offset);
ret.setStatement(); ret.setStatement();
return ret; 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<RefTypeOrTPHOrWildcardOrGeneric> 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) { public Receiver getReceiver(Expression expr) {
if (expr instanceof StaticClassName) { if (expr instanceof StaticClassName) {
return (Receiver) expr; 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<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();
return new ArgumentList(args, offset);
}
/** /**
* Der Parser kann nicht zwischen einer lokalen Variable, einem Feldzugriff und * Der Parser kann nicht zwischen einer lokalen Variable, einem Feldzugriff und
* einer Klassenangabe unterscheiden. * einer Klassenangabe unterscheiden.
@ -359,11 +366,7 @@ public class StatementGenerator {
return new FieldVar(receiver, parts[parts.length - 1], TypePlaceholder.fresh(offset), offset); return new FieldVar(receiver, parts[parts.length - 1], TypePlaceholder.fresh(offset), offset);
} }
private Expression convert(Java17Parser.ExpressionNameContext expressionNameContext) { private Statement convert(Java17Parser.NewinstanceexpressionContext newExpression) {
return generateLocalOrFieldVarOrClassName(expressionNameContext.getText(), expressionNameContext.getStart());
}
private Statement convert(Java17Parser.ClassInstanceCreationExpressionContext newExpression) {
Java17Parser.TypeArgumentsContext genericArgs = null; Java17Parser.TypeArgumentsContext genericArgs = null;
if (newExpression.expressionName() != null) if (newExpression.expressionName() != null)
throw new NotImplementedException(); throw new NotImplementedException();
@ -464,11 +467,15 @@ public class StatementGenerator {
} }
private ArgumentList convertArguments(Java17Parser.ExpressionListContext arglist) { 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<Expression> arguments = new ArrayList<Expression>(); List<Expression> arguments = new ArrayList<Expression>();
for (ExpressionContext arg : arglist.expression()) { for (ExpressionContext arg : arglist.expression()) {
arguments.add(convert(arg)); arguments.add(convert(arg));
} }
if (arguments.size() > 0)
offset = arguments.get(0).getOffset();
return new ArgumentList(arguments, offset); return new ArgumentList(arguments, offset);
} }