WIP: Merge 'select_case' and 'default_case'

Not sure whether I like this change or not... IMO this might be too lax.
This commit is contained in:
Bruno BELANYI 2024-04-09 13:01:49 +01:00
parent 4da69807ad
commit ee37351e81
4 changed files with 926 additions and 1148 deletions

View file

@ -2,10 +2,6 @@ function commaSeparated(elem) {
return seq(elem, repeat(seq(",", elem)), optional(",")) return seq(elem, repeat(seq(",", elem)), optional(","))
} }
function trailingCommaSeparated(elem) {
return repeat(seq(elem, ","))
}
module.exports = grammar({ module.exports = grammar({
name: "blueprint", name: "blueprint",
@ -153,24 +149,16 @@ module.exports = grammar({
_select_cases: ($) => seq( _select_cases: ($) => seq(
"{", "{",
optional(trailingCommaSeparated($.select_case)), optional(repeat(seq($.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(alias("default", $.default), $._string_literal)),
":", ":",
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

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

1977
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -130,10 +130,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))