Skip to content

Commit f8eff0a

Browse files
committed
add type_binding
1 parent ed68dae commit f8eff0a

File tree

4 files changed

+318
-266
lines changed

4 files changed

+318
-266
lines changed

grammar.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ module.exports = grammar({
8383
[$.array, $.array_pattern],
8484
[$.record_field, $.record_pattern],
8585
[$.expression_statement, $.ternary_expression],
86-
[$._type_declaration],
86+
[$.type_declaration],
87+
[$.type_binding],
8788
[$.let_declaration],
8889
[$.let_declaration, $.ternary_expression],
8990
[$.variant_identifier, $.module_identifier],
@@ -110,6 +111,7 @@ module.exports = grammar({
110111
[$.polyvar_type],
111112
[$.let_binding, $.or_pattern],
112113
[$.exception_pattern, $.or_pattern],
114+
[$.type_binding, $._inline_type]
113115
],
114116

115117
rules: {
@@ -262,31 +264,24 @@ module.exports = grammar({
262264
optional('export'),
263265
'type',
264266
optional('rec'),
265-
$._type_declaration
267+
sep1(
268+
seq(repeat($._newline), repeat($.decorator), 'and'),
269+
$.type_binding
270+
)
266271
),
267272

268-
_type_declaration: $ => seq(
269-
choice($.type_identifier, $.type_identifier_path),
273+
type_binding: $ => seq(
274+
field('name', choice($.type_identifier, $.type_identifier_path)),
270275
optional($.type_parameters),
271276
optional(seq(
272-
optional(seq('=', $._type)),
277+
optional(seq('=', $._non_function_inline_type)),
273278
optional(seq(
274279
choice('=', '+='),
275280
optional('private'),
276-
$._type,
281+
field('body', $._type),
277282
)),
278283
repeat($.type_constraint),
279284
)),
280-
optional(alias($._type_declaration_and, $.type_declaration))
281-
),
282-
283-
_type_declaration_and: $ => seq(
284-
// New line here not necessary terminates the statement,
285-
// show this doubt to the parser
286-
repeat($._newline),
287-
repeat($.decorator),
288-
'and',
289-
$._type_declaration
290285
),
291286

292287
type_parameters: $ => seq(

test/corpus/decorators.txt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,18 @@ and bar = {bar: int}
106106
(decorated
107107
(decorator (decorator_identifier))
108108
(type_declaration
109-
(type_identifier)
110-
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))
111-
(type_declaration
112-
(decorator (decorator_identifier))
109+
(type_binding
113110
(type_identifier)
114-
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))
115-
(type_declaration
116-
(decorator (decorator_identifier))
117-
(decorator (decorator_identifier))
111+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier)))))
112+
(decorator (decorator_identifier))
113+
(type_binding
114+
(type_identifier)
115+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier)))))
116+
(decorator (decorator_identifier))
117+
(decorator (decorator_identifier))
118+
(type_binding
118119
(type_identifier)
119-
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier)))))))))
120+
(record_type (record_type_field (property_identifier) (type_annotation (type_identifier))))))))
120121

121122
============================================
122123
Decorator with type

test/corpus/modules.txt

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ include module type of {
4848
(module_identifier_path (module_identifier) (module_identifier))
4949
(arguments
5050
(block
51-
(type_declaration (type_identifier))
51+
(type_declaration (type_binding (type_identifier)))
5252
(let_declaration
5353
(let_binding
5454
(value_identifier)
@@ -93,7 +93,7 @@ module MyModule = {
9393
(source_file
9494
(module_declaration
9595
(module_identifier)
96-
(block (type_declaration (type_identifier)))))
96+
(block (type_declaration (type_binding (type_identifier))))))
9797

9898
===========================================
9999
Signature and definition
@@ -133,11 +133,24 @@ module MyModule: {
133133
(module_declaration
134134
name: (module_identifier)
135135
signature: (module_identifier_path (module_identifier) (module_identifier))
136-
definition: (block (type_declaration (type_identifier))))
136+
definition:
137+
(block
138+
(type_declaration
139+
(type_binding
140+
name: (type_identifier)))))
137141
(module_declaration
138142
name: (module_identifier)
139-
signature: (block (type_declaration (type_identifier)))
140-
definition: (block (type_declaration (type_identifier) (type_identifier)))))
143+
signature:
144+
(block
145+
(type_declaration
146+
(type_binding
147+
name: (type_identifier))))
148+
definition:
149+
(block
150+
(type_declaration
151+
(type_binding
152+
name: (type_identifier)
153+
(type_identifier))))))
141154

142155
===========================================
143156
Module types
@@ -152,7 +165,7 @@ module type t
152165
(source_file
153166
(module_declaration
154167
(module_identifier)
155-
(block (type_declaration (type_identifier))))
168+
(block (type_declaration (type_binding (type_identifier)))))
156169
(module_declaration
157170
(module_identifier)
158171
(module_type_of
@@ -185,7 +198,7 @@ module(SomeFunctor(unpack(x)))
185198
(expression_statement
186199
(module_pack
187200
(block
188-
(type_declaration (type_identifier))
201+
(type_declaration (type_binding (type_identifier)))
189202
(let_declaration
190203
(let_binding
191204
(value_identifier)
@@ -218,19 +231,25 @@ module MyFunctor = (X: {type t}, Y: {type t}): {type tx; type ty} => {
218231
parameters: (functor_parameters
219232
(functor_parameter
220233
(module_identifier)
221-
(module_type_annotation (block (type_declaration (type_identifier)))))
234+
(module_type_annotation (block (type_declaration (type_binding name: (type_identifier))))))
222235
(functor_parameter
223236
(module_identifier)
224-
(module_type_annotation (block (type_declaration (type_identifier))))))
237+
(module_type_annotation (block (type_declaration (type_binding name: (type_identifier)))))))
225238
return_module_type: (module_type_annotation
226-
(block (type_declaration (type_identifier)) (type_declaration (type_identifier))))
239+
(block
240+
(type_declaration
241+
(type_binding name: (type_identifier)))
242+
(type_declaration
243+
(type_binding name: (type_identifier)))))
227244
body: (block
228245
(type_declaration
229-
(type_identifier)
230-
(type_identifier_path (module_identifier) (type_identifier)))
246+
(type_binding
247+
name: (type_identifier)
248+
(type_identifier_path (module_identifier) (type_identifier))))
231249
(type_declaration
232-
(type_identifier)
233-
(type_identifier_path (module_identifier) (type_identifier)))))))
250+
(type_binding
251+
name: (type_identifier)
252+
(type_identifier_path (module_identifier) (type_identifier))))))))
234253

235254
===========================================
236255
Functor signature

0 commit comments

Comments
 (0)