Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for union types #68

Merged
merged 2 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ module.exports = grammar({
[$._primary_expression, $._array_destructing],
[$._array_destructing, $.array_creation_expression],

[$.union_type, $._return_type],

[$.namespace_name],

[$.namespace_name_as_prefix],
Expand Down Expand Up @@ -395,7 +397,9 @@ module.exports = grammar({
field('name', $.variable_name)
),

_type: $ => choice(
_type: $ => alias($.union_type, $.type_list),

_types: $ => choice(
$.optional_type,
$._type_name,
$.primitive_type
Expand All @@ -411,15 +415,19 @@ module.exports = grammar({
)
),

union_type: $ => prec.right(pipeSep1($._types)),

primitive_type: $ => choice(
'array',
'callable',
'callable', // not legal in property types
'iterable',
'bool',
'float',
'int',
'string',
'void'
'void',
'false', // only legal in unions
'null', // only legal in unions
),

cast_type: $ => choice(
Expand Down
50 changes: 50 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,15 @@
]
},
"_type": {
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "union_type"
},
"named": true,
"value": "type_list"
},
"_types": {
"type": "CHOICE",
"members": [
{
Expand Down Expand Up @@ -1854,6 +1863,35 @@
}
]
},
"union_type": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_types"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "|"
},
{
"type": "SYMBOL",
"name": "_types"
}
]
}
}
]
}
},
"primitive_type": {
"type": "CHOICE",
"members": [
Expand Down Expand Up @@ -1888,6 +1926,14 @@
{
"type": "STRING",
"value": "void"
},
{
"type": "STRING",
"value": "false"
},
{
"type": "STRING",
"value": "null"
}
]
},
Expand Down Expand Up @@ -6794,6 +6840,10 @@
"_array_destructing",
"array_creation_expression"
],
[
"union_type",
"_return_type"
],
[
"namespace_name"
],
Expand Down
42 changes: 25 additions & 17 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,7 @@
"named": true,
"subtypes": [
{
"type": "optional_type",
"named": true
},
{
"type": "primitive_type",
"named": true
},
{
"type": "type_name",
"type": "type_list",
"named": true
}
]
Expand Down Expand Up @@ -3066,6 +3058,14 @@
"multiple": true,
"required": true,
"types": [
{
"type": "optional_type",
"named": true
},
{
"type": "primitive_type",
"named": true
},
{
"type": "type_name",
"named": true
Expand Down Expand Up @@ -3637,11 +3637,11 @@
},
{
"type": "boolean",
"named": true
"named": false
},
{
"type": "boolean",
"named": false
"named": true
},
{
"type": "break",
Expand Down Expand Up @@ -3739,6 +3739,10 @@
"type": "extends",
"named": false
},
{
"type": "false",
"named": false
},
{
"type": "final",
"named": false
Expand All @@ -3749,11 +3753,11 @@
},
{
"type": "float",
"named": true
"named": false
},
{
"type": "float",
"named": false
"named": true
},
{
"type": "for",
Expand Down Expand Up @@ -3813,11 +3817,11 @@
},
{
"type": "integer",
"named": true
"named": false
},
{
"type": "integer",
"named": false
"named": true
},
{
"type": "interface",
Expand All @@ -3839,6 +3843,10 @@
"type": "new",
"named": false
},
{
"type": "null",
"named": false
},
{
"type": "null",
"named": true
Expand Down Expand Up @@ -3909,11 +3917,11 @@
},
{
"type": "string",
"named": true
"named": false
},
{
"type": "string",
"named": false
"named": true
},
{
"type": "switch",
Expand Down
Loading