Add variable declarations

This commit is contained in:
Bruno BELANYI 2022-06-01 20:41:25 +02:00
parent 3cf701dbb1
commit 3eb6b0a0d0
5 changed files with 2280 additions and 1721 deletions

View file

@ -227,9 +227,18 @@ module.exports = grammar({
_declaration_chunks: ($) => repeat1($._declaration_chunk), _declaration_chunks: ($) => repeat1($._declaration_chunk),
_declaration_chunk: ($) => choice( _declaration_chunk: ($) => choice(
$.variable_declaration,
$.import_declaration, $.import_declaration,
), ),
variable_declaration: ($) => seq(
"var",
field("name", $.identifier),
optional(seq(":", field("type", $.identifier))),
":=",
field("value", $._expr),
),
import_declaration: ($) => seq( import_declaration: ($) => seq(
"import", "import",
field("file", $.string_literal), field("file", $.string_literal),

View file

@ -1042,12 +1042,70 @@
"_declaration_chunk": { "_declaration_chunk": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "variable_declaration"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "import_declaration" "name": "import_declaration"
} }
] ]
}, },
"variable_declaration": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "var"
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "type",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ":="
},
{
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
}
]
},
"import_declaration": { "import_declaration": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [

View file

@ -1244,6 +1244,10 @@
{ {
"type": "import_declaration", "type": "import_declaration",
"named": true "named": true
},
{
"type": "variable_declaration",
"named": true
} }
] ]
} }
@ -1550,6 +1554,10 @@
"type": "unary_expression", "type": "unary_expression",
"named": true "named": true
}, },
{
"type": "variable_declaration",
"named": true
},
{ {
"type": "while_expression", "type": "while_expression",
"named": true "named": true
@ -1666,6 +1674,110 @@
} }
} }
}, },
{
"type": "variable_declaration",
"named": true,
"fields": {
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"type": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"value": {
"multiple": false,
"required": true,
"types": [
{
"type": "array_expression",
"named": true
},
{
"type": "array_value",
"named": true
},
{
"type": "assignment_expression",
"named": true
},
{
"type": "binary_expression",
"named": true
},
{
"type": "break_expression",
"named": true
},
{
"type": "for_expression",
"named": true
},
{
"type": "function_call",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "if_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "let_expression",
"named": true
},
{
"type": "nil_literal",
"named": true
},
{
"type": "record_expression",
"named": true
},
{
"type": "record_value",
"named": true
},
{
"type": "sequence_expression",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_expression",
"named": true
},
{
"type": "while_expression",
"named": true
}
]
}
}
},
{ {
"type": "while_expression", "type": "while_expression",
"named": true, "named": true,
@ -1848,6 +1960,10 @@
"type": ".", "type": ".",
"named": false "named": false
}, },
{
"type": ":",
"named": false
},
{ {
"type": ":=", "type": ":=",
"named": false "named": false
@ -1936,6 +2052,10 @@
"type": "to", "type": "to",
"named": false "named": false
}, },
{
"type": "var",
"named": false
},
{ {
"type": "while", "type": "while",
"named": false "named": false

File diff suppressed because it is too large Load diff

View file

@ -6,6 +6,50 @@ Empty declarations
(source_file) (source_file)
================================================================================
Variable declaration
================================================================================
var a := 12
--------------------------------------------------------------------------------
(source_file
(variable_declaration
name: (identifier)
value: (integer_literal)))
================================================================================
Variable declaration with type
================================================================================
var a : int := 27
--------------------------------------------------------------------------------
(source_file
(variable_declaration
name: (identifier)
type: (identifier)
value: (integer_literal)))
================================================================================
Multiple variable declarations
================================================================================
var a := 12
var b := 27
--------------------------------------------------------------------------------
(source_file
(variable_declaration
name: (identifier)
value: (integer_literal))
(variable_declaration
name: (identifier)
value: (integer_literal)))
================================================================================ ================================================================================
Import Import
================================================================================ ================================================================================