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 ->` // * stab arguments item in `arg1, left when right ->`
[$.binary_operator, $._stab_clause_arguments_without_parentheses], [$.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: // Given `(-> • /`, stab can be either:
// * stab clause operator in `(-> / / 2)` // * stab clause operator in `(-> / / 2)`
// * operator identifier in `(-> / 2)` // * operator identifier in `(-> / 2)`
@ -738,7 +746,13 @@ module.exports = grammar({
"(", "(",
optional( optional(
choice( 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 $.keywords
) )
), ),

View File

@ -5512,8 +5512,12 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "PREC_RIGHT",
"name": "_expression" "value": 20,
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@ -5525,8 +5529,12 @@
"value": "," "value": ","
}, },
{ {
"type": "SYMBOL", "type": "PREC_RIGHT",
"name": "_expression" "value": 20,
"content": {
"type": "SYMBOL",
"name": "_expression"
}
} }
] ]
} }
@ -5874,6 +5882,10 @@
"binary_operator", "binary_operator",
"_stab_clause_arguments_without_parentheses" "_stab_clause_arguments_without_parentheses"
], ],
[
"_stab_clause_arguments_without_parentheses",
"_stab_clause_arguments_with_parentheses"
],
[ [
"operator_identifier", "operator_identifier",
"stab_clause" "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 foo do
a when b, c when d == e -> 1 a when b, c when d == e -> 1
(a, a when b) -> 1
end end
--- ---
@ -479,6 +480,14 @@ end
(binary_operator (binary_operator
(identifier) (identifier)
(identifier))) (identifier)))
(body
(integer)))
(stab_clause
(arguments
(identifier)
(binary_operator
(identifier)
(identifier)))
(body (body
(integer)))))) (integer))))))

View File

@ -69,7 +69,7 @@ fn x -> x end
many arguments 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 (anonymous_function
(stab_clause (stab_clause
(arguments (arguments
(identifier)
(identifier) (identifier)
(identifier)) (identifier))
(body (body

View File

@ -96,7 +96,7 @@ trailing semicolon
(integer))) (integer)))
===================================== =====================================
stab clauses stab clause / multiple clauses
===================================== =====================================
(x -> x; y -> y (x -> x; y -> y
@ -121,3 +121,110 @@ stab clauses
(identifier)) (identifier))
(body (body
(identifier))))) (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)))))