Add record types
This commit is contained in:
parent
506fab8896
commit
dd0af53fa0
19
grammar.js
19
grammar.js
|
@ -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),
|
||||
|
|
100
src/grammar.json
100
src/grammar.json
|
@ -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": [
|
||||
|
|
|
@ -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
|
||||
|
|
1496
src/parser.c
1496
src/parser.c
File diff suppressed because it is too large
Load diff
|
@ -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
|
||||
================================================================================
|
||||
|
|
Loading…
Reference in a new issue