Skip to content

Commit

Permalink
First version of solving the 'parse new' problem: separate out Call, …
Browse files Browse the repository at this point in the history
…Prop and ArrayRead.
  • Loading branch information
skinny85 committed Aug 22, 2023
1 parent 0e1fab3 commit b7b9372
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
;

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

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

0 comments on commit b7b9372

Please sign in to comment.