Add assignment

This commit is contained in:
Bruno BELANYI 2024-04-07 21:37:52 +01:00
parent fc9f1467df
commit 38bf9532ad
5 changed files with 2184 additions and 47 deletions

View file

@ -1,9 +1,41 @@
module.exports = grammar({
name: "blueprint",
extras: ($) => [
/\s+/,
],
rules: {
// TODO: add the actual grammar rules
source_file: $ => 'hello',
source_file: ($) => repeat($._definition),
_definition: ($) => choice(
$.assignment,
),
// Definitions {{{
assignment: ($) => seq(
field("left", $.identifier),
field("operator", "="),
field("right", $._expr),
),
// }}}
// Expressions {{{
_expr: ($) => choice(
// Literals
$.identifier,
$.integer_literal,
),
// The Blueprint scanner makes use of Go's lexer, so copy their rule
identifier: (_) => /[_\p{XID_Start}][_\p{XID_Continue}]*/,
integer_literal: (_) => /[0-9]+/,
// }}}
}
});

68
src/grammar.json generated
View file

@ -2,14 +2,76 @@
"name": "blueprint",
"rules": {
"source_file": {
"type": "STRING",
"value": "hello"
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_definition"
}
},
"_definition": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "assignment"
}
]
},
"assignment": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "="
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
}
]
},
"_expr": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "integer_literal"
}
]
},
"identifier": {
"type": "PATTERN",
"value": "[_\\p{XID_Start}][_\\p{XID_Continue}]*"
},
"integer_literal": {
"type": "PATTERN",
"value": "[0-9]+"
}
},
"extras": [
{
"type": "PATTERN",
"value": "\\s"
"value": "\\s+"
}
],
"conflicts": [],

62
src/node-types.json generated
View file

@ -1,11 +1,69 @@
[
{
"type": "assignment",
"named": true,
"fields": {
"left": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"operator": {
"multiple": false,
"required": true,
"types": [
{
"type": "=",
"named": false
}
]
},
"right": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "integer_literal",
"named": true
}
]
}
}
},
{
"type": "source_file",
"named": true,
"fields": {}
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "assignment",
"named": true
}
]
}
},
{
"type": "hello",
"type": "=",
"named": false
},
{
"type": "identifier",
"named": true
},
{
"type": "integer_literal",
"named": true
}
]

2015
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,50 @@
================================================================================
Empty file
================================================================================
--------------------------------------------------------------------------------
(source_file)
================================================================================
Integer assignment
================================================================================
foo = 42
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(integer_literal)))
================================================================================
Variable assignment
================================================================================
foo = bar
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(identifier)))
================================================================================
Multiple assignments
================================================================================
foo = 12
bar = 27
--------------------------------------------------------------------------------
(source_file
(assignment
(identifier)
(integer_literal))
(assignment
(identifier)
(integer_literal)))