Compare commits

...

15 commits

Author SHA1 Message Date
Bruno BELANYI f40f760a78 Add README 2024-04-08 02:00:45 +01:00
Bruno BELANYI f9e4049ccd Add 'property' alias
It makes the syntax tree more readable.
2024-04-08 02:00:45 +01:00
Bruno BELANYI 67649085fe Add 'element' field name to list 2024-04-08 02:00:45 +01:00
Bruno BELANYI c4d2cec062 Add 'property' field name where relevant 2024-04-08 02:00:45 +01:00
Bruno BELANYI 4c27f2578d Add modules 2024-04-08 02:00:45 +01:00
Bruno BELANYI d78d9e83b1 Add 'soong_config_variable' selection 2024-04-08 02:00:45 +01:00
Bruno BELANYI 6b4b70172e Add 'select' expression
Except for 'soong_config_variable' which is not yet handled.
2024-04-08 02:00:45 +01:00
Bruno BELANYI f378edc750 Add boolean literals 2024-04-08 02:00:45 +01:00
Bruno BELANYI 1d29c87f8c Add map expressions 2024-04-08 02:00:45 +01:00
Bruno BELANYI 6d4c0d6344 Add list expressions 2024-04-08 02:00:45 +01:00
Bruno BELANYI 7cb0f1ed38 Add string literals
Once again, taking the rules more or less straight from tree-sitter-go.
2024-04-08 02:00:45 +01:00
Bruno BELANYI 1e92f02aa5 Add negative integers 2024-04-08 02:00:45 +01:00
Bruno BELANYI 893d316aa0 Add comments 2024-04-08 02:00:45 +01:00
Bruno BELANYI e1f87618f0 Add '+=' assignment 2024-04-08 02:00:45 +01:00
Bruno BELANYI 73a5d747ef Add assignment 2024-04-08 02:00:45 +01:00
10 changed files with 9673 additions and 41 deletions

6
README.md Normal file
View file

@ -0,0 +1,6 @@
# tree-sitter-blueprint
Tree-sitter grammar for [Blueprint][blueprint-aosp], the meta-build system used
in AOSP for its `Android.bp` files.
[blueprint-aosp]: https://android.googlesource.com/platform/build/blueprint/

View file

