Fix stab clause ambiguities

This commit is contained in:
Jonatan Kłosko 2021-09-28 20:40:20 +02:00
parent bb5f90516e
commit e35f31122f
6 changed files with 267343 additions and 266966 deletions

View File

@ -168,6 +168,14 @@ module.exports = grammar({
// * stab arguments item in `arg1, left when right ->`
[$.binary_operator, $._stab_clause_arguments_without_parentheses],
// Given `((arg1, arg2 • ,`, `arg3` expression can be either:
// * stab parenthesised arguments item in `((arg1, arg2, arg3) ->)`
// * stab non-parenthesised arguments item in `((arg1, arg2, arg3 ->))`
[
$._stab_clause_arguments_without_parentheses,
$._stab_clause_arguments_with_parentheses,
],
// Given `(-> • /`, stab can be either:
// * stab clause operator in `(-> / / 2)`
// * operator identifier in `(-> / 2)`
@ -738,7 +746,13 @@ module.exports = grammar({
"(",
optional(
choice(
seq(sep1($._expression, ","), optional(seq(",", $.keywords))),
seq(
// We need the same expression precedence as below, so that we don't
// discard this rule in favour of the one below. We use right precedence,
// because in this case we can consume expression until the next comma
sep1(prec.right(PREC.WHEN_OP, $._expression), ","),
optional(seq(",", $.keywords))
),
$.keywords
)
),

View File

@ -5512,8 +5512,12 @@
"type": "SEQ",
"members": [
{
"type": "PREC_RIGHT",
"value": 20,
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "REPEAT",
@ -5525,9 +5529,13 @@
"value": ","
},
{
"type": "PREC_RIGHT",
"value": 20,
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
}
@ -5874,6 +5882,10 @@
"binary_operator",
"_stab_clause_arguments_without_parentheses"
],
[
"_stab_clause_arguments_without_parentheses",
"_stab_clause_arguments_with_parentheses"
],
[
"operator_identifier",
"stab_clause"

534152
src/parser.c

File diff suppressed because it is too large Load Diff

View File

@ -461,6 +461,7 @@ stab clause / edge cases / "when" in arguments
foo do
a when b, c when d == e -> 1
(a, a when b) -> 1
end
---
@ -479,6 +480,14 @@ end
(binary_operator
(identifier)
(identifier)))
(body
(integer)))
(stab_clause
(arguments
(identifier)
(binary_operator
(identifier)
(identifier)))
(body
(integer))))))

View File

@ -69,7 +69,7 @@ fn x -> x end
many arguments
=====================================
fn(x, y) -> x + y end
fn(x, y, z) -> x + y end
---
@ -77,6 +77,7 @@ fn(x, y) -> x + y end
(anonymous_function
(stab_clause
(arguments
(identifier)
(identifier)
(identifier))
(body

View File

@ -96,7 +96,7 @@ trailing semicolon
(integer)))
=====================================
stab clauses
stab clause / multiple clauses
=====================================
(x -> x; y -> y
@ -121,3 +121,110 @@ stab clauses
(identifier))
(body
(identifier)))))
=====================================
stab clause / multiple arguments
=====================================
(x, y, z -> x)
((x, y, z) -> x)
---
(source
(block
(stab_clause
(arguments
(identifier)
(identifier)
(identifier))
(body
(identifier))))
(block
(stab_clause
(arguments
(identifier)
(identifier)
(identifier))
(body
(identifier)))))
=====================================
stab clause / guard
=====================================
(x, y when x == y -> 1)
((x, y when x == y -> 1))
((x, y when x == y) -> 1)
(x, y when x, z -> 1)
((x, y when x, z -> 1))
((x, y when x, z) -> 1)
---
(source
(block
(stab_clause
(binary_operator
(arguments
(identifier)
(identifier))
(binary_operator
(identifier)
(identifier)))
(body
(integer))))
(block
(block
(stab_clause
(binary_operator
(arguments
(identifier)
(identifier))
(binary_operator
(identifier)
(identifier)))
(body
(integer)))))
(block
(stab_clause
(arguments
(identifier)
(binary_operator
(identifier)
(binary_operator
(identifier)
(identifier))))
(body
(integer))))
(block
(stab_clause
(arguments
(identifier)
(binary_operator
(identifier)
(identifier))
(identifier))
(body
(integer))))
(block
(block
(stab_clause
(arguments
(identifier)
(binary_operator
(identifier)
(identifier))
(identifier))
(body
(integer)))))
(block
(stab_clause
(arguments
(identifier)
(binary_operator
(identifier)
(identifier))
(identifier))
(body
(integer)))))