Add 'select' expression

Except for 'soong_config_variable' which is not yet handled.
This commit is contained in:
Bruno BELANYI 2024-04-08 01:12:35 +01:00
parent 9dd109b90a
commit 12553c3bb8
5 changed files with 3470 additions and 1495 deletions

View file

@ -2,6 +2,10 @@ 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",
@ -37,6 +41,8 @@ module.exports = grammar({
$.boolean_literal, $.boolean_literal,
$.integer_literal, $.integer_literal,
$._string_literal, $._string_literal,
// Conditionals
$.select_expression,
// Composites // Composites
$.list_expression, $.list_expression,
$.map_expression, $.map_expression,
@ -82,6 +88,50 @@ module.exports = grammar({
), ),
)), )),
select_expression: ($) => seq(
"select",
"(",
$.select_value,
",",
$.select_cases,
")",
),
select_value: ($) => seq(
field("type", alias(
choice("product_variable", "release_variable", "variant"),
$.selection_type,
)),
"(",
field("condition", $._string_literal),
")",
),
select_cases: ($) => seq(
"{",
optional(trailingCommaSeparated($.select_case)),
// default *must* be the last one, enforced at parse-time...
optional(seq($.default_case, ",")),
"}",
),
select_case: ($) => seq(
field("pattern", $._string_literal),
":",
field("value", $._case_value)
),
default_case: ($) => seq(
field("pattern", "default"),
":",
field("value", $._case_value),
),
_case_value: ($) => choice(
alias("unset", $.unset),
$._expr,
),
list_expression: ($) => seq( list_expression: ($) => seq(
"[", "[",
optional(commaSeparated($._expr)), optional(commaSeparated($._expr)),

206
src/grammar.json generated
View file

@ -87,6 +87,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "_string_literal" "name": "_string_literal"
}, },
{
"type": "SYMBOL",
"name": "select_expression"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "list_expression" "name": "list_expression"
@ -237,6 +241,208 @@
] ]
} }
}, },
"select_expression": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "select"
},
{
"type": "STRING",
"value": "("
},
{
"type": "SYMBOL",
"name": "select_value"
},
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "select_cases"
},
{
"type": "STRING",
"value": ")"
}
]
},
"select_value": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "type",
"content": {
"type": "ALIAS",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "product_variable"
},
{
"type": "STRING",
"value": "release_variable"
},
{
"type": "STRING",
"value": "variant"
}
]
},
"named": true,
"value": "selection_type"
}
},
{
"type": "STRING",
"value": "("
},
{
"type": "FIELD",
"name": "condition",
"content": {
"type": "SYMBOL",
"name": "_string_literal"
}
},
{
"type": "STRING",
"value": ")"
}
]
},
"select_cases": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "CHOICE",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "select_case"
},
{
"type": "STRING",
"value": ","
}
]
}
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "default_case"
},
{
"type": "STRING",
"value": ","
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "}"
}
]
},
"select_case": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "pattern",
"content": {
"type": "SYMBOL",
"name": "_string_literal"
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_case_value"
}
}
]
},
"default_case": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "pattern",
"content": {
"type": "STRING",
"value": "default"
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_case_value"
}
}
]
},
"_case_value": {
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "unset"
},
"named": true,
"value": "unset"
},
{
"type": "SYMBOL",
"name": "_expr"
}
]
},
"list_expression": { "list_expression": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [

224
src/node-types.json generated
View file

@ -58,6 +58,10 @@
{ {
"type": "raw_string_literal", "type": "raw_string_literal",
"named": true "named": true
},
{
"type": "select_expression",
"named": true
} }
] ]
} }
@ -73,6 +77,64 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "default_case",
"named": true,
"fields": {
"pattern": {
"multiple": false,
"required": true,
"types": [
{
"type": "default",
"named": false
}
]
},
"value": {
"multiple": false,
"required": true,
"types": [
{
"type": "boolean_literal",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "interpreted_string_literal",
"named": true
},
{
"type": "list_expression",
"named": true
},
{
"type": "map_expression",
"named": true
},
{
"type": "raw_string_literal",
"named": true
},
{
"type": "select_expression",
"named": true
},
{
"type": "unset",
"named": true
}
]
}
}
},
{ {
"type": "integer_literal", "type": "integer_literal",
"named": true, "named": true,
@ -128,6 +190,10 @@
{ {
"type": "raw_string_literal", "type": "raw_string_literal",
"named": true "named": true
},
{
"type": "select_expression",
"named": true
} }
] ]
} }
@ -177,6 +243,140 @@
{ {
"type": "raw_string_literal", "type": "raw_string_literal",
"named": true "named": true
},
{
"type": "select_expression",
"named": true
}
]
}
}
},
{
"type": "select_case",
"named": true,
"fields": {
"pattern": {
"multiple": false,
"required": true,
"types": [
{
"type": "interpreted_string_literal",
"named": true
},
{
"type": "raw_string_literal",
"named": true
}
]
},
"value": {
"multiple": false,
"required": true,
"types": [
{
"type": "boolean_literal",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "interpreted_string_literal",
"named": true
},
{
"type": "list_expression",
"named": true
},
{
"type": "map_expression",
"named": true
},
{
"type": "raw_string_literal",
"named": true
},
{
"type": "select_expression",
"named": true
},
{
"type": "unset",
"named": true
}
]
}
}
},
{
"type": "select_cases",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "default_case",
"named": true
},
{
"type": "select_case",
"named": true
}
]
}
},
{
"type": "select_expression",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "select_cases",
"named": true
},
{
"type": "select_value",
"named": true
}
]
}
},
{
"type": "select_value",
"named": true,
"fields": {
"condition": {
"multiple": false,
"required": true,
"types": [
{
"type": "interpreted_string_literal",
"named": true
},
{
"type": "raw_string_literal",
"named": true
}
]
},
"type": {
"multiple": false,
"required": true,
"types": [
{
"type": "selection_type",
"named": true
} }
] ]
} }
@ -205,6 +405,14 @@
"type": "#", "type": "#",
"named": false "named": false
}, },
{
"type": "(",
"named": false
},
{
"type": ")",
"named": false
},
{ {
"type": "+=", "type": "+=",
"named": false "named": false
@ -233,6 +441,10 @@
"type": "]", "type": "]",
"named": false "named": false
}, },
{
"type": "default",
"named": false
},
{ {
"type": "escape_sequence", "type": "escape_sequence",
"named": true "named": true
@ -249,10 +461,22 @@
"type": "raw_string_literal", "type": "raw_string_literal",
"named": true "named": true
}, },
{
"type": "select",
"named": false
},
{
"type": "selection_type",
"named": true
},
{ {
"type": "true", "type": "true",
"named": false "named": false
}, },
{
"type": "unset",
"named": true
},
{ {
"type": "{", "type": "{",
"named": false "named": false

4277
src/parser.c generated

File diff suppressed because it is too large Load diff

160
test/corpus/select.txt Normal file
View file

@ -0,0 +1,160 @@
================================================================================
Select
================================================================================
foo = select(release_variable("RELEASE_TEST"), {
"d": "d2",
default: unset,
})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(select_expression
(select_value
(selection_type)
(interpreted_string_literal))
(select_cases
(select_case
(interpreted_string_literal)
(interpreted_string_literal))
(default_case
(unset))))))
================================================================================
Select (no default)
================================================================================
foo = select(variant("arch"), {
"x86": "my_x86",
"x86_64": "my_x86_64",
"arm": "my_arm",
"arm64": "my_arm64",
})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(select_expression
(select_value
(selection_type)
(interpreted_string_literal))
(select_cases
(select_case
(interpreted_string_literal)
(interpreted_string_literal))
(select_case
(interpreted_string_literal)
(interpreted_string_literal))
(select_case
(interpreted_string_literal)
(interpreted_string_literal))
(select_case
(interpreted_string_literal)
(interpreted_string_literal))))))
================================================================================
Select (no values)
================================================================================
foo = select(variant("VARIANT"), {})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(select_expression
(select_value
(selection_type)
(interpreted_string_literal))
(select_cases))))
================================================================================
Select (default in wrong order)
================================================================================
foo = select(variant("VARIANT"), {
"x86": "my_x86",
"x86_64": "my_x86_64",
default: unset,
"arm": "my_arm",
"arm64": "my_arm64",
})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(select_expression
(select_value
(selection_type)
(interpreted_string_literal))
(select_cases
(select_case
(interpreted_string_literal)
(interpreted_string_literal))
(select_case
(interpreted_string_literal)
(interpreted_string_literal))
(ERROR
(default_case
(unset)))
(select_case
(interpreted_string_literal)
(interpreted_string_literal))
(select_case
(interpreted_string_literal)
(interpreted_string_literal))))))
================================================================================
Select (no condition)
================================================================================
foo = select(variant(), {
"d": unset,
default: "f2",
})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(select_expression
(select_value
(selection_type)
(MISSING raw_string_literal))
(select_cases
(select_case
(interpreted_string_literal)
(unset))
(default_case
(interpreted_string_literal))))))
================================================================================
Select (invalid type)
================================================================================
foo = select(some_unknown_type("CONDITION"), {
"d": "d2",
default: "f2",
})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(ERROR
(identifier)
(identifier)
(interpreted_string_literal)
(interpreted_string_literal))
(interpreted_string_literal))
(ERROR))