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,10 +298,11 @@ 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) {
GenericmethodContext gmc = new GenericmethodContext(methodContext); GenericmethodContext gmc = new GenericmethodContext(methodContext);
genericdeclarations = gmc.genericMethodDeclaration().genericDeclarationList(); genericdeclarations = gmc.genericMethodDeclaration().genericDeclarationList();
methoddeclaration = gmc.genericMethodDeclaration().methodDeclaration(); methoddeclaration = gmc.genericMethodDeclaration().methodDeclaration();
@ -309,7 +310,7 @@ public class ASTGen {
name = header.identifier().getText(); name = header.identifier().getText();
localgenerics.putAll(createGenerics(genericdeclarations, parentClass, name, reg, generics)); localgenerics.putAll(createGenerics(genericdeclarations, parentClass, name, reg, generics));
gtvDeclarations = TypeGenerator.convert(genericdeclarations, parentClass, name, reg, localgenerics); gtvDeclarations = TypeGenerator.convert(genericdeclarations, parentClass, name, reg, localgenerics);
}else{ } else {
MethoddeclContext mdc = new MethoddeclContext(methodContext); MethoddeclContext mdc = new MethoddeclContext(methodContext);
methoddeclaration = mdc.methodDeclaration(); methoddeclaration = mdc.methodDeclaration();
header = methoddeclaration.methodHeader(); header = methoddeclaration.methodHeader();
@ -317,10 +318,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,71 +158,105 @@ 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())){
case "=": return convertMethodCall(expr, offset);
case "+=": }else if(!Objects.isNull(expr.innerCreator())){
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()) {
}else if()
else if (!op.isEmpty()) {
return convertAssignment(expr, op);
} }
if (stmt.preIncrementExpression() != null) { if (!(op = expr.prefix.getText()).isEmpty()) {
return convert(stmt.preIncrementExpression()); return convertPrefixStatementExpression(expr, offset);
} else if (stmt.preDecrementExpression() != null) { } else if (!(op = expr.postfix.getText()).isEmpty()) {
return convert(stmt.preDecrementExpression()); return convertPostfixStatementExpression(expr, offset);
} else if (stmt.postIncrementExpression() != null) { } else if (!(Objects.isNull(expr.methodCall()))) {
return convert(stmt.postIncrementExpression()); return convertMethodCall(expr, offset);
} 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());
} else } else
throw new NotImplementedException(); 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) { private Statement convertPrefixStatementExpression(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.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 { } else {
return new ExpressionReceiver(expr); throw new NotImplementedException();
} }
} }
private Statement convert(Java17Parser.MethodInvocationContext methodInvocationContext) { private Statement convertPostfixStatementExpression(ExpressionContext expr, Token offset) {
String name; String op = expr.bop.getText();
if (methodInvocationContext.methodName() != null) { Expression argument = convert(expr);
name = methodInvocationContext.methodName().Identifier().getText(); Statement ret;
} else {// if(methodInvocationContext.Identifier() != null){ if (op.equals("++")) {
name = methodInvocationContext.Identifier().getText(); 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; 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) {