Antlr labels für ExpressionContext eingeführt, um StatementGenerator weiterzuentwickeln

This commit is contained in:
luca9913 2023-02-22 22:43:25 +01:00
parent c38bf658fc
commit aa5f79257e
3 changed files with 136 additions and 113 deletions

View File

@ -610,7 +610,7 @@ methodCall
; ;
expression expression
: primary : primary #primaryexpression
| expression bop='.' | expression bop='.'
( (
identifier identifier
@ -619,31 +619,31 @@ expression
| NEW nonWildcardTypeArguments? innerCreator | NEW nonWildcardTypeArguments? innerCreator
| SUPER superSuffix | SUPER superSuffix
| explicitGenericInvocation | explicitGenericInvocation
) ) # dottedexpression
| expression '[' expression ']' | expression '[' expression ']' #arrayexpression
| methodCall | methodCall #methodcallexpression
| NEW creator | NEW creator #newinstanceexpression
| '(' annotation* typeType ('&' typeType)* ')' expression | '(' annotation* typeType ('&' typeType)* ')' expression #castexpression
| expression postfix=('++' | '--') | expression postfix=('++' | '--') #postfixexpression
| prefix=('+'|'-'|'++'|'--') expression | prefix=('+'|'-'|'++'|'--') expression #prefixincdecexpression
| prefix=('~'|'!') expression | prefix=('~'|'!') expression #prefixnegateexpression
| expression bop=('*'|'/'|'%') expression | expression bop=('*'|'/'|'%') expression #mathmuldivmodexpression
| expression bop=('+'|'-') expression | expression bop=('+'|'-') expression #mathaddsubexpression
| expression ('<' '<' | '>' '>' '>' | '>' '>') expression | expression ('<' '<' | '>' '>' '>' | '>' '>') expression #shifexpression
| expression bop=('<=' | '>=' | '>' | '<') expression | expression bop=('<=' | '>=' | '>' | '<') expression #compareexpression
| expression bop=INSTANCEOF (typeType | pattern) | expression bop=INSTANCEOF (typeType | pattern) #instanceofexpression
| expression bop=('==' | '!=') expression | expression bop=('==' | '!=') expression #compareequalityexpression
| expression bop='&' expression | expression bop='&' expression #bitwiseandexpression
| expression bop='^' expression | expression bop='^' expression #bitwisexorexpression
| expression bop='|' expression | expression bop='|' expression #bitwiseorexpression
| expression bop='&&' expression | expression bop='&&' expression #andexpression
| expression bop='||' expression | expression bop='||' expression #orexpression
| <assoc=right> expression bop='?' expression ':' expression | <assoc=right> expression bop='?' expression ':' expression #conditionalassignexpression
| <assoc=right> expression | <assoc=right> expression
bop=('=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '>>=' | '>>>=' | '<<=' | '%=') bop=('=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '>>=' | '>>>=' | '<<=' | '%=')
expression expression #assignexpression
| lambdaExpression // Java8 | lambdaExpression #lambdaexpression // Java8
| switchExpression // Java17 | switchExpression #switchexpression // Java17
// Java 8 methodReference // Java 8 methodReference
| expression '::' typeArguments? identifier | expression '::' typeArguments? identifier

View File

