From 39747f4138a6e4ab2e6945ec4c7ee1f3285d0df5 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Fri, 3 Feb 2017 15:20:26 +0100 Subject: [PATCH] ANTLR Parser SyntaxtreeGenerator erweitern. --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 14 +++ src/de/dhbwstuttgart/parser/JavaTXParser.java | 24 ++++ src/de/dhbwstuttgart/parser/JavaXParser.java | 7 -- .../parser/SyntaxTreeGenerator.java | 115 ++++++++++++++++-- src/de/dhbwstuttgart/parser/antlr/Java8.g4 | 4 +- .../syntaxtree/ClassOrInterface.java | 10 +- .../dhbwstuttgart/syntaxtree/Constructor.java | 8 +- src/de/dhbwstuttgart/syntaxtree/Field.java | 8 +- .../syntaxtree/FieldDeclaration.java | 10 +- .../syntaxtree/GenericDeclarationList.java | 10 +- src/de/dhbwstuttgart/syntaxtree/Method.java | 9 +- .../modifier/fieldModifier/FieldModifier.java | 4 + .../methodModifier/MethodModifier.java | 6 + .../syntaxtree/type/ExtendsWildcardType.java | 3 +- .../dhbwstuttgart/syntaxtree/type/FunN.java | 2 +- .../syntaxtree/type/RefType.java | 5 +- .../syntaxtree/type/SuperWildcardType.java | 9 +- .../syntaxtree/type/TypePlaceholder.java | 66 ++-------- .../dhbwstuttgart/syntaxtree/type/Void.java | 17 +++ .../syntaxtree/type/WildcardType.java | 4 +- .../typecheck/JavaClassName.java | 3 +- .../typecheck/JavaClassRegistry.java | 11 +- test/parser/GeneralParserTest.java | 12 +- 23 files changed, 246 insertions(+), 115 deletions(-) create mode 100644 src/de/dhbwstuttgart/core/JavaTXCompiler.java create mode 100644 src/de/dhbwstuttgart/parser/JavaTXParser.java delete mode 100644 src/de/dhbwstuttgart/parser/JavaXParser.java create mode 100644 src/de/dhbwstuttgart/syntaxtree/modifier/fieldModifier/FieldModifier.java create mode 100644 src/de/dhbwstuttgart/syntaxtree/modifier/methodModifier/MethodModifier.java create mode 100755 src/de/dhbwstuttgart/syntaxtree/type/Void.java diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java new file mode 100644 index 000000000..c3f17a513 --- /dev/null +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -0,0 +1,14 @@ +package de.dhbwstuttgart.core; + +import de.dhbwstuttgart.parser.JavaTXParser; + +import java.io.File; +import java.io.IOException; + +public class JavaTXCompiler { + + public void parse(File sourceFile) throws IOException { + new JavaTXParser().parse(sourceFile); + } + +} diff --git a/src/de/dhbwstuttgart/parser/JavaTXParser.java b/src/de/dhbwstuttgart/parser/JavaTXParser.java new file mode 100644 index 000000000..b16745ece --- /dev/null +++ b/src/de/dhbwstuttgart/parser/JavaTXParser.java @@ -0,0 +1,24 @@ +package de.dhbwstuttgart.parser; + +import de.dhbwstuttgart.parser.antlr.Java8Lexer; +import de.dhbwstuttgart.parser.antlr.Java8Parser; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.modifier.Modifier; +import org.antlr.v4.runtime.ANTLRInputStream; +import org.antlr.v4.runtime.CommonTokenStream; + +import java.io.*; + +public class JavaTXParser { + public SourceFile parse(File sourceFile) throws IOException { + InputStream stream = new FileInputStream(sourceFile); + ANTLRInputStream input = new ANTLRInputStream(stream); + Java8Lexer lexer = new Java8Lexer(input); + CommonTokenStream tokens = new CommonTokenStream(lexer); + Java8Parser parser = new Java8Parser(tokens); + Java8Parser.CompilationUnitContext tree = parser.compilationUnit(); + SyntaxTreeGenerator generator = new SyntaxTreeGenerator(); + return generator.convert(tree); + } +} diff --git a/src/de/dhbwstuttgart/parser/JavaXParser.java b/src/de/dhbwstuttgart/parser/JavaXParser.java deleted file mode 100644 index ab9e7c8c8..000000000 --- a/src/de/dhbwstuttgart/parser/JavaXParser.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.dhbwstuttgart.parser; - -/** - * Created by janulrich on 26.01.17. - */ -public class JavaXParser { -} diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java index 856c821e7..caca178f0 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java @@ -3,20 +3,25 @@ package de.dhbwstuttgart.parser; import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.modifier.*; +import de.dhbwstuttgart.syntaxtree.modifier.fieldModifier.FieldModifier; +import de.dhbwstuttgart.syntaxtree.modifier.methodModifier.MethodModifier; import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typecheck.*; import java.util.ArrayList; +import java.util.Collection; import java.util.List; + +import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; public class SyntaxTreeGenerator{ JavaClassRegistry reg = new JavaClassRegistry(); - String pkgName = null; + String pkgName = ""; List imports = null; public void getNames(Java8Parser.CompilationUnitContext ctx){ if(ctx.packageDeclaration() != null){ - this.pkgName = ""; for(TerminalNode t : ctx.packageDeclaration().Identifier()){ this.pkgName = this.pkgName + "." + t.toString(); } @@ -32,7 +37,7 @@ public class SyntaxTreeGenerator{ else{ nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); } - this.reg.existingClasses.add(new JavaClassName(nameString)); + this.reg.add(nameString); } } else{ @@ -43,7 +48,7 @@ public class SyntaxTreeGenerator{ else{ nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); } - this.reg.existingClasses.add(new JavaClassName(nameString)); + this.reg.add(nameString); } } } @@ -53,7 +58,7 @@ public class SyntaxTreeGenerator{ List classes = new ArrayList<>(); this.getNames(ctx); for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ - ClassOrInterface newClass = null; + ClassOrInterface newClass; if(typeDecl.classDeclaration() != null){ newClass = convertClass(typeDecl.classDeclaration()); } @@ -88,15 +93,105 @@ public class SyntaxTreeGenerator{ } JavaClassName name = convert(ctx.Identifier()); Block class_block = null; - List fielddecl = null; + List fielddecl = convertFields(ctx.classBody()); GenericDeclarationList genericClassParameters = null; - int offset = 0; + Token offset = ctx.getStart(); RefType superClass = null; Boolean isInterface = false; List implementedInterfaces = null; - return new ClassOrInterface(modifiers, name, class_block, fielddecl, genericClassParameters, offset, superClass, isInterface, implementedInterfaces); + return new ClassOrInterface(modifiers, name, fielddecl, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); } - + + private List convertFields(Java8Parser.ClassBodyContext classBodyContext) { + List ret = new ArrayList<>(); + for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ + if(classMember.classMemberDeclaration()!= null){ + Java8Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); + if(classMemberDeclarationContext.fieldDeclaration()!=null){ + ret.addAll(convert(classMember.classMemberDeclaration().fieldDeclaration())); + }else if(classMemberDeclarationContext.methodDeclaration()!= null){ + ret.add(convert(classMemberDeclarationContext.methodDeclaration())); + } + } + } + return ret; + } + + private Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext) { + Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader(); + String name = header.methodDeclarator().Identifier().getText(); + + RefType retType; + if(header.result() != null){ + if(header.result().unannType() != null) + retType = convert(header.result().unannType()); + else retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart()); + }else{ + retType = TypePlaceholder.fresh(header.getStart()); + } + List modifiers = convert(methodDeclarationContext.methodModifier()); + ParameterList parameterList = convert(header.methodDeclarator().formalParameterList()); + Block block = null; + if(methodDeclarationContext.methodBody().block() == null){ + if(! modifiers.contains(new Abstract())){ + //TODO: Error! Abstrakte Methode ohne abstrakt Keyword + } + }else{ + block = convert(methodDeclarationContext.methodBody().block()); + } + GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), methodDeclarationContext.methodHeader().getStart()); + if(methodDeclarationContext.methodHeader().typeParameters() != null){ + gtvDeclarations = convert(methodDeclarationContext.methodHeader().typeParameters()); + } + return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, methodDeclarationContext.getStart()); + } + + private GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext) { + return null; + } + + private Block convert(Java8Parser.BlockContext block) { + return null; + } + + private ParameterList convert(Java8Parser.FormalParameterListContext formalParameterListContext) { + List ret = new ArrayList<>(); + if(formalParameterListContext != null){ + + } + return new ParameterList(ret); + } + + private List convert(List methodModifierContexts) { + //TODO + return new ArrayList<>(); + } + + private List convert(Java8Parser.FieldDeclarationContext fieldDeclarationContext) { + List ret = new ArrayList<>(); + List modifiers = new ArrayList<>(); + for(Java8Parser.FieldModifierContext fieldModifierContext : fieldDeclarationContext.fieldModifier()){ + //TODO + } + RefType fieldType = convert(fieldDeclarationContext.unannType()); + for(Java8Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList().variableDeclarator()){ + String fieldName = varCtx.variableDeclaratorId().getText(); + if(varCtx.variableInitializer() != null){ + //Feld mit Initialwert + //TODO + }else{ + //Feld ohne Initialwert + ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart())); + } + } + return ret; + } + + private RefType convert(Java8Parser.UnannTypeContext unannTypeContext) { + //TODO + return null; + } + private Modifier convert(Java8Parser.ClassModifierContext ctx){ Modifier newModifier = null; if(ctx.annotation() == null){ @@ -137,7 +232,7 @@ public class SyntaxTreeGenerator{ else{ name = t.toString(); } - return new JavaClassName(name); + return this.reg.getName(name); } private ClassOrInterface convertEnum(Java8Parser.EnumDeclarationContext ctx){ diff --git a/src/de/dhbwstuttgart/parser/antlr/Java8.g4 b/src/de/dhbwstuttgart/parser/antlr/Java8.g4 index 9f1864040..eb2b7bf67 100644 --- a/src/de/dhbwstuttgart/parser/antlr/Java8.g4 +++ b/src/de/dhbwstuttgart/parser/antlr/Java8.g4 @@ -321,7 +321,7 @@ classBodyDeclaration : classMemberDeclaration | instanceInitializer | staticInitializer - | constructorDeclaration +// | constructorDeclaration ; classMemberDeclaration @@ -443,7 +443,7 @@ methodModifier methodHeader : result? methodDeclarator throws_? - | typeParameters annotation* result methodDeclarator throws_? + | typeParameters annotation* result? methodDeclarator throws_? ; result diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index 89fe9daa5..d7c9748b0 100755 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -6,6 +6,7 @@ import de.dhbwstuttgart.syntaxtree.modifier.Modifiers; import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.statement.Expr; import de.dhbwstuttgart.syntaxtree.type.RefType; +import org.antlr.v4.runtime.Token; import java.util.ArrayList; import java.util.List; @@ -16,24 +17,21 @@ import java.util.List; public class ClassOrInterface extends GTVDeclarationContext implements IItemWithOffset, Generic{ protected Modifiers modifiers; protected JavaClassName name; - private Block class_block; private List fielddecl = new ArrayList<>(); private GenericDeclarationList genericClassParameters; - private int offset; + private Token offset; private RefType superClass; protected boolean isInterface; private List implementedInterfaces; - public ClassOrInterface(Modifiers modifiers, JavaClassName name, Block class_block, List fielddecl, GenericDeclarationList genericClassParameters, int offset, RefType superClass, Boolean isInterface, List implementedInterfaces){ + public ClassOrInterface(Modifiers modifiers, JavaClassName name, List fielddecl, GenericDeclarationList genericClassParameters, + RefType superClass, Boolean isInterface, List implementedInterfaces, Token offset){ if(modifiers != null){ this.modifiers = modifiers; } if(name != null){ this.name = name; } - if(class_block != null){ - this.class_block = class_block; - } if(fielddecl != null){ this.fielddecl = fielddecl; } diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index 7b074f679..074a08b48 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -1,16 +1,20 @@ package de.dhbwstuttgart.syntaxtree; +import de.dhbwstuttgart.syntaxtree.modifier.methodModifier.MethodModifier; import de.dhbwstuttgart.syntaxtree.type.RefType; +import org.antlr.v4.runtime.Token; import org.apache.bcel.Constants; import org.apache.bcel.generic.InstructionList; import de.dhbwstuttgart.typecheck.JavaClassName; import de.dhbwstuttgart.syntaxtree.statement.Block; +import java.util.List; + public class Constructor extends Method { - public Constructor(String name, RefType returnType, ParameterList parameterList, Block block, GenericDeclarationList gtvDeclarations, int offset) { - super(name, returnType, parameterList, block, gtvDeclarations, offset); + public Constructor(String name, RefType returnType, List modifiers, ParameterList parameterList, Block block, GenericDeclarationList gtvDeclarations, Token offset) { + super(name, returnType, modifiers, parameterList, block, gtvDeclarations, offset); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index f9f2be2e1..f6a0210f2 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -1,15 +1,19 @@ package de.dhbwstuttgart.syntaxtree; +import de.dhbwstuttgart.syntaxtree.modifier.fieldModifier.FieldModifier; import de.dhbwstuttgart.syntaxtree.type.RefType; +import org.antlr.v4.runtime.Token; -public abstract class Field extends GTVDeclarationContext implements Generic { +import java.util.List; + +public class Field extends GTVDeclarationContext implements Generic { private String name; private GenericDeclarationList genericParameters; - public Field(String name, RefType type, int offset){ + public Field(String name, RefType type, List modifier, Token offset){ this.name = name; } diff --git a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java index 0e09b61ae..f0aafb3c5 100644 --- a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java +++ b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java @@ -1,5 +1,8 @@ package de.dhbwstuttgart.syntaxtree; +import com.sun.org.apache.xpath.internal.operations.Mod; +import de.dhbwstuttgart.syntaxtree.modifier.fieldModifier.FieldModifier; +import org.antlr.v4.runtime.Token; import org.apache.bcel.generic.FieldGen; import org.apache.bcel.generic.FieldInstruction; import org.apache.bcel.generic.InstructionList; @@ -9,6 +12,9 @@ import org.apache.bcel.generic.ClassGen; import de.dhbwstuttgart.syntaxtree.statement.Expr; import de.dhbwstuttgart.syntaxtree.type.RefType; + +import java.util.List; + /** * Eine Feldinitialisation steht für eine Felddeklaration mit gleichzeitiger Wertzuweisung * Beispiel: 'public Feld FeldVar = FeldWert;' @@ -23,8 +29,8 @@ public class FieldDeclaration extends Field{ * Dieser Konstruktor der FieldDeclaration erstellt den Syntaxknoten vollständig. * Kein nachträgliches hinzfügen von Informationen oder aufrufen von parserPostProcessing ist notwendig. */ - public FieldDeclaration(String name, RefType typ, Expr value){ - super(name, typ, 0);//Dieser Deklarator wird nicht vom Parser aufgerufen. Dadurch gibt es auch keinen Offset + public FieldDeclaration(String name, RefType typ, List modifier, Expr value, Token offset){ + super(name, typ, modifier, offset);//Dieser Deklarator wird nicht vom Parser aufgerufen. Dadurch gibt es auch keinen Offset this.wert = value; } diff --git a/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java b/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java index cdf9a633e..7b2a52e6c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java +++ b/src/de/dhbwstuttgart/syntaxtree/GenericDeclarationList.java @@ -1,5 +1,7 @@ package de.dhbwstuttgart.syntaxtree; +import org.antlr.v4.runtime.Token; + import java.util.*; @@ -11,17 +13,13 @@ import java.util.*; */ public class GenericDeclarationList extends SyntaxTreeNode implements Iterable{ - private int offsetOfLastElement; + private Token offsetOfLastElement; private List gtvs = new ArrayList<>(); - public GenericDeclarationList(List values, int endOffset) { + public GenericDeclarationList(List values, Token endOffset) { gtvs = values; this.offsetOfLastElement = endOffset; } - - public int getEndOffset(){ - return offsetOfLastElement; - } @Override public Iterator iterator() { diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 8c48ed6de..2a5953f76 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -3,7 +3,10 @@ package de.dhbwstuttgart.syntaxtree; import java.util.ArrayList; import java.util.List; +import de.dhbwstuttgart.syntaxtree.modifier.fieldModifier.FieldModifier; +import de.dhbwstuttgart.syntaxtree.modifier.methodModifier.MethodModifier; import de.dhbwstuttgart.syntaxtree.type.RefType; +import org.antlr.v4.runtime.Token; import org.apache.bcel.Constants; import org.apache.bcel.generic.InstructionList; @@ -31,9 +34,9 @@ public class Method extends Field implements IItemWithOffset private List types_in_parameterlist = new ArrayList<>(); private Modifiers modifiers; - public Method(String name, RefType returnType, ParameterList parameterList, Block block, - GenericDeclarationList gtvDeclarations, int offset) { - super(name, returnType, offset); + public Method(String name, RefType returnType, List modifiers, ParameterList parameterList, Block block, + GenericDeclarationList gtvDeclarations, Token offset) { + super(name, returnType, modifiers, offset); this.name = name; this.parameterlist = parameterList; this.block = block; diff --git a/src/de/dhbwstuttgart/syntaxtree/modifier/fieldModifier/FieldModifier.java b/src/de/dhbwstuttgart/syntaxtree/modifier/fieldModifier/FieldModifier.java new file mode 100644 index 000000000..29f2c2fcc --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/modifier/fieldModifier/FieldModifier.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.syntaxtree.modifier.fieldModifier; + +public interface FieldModifier { +} diff --git a/src/de/dhbwstuttgart/syntaxtree/modifier/methodModifier/MethodModifier.java b/src/de/dhbwstuttgart/syntaxtree/modifier/methodModifier/MethodModifier.java new file mode 100644 index 000000000..3e161bc2d --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/modifier/methodModifier/MethodModifier.java @@ -0,0 +1,6 @@ +package de.dhbwstuttgart.syntaxtree.modifier.methodModifier; + +import de.dhbwstuttgart.syntaxtree.modifier.fieldModifier.FieldModifier; + +public interface MethodModifier extends FieldModifier { +} diff --git a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java index 6a41a72dc..69835d0aa 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.type; +import org.antlr.v4.runtime.Token; /** * Stellt eine Wildcard mit oberer Grenze dar. @@ -16,7 +17,7 @@ public class ExtendsWildcardType extends WildcardType{ * Author: Arne Lüdtke
* Standard Konstruktor für eine ExtendsWildcard */ - public ExtendsWildcardType (int offset, RefType extendsType) + public ExtendsWildcardType ( RefType extendsType,Token offset) { super(extendsType, offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/FunN.java b/src/de/dhbwstuttgart/syntaxtree/type/FunN.java index 3005e89ff..b07e6d5a5 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/FunN.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/FunN.java @@ -29,7 +29,7 @@ public class FunN extends RefType { * @return */ public FunN(RefType R, List T) { - super(null,0); + super(null,null); } /** diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index b6d5af472..f5a245585 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -4,6 +4,7 @@ import java.util.List; import de.dhbwstuttgart.typecheck.JavaClassName; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; +import org.antlr.v4.runtime.Token; public class RefType extends SyntaxTreeNode @@ -25,7 +26,7 @@ public class RefType extends SyntaxTreeNode */ private boolean primitiveFlag=false; - public RefType(JavaClassName fullyQualifiedName, int offset) + public RefType(JavaClassName fullyQualifiedName, Token offset) { this.name = (fullyQualifiedName); } @@ -38,7 +39,7 @@ public class RefType extends SyntaxTreeNode return hash; } - public RefType(JavaClassName fullyQualifiedName, List parameter, int offset) + public RefType(JavaClassName fullyQualifiedName, List parameter, Token offset) { this(fullyQualifiedName, offset); if(parameter != null && parameter.size()>0)this.parameter = parameter; diff --git a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java index 20efd8e78..23f4829a6 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.syntaxtree.type; +import org.antlr.v4.runtime.Token; + /** * Stellt eine Wildcard mit unterer Grenze dar. * z.B. void test(? super Integer var){..} @@ -11,16 +13,11 @@ package de.dhbwstuttgart.syntaxtree.type; public class SuperWildcardType extends WildcardType{ - public SuperWildcardType(RefType innerType){ - super(null, 0); - - } - /** * Author: Arne Lüdtke
* Standard Konstruktor für eine SuperWildcard */ - public SuperWildcardType(int offset, RefType innerType) + public SuperWildcardType(Token offset, RefType innerType) { super(innerType, offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java index 4d9dc7ab4..41788d636 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.syntaxtree.type; import java.util.Hashtable; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; +import org.antlr.v4.runtime.Token; /** * Repr�sentiert einen Typparameter f�r einen vom Programmierer nicht angegeben @@ -15,7 +16,8 @@ public class TypePlaceholder extends RefType { private static Hashtable m_TypePlaceholdersRegistry = new Hashtable(); - private SyntaxTreeNode parent; + + private final String name; @@ -25,9 +27,10 @@ public class TypePlaceholder extends RefType *
Author: J�rg B�uerle * @param typeName Der Name der TypePlaceholder-Variablen */ - private TypePlaceholder(String typeName, SyntaxTreeNode parent) + private TypePlaceholder(String name) { - super(null, -1); + super(null,null); + this.name = name; } /** @@ -61,8 +64,7 @@ public class TypePlaceholder extends RefType * @param listener * @return */ - public static TypePlaceholder fresh(SyntaxTreeNode parent){ - + public static TypePlaceholder fresh(Token position){ return null; } @@ -82,62 +84,10 @@ public class TypePlaceholder extends RefType return false; } } - - /** - * L�scht die komplette Registry von TypePlaceholders. Sollte nur und - * ausschlie�lich von MyCompiler.init() aufgerufen werden!!! - *
Author: J�rg B�uerle - */ - public static void deleteRegistry() - { - m_TypePlaceholdersRegistry.clear(); - m_TypePlaceholdersRegistry = new Hashtable(); - } - - /** - * Diese Methode sollte nur sehr sparsam und mit Vorsicht verwendet werden, da die - * Registrierung neuer Listener �ber die zentrale Instanz in der Registry laufen - * muss.
- * Diese Methode erzeugt eine Kopie dieser zentralen Instanz. Listener, die sich - * �ber diese Kopie registrieren, werden sp�ter nicht �ber Status�nderungen - * benachrichtigt, d.h. ihre TypePlaceholders werden nicht substituiert. - *
Author: J�rg B�uerle - * @return - */ - public TypePlaceholder clone() - { - TypePlaceholder dolly = new TypePlaceholder(name.toString(), this.parent); - return dolly; - } public String toString() { - return "TPH " + this.getName(); - } - - /////////////////////////////////////////////////////////////////// - // Spezialfunktionen, f�r makeFC() und unify() - // Sollten sonst nicht aufgerufen werden... - /////////////////////////////////////////////////////////////////// - /** - * Backdoor-Hilfsfunktion, die f�r makeFC() und unify() - * ben�tigt wird, um neue TypePlaceholders zu erzeugen. Die erzeugten Variablen - * werden nicht registriert und sind v�llig losgel�st vom abstrakten Syntaxbaum. - *
Author: J�rg B�uerle - * @return Eine neue TypePlaceholder - */ - public static TypePlaceholder backdoorFresh() - { - - //PL 05-08-22 SEHR GEFAEHRLICH - //backdoorvars werden registiert, weil am Ende beim execute - //auf den CSubstitution nicht registrierte Variablen zu - //Exceptions fuehrt - TypePlaceholder typeVar = new TypePlaceholder(null, null); - m_TypePlaceholdersRegistry.put(typeVar.getName().toString(), typeVar); - return typeVar; - - //return new TypePlaceholder(makeNewName()); + return "TPH " + this.name; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/Void.java b/src/de/dhbwstuttgart/syntaxtree/type/Void.java new file mode 100755 index 000000000..a9ecdd808 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/type/Void.java @@ -0,0 +1,17 @@ +package de.dhbwstuttgart.syntaxtree.type; + +import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; +import de.dhbwstuttgart.typecheck.JavaClassName; +import org.antlr.v4.runtime.Token; + +import java.util.ArrayList; +import java.util.List; + + +public class Void extends RefType +{ + public Void(Token offset) { + super(JavaClassName.Void, offset); + } +} + diff --git a/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java index 3cb00c92a..4a0f8cc15 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java @@ -1,5 +1,7 @@ package de.dhbwstuttgart.syntaxtree.type; +import org.antlr.v4.runtime.Token; + /** * Stellt eine Wildcard in Java dar. * z.B. void Test(? var){..} @@ -15,7 +17,7 @@ public abstract class WildcardType extends RefType{ * Author: Arne Lüdtke
* Standard Konstruktor für eine Wildcard */ - public WildcardType(RefType innerType, int offset) + public WildcardType(RefType innerType, Token offset) { super(innerType.getName(), offset); this.innerType = innerType; diff --git a/src/de/dhbwstuttgart/typecheck/JavaClassName.java b/src/de/dhbwstuttgart/typecheck/JavaClassName.java index e0a39ea86..f037a5ba3 100644 --- a/src/de/dhbwstuttgart/typecheck/JavaClassName.java +++ b/src/de/dhbwstuttgart/typecheck/JavaClassName.java @@ -11,10 +11,11 @@ import java.util.List; */ public class JavaClassName { + public static final JavaClassName Void = new JavaClassName("void"); private String name; private PackageName packageName; - public JavaClassName(String name){ + JavaClassName(String name){ if(name == null)throw new NullPointerException(); String[] names = name.split("[.]"); diff --git a/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java b/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java index d4600d19a..96176bb2e 100644 --- a/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java +++ b/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java @@ -9,10 +9,19 @@ import java.util.List; public class JavaClassRegistry { public List existingClasses = new ArrayList<>(); + public void add(String className){ + existingClasses.add(new JavaClassName(className)); + } + public JavaClassName getName(String className) { for(JavaClassName name : existingClasses){ - if(name.toString().equals(className))return name; + if(name.equals(new JavaClassName(className)))return name; } throw new TypeNotPresentException(className, new Throwable()); } + + @Override + public String toString(){ + return existingClasses.toString(); + } } diff --git a/test/parser/GeneralParserTest.java b/test/parser/GeneralParserTest.java index e225c68ed..386426b39 100644 --- a/test/parser/GeneralParserTest.java +++ b/test/parser/GeneralParserTest.java @@ -11,6 +11,8 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.parser.JavaTXParser; import junit.framework.TestCase; import org.junit.Test; @@ -34,13 +36,15 @@ public class GeneralParserTest{ List filenames = new ArrayList(); filenames.add("FieldInitializationTest.jav"); filenames.add("ImportTest.jav"); - filenames.add("BoundedParameter.jav"); - filenames.add("GenericFieldVarTest.jav"); + //filenames.add("BoundedParameter.jav"); + //filenames.add("GenericFieldVarTest.jav"); filenames.add("FieldVarTest.jav"); - //MyCompilerAPI compiler = MyCompiler.getAPI(config); + filenames.add("StructuralTypes.jav"); + JavaTXParser parser = new JavaTXParser(); try{ for(String filename : filenames) { - //compiler.parse(new File(rootDirectory + filename)); + System.out.println("Teste: "+filename); + parser.parse(new File(rootDirectory + filename)); //TODO: Test ANTLR Parser } }catch(Exception exc){