Add modules

This commit is contained in:
Bruno BELANYI 2024-04-08 01:36:27 +01:00
parent a78ad8139f
commit 8bd249fc90
5 changed files with 2209 additions and 1317 deletions

View file

@ -19,6 +19,7 @@ module.exports = grammar({
_definition: ($) => choice(
$.assignment,
$.module,
),
comment: (_) => seq("#", /.*/),
@ -31,6 +32,26 @@ module.exports = grammar({
field("right", $._expr),
),
module: ($) => choice(
$._old_module,
$._new_module,
),
// This syntax is deprecated, but still accepted
_old_module: ($) => seq(
field("type", $.identifier),
"{",
optional(commaSeparated($._colon_property)),
"}",
),
_new_module: ($) => seq(
$.identifier,
"(",
optional(commaSeparated($._equal_property)),
")",
),
// }}}
// Expressions {{{
@ -168,6 +189,12 @@ module.exports = grammar({
field("value", $._expr),
),
_equal_property: ($) => seq(
field("field", $.identifier),
"=",
field("value", $._expr),
),
// }}}
}
});

170
src/grammar.json generated
View file

@ -14,6 +14,10 @@
{
"type": "SYMBOL",
"name": "assignment"
},
{
"type": "SYMBOL",
"name": "module"
}
]
},
@ -68,6 +72,147 @@
}
]
},
"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": "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": "}"
}
]
},
"_new_module": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_equal_property"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_equal_property"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
"_expr": {
"type": "CHOICE",
"members": [
@ -647,6 +792,31 @@
}
}
]
},
"_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": [

78
src/node-types.json generated
View file

@ -252,6 +252,80 @@
}
}
},
{
"type": "module",
"named": true,
"fields": {
"field": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"type": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"value": {
"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
}
]
}
},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
},
{
"type": "select_case",
"named": true,
@ -451,6 +525,10 @@
{
"type": "assignment",
"named": true
},
{
"type": "module",
"named": true
}
]
}

3135
src/parser.c generated

File diff suppressed because it is too large Load diff

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

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