Add map expressions

This commit is contained in:
Bruno BELANYI 2024-04-07 23:36:21 +01:00
parent e13f15e8db
commit db2e88a539
5 changed files with 912 additions and 268 deletions

View file

@ -38,6 +38,7 @@ module.exports = grammar({
$._string_literal,
// Composites
$.list_expression,
$.map_expression,
),
// The Blueprint scanner makes use of Go's lexer, so copy their rule
@ -84,6 +85,22 @@ module.exports = grammar({
"]",
),
map_expression: ($) => seq(
"{",
optional(commaSeparated($._colon_property)),
"}",
),
// }}}
// Properties {{{
_colon_property: ($) => seq(
field("field", $.identifier),
":",
field("value", $._expr),
),
// }}}
}
});

87
src/grammar.json generated
View file

@ -86,6 +86,10 @@
{
"type": "SYMBOL",
"name": "list_expression"
},
{
"type": "SYMBOL",
"name": "map_expression"
}
]
},
@ -273,6 +277,89 @@
"value": "]"
}
]
},
"map_expression": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_colon_property"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_colon_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"
}
}
]
}
},
"extras": [

66
src/node-types.json generated
View file

@ -47,6 +47,10 @@
"type": "list_expression",
"named": true
},
{
"type": "map_expression",
"named": true
},
{
"type": "raw_string_literal",
"named": true
@ -104,6 +108,10 @@
"type": "list_expression",
"named": true
},
{
"type": "map_expression",
"named": true
},
{
"type": "raw_string_literal",
"named": true
@ -111,6 +119,52 @@
]
}
},
{
"type": "map_expression",
"named": true,
"fields": {
"field": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"value": {
"multiple": true,
"required": false,
"types": [
{
"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": "source_file",
"named": true,
@ -146,6 +200,10 @@
"type": "-",
"named": false
},
{
"type": ":",
"named": false
},
{
"type": "=",
"named": false
@ -169,5 +227,13 @@
{
"type": "raw_string_literal",
"named": true
},
{
"type": "{",
"named": false
},
{
"type": "}",
"named": false
}
]

893
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -186,3 +186,120 @@ foo = [
(identifier)
(list_expression
(ERROR))))
================================================================================
Map (empty)
================================================================================
foo = {}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression)))
================================================================================
Map (singleton)
================================================================================
foo = {foo:42}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(identifier)
(integer_literal))))
================================================================================
Map (singleton multiline)
================================================================================
foo = {
foo: 42
}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(identifier)
(integer_literal))))
================================================================================
Map (singleton trailing comma)
================================================================================
foo = {
foo: 42,
}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(identifier)
(integer_literal))))
================================================================================
Map (mixed values)
================================================================================
foo = {
answer: 42,
value: "foobar",
}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(identifier)
(integer_literal)
(identifier)
(interpreted_string_literal))))
================================================================================
Map (map of map)
================================================================================
foo = {
the: {answer: 42},
}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(identifier)
(map_expression
(identifier)
(integer_literal)))))
================================================================================
Map (rogue comma)
================================================================================
foo = {
,
}
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(map_expression
(ERROR))))