@ -298,7 +298,8 @@ public class ASTGen {
RefType superClass, GenericsRegistry generics) { RefType superClass, GenericsRegistry generics) {
GenericsRegistry localgenerics = generics; GenericsRegistry localgenerics = generics;
MethodDeclarationContext methoddeclaration; MethodDeclarationContext methoddeclaration;
GenericDeclarationListContext genericdeclarations;GenericDeclarationList gtvDeclarations; GenericDeclarationListContext genericdeclarations;
GenericDeclarationList gtvDeclarations;
MethodHeaderContext header; MethodHeaderContext header;
String name; String name;
if (methodContext instanceof GenericmethodContext) { if (methodContext instanceof GenericmethodContext) {
@ -318,10 +319,9 @@ public class ASTGen {
RefTypeOrTPHOrWildcardOrGeneric retType; RefTypeOrTPHOrWildcardOrGeneric retType;
if (header.refType() != null) { if (header.refType() != null) {
retType = TypeGenerator.convert(header.refType()) retType = TypeGenerator.convert(header.refType());
} }
return null; return null;
} }

View File

@ -11,6 +11,7 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.ContinuestmtContext;
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.MethodCallContext;
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;
@ -157,10 +158,39 @@ public class StatementGenerator {
private Statement convert(Java17Parser.StmtexpressionContext stmt) { private Statement convert(Java17Parser.StmtexpressionContext stmt) {
ExpressionContext expr = stmt.statementExpression; ExpressionContext expr = stmt.statementExpression;
String op; Token offset = stmt.getStart();
if (!(op = expr.bop.getText()).isEmpty()) { String op = expr.bop.getText();
switch (op) { 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 (!(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 convertAssignment(ExpressionContext expr, String operator) {
switch (operator) {
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;
case "+=": case "+=":
case "-=": case "-=":
case "*=": case "*=":
@ -172,56 +202,61 @@ public class StatementGenerator {
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: default:
throw new NotImplementedException(); throw new NotImplementedException();
} }
} else if (!(op = expr.prefix.getText()).isEmpty()) {
} else if (!(op = expr.postfix.getText()).isEmpty()) {
} }
if (stmt.preIncrementExpression() != null) { private Statement convertPrefixStatementExpression(ExpressionContext expr, Token offset) {
return convert(stmt.preIncrementExpression()); String op = expr.bop.getText();
} else if (stmt.preDecrementExpression() != null) { Expression argument = convert(expr);
return convert(stmt.preDecrementExpression()); Statement ret;
} else if (stmt.postIncrementExpression() != null) { if (op.equals("++")) {
return convert(stmt.postIncrementExpression()); ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset);
} else if (stmt.postDecrementExpression() != null) { ret.setStatement();
return convert(stmt.postDecrementExpression()); return ret;
} else if (stmt.methodInvocation() != null) { } else if (op.equals("--")) {
return convert(stmt.methodInvocation()); ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, argument, TypePlaceholder.fresh(offset), offset);
} else if (stmt.classInstanceCreationExpression() != null) { ret.setStatement();
return convert(stmt.classInstanceCreationExpression()); return ret;
} else } else {
throw new NotImplementedException(); throw new NotImplementedException();
} }
private Statement convert(Java17Parser.AssignmentContext stmt) {
} }
public Receiver getReceiver(Expression expr) { private Statement convertPostfixStatementExpression(ExpressionContext expr, Token offset) {
if (expr instanceof StaticClassName) { String op = expr.bop.getText();
return (Receiver) expr; 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 { } else {
return new ExpressionReceiver(expr); throw new NotImplementedException();
} }
} }
private Statement convert(Java17Parser.MethodInvocationContext methodInvocationContext) { private Statement convertMethodCall(ExpressionContext expr, Token offset) {
MethodCallContext methodc = expr.methodCall();
String name; String name;
if (methodInvocationContext.methodName() != null) {
name = methodInvocationContext.methodName().Identifier().getText();
} else {// if(methodInvocationContext.Identifier() != null){
name = methodInvocationContext.Identifier().getText();
}
Expression receiver; 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) { if (methodInvocationContext.typeName() != null) {
receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(),
methodInvocationContext.typeName().getStart()); methodInvocationContext.typeName().getStart());
@ -236,17 +271,26 @@ public class StatementGenerator {
} else } else
throw new NotImplementedException(); throw new NotImplementedException();
ArgumentList argumentList = convert(methodInvocationContext.argumentList()); ArgumentList argumentList = convertArguments(methodc.expressionList());
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> argTypes = argumentList.getArguments().stream() ArrayList<RefTypeOrTPHOrWildcardOrGeneric> argTypes = argumentList.getArguments().stream()
.map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())) .map(x -> (RefTypeOrTPHOrWildcardOrGeneric) TypePlaceholder.fresh(offset))
.collect(Collectors.toCollection(ArrayList::new)); .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()), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()),
argTypes, methodInvocationContext.getStart()); argTypes, methodInvocationContext.getStart());
ret.setStatement(); ret.setStatement();
return ret; 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) { private ArgumentList convert(Java17Parser.ArgumentListContext argumentListContext) {
if (argumentListContext == null) if (argumentListContext == null)
return new ArgumentList(new ArrayList<>(), new NullToken()); return new ArgumentList(new ArrayList<>(), new NullToken());
@ -344,36 +388,6 @@ public class StatementGenerator {
return ret; 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) { private AssignLeftSide convert(String leftHandSide, Token start) {
Expression leftSide = generateLocalOrFieldVarOrClassName(leftHandSide, start); Expression leftSide = generateLocalOrFieldVarOrClassName(leftHandSide, start);
if (leftSide instanceof FieldVar) if (leftSide instanceof FieldVar)
@ -449,6 +463,15 @@ public class StatementGenerator {
throw new NotImplementedException(); throw new NotImplementedException();
} }
private ArgumentList convertArguments(Java17Parser.ExpressionListContext arglist) {
Token offset = arglist.getStart();
List<Expression> arguments = new ArrayList<Expression>();
for (ExpressionContext arg : arglist.expression()) {
arguments.add(convert(arg));
}
return new ArgumentList(arguments, offset);
}
private List<Statement> convert(Java17Parser.LocalVariableDeclarationContext declaration) { private List<Statement> convert(Java17Parser.LocalVariableDeclarationContext declaration) {
List<Statement> ret = new ArrayList<>(); List<Statement> ret = new ArrayList<>();
if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) { if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) {