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
: 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
| <assoc=right> 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
| <assoc=right> expression bop='?' expression ':' expression #conditionalassignexpression
| <assoc=right> expression
bop=('=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '>>=' | '>>>=' | '<<=' | '%=')
expression
| lambdaExpression // Java8
| switchExpression // Java17
expression #assignexpression
| lambdaExpression #lambdaexpression // Java8
| switchExpression #switchexpression // Java17
// Java 8 methodReference
| expression '::' typeArguments? identifier

View File

@ -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;
}

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.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<RefTypeOrTPHOrWildcardOrGeneric> 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<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) {
List<Statement> ret = new ArrayList<>();
if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) {