Skip to content

Commit

Permalink
Adds support for intersection types
Browse files Browse the repository at this point in the history
  • Loading branch information
cfroystad committed Jul 16, 2022
1 parent ece74b2 commit 49a69af
Show file tree
Hide file tree
Showing 5 changed files with 60,312 additions and 59,843 deletions.
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

0 comments on commit 49a69af

Please sign in to comment.