Add variable declarations
This commit is contained in:
parent
3cf701dbb1
commit
3eb6b0a0d0
|
@ -227,9 +227,18 @@ module.exports = grammar({
|
|||
_declaration_chunks: ($) => repeat1($._declaration_chunk),
|
||||
|
||||
_declaration_chunk: ($) => choice(
|
||||
$.variable_declaration,
|
||||
$.import_declaration,
|
||||
),
|
||||
|
||||
variable_declaration: ($) => seq(
|
||||
"var",
|
||||
field("name", $.identifier),
|
||||
optional(seq(":", field("type", $.identifier))),
|
||||
":=",
|
||||
field("value", $._expr),
|
||||
),
|
||||
|
||||
import_declaration: ($) => seq(
|
||||
"import",
|
||||
field("file", $.string_literal),
|
||||
|
|
|
@ -1042,12 +1042,70 @@
|
|||
"_declaration_chunk": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "variable_declaration"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"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": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
|
|
|
@ -1244,6 +1244,10 @@
|
|||
{
|
||||
"type": "import_declaration",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "variable_declaration",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -1550,6 +1554,10 @@
|
|||
"type": "unary_expression",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "variable_declaration",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "while_expression",
|
||||
"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",
|
||||
"named": true,
|
||||
|
@ -1848,6 +1960,10 @@
|
|||
"type": ".",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": ":",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": ":=",
|
||||
"named": false
|
||||
|
@ -1936,6 +2052,10 @@
|
|||
"type": "to",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "var",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "while",
|
||||
"named": false
|
||||
|
|
3770
src/parser.c
3770
src/parser.c
File diff suppressed because it is too large
Load diff
|
@ -6,6 +6,50 @@ Empty declarations
|
|||
|
||||
(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
|
||||
================================================================================
|
||||
|
|
Loading…
Reference in a new issue