@ -1,9 +1,207 @@
function commaSeparated(elem) {
return seq(elem, repeat(seq(",", elem)), optional(","))
}
function trailingCommaSeparated(elem) {
return repeat(seq(elem, ","))
}
module.exports = grammar({
name: "blueprint",
extras: ($) => [
/\s+/,
$.comment,
],
rules: {
// TODO: add the actual grammar rules
source_file: $ => 'hello',
source_file: ($) => repeat($._definition),
_definition: ($) => choice(
$.assignment,
$.module,
),
comment: (_) => seq("#", /.*/),
// Definitions {{{
assignment: ($) => seq(
field("left", $.identifier),
field("operator", choice("=", "+=")),
field("right", $._expr),
),
module: ($) => choice(
$._old_module,
$._new_module,
),
// This syntax is deprecated, but still accepted
_old_module: ($) => seq(
field("type", $.identifier),
"{",
optional(commaSeparated(
alias(field("property", $._colon_property), $.property)
)),
"}",
),
_new_module: ($) => seq(
$.identifier,
"(",
optional(commaSeparated(
alias(field("property", $._equal_property), $.property)
)),
")",
),
// }}}
// Expressions {{{
_expr: ($) => choice(
// Literals
$.identifier,
$.boolean_literal,
$.integer_literal,
$._string_literal,
// Conditionals
$.select_expression,
// Composites
$.list_expression,
$.map_expression,
),
// The Blueprint scanner makes use of Go's lexer, so copy their rule
identifier: (_) => /[_\p{XID_Start}][_\p{XID_Continue}]*/,
boolean_literal: (_) => choice("true", "false"),
integer_literal: (_) => seq(optional("-"), /[0-9]+/),
// The Blueprint scanner makes use of Go's lexer, so copy their rule
_string_literal: ($) => choice(
$.raw_string_literal,
$.interpreted_string_literal,
),
raw_string_literal: (_) => token(seq(
"`",
/[^`]+/,
"`",
)),
interpreted_string_literal: $ => seq(
'"',
repeat(choice(
// Allow all characters without special meaning, disallow newlines
/[^"\n\\]+/,
$.escape_sequence,
)),
token.immediate('"'),
),
escape_sequence: (_) => token.immediate(seq(
'\\',
choice(
/[^xuU]/,
/\d{2,3}/,
/x[0-9a-fA-F]{2,}/,
/u[0-9a-fA-F]{4}/,
/U[0-9a-fA-F]{8}/,
),
)),
select_expression: ($) => seq(
"select",
"(",
choice($.select_value, $.soong_config_variable),
",",
$.select_cases,
")",
),
select_value: ($) => seq(
field("type", alias(
choice("product_variable", "release_variable", "variant"),
$.selection_type,
)),
"(",
field("condition", $._string_literal),
")",
),
soong_config_variable: ($) => seq(
field("type", alias("soong_config_variable", $.selection_type)),
"(",
field(
"condition",
seq(
field("namespace", $._string_literal),
",",
field("variable", $._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(
"[",
optional(commaSeparated(field("element", $._expr))),
"]",
),
map_expression: ($) => seq(
"{",
optional(commaSeparated(
alias(field("property", $._colon_property), $.property)
)),
"}",
),
// }}}
// Properties {{{
_colon_property: ($) => seq(
field("field", $.identifier),
":",
field("value", $._expr),
),
_equal_property: ($) => seq(
field("field", $.identifier),
"=",
field("value", $._expr),
),
// }}}
}
});

885
src/grammar.json generated
View file

@ -2,14 +2,893 @@
"name": "blueprint",
"rules": {
"source_file": {
"type": "STRING",
"value": "hello"
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_definition"
}
},
"_definition": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "assignment"
},
{
"type": "SYMBOL",
"name": "module"
}
]
},
"comment": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "#"
},
{
"type": "PATTERN",
"value": ".*"
}
]
},
"assignment": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "STRING",
"value": "+="
}
]
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
}
]
},
"module": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_old_module"
},
{
"type": "SYMBOL",
"name": "_new_module"
}
]
},
"_old_module": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "type",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "STRING",
"value": "{"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "FIELD",
"name": "property",
"content": {
"type": "SYMBOL",
"name": "_colon_property"
}
},
"named": true,
"value": "property"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "ALIAS",
"content": {
"type": "FIELD",
"name": "property",
"content": {
"type": "SYMBOL",
"name": "_colon_property"
}
},
"named": true,
"value": "property"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "}"
}
]
},
"_new_module": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "FIELD",
"name": "property",
"content": {
"type": "SYMBOL",
"name": "_equal_property"
}
},
"named": true,
"value": "property"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "ALIAS",
"content": {
"type": "FIELD",
"name": "property",
"content": {
"type": "SYMBOL",
"name": "_equal_property"
}
},
"named": true,
"value": "property"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
"_expr": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "boolean_literal"
},
{
"type": "SYMBOL",
"name": "integer_literal"
},
{
"type": "SYMBOL",
"name": "_string_literal"
},
{
"type": "SYMBOL",
"name": "select_expression"
},
{
"type": "SYMBOL",
"name": "list_expression"
},
{
"type": "SYMBOL",
"name": "map_expression"
}
]
},
"identifier": {
"type": "PATTERN",
"value": "[_\\p{XID_Start}][_\\p{XID_Continue}]*"
},
"boolean_literal": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "true"
},
{
"type": "STRING",
"value": "false"
}
]
},
"integer_literal": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "-"
},
{
"type": "BLANK"
}
]
},
{
"type": "PATTERN",
"value": "[0-9]+"
}
]
},
"_string_literal": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "raw_string_literal"
},
{
"type": "SYMBOL",
"name": "interpreted_string_literal"
}
]
},
"raw_string_literal": {
"type": "TOKEN",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "`"
},
{
"type": "PATTERN",
"value": "[^`]+"
},
{
"type": "STRING",
"value": "`"
}
]
}
},
"interpreted_string_literal": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\""
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^\"\\n\\\\]+"
},
{
"type": "SYMBOL",
"name": "escape_sequence"
}
]
}
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "\""
}
}
]
},
"escape_sequence": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\\"
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[^xuU]"
},
{
"type": "PATTERN",
"value": "\\d{2,3}"
},
{
"type": "PATTERN",
"value": "x[0-9a-fA-F]{2,}"
},
{
"type": "PATTERN",
"value": "u[0-9a-fA-F]{4}"
},
{
"type": "PATTERN",
"value": "U[0-9a-fA-F]{8}"
}
]
}
]
}
},
"select_expression": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "select"
},
{
"type": "STRING",
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "select_value"
},
{
"type": "SYMBOL",
"name": "soong_config_variable"
}
]
},
{
"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": ")"
}
]
},
"soong_config_variable": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "type",
"content": {
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "soong_config_variable"
},
"named": true,
"value": "selection_type"
}
},
{
"type": "STRING",
"value": "("
},
{
"type": "FIELD",
"name": "condition",
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "namespace",
"content": {
"type": "SYMBOL",
"name": "_string_literal"
}
},
{
"type": "STRING",
"value": ","
},
{
"type": "FIELD",
"name": "variable",
"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": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "["
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "element",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "FIELD",
"name": "element",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "]"
}
]
},
"map_expression": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "FIELD",
"name": "property",
"content": {
"type": "SYMBOL",
"name": "_colon_property"
}
},
"named": true,
"value": "property"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "ALIAS",
"content": {
"type": "FIELD",
"name": "property",
"content": {
"type": "SYMBOL",
"name": "_colon_property"
}
},
"named": true,
"value": "property"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "}"
}
]
},
"_colon_property": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "field",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
}
]
},
"_equal_property": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "field",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "STRING",
"value": "="
},
{
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
}
]
}
},
"extras": [
{
"type": "PATTERN",
"value": "\\s"
"value": "\\s+"
},
{
"type": "SYMBOL",
"name": "comment"
}
],
"conflicts": [],

