Skip to content

Commit

Permalink
Allow trailing dot on arbitrary expressions (#77)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Ben Sherman <[email protected]>
  • Loading branch information
bentsherman authored Nov 26, 2024
1 parent 99a6ccf commit a641cf5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 26 deletions.
7 changes: 1 addition & 6 deletions modules/compiler/src/main/antlr/ScriptParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ expression
fb=expression #conditionalExprAlt

// incomplete expression
| incompleteExpression #incompleteExprAlt
| expression nls (DOT | SPREAD_DOT | SAFE_DOT) #incompleteExprAlt
;

primary
Expand Down Expand Up @@ -628,11 +628,6 @@ namedArg
: namedProperty COLON expression
;

// -- incomplete expression
incompleteExpression
: identifier (DOT identifier)* DOT
;

//
// types
//
Expand Down
26 changes: 6 additions & 20 deletions modules/compiler/src/main/java/script/parser/ScriptAstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,12 @@ private Expression expression(ExpressionContext ctx) {
if( ctx instanceof UnaryNotExprAltContext unac )
return ast( unaryNot(expression(unac.expression()), unac.op), unac );

if( ctx instanceof IncompleteExprAltContext iac )
return incompleteExpression(iac.incompleteExpression());
if( ctx instanceof IncompleteExprAltContext iac ) {
var object = expression(iac.expression());
var result = ast( propX(object, ""), iac );
collectSyntaxError(new SyntaxException("Incomplete expression", result));
return result;
}

throw createParsingFailedException("Invalid expression: " + ctx.getText(), ctx);
}
Expand Down Expand Up @@ -1497,24 +1501,6 @@ private void checkDuplicateNamedArg(List<MapEntryExpression> namedArgs, MapEntry
}
}

private Expression incompleteExpression(IncompleteExpressionContext ctx) {
var prop = propertyExpression(ctx.identifier());
var result = ast( propX(prop, ""), ctx );
collectSyntaxError(new SyntaxException("Incomplete expression", result));
return result;
}

private Expression propertyExpression(List<IdentifierContext> idents) {
var head = idents.get(0);
Expression result = variableName(head);
for( int i = 1; i < idents.size(); i++ ) {
var ident = idents.get(i);
var name = ast( constX(identifier(ident)), ident );
result = ast( propX(result, name), result, name );
}
return result;
}

/// MISCELLANEOUS

private Parameter[] formalParameterList(FormalParameterListContext ctx) {
Expand Down

0 comments on commit a641cf5

Please sign in to comment.