From bd382f679a28591a761c67cb69ade73738ae5492 Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Mon, 21 Aug 2023 23:42:17 -0700 Subject: [PATCH] Show a solution to the parsing problem: a duplicated Call rule, in expr6. I probably won't use it in the article, as it adds complexity. --- .../part_12/parsing/antlr/EasyScript.g4 | 1 + .../parsing/EasyScriptTruffleParser.java | 10 ++++++ .../truffle/part_12/ParsingTest.java | 35 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 part-12/src/test/java/com/endoflineblog/truffle/part_12/ParsingTest.java 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 7bbab73b..7642410a 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 @@ -47,6 +47,7 @@ expr6 : literal #LiteralExpr5 | ID #ReferenceExpr5 | '[' (expr1 (',' expr1)*)? ']' #ArrayLiteralExpr5 | 'new' constr=expr6 '(' (expr1 (',' expr1)*)? ')' #NewExpr5 + | expr6 '(' (expr1 (',' expr1)*)? ')' #CallExpr6 | '(' 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 be394b53..24444fef 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 @@ -491,6 +491,8 @@ private EasyScriptExprNode parseExpr6(EasyScriptParser.Expr6Context expr6) { return parseArrayLiteralExpr((EasyScriptParser.ArrayLiteralExpr5Context) expr6); } else if (expr6 instanceof EasyScriptParser.NewExpr5Context) { return this.parseNewExpr((EasyScriptParser.NewExpr5Context) expr6); + } else if (expr6 instanceof EasyScriptParser.CallExpr6Context) { + return parseCallExpr6((EasyScriptParser.CallExpr6Context) expr6); } else { return parseExpr1(((EasyScriptParser.PrecedenceOneExpr5Context) expr6).expr1()); } @@ -568,6 +570,14 @@ private FunctionCallExprNode parseCallExpr(EasyScriptParser.CallExpr5Context cal .collect(Collectors.toList())); } + private FunctionCallExprNode parseCallExpr6(EasyScriptParser.CallExpr6Context callExpr) { + return new FunctionCallExprNode( + parseExpr6(callExpr.expr6()), + callExpr.expr1().stream() + .map(this::parseExpr1) + .collect(Collectors.toList())); + } + private EasyScriptExprNode parseIntLiteral(String text) { try { return new IntLiteralExprNode(Integer.parseInt(text)); diff --git a/part-12/src/test/java/com/endoflineblog/truffle/part_12/ParsingTest.java b/part-12/src/test/java/com/endoflineblog/truffle/part_12/ParsingTest.java new file mode 100644 index 00000000..4e61b6c0 --- /dev/null +++ b/part-12/src/test/java/com/endoflineblog/truffle/part_12/ParsingTest.java @@ -0,0 +1,35 @@ +package com.endoflineblog.truffle.part_12; + +import org.graalvm.polyglot.Context; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ParsingTest { + private Context context; + + @BeforeEach + public void setUp() { + this.context = Context.create(); + } + + @AfterEach + public void tearDown() { + this.context.close(); + } + + @Test + public void multiple_calls_of_calls_are_parsed() { + this.context.parse("ezs", "a()()()"); + } + + @Test + public void property_reads_of_property_reads_are_parsed() { + this.context.parse("ezs", "a.b.c"); + } + + @Test + public void method_calls_of_calls_are_parsed() { + this.context.parse("ezs", "a().b().c()"); + } +}