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 intersection types #145

Merged
merged 1 commit into from
Jul 16, 2022
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
12 changes: 8 additions & 4 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ module.exports = grammar({
[$._primary_expression, $._array_destructing_element],

[$.union_type, $._return_type],
[$.union_type, $.intersection_type],
[$.intersection_type],
[$.if_statement],

[$.namespace_name],
Expand Down Expand Up @@ -149,7 +151,7 @@ module.exports = grammar({

empty_statement: $ => prec(-1, ';'),

reference_modifier: $ => '&',
reference_modifier: $ => token('&'),

function_static_declaration: $ => seq(
keyword('static'),
Expand Down Expand Up @@ -481,7 +483,7 @@ module.exports = grammar({
field('name', $.variable_name)
),

_type: $ => $.union_type,
_type: $ => choice($.union_type, $.intersection_type),

_types: $ => choice(
$.optional_type,
Expand All @@ -503,6 +505,8 @@ module.exports = grammar({

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

intersection_type: $ => ampSep1($._types),

primitive_type: $ => choice(
'array',
keyword('callable'), // not legal in property types
Expand Down Expand Up @@ -1498,8 +1502,8 @@ function pipeSep1(rule) {
return seq(rule, repeat(seq('|', rule)));
}

function pipeSep(rule) {
return optional(commaSep1(rule));
function ampSep1(rule) {
return seq(rule, repeat(seq(token('&'), rule)));
}

function backtick_chars() {
Expand Down
55 changes: 51 additions & 4 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,11 @@
}
},
"reference_modifier": {
"type": "STRING",
"value": "&"
"type": "TOKEN",
"content": {
"type": "STRING",
"value": "&"
}
},
"function_static_declaration": {
"type": "SEQ",
Expand Down Expand Up @@ -2495,8 +2498,17 @@
]
},
"_type": {
"type": "SYMBOL",
"name": "union_type"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "union_type"
},
{
"type": "SYMBOL",
"name": "intersection_type"
}
]
},
"_types": {
"type": "CHOICE",
Expand Down Expand Up @@ -2583,6 +2595,34 @@
]
}
},
"intersection_type": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_types"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "TOKEN",
"content": {
"type": "STRING",
"value": "&"
}
},
{
"type": "SYMBOL",
"name": "_types"
}
]
}
}
]
},
"primitive_type": {
"type": "CHOICE",
"members": [
Expand Down Expand Up @@ -8807,6 +8847,13 @@
"union_type",
"_return_type"
],
[
"union_type",
"intersection_type"
],
[
"intersection_type"
],
[
"if_statement"
],
Expand Down
31 changes: 29 additions & 2 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@
"type": "_type",
"named": true,
"subtypes": [
{
"type": "intersection_type",
"named": true
},
{
"type": "union_type",
"named": true
Expand Down Expand Up @@ -2486,6 +2490,29 @@
]
}
},
{
"type": "intersection_type",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "named_type",
"named": true
},
{
"type": "optional_type",
"named": true
},
{
"type": "primitive_type",
"named": true
}
]
}
},
{
"type": "list_literal",
"named": true,
Expand Down Expand Up @@ -5407,11 +5434,11 @@
},
{
"type": "null",
"named": true
"named": false
},
{
"type": "null",
"named": false
"named": true
},
{
"type": "or",
Expand Down
Loading