Add function declarations

This commit is contained in:
Bruno BELANYI 2022-06-01 20:48:32 +02:00
parent 3eb6b0a0d0
commit e898312c95
5 changed files with 3466 additions and 2499 deletions

View file

@ -226,10 +226,42 @@ module.exports = grammar({
_declaration_chunks: ($) => repeat1($._declaration_chunk), _declaration_chunks: ($) => repeat1($._declaration_chunk),
_declaration_chunk: ($) => choice( _declaration_chunk: ($) => prec.left(
choice(
repeat1(choice($.function_declaration, $.primitive_declaration)),
$.variable_declaration, $.variable_declaration,
$.import_declaration, $.import_declaration,
), ),
),
function_declaration: ($) => seq(
"function",
$._function_declaration_common,
"=",
field("body", $._expr),
),
primitive_declaration: ($) => seq(
"primitive",
$._function_declaration_common,
),
_function_declaration_common: ($) => seq(
field("name", $.identifier),
field("parameters", $.parameters),
),
parameters: ($) => seq(
"(",
field("parameters", sepBy(",", $._typed_field)),
")",
),
_typed_field: ($) => seq(
field("name", $.identifier),
":",
field("type", $.identifier),
),
variable_declaration: ($) => seq( variable_declaration: ($) => seq(
"var", "var",

View file

@ -1040,8 +1040,27 @@
} }
}, },
"_declaration_chunk": { "_declaration_chunk": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "function_declaration"
},
{
"type": "SYMBOL",
"name": "primitive_declaration"
}
]
}
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "variable_declaration" "name": "variable_declaration"
@ -1051,6 +1070,141 @@
"name": "import_declaration" "name": "import_declaration"
} }
] ]
}
},
"function_declaration": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "function"
},
{
"type": "SYMBOL",
"name": "_function_declaration_common"
},
{
"type": "STRING",
"value": "="
},
{
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "_expr"
}
}
]
},
"primitive_declaration": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "primitive"
},
{
"type": "SYMBOL",
"name": "_function_declaration_common"
}
]
},
"_function_declaration_common": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "FIELD",
"name": "parameters",
"content": {
"type": "SYMBOL",
"name": "parameters"
}
}
]
},
"parameters": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "FIELD",
"name": "parameters",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_typed_field"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_typed_field"
}
]
}
}
]
},
{
"type": "BLANK"
}
]
}
},
{
"type": "STRING",
"value": ")"
}
]
},
"_typed_field": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "type",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
}
]
}, },
"variable_declaration": { "variable_declaration": {
"type": "SEQ", "type": "SEQ",

View file

@ -895,6 +895,110 @@
} }
} }
}, },
{
"type": "function_declaration",
"named": true,
"fields": {
"body": {
"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
}
]
},
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"parameters": {
"multiple": false,
"required": true,
"types": [
{
"type": "parameters",
"named": true
}
]
}
}
},
{ {
"type": "if_expression", "type": "if_expression",
"named": true, "named": true,
@ -1241,10 +1345,18 @@
"multiple": true, "multiple": true,
"required": false, "required": false,
"types": [ "types": [
{
"type": "function_declaration",
"named": true
},
{ {
"type": "import_declaration", "type": "import_declaration",
"named": true "named": true
}, },
{
"type": "primitive_declaration",
"named": true
},
{ {
"type": "variable_declaration", "type": "variable_declaration",
"named": true "named": true
@ -1253,6 +1365,76 @@
} }
} }
}, },
{
"type": "parameters",
"named": true,
"fields": {
"name": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"parameters": {
"multiple": true,
"required": false,
"types": [
{
"type": ",",
"named": false
},
{
"type": ":",
"named": false
},
{
"type": "identifier",
"named": true
}
]
},
"type": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
}
},
{
"type": "primitive_declaration",
"named": true,
"fields": {
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"parameters": {
"multiple": false,
"required": true,
"types": [
{
"type": "parameters",
"named": true
}
]
}
}
},
{ {
"type": "record_expression", "type": "record_expression",
"named": true, "named": true,
@ -1510,6 +1692,10 @@
"type": "function_call", "type": "function_call",
"named": true "named": true
}, },
{
"type": "function_declaration",
"named": true
},
{ {
"type": "identifier", "type": "identifier",
"named": true "named": true
@ -1534,6 +1720,10 @@
"type": "nil_literal", "type": "nil_literal",
"named": true "named": true
}, },
{
"type": "primitive_declaration",
"named": true
},
{ {
"type": "record_expression", "type": "record_expression",
"named": true "named": true
@ -2008,6 +2198,10 @@
"type": "for", "type": "for",
"named": false "named": false
}, },
{
"type": "function",
"named": false
},
{ {
"type": "identifier", "type": "identifier",
"named": true "named": true
@ -2044,6 +2238,10 @@
"type": "operator", "type": "operator",
"named": true "named": true
}, },
{
"type": "primitive",
"named": false
},
{ {
"type": "then", "type": "then",
"named": false "named": false

File diff suppressed because it is too large Load diff

View file

@ -6,6 +6,67 @@ Empty declarations
(source_file) (source_file)
================================================================================
Function declaration
================================================================================
function func(a: int, b: int) = "string"
--------------------------------------------------------------------------------
(source_file
(function_declaration
name: (identifier)
parameters: (parameters
name: (identifier)
type: (identifier)
name: (identifier)
type: (identifier))
body: (string_literal)))
================================================================================
Function declaration single parameter
================================================================================
function func(a: int) = "string"
--------------------------------------------------------------------------------
(source_file
(function_declaration
name: (identifier)
parameters: (parameters
name: (identifier)
type: (identifier))
body: (string_literal)))
================================================================================
Function declaration no parameters
================================================================================
function func() = "string"
--------------------------------------------------------------------------------
(source_file
(function_declaration
name: (identifier)
parameters: (parameters)
body: (string_literal)))
================================================================================
Primitive declaration
================================================================================
primitive prim()
--------------------------------------------------------------------------------
(source_file
(primitive_declaration
name: (identifier)
parameters: (parameters)))
================================================================================ ================================================================================
Variable declaration Variable declaration
================================================================================ ================================================================================