Parser um return und method call erweitern

This commit is contained in:
JanUlrich 2017-02-17 16:34:40 +01:00
parent fa4a24c653
commit 656b014ad4
31 changed files with 139 additions and 85 deletions

View File

@ -1,25 +0,0 @@
package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.parser.antlr.Java8BaseListener;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
public class CompilationUnitEvaluator extends Java8BaseListener {
@Override
public void enterCompilationUnit(Java8Parser.CompilationUnitContext ctx) {
System.out.println("SourceFile(");
}
@Override
public void exitCompilationUnit(Java8Parser.CompilationUnitContext ctx) {
System.out.println(")");
}
@Override
public void enterPackageDeclaration(Java8Parser.PackageDeclarationContext ctx) {
System.out.println("package(");
}
@Override
public void exitPackageDeclaration(Java8Parser.PackageDeclarationContext ctx) {
System.out.println(ctx.Identifier());
System.out.println(")");
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.parser;
import com.sun.corba.se.spi.monitoring.StatisticMonitoredAttribute;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.modifier.*;
@ -178,6 +179,71 @@ public class SyntaxTreeGenerator{
}
private Block convert(Java8Parser.BlockContext block) {
List<Statement> statements = new ArrayList<>();
if(block.blockStatements() != null)
for(Java8Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()){
List<Statement> stmt = convert(statementContext);
statements.addAll(stmt);
}
return new Block(statements, block.getStart());
}
private List<Statement> convert(Java8Parser.BlockStatementContext statementContext) {
List<Statement> ret = new ArrayList<>();
if(statementContext.localVariableDeclarationStatement() != null){
Java8Parser.LocalVariableDeclarationContext declaration = statementContext.localVariableDeclarationStatement().localVariableDeclaration();
RefTypeOrTPH type;
if(declaration.unannType()==null){
type = TypePlaceholder.fresh(declaration.getStart());
}else{
type = convert(declaration.unannType());
}
for(Java8Parser.VariableDeclaratorContext varDecl : declaration.variableDeclaratorList().variableDeclarator()){
TerminalNode name = varDecl.variableDeclaratorId().Identifier();
ret.add(new LocalVarDecl(name.getText(), type, name.getSymbol()));
}
return ret;
}else if(statementContext.classDeclaration() != null){
throw new NotImplementedException();
}else{
Java8Parser.StatementContext stmt = statementContext.statement();
if(stmt.statementWithoutTrailingSubstatement() != null){
if(stmt.statementWithoutTrailingSubstatement().returnStatement() != null){
Java8Parser.ReturnStatementContext returnStatementContext = stmt.statementWithoutTrailingSubstatement().returnStatement();
Statement retExpr = convert(returnStatementContext.expression());
ret.add(new Return(retExpr,returnStatementContext.getStart()));
}else if(stmt.statementWithoutTrailingSubstatement().expressionStatement().statementExpression().methodInvocation() != null){
Java8Parser.MethodInvocationContext methodInvocationContext = stmt.statementWithoutTrailingSubstatement().expressionStatement().statementExpression().methodInvocation();
ret.add(convert(methodInvocationContext));
}else throw new NotImplementedException();
}else if(stmt.whileStatement() != null){
throw new NotImplementedException();
}else throw new NotImplementedException();
return ret;
}
}
private Statement convert(Java8Parser.MethodInvocationContext methodInvocationContext) {
String name = methodInvocationContext.methodName().Identifier().getText();
Expr receiver;
if(methodInvocationContext.typeName() == null){
receiver = new This(methodInvocationContext.getStart());
}else{
receiver = convert(methodInvocationContext.typeName());
}
ArgumentList argumentList = null;
return new MethodCall(new Receiver(receiver), name, argumentList, methodInvocationContext.getStart());
}
private Expr convert(Java8Parser.TypeNameContext typeNameContext) {
return null;
}
private Statement convert(Java8Parser.ExpressionContext expression) {
return null;
}

View File

@ -352,7 +352,7 @@ variableDeclaratorList
;
variableDeclarator
: variableDeclaratorId ('=' variableInitializer)?
: variableDeclaratorId //('=' variableInitializer)? //auskommentiert, weil variablenDecklaration sonst nicht eindeutig
;
variableDeclaratorId

View File

@ -2,12 +2,13 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH;
import org.antlr.v4.runtime.Token;
public class Assign extends ExprStmt
{
public Assign(RefTypeOrTPH type, int offset) {
public Assign(RefTypeOrTPH type, Token offset) {
super(type, offset);
}

View File

@ -14,7 +14,7 @@ public class Binary extends BinaryExpr
public Binary(int offset, int variableLength)
{
super(offset,variableLength);
super(null);
}

View File

@ -1,17 +1,14 @@
package de.dhbwstuttgart.syntaxtree.statement;
import org.antlr.v4.runtime.Token;
public abstract class BinaryExpr extends Expr
{
public BinaryExpr(int offset,int variableLength)
public BinaryExpr(Token offset)
{
super(null,variableLength);
super(null,null);
}
// abstract public void if_codegen(ClassFile classfile, Code_attribute code, boolean sw) throws jvmCode_Exception;
// abstract public void not_codegen(ClassFile classfile, Code_attribute code) throws jvmCode_Exception;
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.statement;
import java.util.*;
import org.antlr.v4.runtime.Token;
import org.apache.bcel.Constants;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
@ -18,8 +19,8 @@ public class Block extends Statement
private ConstantPoolGen _cp;
private ClassGen _cg;
public Block(List<Statement> statements, int offset) {
super(null, 0);
public Block(List<Statement> statements, Token offset) {
super(null, offset);
this.statements = statements;
}

View File

@ -7,7 +7,7 @@ public class CastExpr extends Expr
{
public CastExpr(RefTypeOrTPH castType, Expr expr, int offset)
{
super(null, 0);
super(null, null);
}
public Expr expr;

View File

@ -12,7 +12,7 @@ public class EmptyStmt extends Statement
{
public EmptyStmt()
{
super(null,-1);
super(null,null);
}

View File

@ -3,12 +3,13 @@ package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import org.antlr.v4.runtime.Token;
public abstract class Executeable extends SyntaxTreeNode
public abstract class Executable extends SyntaxTreeNode
{
private RefTypeOrTPH type;
public Executeable(RefTypeOrTPH type, int offset) {
public Executable(RefTypeOrTPH type, Token offset) {
this.type = type;
}

View File

@ -2,12 +2,13 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH;
import org.antlr.v4.runtime.Token;
public abstract class Expr extends Executeable
public abstract class Expr extends Executable
{
public Expr(RefTypeOrTPH type, int offset) {
public Expr(RefTypeOrTPH type, Token offset) {
super(type, offset);
}

View File

@ -1,10 +1,11 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH;
import org.antlr.v4.runtime.Token;
public abstract class ExprStmt extends Executeable{
public abstract class ExprStmt extends Executable {
public ExprStmt(RefTypeOrTPH type, int offset) {
public ExprStmt(RefTypeOrTPH type, Token offset) {
super(type, offset);
}
}

View File

@ -18,7 +18,7 @@ public class ForStmt extends Statement
public ForStmt(int offset, int variableLength)
{
super(null,variableLength);
super(null,null);
}
void sc_check(List<ClassOrInterface> classname, Hashtable ch, Hashtable<String, String> bh, boolean ext, Hashtable parach, Hashtable<String, Hashtable> parabh)

View File

@ -25,7 +25,7 @@ public class IfStmt extends Statement
{
public IfStmt(int offset, int variableLength)
{
super(null,variableLength);
super(null,null);
}
public boolean hamaDebug = true; //hama: Debug Ausgaben von mir ein- bzw. ausschalten

View File

@ -15,7 +15,7 @@ public class InstVar extends Expr
protected String type; //???? BRAUCHT MAN DEN???
public InstVar(Expr e, String n, int offset)
{
super(null,n.length());
super(null,null);
expr = e;
}

View File

@ -13,7 +13,7 @@ public class InstanceOf extends BinaryExpr
public InstanceOf(int offset,int variableLength)
{
super(offset,variableLength);
super(null);
// #JB# 20.04.2005
}

View File

@ -22,7 +22,7 @@ public class LambdaExpression extends Expr{
private ParameterList params;
public LambdaExpression(int offset, int variableLength) {
super(null, variableLength);
super(null, null);
}

View File

@ -12,7 +12,7 @@ public abstract class Literal extends Expr
private boolean primitiveFlag=true;
public Literal(RefTypeOrTPH o, int i) {
super(null, i);
super(null, null);
}
public void setPrimitiveFlag(boolean b)

View File

@ -2,6 +2,8 @@ package de.dhbwstuttgart.syntaxtree.statement;
import java.util.Enumeration;
import java.util.Hashtable;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import org.antlr.v4.runtime.Token;
import org.apache.bcel.Constants;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.Instruction;
@ -16,17 +18,14 @@ import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
public class LocalOrFieldVarOrClassname extends Expr
{
private boolean isFieldAccess = false;
private final String name;
private boolean isFieldAccess = false;
private boolean isClassAccess = false;
public LocalOrFieldVarOrClassname(int offset, int variableLength)
public LocalOrFieldVarOrClassname(String n, Token offset)
{
super(null,variableLength);
}
public LocalOrFieldVarOrClassname(String n, int offset)
{
super(null,n.length());
super(TypePlaceholder.fresh(offset),offset);
this.name = n;
}

View File

@ -1,13 +1,19 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH;
import org.antlr.v4.runtime.Token;
public class LocalVarDecl extends Statement
{
public LocalVarDecl(int offset,int variableLength)
private String name;
public LocalVarDecl(String name, RefTypeOrTPH type, Token offset)
{
super(null,variableLength);
super(type, offset);
this.name = name;
}
public Block block;
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.statement;
import java.util.Hashtable;
import org.antlr.v4.runtime.Token;
import org.apache.bcel.Const;
import org.apache.bcel.Constants;
import org.apache.bcel.generic.ClassGen;
@ -14,17 +15,13 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class MethodCall extends Expr
public class MethodCall extends Statement
{
public MethodCall(Receiver receiver, String methodName, ArgumentList argumentList, int offset){
this(offset, 0);
public MethodCall(Receiver receiver, String methodName, ArgumentList argumentList, Token offset){
super(TypePlaceholder.fresh(offset),offset);
this.set_ArgumentList(argumentList);
}
public MethodCall(int offset, int variableLength)
{
super(null,variableLength);
}
/**
* Diese Variable speichert die Expression, welche die Klasse von welcher die Methode aufgerufen wird darstellt.

View File

@ -8,7 +8,7 @@ public class NewArray extends Expr
{
public NewArray(int offset,int variableLength)
{
super(null,variableLength);
super(null,null);
}
private RefTypeOrTPH type;
public List<Expr> expr;

View File

@ -11,7 +11,7 @@ public class NewClass extends Expr
{
public NewClass(int offset,int variableLength)
{
super(null,variableLength);
super(null,null);
}
private ArgumentList arglist;

View File

@ -1,6 +1,8 @@
package de.dhbwstuttgart.syntaxtree.statement;
import java.util.Hashtable;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH;
import org.antlr.v4.runtime.Token;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.generic.InstructionList;
@ -13,9 +15,9 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class Return extends Statement
{
public Return(int offset,int variableLength)
public Return(Statement retExpr, Token offset)
{
super(null,variableLength);
super(null,null);
}
public Expr retexpr;

View File

@ -1,13 +1,14 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH;
import org.antlr.v4.runtime.Token;
public abstract class Statement extends Executeable
public abstract class Statement extends Executable
{
public Statement(RefTypeOrTPH type, int offset)
public Statement(RefTypeOrTPH type, Token offset)
{
super(type,offset);
}

View File

@ -1,16 +1,13 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import org.antlr.v4.runtime.Token;
public class This extends Expr
{
public This(int offset,int variableLength)
public This(Token offset)
{
super(null,variableLength);
}
public This(SyntaxTreeNode parent){
this(0,0);
super(null,null);
}
public ArgumentList arglist;

View File

@ -8,7 +8,7 @@ public class ThisCall extends MethodCall
{
public ThisCall(Receiver receiver, ArgumentList arglist, int offset)
{
super(null, null, null, 0);
super(null, null, null, null);
}

View File

@ -8,7 +8,7 @@ public abstract class UnaryExpr extends MethodCall
public UnaryExpr(int offset,int variableLength)
{
super(offset,variableLength);
super(null,null,null,null);
}

View File

@ -5,7 +5,7 @@ public class WhileStmt extends Statement
{
public WhileStmt(int offset, int variableLength)
{
super(null,variableLength);
super(null,null);
}
public Expr expr;

View File

@ -27,6 +27,7 @@ public class GeneralParserTest{
List<String> filenames = new ArrayList<String>();
filenames.add("FieldInitializationTest.jav");
filenames.add("ImportTest.jav");
filenames.add("StatementsTest.jav");
filenames.add("ImportTestGeneric.jav");
//filenames.add("BoundedParameter.jav");
//filenames.add("GenericFieldVarTest.jav");

View File

@ -0,0 +1,8 @@
class Statements{
public void methodeTest(){
methodeTest();
return null;
}
}