forked from JavaTX/JavaCompilerCore
Merge mit antlr
This commit is contained in:
commit
f2ddce2e89
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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<JavaClassName> 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<ClassOrInterface> 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<Modifier> 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<Field> fielddecl = null;
|
||||
GenericDeclarationList genericClassParameters = null;
|
||||
int offset = 0;
|
||||
RefType superClass = null;
|
||||
Boolean isInterface = false;
|
||||
List<RefType> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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).
|
||||
|
||||
|
@ -32,10 +32,31 @@ public class ClassOrInterface extends GTVDeclarationContext implements IItemWith
|
||||
private RefType superClass;
|
||||
protected boolean isInterface;
|
||||
private List<RefType> implementedInterfaces;
|
||||
private List<Field> fields;
|
||||
|
||||
public ClassOrInterface(int offset) {
|
||||
super(offset);
|
||||
|
||||
public ClassOrInterface(Modifiers modifiers, JavaClassName name, Block class_block, List<Field> fielddecl, GenericDeclarationList genericClassParameters, int offset, RefType superClass, Boolean isInterface, List<RefType> 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
|
||||
* <br/>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.
|
||||
|
@ -37,7 +37,6 @@ public class SourceFile extends SyntaxTreeNode{
|
||||
* SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar.
|
||||
*/
|
||||
public SourceFile(String pkgName,List<ClassOrInterface> classDefinitions,List<JavaClassName> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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> modifier = new Menge<Modifier>();
|
||||
// ino.end
|
||||
|
||||
// ino.method.getModifiers.24041.definition
|
||||
public Menge<Modifier> getModifiers()
|
||||
// ino.end
|
||||
// ino.method.getModifiers.24041.body
|
||||
{
|
||||
return this.modifier;
|
||||
}
|
||||
// ino.end
|
||||
|
||||
// ino.method.setModifier.24044.definition
|
||||
public void setModifier(Menge<Modifier> 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> modifier = new ArrayList<Modifier>();
|
||||
|
||||
// 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.size(); i++) {
|
||||
// Anmerkung: Vergleich mit instanceof nicht moeglich
|
||||
class2 = modifier.elementAt(i).getClass().toString();
|
||||
|
||||
if (class2.equals(class1)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// ino.end
|
||||
|
||||
// ino.method.ensureAbstract.24053.defdescription type=javadoc
|
||||
/**
|
||||
* Stellt sicher, dass ABSTRACT in der Modifierliste
|
||||
* vorkommt. Wird u.a. bei Interfaces benoetigt.
|
||||
*
|
||||
*/
|
||||
// ino.end
|
||||
// ino.method.ensureAbstract.24053.definition
|
||||
public void ensureAbstract()
|
||||
// ino.end
|
||||
// ino.method.ensureAbstract.24053.body
|
||||
{
|
||||
if (!includesModifier(new Abstract()))
|
||||
modifier.addElement(new Abstract());
|
||||
}
|
||||
// ino.end
|
||||
|
||||
public void ensurePublic()
|
||||
{
|
||||
if (!includesModifier(new Public()))
|
||||
modifier.addElement(new Public());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Bitmaskenwert fuer die enthaltenen Access-Modifier
|
||||
* zurueck.
|
||||
*/
|
||||
public short calculate_access_flags()
|
||||
{
|
||||
short ret = 0;
|
||||
|
||||
for (int i = 0; i < modifier.size(); i++) {
|
||||
ret += modifier.elementAt(i).getBitmask();
|
||||
}
|
||||
// Falls nichts angegeben, auf Public setzen
|
||||
if (ret == 0) {
|
||||
Public p = new Public();
|
||||
ret = p.getBitmask();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public JavaCodeResult printJavaCode(ResultSet resultSet) {
|
||||
JavaCodeResult ret = new JavaCodeResult();
|
||||
for(Modifier mod : this.modifier){
|
||||
ret.attach(mod.printJavaCode(resultSet)).attach( " ");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public Modifiers(List<Modifier> modifier){
|
||||
this.modifier = modifier;
|
||||
}
|
||||
|
||||
public List<Modifier> getModifierList(){
|
||||
return this.modifier;
|
||||
}
|
||||
}
|
||||
// ino.end
|
||||
|
||||
|
20
src/de/dhbwstuttgart/syntaxtree/modifier/Strictfp.java
Executable file
20
src/de/dhbwstuttgart/syntaxtree/modifier/Strictfp.java
Executable file
@ -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
|
Loading…
Reference in New Issue
Block a user