Add multi-valued select expression

This commit is contained in:
Bruno BELANYI 2024-04-23 15:43:10 +00:00
parent 96ae542119
commit f267a5be95
5 changed files with 1926 additions and 1178 deletions

View file

@ -131,7 +131,12 @@ module.exports = grammar({
")", ")",
), ),
select_value: ($) => seq( select_value: ($) => choice(
$._select_value,
seq("(", commaSeparatedOptTrailing($._select_value), ")"),
),
_select_value: ($) => seq(
field("name", $.identifier), field("name", $.identifier),
"(", "(",
field("arguments", optional(commaSeparatedNoTrailing($._string_literal))), field("arguments", optional(commaSeparatedNoTrailing($._string_literal))),
@ -145,15 +150,22 @@ module.exports = grammar({
), ),
select_case: ($) => seq( select_case: ($) => seq(
field("pattern", choice( field("pattern", $.select_pattern),
$._string_literal,
$.boolean_literal,
alias("default", $.default),
)),
":", ":",
field("value", $._case_value) field("value", $._case_value)
), ),
select_pattern: ($) => choice(
$._select_pattern,
seq("(", commaSeparatedOptTrailing($._select_pattern), ")"),
),
_select_pattern: ($) => choice(
$._string_literal,
$.boolean_literal,
alias("default", $.default),
),
_case_value: ($) => choice( _case_value: ($) => choice(
alias("unset", $.unset), alias("unset", $.unset),
$._expr, $._expr,

162
src/grammar.json generated
View file

@ -487,6 +487,65 @@
] ]
}, },
"select_value": { "select_value": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_select_value"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_select_value"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_select_value"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "STRING",
"value": ")"
}
]
}
]
},
"_select_value": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -588,26 +647,8 @@
"type": "FIELD", "type": "FIELD",
"name": "pattern", "name": "pattern",
"content": { "content": {
"type": "CHOICE", "type": "SYMBOL",
"members": [ "name": "select_pattern"
{
"type": "SYMBOL",
"name": "_string_literal"
},
{
"type": "SYMBOL",
"name": "boolean_literal"
},
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "default"
},
"named": true,
"value": "default"
}
]
} }
}, },
{ {
@ -624,6 +665,87 @@
} }
] ]
}, },
"select_pattern": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_select_pattern"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_select_pattern"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_select_pattern"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "STRING",
"value": ")"
}
]
}
]
},
"_select_pattern": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_string_literal"
},
{
"type": "SYMBOL",
"name": "boolean_literal"
},
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "default"
},
"named": true,
"value": "default"
}
]
},
"_case_value": { "_case_value": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [

43
src/node-types.json generated
View file

@ -354,19 +354,7 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "boolean_literal", "type": "select_pattern",
"named": true
},
{
"type": "default",
"named": true
},
{
"type": "interpreted_string_literal",
"named": true
},
{
"type": "raw_string_literal",
"named": true "named": true
} }
] ]
@ -453,6 +441,33 @@
] ]
} }
}, },
{
"type": "select_pattern",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "boolean_literal",
"named": true
},
{
"type": "default",
"named": true
},
{
"type": "interpreted_string_literal",
"named": true
},
{
"type": "raw_string_literal",
"named": true
}
]
}
},
{ {
"type": "select_value", "type": "select_value",
"named": true, "named": true,
@ -476,7 +491,7 @@
] ]
}, },
"name": { "name": {
"multiple": false, "multiple": true,
"required": true, "required": true,
"types": [ "types": [
{ {

2753
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -19,10 +19,12 @@ foo = select(release_variable("RELEASE_TEST"), {
(interpreted_string_literal)) (interpreted_string_literal))
(select_cases (select_cases
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(default) (select_pattern
(default))
(unset)))))) (unset))))))
================================================================================ ================================================================================
@ -47,10 +49,12 @@ foo = select(soong_config_variable("my_namespace", "my_var"), {
(interpreted_string_literal)) (interpreted_string_literal))
(select_cases (select_cases
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(unset)) (unset))
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)))))) (interpreted_string_literal))))))
================================================================================ ================================================================================
@ -76,16 +80,20 @@ foo = select(variant("arch"), {
(interpreted_string_literal)) (interpreted_string_literal))
(select_cases (select_cases
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)))))) (interpreted_string_literal))))))
================================================================================ ================================================================================
@ -130,19 +138,24 @@ foo = select(variant("VARIANT"), {
(interpreted_string_literal)) (interpreted_string_literal))
(select_cases (select_cases
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(default) (select_pattern
(default))
(unset)) (unset))
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)))))) (interpreted_string_literal))))))
================================================================================ ================================================================================
@ -165,10 +178,12 @@ foo = select(variant(), {
(identifier)) (identifier))
(select_cases (select_cases
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(unset)) (unset))
(select_case (select_case
(default) (select_pattern
(default))
(interpreted_string_literal)))))) (interpreted_string_literal))))))
================================================================================ ================================================================================
@ -192,10 +207,12 @@ foo = select(some_unknown_type("CONDITION"), {
(interpreted_string_literal)) (interpreted_string_literal))
(select_cases (select_cases
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(default) (select_pattern
(default))
(interpreted_string_literal)))))) (interpreted_string_literal))))))
================================================================================ ================================================================================
@ -220,10 +237,12 @@ foo = select(release_variable("ONE", "TWO"), {
(interpreted_string_literal)) (interpreted_string_literal))
(select_cases (select_cases
(select_case (select_case
(interpreted_string_literal) (select_pattern
(interpreted_string_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(default) (select_pattern
(default))
(interpreted_string_literal)))))) (interpreted_string_literal))))))
================================================================================ ================================================================================
@ -248,10 +267,65 @@ foo = select(some_boolean("VALUE",), {
(ERROR)) (ERROR))
(select_cases (select_cases
(select_case (select_case
(boolean_literal) (select_pattern
(boolean_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(boolean_literal) (select_pattern
(boolean_literal))
(interpreted_string_literal))))))
================================================================================
Select (trailing comma in values)
================================================================================
foo = select((
arch(),
os(),
), {
(default, default): "true",
})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(select_expression
(select_value
(identifier)
(identifier))
(select_cases
(select_case
(select_pattern
(default)
(default))
(interpreted_string_literal))))))
================================================================================
Select (trailing comma in pattern)
================================================================================
foo = select((arch(), os()), {
(default, default,): "true",
})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(operator)
(select_expression
(select_value
(identifier)
(identifier))
(select_cases
(select_case
(select_pattern
(default)
(default))
(interpreted_string_literal)))))) (interpreted_string_literal))))))
================================================================================ ================================================================================
@ -275,10 +349,12 @@ foo = select(some_boolean("IS_TRUE"), {
(interpreted_string_literal)) (interpreted_string_literal))
(select_cases (select_cases
(select_case (select_case
(boolean_literal) (select_pattern
(boolean_literal))
(interpreted_string_literal)) (interpreted_string_literal))
(select_case (select_case
(boolean_literal) (select_pattern
(boolean_literal))
(interpreted_string_literal)))))) (interpreted_string_literal))))))
================================================================================ ================================================================================