Relax ordering in 'select' cases

We shouldn't really care whether or not `default` *is* the last value...
This commit is contained in:
Bruno BELANYI 2024-04-23 14:38:32 +00:00
parent 93a71d095b
commit b2e113a821
4 changed files with 1035 additions and 1241 deletions

View file

@ -154,23 +154,18 @@ module.exports = grammar({
select_cases: ($) => seq( select_cases: ($) => seq(
"{", "{",
optional(trailingCommaSeparated($.select_case)), optional(trailingCommaSeparated($.select_case)),
// default *must* be the last one, enforced at parse-time...
optional(seq(alias($.default_case, $.select_case), ",")),
"}", "}",
), ),
select_case: ($) => seq( select_case: ($) => seq(
field("pattern", $._string_literal), field("pattern", choice(
$._string_literal,
alias("default", $.default),
)),
":", ":",
field("value", $._case_value) field("value", $._case_value)
), ),
default_case: ($) => seq(
field("pattern", alias("default", $.default)),
":",
field("value", $._case_value),
),
_case_value: ($) => choice( _case_value: ($) => choice(
alias("unset", $.unset), alias("unset", $.unset),
$._expr, $._expr,

56
src/grammar.json generated
View file

@ -628,32 +628,6 @@
} }
] ]
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "default_case"
},
"named": true,
"value": "select_case"
},
{
"type": "STRING",
"value": ","
}
]
},
{
"type": "BLANK"
}
]
},
{ {
"type": "STRING", "type": "STRING",
"value": "}" "value": "}"
@ -667,31 +641,13 @@
"type": "FIELD", "type": "FIELD",
"name": "pattern", "name": "pattern",
"content": { "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_string_literal"
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_case_value"
}
}
]
},
"default_case": {
"type": "SEQ",
"members": [ "members": [
{ {
"type": "FIELD", "type": "SYMBOL",
"name": "pattern", "name": "_string_literal"
"content": { },
{
"type": "ALIAS", "type": "ALIAS",
"content": { "content": {
"type": "STRING", "type": "STRING",
@ -700,6 +656,8 @@
"named": true, "named": true,
"value": "default" "value": "default"
} }
]
}
}, },
{ {
"type": "STRING", "type": "STRING",

2182
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -135,10 +135,9 @@ foo = select(variant("VARIANT"), {
(select_case (select_case
(interpreted_string_literal) (interpreted_string_literal)
(interpreted_string_literal)) (interpreted_string_literal))
(ERROR
(select_case (select_case
(default) (default)
(unset))) (unset))
(select_case (select_case
(interpreted_string_literal) (interpreted_string_literal)
(interpreted_string_literal)) (interpreted_string_literal))