Skip to content

Commit

Permalink
Show a solution to the parsing problem: a duplicated Call rule, in ex…
Browse files Browse the repository at this point in the history
…pr6.

I probably won't use it in the article, as it adds complexity.
  • Loading branch information
skinny85 committed Aug 22, 2023
1 parent b7b9372 commit bd382f6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ expr6 : literal #LiteralExpr5
| ID #ReferenceExpr5
| '[' (expr1 (',' expr1)*)? ']' #ArrayLiteralExpr5
| 'new' constr=expr6 '(' (expr1 (',' expr1)*)? ')' #NewExpr5
| expr6 '(' (expr1 (',' expr1)*)? ')' #CallExpr6
| '(' expr1 ')' #PrecedenceOneExpr5
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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()");
}
}

0 comments on commit bd382f6

Please sign in to comment.