Add record types

This commit is contained in:
Bruno BELANYI 2022-06-01 20:53:39 +02:00
parent 506fab8896
commit dd0af53fa0
5 changed files with 970 additions and 736 deletions

View file

@ -244,10 +244,23 @@ module.exports = grammar({
_type: ($) => choice(
$.type_alias,
$.record_type,
),
type_alias: ($) => $.identifier,
record_type: ($) => seq(
"{",
sepBy(",", $._typed_field),
"}",
),
_typed_field: ($) => seq(
field("name", $.identifier),
":",
field("type", $.identifier),
),
function_declaration: ($) => seq(
"function",
$._function_declaration_common,
@ -271,12 +284,6 @@ module.exports = grammar({
")",
),
_typed_field: ($) => seq(
field("name", $.identifier),
":",
field("type", $.identifier),
),
variable_declaration: ($) => seq(
"var",
field("name", $.identifier),

View file

@ -1114,6 +1114,10 @@
{
"type": "SYMBOL",
"name": "type_alias"
},
{
"type": "SYMBOL",
"name": "record_type"
}
]
},
@ -1121,6 +1125,77 @@
"type": "SYMBOL",
"name": "identifier"
},
"record_type": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"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"
}
}
]
},
"function_declaration": {
"type": "SEQ",
"members": [
@ -1230,31 +1305,6 @@
}
]
},
"_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": {
"type": "SEQ",
"members": [

View file

@ -1543,6 +1543,32 @@
}
}
},
{
"type": "record_type",
"named": true,
"fields": {
"name": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"type": {
"multiple": true,
"required": false,
"types": [
{
"type": "identifier",
"named": true
}
]
}
}
},
{
"type": "record_value",
"named": true,
@ -1811,6 +1837,10 @@
"multiple": false,
"required": true,
"types": [
{
"type": "record_type",
"named": true
},
{
"type": "type_alias",
"named": true

File diff suppressed because it is too large Load diff

View file

@ -20,6 +20,67 @@ type alias = int
value: (type_alias
(identifier))))
================================================================================
Record type declaration
================================================================================
type record = { a: int, b: str }
--------------------------------------------------------------------------------
(source_file
(type_declaration
name: (identifier)
value: (record_type
name: (identifier)
type: (identifier)
name: (identifier)
type: (identifier))))
================================================================================
Record type declaration single field
================================================================================
type record = { a: int }
--------------------------------------------------------------------------------
(source_file
(type_declaration
name: (identifier)
value: (record_type
name: (identifier)
type: (identifier))))
================================================================================
Record type declaration no fields
================================================================================
type record = { }
--------------------------------------------------------------------------------
(source_file
(type_declaration
name: (identifier)
value: (record_type)))
================================================================================
Record type declaration trailing comma
================================================================================
type record = { a: int, }
--------------------------------------------------------------------------------
(source_file
(type_declaration
name: (identifier)
value: (record_type
name: (identifier)
type: (identifier)
(ERROR))))
================================================================================
Function declaration
================================================================================