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: ($) => choice(
$.type_alias, $.type_alias,
$.record_type,
), ),
type_alias: ($) => $.identifier, type_alias: ($) => $.identifier,
record_type: ($) => seq(
"{",
sepBy(",", $._typed_field),
"}",
),
_typed_field: ($) => seq(
field("name", $.identifier),
":",
field("type", $.identifier),
),
function_declaration: ($) => seq( function_declaration: ($) => seq(
"function", "function",
$._function_declaration_common, $._function_declaration_common,
@ -271,12 +284,6 @@ module.exports = grammar({
")", ")",
), ),
_typed_field: ($) => seq(
field("name", $.identifier),
":",
field("type", $.identifier),
),
variable_declaration: ($) => seq( variable_declaration: ($) => seq(
"var", "var",
field("name", $.identifier), field("name", $.identifier),

View file

@ -1114,6 +1114,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "type_alias" "name": "type_alias"
},
{
"type": "SYMBOL",
"name": "record_type"
} }
] ]
}, },
@ -1121,6 +1125,77 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "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": { "function_declaration": {
"type": "SEQ", "type": "SEQ",
"members": [ "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": { "variable_declaration": {
"type": "SEQ", "type": "SEQ",
"members": [ "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", "type": "record_value",
"named": true, "named": true,
@ -1811,6 +1837,10 @@
"multiple": false, "multiple": false,
"required": true, "required": true,
"types": [ "types": [
{
"type": "record_type",
"named": true
},
{ {
"type": "type_alias", "type": "type_alias",
"named": true "named": true

File diff suppressed because it is too large Load diff

View file

@ -20,6 +20,67 @@ type alias = int
value: (type_alias value: (type_alias
(identifier)))) (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 Function declaration
================================================================================ ================================================================================