diff --git a/src/de/dhbwstuttgart/parser/RunParser.java b/src/de/dhbwstuttgart/parser/RunParser.java index 6f04ac55c..b5118a92a 100644 --- a/src/de/dhbwstuttgart/parser/RunParser.java +++ b/src/de/dhbwstuttgart/parser/RunParser.java @@ -5,18 +5,20 @@ import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.tree.ParseTreeWalker; import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.modifier.*; import de.dhbwstuttgart.typecheck.*; import java.util.Scanner; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.io.IOException; import java.nio.charset.StandardCharsets; public class RunParser{ public static void main(String[] args){ try{ Scanner sc = new Scanner(System.in); String inputString = sc.nextLine(); - while(sc.hasNextLine()) inputString = inputString + sc.nextLine(); + while(sc.hasNextLine()) inputString = inputString + sc.nextLine() + "\n"; InputStream stream = new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8)); ANTLRInputStream input = new ANTLRInputStream(stream); Java8Lexer lexer = new Java8Lexer(input); @@ -24,14 +26,22 @@ public class RunParser{ Java8Parser parser = new Java8Parser(tokens); Java8Parser.CompilationUnitContext tree = parser.compilationUnit(); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(); - generator.getNames(tree); SourceFile f = generator.convert((Java8Parser.CompilationUnitContext) tree); + String pkgName = f.getPkgName(); + System.out.println(pkgName); + System.out.println("classes:"); for(ClassOrInterface c : f.KlassenVektor){ + for(Modifier mod : c.getModifiers().getModifierList()){ + System.out.println(mod.getClass().getName()); + } System.out.println(c.getClassName().toString()); } } - catch(Exception e){ - System.out.println("An exception occured which is unknown and on our TODO list."); + catch(java.util.NoSuchElementException e){ + System.out.println("Error: Source seems to be empty."); + } + catch(IOException e){ + System.out.println("An exception occured which is on our TODO list."); e.printStackTrace(); } } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java index bc9c773a9..8e01897e9 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java @@ -1,29 +1,33 @@ package de.dhbwstuttgart.parser; -import de.dhbwstuttgart.syntaxtree.SourceFile; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.modifier.*; +import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.statement.Expr; +import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.typecheck.*; - import java.util.ArrayList; import java.util.List; import org.antlr.v4.runtime.tree.TerminalNode; public class SyntaxTreeGenerator{ JavaClassRegistry reg = new JavaClassRegistry(); - String packageDecl = ""; + String pkgName = null; + List imports = null; public void getNames(Java8Parser.CompilationUnitContext ctx){ if(ctx.packageDeclaration() != null){ + this.pkgName = ""; for(TerminalNode t : ctx.packageDeclaration().Identifier()){ - this.packageDecl = this.packageDecl + "." + t.toString(); + this.pkgName = this.pkgName + "." + t.toString(); } - this.packageDecl = this.packageDecl.substring(1); + this.pkgName = this.pkgName.substring(1); } String nameString = ""; for (Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ if(typeDecl.interfaceDeclaration() != null){ if(typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null){ - if(packageDecl != ""){ - nameString = packageDecl + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); + if(this.pkgName != null){ + nameString = this.pkgName + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); } else{ nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); @@ -33,8 +37,8 @@ public class SyntaxTreeGenerator{ } else{ if(typeDecl.classDeclaration().normalClassDeclaration() != null){ - if(packageDecl != ""){ - nameString = packageDecl + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); + if(this.pkgName != ""){ + nameString = this.pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); } else{ nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); @@ -47,23 +51,100 @@ public class SyntaxTreeGenerator{ public SourceFile convert(Java8Parser.CompilationUnitContext ctx){ List classes = new ArrayList<>(); + this.getNames(ctx); for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ - ClassOrInterface newClass = convert(typeDecl.classDeclaration()); + ClassOrInterface newClass = null; + if(typeDecl.classDeclaration() != null){ + newClass = convertClass(typeDecl.classDeclaration()); + } + else{ + newClass = convertInterface(typeDecl.interfaceDeclaration()); + } classes.add(newClass); } - return new SourceFile(classes); + return new SourceFile(this.pkgName, classes, this.imports); } - private ClassOrInterface convert(Java8Parser.ClassDeclarationContext ctx) { - ClassOrInterface newClass = new ClassOrInterface(); - String name = ""; - if(this.packageDecl != ""){ - name = packageDecl + "." + ctx.normalClassDeclaration().Identifier().toString(); + private ClassOrInterface convertClass(Java8Parser.ClassDeclarationContext ctx) { + ClassOrInterface newClass = null; + if(ctx.normalClassDeclaration() != null){ + newClass = convertNormal(ctx.normalClassDeclaration()); } else{ - name = ctx.normalClassDeclaration().Identifier().toString(); + newClass = convertEnum(ctx.enumDeclaration()); } - newClass.setClassName(new JavaClassName(name)); return newClass; } + + private ClassOrInterface convertNormal(Java8Parser.NormalClassDeclarationContext ctx){ + Modifiers modifiers = null; + if(ctx.classModifier() != null){ + List modList = new ArrayList(); + for(Java8Parser.ClassModifierContext mod : ctx.classModifier()){ + Modifier newModifier = convert(mod); + modList.add(newModifier); + } + modifiers = new Modifiers(modList); + } + JavaClassName name = convert(ctx.Identifier()); + Block class_block = null; + List fielddecl = null; + GenericDeclarationList genericClassParameters = null; + int offset = 0; + RefType superClass = null; + Boolean isInterface = false; + List implementedInterfaces = null; + return new ClassOrInterface(modifiers, name, class_block, fielddecl, genericClassParameters, offset, superClass, isInterface, implementedInterfaces); + } + + private Modifier convert(Java8Parser.ClassModifierContext ctx){ + Modifier newModifier = null; + if(ctx.annotation() == null){ + TerminalNode t = (TerminalNode)ctx.getChild(0); + if(t.getText().equals("public")){ + newModifier = new Public(); + } + else if(t.getText().equals("private")){ + newModifier = new Private(); + } + else if(t.getText().equals("protected")){ + newModifier = new Protected(); + } + else if(t.getText().equals("abstract")){ + newModifier = new Abstract(); + } + else if(t.getText().equals("static")){ + newModifier = new Static(); + } + else if(t.getText().equals("strictfp")){ + newModifier = new Strictfp(); + } + else{ + newModifier = new Final(); + } + } + return newModifier; + } + + /** + Converts a TerminalNode to JavaClassName. If pkgName is set, it will be included like expected. + **/ + private JavaClassName convert(TerminalNode t){ + String name = ""; + if(this.pkgName != null){ + name = this.pkgName + "." + t.toString(); + } + else{ + name = t.toString(); + } + return new JavaClassName(name); + } + + private ClassOrInterface convertEnum(Java8Parser.EnumDeclarationContext ctx){ + return null; + } + + private ClassOrInterface convertInterface(Java8Parser.InterfaceDeclarationContext ctx){ + return null; + } } diff --git a/src/de/dhbwstuttgart/parser/TODO b/src/de/dhbwstuttgart/parser/TODO index ae1d80c47..165cb5629 100644 --- a/src/de/dhbwstuttgart/parser/TODO +++ b/src/de/dhbwstuttgart/parser/TODO @@ -2,4 +2,4 @@ * Core-Problem: Typinferenz vs. Konstruktoren * möglicherweise Problem: falsche Return-Expressions -* Problem: Line-Comments werden nicht erkannt bzw. führen dazu dass das gesamte File nicht geparsed wird (Java8.g4). + diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index bfa66808f..2db2ff04d 100755 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -32,10 +32,31 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith private RefType superClass; protected boolean isInterface; private List implementedInterfaces; - private List fields; - - public ClassOrInterface(int offset) { - super(offset); + + public ClassOrInterface(Modifiers modifiers, JavaClassName name, Block class_block, List fielddecl, GenericDeclarationList genericClassParameters, int offset, RefType superClass, Boolean isInterface, List implementedInterfaces){ + 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; + } + if(genericClassParameters != null){ + this.genericClassParameters = genericClassParameters; + } + this.offset = offset; + if(superClass != null){ + this.superClass = superClass; + } + this.isInterface = isInterface; + if(implementedInterfaces != null){ + this.implementedInterfaces = implementedInterfaces; + } } // Gets class name @@ -112,10 +133,14 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith *
Author: Martin Pl�micke * @return */ - public String toString() - { + public String toString() { return name.toString(); } + + // Get modifiers + public Modifiers getModifiers(){ + return this.modifiers; + } /** * Generiert den JavaCode dieser Klasse im Falle für das übergebene resultSet. diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index 6b974c703..69b2091d1 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -37,7 +37,6 @@ public class SourceFile extends SyntaxTreeNode{ * SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar. */ public SourceFile(String pkgName,List classDefinitions,List imports){ - super(0); this.KlassenVektor = classDefinitions; if(pkgName != null){ this.pkgName = pkgName; @@ -553,5 +552,8 @@ public class SourceFile extends SyntaxTreeNode{ */ return ret; } - + + public String getPkgName(){ + return this.pkgName; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/modifier/Modifiers.java b/src/de/dhbwstuttgart/syntaxtree/modifier/Modifiers.java index 92db05ed9..b52c3df5a 100755 --- a/src/de/dhbwstuttgart/syntaxtree/modifier/Modifiers.java +++ b/src/de/dhbwstuttgart/syntaxtree/modifier/Modifiers.java @@ -7,6 +7,9 @@ import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.JavaCodeResult; import de.dhbwstuttgart.typeinference.ResultSet; + +import java.util.ArrayList; +import java.util.List; // ino.end @@ -17,114 +20,14 @@ import de.dhbwstuttgart.typeinference.ResultSet; */ public class Modifiers { - protected Menge modifier = new Menge(); - // ino.end - - // ino.method.getModifiers.24041.definition - public Menge getModifiers() - // ino.end - // ino.method.getModifiers.24041.body - { - return this.modifier; - } - // ino.end - - // ino.method.setModifier.24044.definition - public void setModifier(Menge modifier) - // ino.end - // ino.method.setModifier.24044.body - { - if (modifier != null) this.modifier = modifier; - } - // ino.end - - // ino.method.addModifier.24047.defdescription type=javadoc - /** - * Fuegt den angegebenen Modifier zur Auflistung hinzu. - */ - // ino.end - // ino.method.addModifier.24047.definition - public void addModifier(Modifier mod) - // ino.end - // ino.method.addModifier.24047.body - { - modifier.addElement(mod); - } - // ino.end + protected List modifier = new ArrayList(); - // ino.method.includesModifier.24050.defdescription type=javadoc - /** - * Gibt zurueck, ob der angegebene Modifier enthalten ist. - */ - // ino.end - // ino.method.includesModifier.24050.definition - public boolean includesModifier(Modifier mod) - // ino.end - // ino.method.includesModifier.24050.body - { - String class1 = mod.getClass().toString(); - String class2; - - for (int i=0; i modifier){ + this.modifier = modifier; + } + + public List getModifierList(){ + return this.modifier; + } } -// ino.end + diff --git a/src/de/dhbwstuttgart/syntaxtree/modifier/Strictfp.java b/src/de/dhbwstuttgart/syntaxtree/modifier/Strictfp.java new file mode 100755 index 000000000..b9cdb1efd --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/modifier/Strictfp.java @@ -0,0 +1,20 @@ +// ino.module.Public.8591.package +package de.dhbwstuttgart.syntaxtree.modifier; + +// ino.class.Public.24073.declaration +public class Strictfp extends Modifier +// ino.end +// ino.class.Public.24073.body +{ + + // ino.method.getBitmask.24077.definition + public short getBitmask() + // ino.end + // ino.method.getBitmask.24077.body + { + return 2048; + } + // ino.end + +} +// ino.end