596
src/node-types.json generated
View file

@ -1,11 +1,603 @@
[
{
"type": "source_file",
"type": "assignment",
"named": true,
"fields": {
"left": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"operator": {
"multiple": false,
"required": true,
"types": [
{
"type": "+=",
"named": false
},
{
"type": "=",
"named": false
}
]
},
"right": {
"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": "boolean_literal",
"named": true,
"fields": {}
},
{
"type": "hello",
"type": "comment",
"named": true,
"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",
"named": true,
"fields": {}
},
{
"type": "interpreted_string_literal",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "escape_sequence",
"named": true
}
]
}
},
{
"type": "list_expression",
"named": true,
"fields": {
"element": {
"multiple": true,
"required": false,
"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": "map_expression",
"named": true,
"fields": {
"property": {
"multiple": true,
"required": false,
"types": [
{
"type": "property",
"named": true
}
]
}
}
},
{
"type": "module",
"named": true,
"fields": {
"property": {
"multiple": true,
"required": false,
"types": [
{
"type": "property",
"named": true
}
]
},
"type": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
{
"type": "property",
"named": true,
"fields": {
"field": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"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": "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": "soong_config_variable",
"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
}
]
}
}
},
{
"type": "soong_config_variable",
"named": true,
"fields": {
"condition": {
"multiple": false,
"required": true,
"types": [
{
"type": ",",
"named": false
}
]
},
"namespace": {
"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
}
]
},
"variable": {
"multiple": false,
"required": true,
"types": [
{
"type": "interpreted_string_literal",
"named": true
},
{
"type": "raw_string_literal",
"named": true
}
]
}
}
},
{
"type": "source_file",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "assignment",
"named": true
},
{
"type": "module",
"named": true
}
]
}
},
{
"type": "\"",
"named": false
},
{
"type": "#",
"named": false
},
{
"type": "(",
"named": false
},
{
"type": ")",
"named": false
},
{
"type": "+=",
"named": false
},
{
"type": ",",
"named": false
},
{
"type": "-",
"named": false
},
{
"type": ":",
"named": false
},
{
"type": "=",
"named": false
},
{
"type": "[",
"named": false
},
{
"type": "]",
"named": false
},
{
"type": "default",
"named": false
},
{
"type": "escape_sequence",
"named": true
},
{
"type": "false",
"named": false
},
{
"type": "identifier",
"named": true
},
{
"type": "raw_string_literal",
"named": true
},
{
"type": "select",
"named": false
},
{
"type": "selection_type",
"named": true
},
{
"type": "true",
"named": false
},
{
"type": "unset",
"named": true
},
{
"type": "{",
"named": false
},
{
"type": "}",
"named": false
}
]

7239
src/parser.c generated

File diff suppressed because it is too large Load diff

34
test/corpus/comments.txt Normal file
View file

@ -0,0 +1,34 @@
================================================================================
Empty comment
================================================================================
#
--------------------------------------------------------------------------------
(source_file
(comment))
================================================================================
Single comment
================================================================================
# This is a comment
--------------------------------------------------------------------------------
(source_file
(comment))
================================================================================
Multiple comments
================================================================================
# This is a comment
# This is a second comment
--------------------------------------------------------------------------------
(source_file
(comment)
(comment))

View file

@ -0,0 +1,96 @@
================================================================================
Empty file
================================================================================
--------------------------------------------------------------------------------
(source_file)
================================================================================
Integer assignment
================================================================================
foo = 42
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(integer_literal)))
================================================================================
Variable assignment
================================================================================
foo = bar
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(identifier)))
================================================================================
Addition assignement
================================================================================
foo += 12
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(integer_literal)))
================================================================================
Multiple assignments
================================================================================
foo = 12
bar = 27
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal)))
================================================================================
Various assignements
================================================================================
foo = 0
foo += 12
bar = 27
baz = 42
qux = 1
qux += 1
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal)))

346
test/corpus/expressions.txt Normal file
View file

