Skip to content

Commit 72a9af0

Browse files
authored
Small improvements for records and instanceof (#117)
* Support for record declarations as a top level declaration * Add support for generics in records * Add support for pattern matching for instanceof * Fix parse errors when 'record' is used as an identifier
1 parent ac14b4b commit 72a9af0

File tree

8 files changed

+34369
-33300
lines changed

8 files changed

+34369
-33300
lines changed

grammar.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ module.exports = grammar({
227227
instanceof_expression: $ => prec(PREC.REL, seq(
228228
field('left', $.expression),
229229
'instanceof',
230-
field('right', $._type)
230+
field('right', $._type),
231+
field('name', optional(choice($.identifier, $._reserved_identifier)))
231232
)),
232233

233234
lambda_expression: $ => seq(
@@ -635,6 +636,7 @@ module.exports = grammar({
635636
$.package_declaration,
636637
$.import_declaration,
637638
$.class_declaration,
639+
$.record_declaration,
638640
$.interface_declaration,
639641
$.annotation_type_declaration,
640642
$.enum_declaration,
@@ -902,6 +904,7 @@ module.exports = grammar({
902904
optional($.modifiers),
903905
'record',
904906
field('name', $.identifier),
907+
optional(field('type_parameters', $.type_parameters)),
905908
field('parameters', $.formal_parameters),
906909
field('body', $.class_body)
907910
),
@@ -1131,7 +1134,8 @@ module.exports = grammar({
11311134

11321135
_reserved_identifier: $ => alias(choice(
11331136
'open',
1134-
'module'
1137+
'module',
1138+
'record'
11351139
), $.identifier),
11361140

11371141
this: $ => 'this',
-1.66 KB
Binary file not shown.

src/grammar.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,31 @@
20172017
"type": "SYMBOL",
20182018
"name": "_type"
20192019
}
2020+
},
2021+
{
2022+
"type": "FIELD",
2023+
"name": "name",
2024+
"content": {
2025+
"type": "CHOICE",
2026+
"members": [
2027+
{
2028+
"type": "CHOICE",
2029+
"members": [
2030+
{
2031+
"type": "SYMBOL",
2032+
"name": "identifier"
2033+
},
2034+
{
2035+
"type": "SYMBOL",
2036+
"name": "_reserved_identifier"
2037+
}
2038+
]
2039+
},
2040+
{
2041+
"type": "BLANK"
2042+
}
2043+
]
2044+
}
20202045
}
20212046
]
20222047
}
@@ -4366,6 +4391,10 @@
43664391
"type": "SYMBOL",
43674392
"name": "class_declaration"
43684393
},
4394+
{
4395+
"type": "SYMBOL",
4396+
"name": "record_declaration"
4397+
},
43694398
{
43704399
"type": "SYMBOL",
43714400
"name": "interface_declaration"
@@ -5724,6 +5753,22 @@
57245753
"name": "identifier"
57255754
}
57265755
},
5756+
{
5757+
"type": "CHOICE",
5758+
"members": [
5759+
{
5760+
"type": "FIELD",
5761+
"name": "type_parameters",
5762+
"content": {
5763+
"type": "SYMBOL",
5764+
"name": "type_parameters"
5765+
}
5766+
},
5767+
{
5768+
"type": "BLANK"
5769+
}
5770+
]
5771+
},
57275772
{
57285773
"type": "FIELD",
57295774
"name": "parameters",
@@ -6882,6 +6927,10 @@
68826927
{
68836928
"type": "STRING",
68846929
"value": "module"
6930+
},
6931+
{
6932+
"type": "STRING",
6933+
"value": "record"
68856934
}
68866935
]
68876936
},

src/node-types.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@
160160
{
161161
"type": "package_declaration",
162162
"named": true
163+
},
164+
{
165+
"type": "record_declaration",
166+
"named": true
163167
}
164168
]
165169
},
@@ -2150,6 +2154,16 @@
21502154
}
21512155
]
21522156
},
2157+
"name": {
2158+
"multiple": false,
2159+
"required": false,
2160+
"types": [
2161+
{
2162+
"type": "identifier",
2163+
"named": true
2164+
}
2165+
]
2166+
},
21532167
"right": {
21542168
"multiple": false,
21552169
"required": true,
@@ -2896,6 +2910,16 @@
28962910
"named": true
28972911
}
28982912
]
2913+
},
2914+
"type_parameters": {
2915+
"multiple": false,
2916+
"required": false,
2917+
"types": [
2918+
{
2919+
"type": "type_parameters",
2920+
"named": true
2921+
}
2922+
]
28992923
}
29002924
},
29012925
"children": {

0 commit comments

Comments
 (0)