From af1f7f28e295e3cfc1c944c995c5c99f9c2e00f6 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Fri, 7 Apr 2017 02:33:20 +0200 Subject: [PATCH] =?UTF-8?q?New=20Aufruf=20im=20Parser=20anf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StatementGenerator.java | 38 ++++++++++--------- .../SyntaxTreeGenerator/TypeGenerator.java | 3 +- .../syntaxtree/statement/NewClass.java | 18 +++++++-- test/parser/GeneralParserTest.java | 1 + test/parser/NewTest.jav | 5 +++ 5 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 test/parser/NewTest.jav diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 68b6afda..6a5be13b 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -710,46 +710,48 @@ public class StatementGenerator { return convert(expression.expression()); }else if(expression.methodInvocation_lfno_primary() != null){ return convert(expression.methodInvocation_lfno_primary()); + }else if(expression.classInstanceCreationExpression_lfno_primary() != null) { + return convert(expression.classInstanceCreationExpression_lfno_primary()); }else{ throw new NotImplementedException(); } } - private Expression convert(Java8Parser.LiteralContext literal) { - /*if(literal.IntegerLiteral() != null){ + private Expression convert(Java8Parser.ClassInstanceCreationExpression_lfno_primaryContext newExpression) { + if(newExpression.expressionName()!= null)throw new NotImplementedException(); - RefType type = new RefType(reg.getName("java.lang.Integer"),literal.getStart()); - return new IntLiteral(type, - Integer.parseInt(literal.toString()), - literal.getStart()); - }else if(literal.FloatingPointLiteral() != null){ - RefType type = new RefType(reg.getName("java.lang.Float"),literal.getStart()); - return new FloatLiteral(type, - Float.parseFloat(literal.toString()), - literal.getStart()); - */ - if(literal.IntegerLiteral() != null){ - Number value = Double.parseDouble(literal.toString()); + TerminalNode identifier = newExpression.Identifier(0); + RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(),identifier.getSymbol(),reg,generics); + + ArgumentList args = convert(newExpression.argumentList()); + return new NewClass(newClass, args, newExpression.getStart()); + } + + private Expression convert(Java8Parser.LiteralContext literal) { + if(literal.IntegerLiteral() != null || literal.FloatingPointLiteral()!= null){ + Number value = Double.parseDouble(literal.IntegerLiteral().getText()); return new NumberLiteral(TypePlaceholder.fresh(literal.getStart()), value, literal.getStart()); }else if(literal.BooleanLiteral() != null){ RefType type = new RefType(reg.getName("java.lang.Boolean"),literal.getStart()); return new BoolLiteral(type, - Boolean.parseBoolean(literal.toString()), + Boolean.parseBoolean(literal.BooleanLiteral().getText()), literal.getStart()); }else if(literal.CharacterLiteral() != null){ RefType type = new RefType(reg.getName("java.lang.Character"),literal.getStart()); return new CharLiteral(type, - literal.toString().charAt(0), + literal.CharacterLiteral().getText().charAt(0), literal.getStart()); }else if(literal.StringLiteral()!=null){ RefType type = new RefType(reg.getName("java.lang.String"),literal.getStart()); return new StringLiteral(type, - literal.toString(), + literal.StringLiteral().getText(), literal.getStart()); - }else {// if(literal.NullLiteral() != null){ + }else if(literal.NullLiteral() != null){ return new Null(TypePlaceholder.fresh(literal.getStart()), literal.getStart()); + }else { + throw new NotImplementedException(); } } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index dc0d71af..43609a08 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -77,7 +77,7 @@ public class TypeGenerator { return convertTypeName(referenceTypeContext.getText(), referenceTypeContext.getStart(), reg, generics); } - private static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(String name, Token offset, JavaClassRegistry reg, GenericsRegistry generics){ + public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(String name, Token offset, JavaClassRegistry reg, GenericsRegistry generics){ if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein: if(generics.keySet().contains(name)){ return new GenericRefType(new GenericTypeName(generics.get(name),name), offset); @@ -87,4 +87,5 @@ public class TypeGenerator { } return new RefType(reg.getName(name), offset); } + } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java index b5c5fd30..17e3e078 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/NewClass.java @@ -2,9 +2,11 @@ package de.dhbwstuttgart.syntaxtree.statement; import java.util.ArrayList; import java.util.List; +import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import org.antlr.v4.runtime.Token; import sun.reflect.generics.reflectiveObjects.NotImplementedException; @@ -17,9 +19,19 @@ public class NewClass extends Statement private ArgumentList arglist; private boolean isStatement = false; - private List parameterList = new ArrayList<>(); - - + + /** + * + * @param newClass Typ der Instanzierten Klasse + * @param args Argumente mit denen der New-Call aufgerufen wurde + * @param start + */ + public NewClass(RefType newClass, ArgumentList args, Token start) { + super(newClass, start); + this.arglist = args; + } + + public ArgumentList getArgumentList() { return this.arglist; diff --git a/test/parser/GeneralParserTest.java b/test/parser/GeneralParserTest.java index 9cb20ac6..4617b8a5 100644 --- a/test/parser/GeneralParserTest.java +++ b/test/parser/GeneralParserTest.java @@ -25,6 +25,7 @@ public class GeneralParserTest{ List filenames = new ArrayList(); + filenames.add("NewTest.jav"); filenames.add("FieldInitializationTest.jav"); filenames.add("ImportTest.jav"); filenames.add("CastTest.jav"); diff --git a/test/parser/NewTest.jav b/test/parser/NewTest.jav new file mode 100644 index 00000000..992a2e8d --- /dev/null +++ b/test/parser/NewTest.jav @@ -0,0 +1,5 @@ + +class NewTest { +main() { return new NewTest<>().mt(new NewTest(1) , new NewTest(2) , new +NewTest(3)); } +} \ No newline at end of file