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,

74
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,38 +641,22 @@
"type": "FIELD", "type": "FIELD",
"name": "pattern", "name": "pattern",
"content": { "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_string_literal" "members": [
} {
}, "type": "SYMBOL",
{ "name": "_string_literal"
"type": "STRING", },
"value": ":" {
}, "type": "ALIAS",
{ "content": {
"type": "FIELD", "type": "STRING",
"name": "value", "value": "default"
"content": { },
"type": "SYMBOL", "named": true,
"name": "_case_value" "value": "default"
} }
} ]
]
},
"default_case": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "pattern",
"content": {
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "default"
},
"named": true,
"value": "default"
} }
}, },
{ {

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))