From 82a1e8618610294365c7be292500a54ffad39b14 Mon Sep 17 00:00:00 2001 From: Jakob Herrmann Date: Thu, 15 Dec 2016 11:21:33 +0100 Subject: [PATCH 1/4] * Started implementing CompilationUnitEvaluator.java which is supposed to be the base class for syntax tree construction. * Deleted JavaClassName and JavaClassRegistry as they cannot be compiled so far (backups exist). --- .../parser/CompilationUnitEvaluator.java | 12 ++ .../dhbwstuttgart/parser/JavaClassName.java | 130 ------------------ .../parser/JavaClassRegistry.java | 18 --- 3 files changed, 12 insertions(+), 148 deletions(-) create mode 100644 src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java delete mode 100644 src/de/dhbwstuttgart/parser/JavaClassName.java delete mode 100644 src/de/dhbwstuttgart/parser/JavaClassRegistry.java diff --git a/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java b/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java new file mode 100644 index 000000000..496dd2903 --- /dev/null +++ b/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java @@ -0,0 +1,12 @@ +package de.dhbwstuttgart.parser; +import org.antlr.v4.runtime.tree.*; +import de.dhbwstuttgart.syntaxtree.*; +public class CompilationUnitEvaluator extends Java8BaseListener{ + @Override + public void exitCompilationUnit(Java8Parser.CompilationUnitContext ctx) { + // SourceFile result = new SourceFile(new List()); + System.out.println("sourceFile("); + System.out.println(ctx.packageDeclaration()); + System.out.println(")"); + } +} diff --git a/src/de/dhbwstuttgart/parser/JavaClassName.java b/src/de/dhbwstuttgart/parser/JavaClassName.java deleted file mode 100644 index 4e6ba81ba..000000000 --- a/src/de/dhbwstuttgart/parser/JavaClassName.java +++ /dev/null @@ -1,130 +0,0 @@ -package de.dhbwstuttgart.parser; - -import de.dhbwstuttgart.typeinference.Menge; - -import de.dhbwstuttgart.syntaxtree.type.RefType; - -/** - * Stellt den Namen einer Java Klasse dar. - * Dieser kann auch den Packagenamen mit beinhalten: de.dhbwstuttgart.typeinference.Menge - * - * @author janulrich - * - * - */ -public class JavaClassName { - - private String name; - private PackageName packageName; - - /** - * TODO: JavaClassName sollten aus den Assumptions generiert werden. - * Diese wissen, welche Typen und Typnamen existieren und können direkt auf Korrektheit prüfen. - */ - JavaClassName(String name){ - if(name == null)throw new NullPointerException(); - - String[] names = name.split("[.]"); - boolean match = true; - if(names.length == 1){ - //packageName = new PackageName(); - this.name = name; - }else { - name = names[names.length-1]; - Menge packageNames = new Menge(); - for(int i = 0; i names = new Menge(); - - public PackageName(Menge packageNames) { - names = packageNames; - } - - public PackageName() { - //Do nothing - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((names == null) ? 0 : names.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - PackageName other = (PackageName) obj; - if (names == null) { - if (other.names != null) - return false; - } else if (!names.equals(other.names)) - return false; - return true; - } - - @Override - public String toString() { - String ret = ""; - if(names == null)return ""; - for(String n : names)ret+=n+"."; - return ret; - } -} diff --git a/src/de/dhbwstuttgart/parser/JavaClassRegistry.java b/src/de/dhbwstuttgart/parser/JavaClassRegistry.java deleted file mode 100644 index 4282f662e..000000000 --- a/src/de/dhbwstuttgart/parser/JavaClassRegistry.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.dhbwstuttgart.parser; - -import java.util.ArrayList; -import java.util.List; - -/** - * Speichert die Klassen im aktuellen Projektscope - */ -public class JavaClassRegistry { - public List existingClasses = new ArrayList<>(); - - public JavaClassName getName(String className) { - for(JavaClassName name : existingClasses){ - if(name.toString().equals(className))return name; - } - throw new TypeNotPresentException(className, new Throwable()); - } -} From 81e223334b15150a0af6337012f27f6177af69eb Mon Sep 17 00:00:00 2001 From: Jakob Herrmann Date: Thu, 15 Dec 2016 12:05:23 +0100 Subject: [PATCH 2/4] Created RunParser which is supposed the main class (still rather experimental). --- src/de/dhbwstuttgart/parser/RunParser.java | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/de/dhbwstuttgart/parser/RunParser.java diff --git a/src/de/dhbwstuttgart/parser/RunParser.java b/src/de/dhbwstuttgart/parser/RunParser.java new file mode 100644 index 000000000..d4a6a7400 --- /dev/null +++ b/src/de/dhbwstuttgart/parser/RunParser.java @@ -0,0 +1,31 @@ +package de.dhbwstuttgart.parser; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.ANTLRInputStream; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.tree.ParseTreeWalker; +import de.dhbwstuttgart.syntaxtree.*; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +public class RunParser{ + public static void main(String[] args){ + try{ + String inputString = args[0]; + InputStream stream = new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8)); + ANTLRInputStream input = new ANTLRInputStream(stream); + Java8Lexer lexer = new Java8Lexer(input); + CommonTokenStream tokens = new CommonTokenStream(lexer); + Java8Parser parser = new Java8Parser(tokens); + ParserRuleContext tree = parser.compilationUnit(); + ParseTreeWalker walker = new ParseTreeWalker(); + CompilationUnitEvaluator eval = new CompilationUnitEvaluator(); + walker.walk(eval, tree); + } + catch(Exception e){ + System.out.println("An exception occured which is unknown and on our TODO list."); + e.printStackTrace(); + } + } +} From b0a6585252135383c3a3b594e5937168e58de3a1 Mon Sep 17 00:00:00 2001 From: Jakob Herrmann Date: Thu, 15 Dec 2016 13:35:07 +0100 Subject: [PATCH 3/4] Small changes, i.e. RunParser now reads from System.in. --- src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java | 7 +++++-- src/de/dhbwstuttgart/parser/RunParser.java | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java b/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java index 496dd2903..9260bccf5 100644 --- a/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java +++ b/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java @@ -2,11 +2,14 @@ package de.dhbwstuttgart.parser; import org.antlr.v4.runtime.tree.*; import de.dhbwstuttgart.syntaxtree.*; public class CompilationUnitEvaluator extends Java8BaseListener{ + @Override + public void enterCompilationUnit(Java8Parser.CompilationUnitContext ctx) { + System.out.println("Entering compilationUnit..."); + } @Override public void exitCompilationUnit(Java8Parser.CompilationUnitContext ctx) { - // SourceFile result = new SourceFile(new List()); System.out.println("sourceFile("); - System.out.println(ctx.packageDeclaration()); + System.out.println(ctx.packageDeclaration().Identifier()); System.out.println(")"); } } diff --git a/src/de/dhbwstuttgart/parser/RunParser.java b/src/de/dhbwstuttgart/parser/RunParser.java index d4a6a7400..9b1c86539 100644 --- a/src/de/dhbwstuttgart/parser/RunParser.java +++ b/src/de/dhbwstuttgart/parser/RunParser.java @@ -6,13 +6,16 @@ import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.tree.ParseTreeWalker; import de.dhbwstuttgart.syntaxtree.*; +import java.util.Scanner; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class RunParser{ public static void main(String[] args){ try{ - String inputString = args[0]; + Scanner sc = new Scanner(System.in); + String inputString = sc.nextLine(); + while(sc.hasNextLine()) inputString = inputString + sc.nextLine(); InputStream stream = new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8)); ANTLRInputStream input = new ANTLRInputStream(stream); Java8Lexer lexer = new Java8Lexer(input); From e1d68291c9b253ed54825319cd0b7de552e36021 Mon Sep 17 00:00:00 2001 From: Jakob Herrmann Date: Thu, 15 Dec 2016 15:01:57 +0100 Subject: [PATCH 4/4] Added some basic handling of package declarations. --- .../parser/CompilationUnitEvaluator.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java b/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java index 9260bccf5..fa1b7d1d4 100644 --- a/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java +++ b/src/de/dhbwstuttgart/parser/CompilationUnitEvaluator.java @@ -4,12 +4,21 @@ import de.dhbwstuttgart.syntaxtree.*; public class CompilationUnitEvaluator extends Java8BaseListener{ @Override public void enterCompilationUnit(Java8Parser.CompilationUnitContext ctx) { - System.out.println("Entering compilationUnit..."); + System.out.println("SourceFile("); } @Override public void exitCompilationUnit(Java8Parser.CompilationUnitContext ctx) { - System.out.println("sourceFile("); - System.out.println(ctx.packageDeclaration().Identifier()); + 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(")"); } }