diff --git a/part-12/src/main/antlr/com/endoflineblog/truffle/part_12/parsing/antlr/EasyScript.g4 b/part-12/src/main/antlr/com/endoflineblog/truffle/part_12/parsing/antlr/EasyScript.g4 index f52d4a23..7bbab73b 100644 --- a/part-12/src/main/antlr/com/endoflineblog/truffle/part_12/parsing/antlr/EasyScript.g4 +++ b/part-12/src/main/antlr/com/endoflineblog/truffle/part_12/parsing/antlr/EasyScript.g4 @@ -38,13 +38,15 @@ expr4 : left=expr4 o=('+' | '-') right=expr5 #AddSubtractExpr4 | '-' expr5 #UnaryMinusExpr4 | expr5 #PrecedenceFiveExpr4 ; -expr5 : literal #LiteralExpr5 - | ID #ReferenceExpr5 - | expr5 '.' ID #PropertyReadExpr5 - | 'new' constr=expr5 '(' (expr1 (',' expr1)*)? ')' #NewExpr5 - | '[' (expr1 (',' expr1)*)? ']' #ArrayLiteralExpr5 +expr5 : expr5 '.' ID #PropertyReadExpr5 | arr=expr5 '[' index=expr1 ']' #ArrayIndexReadExpr5 | expr5 '(' (expr1 (',' expr1)*)? ')' #CallExpr5 + | expr6 #PrecedenceSixExpr5 + ; +expr6 : literal #LiteralExpr5 + | ID #ReferenceExpr5 + | '[' (expr1 (',' expr1)*)? ']' #ArrayLiteralExpr5 + | 'new' constr=expr6 '(' (expr1 (',' expr1)*)? ')' #NewExpr5 | '(' expr1 ')' #PrecedenceOneExpr5 ; diff --git a/part-12/src/main/java/com/endoflineblog/truffle/part_12/parsing/EasyScriptTruffleParser.java b/part-12/src/main/java/com/endoflineblog/truffle/part_12/parsing/EasyScriptTruffleParser.java index b6cb37ec..be394b53 100644 --- a/part-12/src/main/java/com/endoflineblog/truffle/part_12/parsing/EasyScriptTruffleParser.java +++ b/part-12/src/main/java/com/endoflineblog/truffle/part_12/parsing/EasyScriptTruffleParser.java @@ -471,22 +471,28 @@ private NegationExprNode parseUnaryMinusExpr(EasyScriptParser.UnaryMinusExpr4Con } private EasyScriptExprNode parseExpr5(EasyScriptParser.Expr5Context expr5) { - if (expr5 instanceof EasyScriptParser.LiteralExpr5Context) { - return parseLiteralExpr((EasyScriptParser.LiteralExpr5Context) expr5); - } else if (expr5 instanceof EasyScriptParser.ReferenceExpr5Context) { - return parseReference(((EasyScriptParser.ReferenceExpr5Context) expr5).ID().getText()); - } else if (expr5 instanceof EasyScriptParser.PropertyReadExpr5Context) { + if (expr5 instanceof EasyScriptParser.PropertyReadExpr5Context) { return this.parsePropertyReadExpr((EasyScriptParser.PropertyReadExpr5Context) expr5); - } else if (expr5 instanceof EasyScriptParser.NewExpr5Context) { - return this.parseNewExpr((EasyScriptParser.NewExpr5Context) expr5); - } else if (expr5 instanceof EasyScriptParser.ArrayLiteralExpr5Context) { - return parseArrayLiteralExpr((EasyScriptParser.ArrayLiteralExpr5Context) expr5); } else if (expr5 instanceof EasyScriptParser.ArrayIndexReadExpr5Context) { return this.parseArrayIndexReadExpr((EasyScriptParser.ArrayIndexReadExpr5Context) expr5); } else if (expr5 instanceof EasyScriptParser.CallExpr5Context) { return parseCallExpr((EasyScriptParser.CallExpr5Context) expr5); } else { - return parseExpr1(((EasyScriptParser.PrecedenceOneExpr5Context) expr5).expr1()); + return this.parseExpr6(((EasyScriptParser.PrecedenceSixExpr5Context) expr5).expr6()); + } + } + + private EasyScriptExprNode parseExpr6(EasyScriptParser.Expr6Context expr6) { + if (expr6 instanceof EasyScriptParser.LiteralExpr5Context) { + return parseLiteralExpr((EasyScriptParser.LiteralExpr5Context) expr6); + } else if (expr6 instanceof EasyScriptParser.ReferenceExpr5Context) { + return parseReference(((EasyScriptParser.ReferenceExpr5Context) expr6).ID().getText()); + } else if (expr6 instanceof EasyScriptParser.ArrayLiteralExpr5Context) { + return parseArrayLiteralExpr((EasyScriptParser.ArrayLiteralExpr5Context) expr6); + } else if (expr6 instanceof EasyScriptParser.NewExpr5Context) { + return this.parseNewExpr((EasyScriptParser.NewExpr5Context) expr6); + } else { + return parseExpr1(((EasyScriptParser.PrecedenceOneExpr5Context) expr6).expr1()); } } @@ -539,7 +545,7 @@ private EasyScriptExprNode parseNewExpr(EasyScriptParser.NewExpr5Context newExpr newExpr.expr1().stream() .map(this::parseExpr1) .collect(Collectors.toList()), - this.parseExpr5(newExpr.constr)); + this.parseExpr6(newExpr.constr)); } private ArrayLiteralExprNode parseArrayLiteralExpr(EasyScriptParser.ArrayLiteralExpr5Context arrayLiteralExpr) { diff --git a/part-12/src/test/java/com/endoflineblog/truffle/part_12/ClassTest.java b/part-12/src/test/java/com/endoflineblog/truffle/part_12/ClassTest.java index 5ae3ae0e..61c7ca16 100644 --- a/part-12/src/test/java/com/endoflineblog/truffle/part_12/ClassTest.java +++ b/part-12/src/test/java/com/endoflineblog/truffle/part_12/ClassTest.java @@ -59,8 +59,7 @@ void methods_can_be_called_on_class_instances() { " return 'A.a'; " + " } " + "} " + - "const a = new A(); " + - "a.a(); "); + "new A().a();"); assertEquals("A.a", result.asString()); }