Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect parsing of binary expressions (fixes #153) #158

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,34 @@ a
(simple_identifier)
(simple_identifier)))

================================================================================
Conjunction with call sites
================================================================================

a && b()

a
&& c.b()

--------------------------------------------------------------------------------

(source_file
(conjunction_expression
(simple_identifier)
(call_expression
(simple_identifier)
(call_suffix
(value_arguments))))
(conjunction_expression
(simple_identifier)
(call_expression
(navigation_expression
(simple_identifier)
(navigation_suffix
(simple_identifier)))
(call_suffix
(value_arguments)))))

================================================================================
Bitwise operations
================================================================================
Expand Down
25 changes: 25 additions & 0 deletions corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,31 @@ else if bar {
(if_statement
(simple_identifier))))

================================================================================
If try
================================================================================

if try limit == nil && singleResult && !expectsSingleResult {
return a
}

--------------------------------------------------------------------------------

(source_file
(if_statement
(conjunction_expression
(try_expression
(equality_expression
(simple_identifier)))
(conjunction_expression
(simple_identifier)
(prefix_expression
(bang)
(simple_identifier))))
(statements
(control_transfer_statement
(simple_identifier)))))

================================================================================
Guard statements
================================================================================
Expand Down
26 changes: 18 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const PRECS = {
try: -2,
call_suffix: -2,
range_suffix: -2,
ternary_suffix: -2,
ternary_binary_suffix: -2,
await: -2,
assignment: -3,
comment: -3,
Expand Down Expand Up @@ -123,7 +123,7 @@ module.exports = grammar({
// a ? b : c () could be calling c(), or it could be calling a function that's produced by the result of
// `(a ? b : c)`. We have a small hack to force it to be the former of these by intentionally introducing a
// conflict.
[$.call_suffix, $.expr_hack_at_ternary_call_suffix],
[$.call_suffix, $.expr_hack_at_ternary_binary_call_suffix],
// try {expression} is a bit magic and applies quite broadly: `try foo()` and `try foo { }` show that this is right
// associative, and `try foo ? bar() : baz` even more so. But it doesn't always win: something like
// `if try foo { } ...` should award its braces to the `if`. In order to make this actually happen, we need to parse
Expand Down Expand Up @@ -593,7 +593,16 @@ module.exports = grammar({
seq(
field("lhs", $._expression),
field("op", $._conjunction_operator),
field("rhs", $._expression)
prec.left(
PRECS.ternary_binary_suffix,
field(
"rhs",
choice(
$._expression,
alias($.expr_hack_at_ternary_binary_call, $.call_expression)
)
)
)
)
),
disjunction_expression: ($) =>
Expand Down Expand Up @@ -688,6 +697,7 @@ module.exports = grammar({
// left associativity for the direct calls, which is technically wrong but is the only way to resolve the
// ambiguity of `if foo { ... }` in the correct direction.
prec.right(-2, $._expression),
prec.left(0, $._binary_expression),
prec.left(0, $.call_expression),
// Similarly special case the ternary expression, where `try` may come earlier than it is actually needed.
// When the parser just encounters some identifier after a `try`, it should prefer the `call_expression` (so
Expand Down Expand Up @@ -726,23 +736,23 @@ module.exports = grammar({
field("if_true", $._expression),
":",
prec.left(
PRECS.ternary_suffix,
PRECS.ternary_binary_suffix,
field(
"if_false",
choice(
$._expression,
alias($.expr_hack_at_ternary_call, $.call_expression)
alias($.expr_hack_at_ternary_binary_call, $.call_expression)
)
)
)
)
),
expr_hack_at_ternary_call: ($) =>
expr_hack_at_ternary_binary_call: ($) =>
seq(
$._expression,
alias($.expr_hack_at_ternary_call_suffix, $.call_suffix)
alias($.expr_hack_at_ternary_binary_call_suffix, $.call_suffix)
),
expr_hack_at_ternary_call_suffix: ($) =>
expr_hack_at_ternary_binary_call_suffix: ($) =>
prec(PRECS.call_suffix, $.value_arguments),
call_expression: ($) => prec(PRECS.call, seq($._expression, $.call_suffix)),
_primary_expression: ($) =>
Expand Down