From 40d87c86361f2a165eab738e9e60b694553fc851 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Fri, 17 Feb 2017 17:20:50 +0100 Subject: [PATCH] Method Parsen verbessert --- .../parser/SyntaxTreeGenerator.java | 28 ++++++++----- test/parser/FeatherWeightJava.jav | 11 +++++ test/parser/FeatherWeightJavaTest.java | 41 +++++++++++++++++++ test/parser/GeneralParserTest.java | 1 + 4 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 test/parser/FeatherWeightJava.jav create mode 100644 test/parser/FeatherWeightJavaTest.java diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java index 681c6f2a3..05685b986 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java @@ -12,6 +12,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPH; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typecheck.*; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.antlr.v4.runtime.Token; @@ -249,17 +250,24 @@ public class SyntaxTreeGenerator{ private ParameterList convert(Java8Parser.FormalParameterListContext formalParameterListContext) { List ret = new ArrayList<>(); - if(formalParameterListContext != null && formalParameterListContext.formalParameters() != null && formalParameterListContext.formalParameters().formalParameter() != null){ - for(Java8Parser.FormalParameterContext fp : formalParameterListContext.formalParameters().formalParameter()){ - String paramName = convert(fp.variableDeclaratorId()); - RefTypeOrTPH type; - if(fp.unannType() != null){ - type = convert(fp.unannType()); - }else{ - type = TypePlaceholder.fresh(fp.getStart()); - } - ret.add(new FormalParameter(paramName, type, fp.getStart())); + List fps = new ArrayList<>(); + if(formalParameterListContext.lastFormalParameter().formalParameter() == null)throw new NotImplementedException(); + + if(formalParameterListContext != null && formalParameterListContext.formalParameters() != null + && formalParameterListContext.formalParameters().formalParameter() != null){ + fps = new ArrayList<>(formalParameterListContext.formalParameters().formalParameter()); + } + fps.add(formalParameterListContext.lastFormalParameter().formalParameter()); + + for(Java8Parser.FormalParameterContext fp : fps){ + String paramName = convert(fp.variableDeclaratorId()); + RefTypeOrTPH type; + if(fp.unannType() != null){ + type = convert(fp.unannType()); + }else{ + type = TypePlaceholder.fresh(fp.getStart()); } + ret.add(new FormalParameter(paramName, type, fp.getStart())); } return new ParameterList(ret); } diff --git a/test/parser/FeatherWeightJava.jav b/test/parser/FeatherWeightJava.jav new file mode 100644 index 000000000..e607b12a2 --- /dev/null +++ b/test/parser/FeatherWeightJava.jav @@ -0,0 +1,11 @@ + +class FeatherWeightJava { + mt4(a,b,c) { return a.add(b).sub(c) ; } + + mt1(a) {return a; } + + mt2(a) {return a.f; } + + mt3(a) {return a.add(); } +} + diff --git a/test/parser/FeatherWeightJavaTest.java b/test/parser/FeatherWeightJavaTest.java new file mode 100644 index 000000000..e484b12eb --- /dev/null +++ b/test/parser/FeatherWeightJavaTest.java @@ -0,0 +1,41 @@ +package parser; + +import de.dhbwstuttgart.parser.JavaTXParser; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.modifier.Modifier; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +public class FeatherWeightJavaTest { + private static final String rootDirectory = System.getProperty("user.dir")+"/test/parser/"; + + @Test + public void test() throws IOException, ClassNotFoundException { + + JavaTXParser parser = new JavaTXParser(); + SourceFile f = parser.parse(new File(rootDirectory + "FeatherWeightJava.jav")); + + String pkgName = f.getPkgName(); + System.out.println("package: " + 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()); + System.out.println("{"); + for(Field field : c.getFieldDecl()){ + System.out.println(field.getName()); + if(field instanceof Method){ + System.out.println(((Method) field).parameterlist.formalparameter.toString()); + } + } + System.out.println("}"); + } + } +} diff --git a/test/parser/GeneralParserTest.java b/test/parser/GeneralParserTest.java index b19d23350..260782335 100644 --- a/test/parser/GeneralParserTest.java +++ b/test/parser/GeneralParserTest.java @@ -28,6 +28,7 @@ public class GeneralParserTest{ filenames.add("FieldInitializationTest.jav"); filenames.add("ImportTest.jav"); filenames.add("StatementsTest.jav"); + filenames.add("FeatherWeightJava.jav"); filenames.add("ImportTestGeneric.jav"); //filenames.add("BoundedParameter.jav"); //filenames.add("GenericFieldVarTest.jav");