@ -0,0 +1,346 @@
================================================================================
Boolean
================================================================================
foo = false
foo = true
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(boolean_literal))
(assignment
(identifier)
(boolean_literal)))
================================================================================
Boolean (identifier/literal)
================================================================================
true = false
false = true
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(boolean_literal))
(assignment
(identifier)
(boolean_literal)))
================================================================================
Integer
================================================================================
foo = 0
foo = 42
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal)))
================================================================================
Integer (negative)
================================================================================
foo = -0
foo = -42
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal)))
================================================================================
String
================================================================================
foo = "Hello World!"
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(interpreted_string_literal)))
================================================================================
String (escape)
================================================================================
foo = "Hello\nWorld!"
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(interpreted_string_literal
(escape_sequence))))
================================================================================
String (raw)
================================================================================
foo = `Hello\nWorld!`
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(raw_string_literal)))
================================================================================
List (empty)
================================================================================
foo = []
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(list_expression)))
================================================================================
List (singleton)
================================================================================
foo = [42]
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(list_expression
(integer_literal))))
================================================================================
List (singleton multiline)
================================================================================
foo = [
42
]
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(list_expression
(integer_literal))))
================================================================================
List (singleton trailing comma)
================================================================================
foo = [
42,
]
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(list_expression
(integer_literal))))
================================================================================
List (mixed values)
================================================================================
foo = [
42,
"foobar",
]
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(list_expression
(integer_literal)
(interpreted_string_literal))))
================================================================================
List (list of list)
================================================================================
foo = [
[42],
["foobar"],
[],
]
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(list_expression
(list_expression
(integer_literal))
(list_expression
(interpreted_string_literal))
(list_expression))))
================================================================================
List (rogue comma)
================================================================================
foo = [
,
]
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(list_expression
(ERROR))))
================================================================================
Map (empty)
================================================================================
foo = {}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression)))
================================================================================
Map (singleton)
================================================================================
foo = {foo:42}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(property
(identifier)
(integer_literal)))))
================================================================================
Map (singleton multiline)
================================================================================
foo = {
foo: 42
}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(property
(identifier)
(integer_literal)))))
================================================================================
Map (singleton trailing comma)
================================================================================
foo = {
foo: 42,
}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(property
(identifier)
(integer_literal)))))
================================================================================
Map (mixed values)
================================================================================
foo = {
answer: 42,
value: "foobar",
}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(property
(identifier)
(integer_literal))
(property
(identifier)
(interpreted_string_literal)))))
================================================================================
Map (map of map)
================================================================================
foo = {
the: {answer: 42},
}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(property
(identifier)
(map_expression
(property
(identifier)
(integer_literal)))))))
================================================================================
Map (rogue comma)
================================================================================
foo = {
,
}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(ERROR))))

123
test/corpus/modules.txt Normal file
View file

@ -0,0 +1,123 @@
================================================================================
Empty module
================================================================================
foo{}
--------------------------------------------------------------------------------
(source_file
(module
(identifier)))
================================================================================
Empty module (newlines)
================================================================================
foo {
}
--------------------------------------------------------------------------------
(source_file
(module
(identifier)))
================================================================================
Single property
================================================================================
foo {
bar: 42
}
--------------------------------------------------------------------------------
(source_file
(module
(identifier)
(property
(identifier)
(integer_literal))))
================================================================================
Single property (trailing comma)
================================================================================
foo {
bar: 42,
}
--------------------------------------------------------------------------------
(source_file
(module
(identifier)
(property
(identifier)
(integer_literal))))
================================================================================
Mixed values
================================================================================
foo {
active: true,
value: "foo",
answer: 42,
}
--------------------------------------------------------------------------------
(source_file
(module
(identifier)
(property
(identifier)
(boolean_literal))
(property
(identifier)
(interpreted_string_literal))
(property
(identifier)
(integer_literal))))
================================================================================
Complex value
================================================================================
foo {
some: [
{
complex: "value",
}
],
}
--------------------------------------------------------------------------------
(source_file
(module
(identifier)
(property
(identifier)
(list_expression
(map_expression
(property
(identifier)
(interpreted_string_literal)))))))
================================================================================
Rogue comma
================================================================================
foo {
,
}
--------------------------------------------------------------------------------
(source_file
(module
(identifier)
(ERROR)))

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

@ -0,0 +1,187 @@
================================================================================
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 (soong config variable)
================================================================================
foo = select(soong_config_variable("my_namespace", "my_var"), {
"foo": unset,
"default": "bar",
})
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(select_expression
(soong_config_variable
(selection_type)
(interpreted_string_literal)
(interpreted_string_literal))
(select_cases
(select_case
(interpreted_string_literal)
(unset))
(select_case
(interpreted_string_literal)
(interpreted_string_literal))))))
================================================================================
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))