Skip to content

Commit 72fb207

Browse files
authored
Merge pull request #132 from aspeddro/module-type
support first class modules
2 parents de1d7b0 + e0522d3 commit 72fb207

File tree

7 files changed

+163
-35
lines changed

7 files changed

+163
-35
lines changed

grammar.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ module.exports = grammar({
167167
'module',
168168
optional('rec'),
169169
optional('type'),
170-
field('name', $.module_identifier),
170+
field('name', choice($.module_identifier, $._type_identifier)),
171171
optional(seq(
172172
':',
173173
field('signature', choice($.block, $.module_expression, $.functor)),
@@ -185,6 +185,8 @@ module.exports = grammar({
185185
$.extension_expression,
186186
),
187187

188+
module_unpack: $ => seq('unpack', $.call_arguments),
189+
188190
functor: $ => seq(
189191
field('parameters', $.functor_parameters),
190192
optional(field('return_module_type', $.module_type_annotation)),
@@ -277,6 +279,17 @@ module.exports = grammar({
277279
$.object_type,
278280
$.generic_type,
279281
$.unit_type,
282+
$.module_pack_type,
283+
),
284+
285+
module_pack_type: $ => seq(
286+
'module',
287+
'(',
288+
choice($.module_identifier, $._type_identifier),
289+
optional(
290+
seq('with', sep1('and', seq('type', $._type_identifier, '=', $._type_identifier)))
291+
),
292+
')'
280293
),
281294

282295
tuple_type: $ => prec.dynamic(-1, seq(
@@ -429,6 +442,7 @@ module.exports = grammar({
429442
$.record_pattern,
430443
$.array_pattern,
431444
$.list_pattern,
445+
$.module_unpack_pattern,
432446
$.unit
433447
),
434448

@@ -475,7 +489,7 @@ module.exports = grammar({
475489
$.pipe_expression,
476490
$.subscript_expression,
477491
$.member_expression,
478-
$.module_destructuring_expression,
492+
$.module_pack_expression,
479493
$.extension_expression,
480494
),
481495

@@ -702,8 +716,12 @@ module.exports = grammar({
702716
),
703717
)),
704718

705-
module_destructuring_expression: $ => seq(
706-
'module', '(', choice($.module_identifier, $.module_identifier_path), ')'
719+
module_pack_expression: $ => seq(
720+
'module',
721+
'(',
722+
choice($.module_expression, $.block),
723+
optional(seq(':', $.module_expression)),
724+
')'
707725
),
708726

709727
call_arguments: $ => seq(
@@ -772,7 +790,7 @@ module.exports = grammar({
772790

773791
type_parameter: $ => seq(
774792
'type',
775-
$.type_identifier,
793+
repeat1($.type_identifier),
776794
),
777795

778796
_labeled_parameter_default_value: $ => seq(
@@ -886,6 +904,10 @@ module.exports = grammar({
886904
choice($.value_identifier, $.list_pattern, $.array_pattern),
887905
),
888906

907+
module_unpack_pattern: $ => seq(
908+
'module', '(', $.module_identifier, ')',
909+
),
910+
889911
_jsx_element: $ => choice($.jsx_element, $.jsx_self_closing_element),
890912

891913
jsx_element: $ => seq(
@@ -1207,7 +1229,8 @@ module.exports = grammar({
12071229
$.module_identifier_path,
12081230
$.module_type_of,
12091231
$.functor_use,
1210-
$.module_type_constraint
1232+
$.module_type_constraint,
1233+
$.module_unpack,
12111234
),
12121235

12131236
module_identifier_path: $ => prec.left(seq(

queries/highlights.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
"async"
114114
"await"
115115
"with"
116+
"unpack"
116117
] @keyword
117118

118119
[

test/corpus/let_bindings.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,55 @@ let test = (. ~attr) => ()
247247
(labeled_parameter
248248
(value_identifier))))
249249
(unit))))
250+
251+
===========================================
252+
Destructuring module
253+
===========================================
254+
255+
256+
let {foo, bar} = module(User)
257+
let {baz, _} = module(User.Inner)
258+
259+
---
260+
261+
(source_file
262+
(let_binding
263+
(record_pattern
264+
(value_identifier)
265+
(value_identifier))
266+
(module_pack_expression (module_identifier)))
267+
268+
(let_binding
269+
(record_pattern
270+
(value_identifier)
271+
(value_identifier))
272+
(module_pack_expression
273+
(module_identifier_path
274+
(module_identifier)
275+
(module_identifier)))))
276+
277+
===========================================
278+
Packing module
279+
===========================================
280+
281+
let foo = module(Bar)
282+
283+
---
284+
285+
(source_file
286+
(let_binding
287+
(value_identifier)
288+
(module_pack_expression (module_identifier))))
289+
290+
===========================================
291+
Unpacking module
292+
===========================================
293+
294+
let module(Bar) = foo
295+
296+
---
297+
298+
(source_file
299+
(let_binding
300+
(module_unpack_pattern (module_identifier))
301+
(value_identifier)))

test/corpus/modules.txt

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,6 @@ open! Foo.Bar
1212
(open_statement
1313
(module_identifier_path (module_identifier) (module_identifier))))
1414

15-
===========================================
16-
Module destructuring
17-
===========================================
18-
19-
20-
let {foo, bar} = module(User)
21-
let {baz, _} = module(User.Inner)
22-
23-
---
24-
25-
(source_file
26-
(let_binding
27-
(record_pattern
28-
(value_identifier)
29-
(value_identifier))
30-
(module_destructuring_expression (module_identifier)))
31-
32-
(let_binding
33-
(record_pattern
34-
(value_identifier)
35-
(value_identifier))
36-
(module_destructuring_expression
37-
(module_identifier_path
38-
(module_identifier)
39-
(module_identifier)))))
40-
4115
===========================================
4216
Include
4317
===========================================
@@ -156,6 +130,7 @@ Module types
156130

157131
module type S1 = { type t }
158132
module type S2 = module type of MyModule.Submod
133+
module type t
159134

160135
---
161136

@@ -165,7 +140,46 @@ module type S2 = module type of MyModule.Submod
165140
(block (type_declaration (type_identifier))))
166141
(module_declaration
167142
(module_identifier)
168-
(module_type_of (module_identifier_path (module_identifier) (module_identifier)))))
143+
(module_type_of (module_identifier_path (module_identifier) (module_identifier))))
144+
(module_declaration (type_identifier)))
145+
146+
===========================================
147+
First Class module
148+
===========================================
149+
150+
module(LightTheme)
151+
module(A: A)
152+
module(
153+
{
154+
type t
155+
let foo = "Hello"
156+
}: X
157+
)
158+
module(SomeFunctor(unpack(x)))
159+
160+
---
161+
162+
(source_file
163+
(expression_statement
164+
(module_pack_expression (module_identifier)))
165+
(expression_statement
166+
(module_pack_expression (module_identifier) (module_identifier)))
167+
(expression_statement
168+
(module_pack_expression
169+
(block
170+
(type_declaration (type_identifier))
171+
(let_binding
172+
(value_identifier)
173+
(string (string_fragment))))
174+
(module_identifier)))
175+
(expression_statement
176+
(module_pack_expression
177+
(functor_use
178+
(module_identifier)
179+
(arguments
180+
(module_unpack
181+
(call_arguments (value_identifier))))))))
182+
169183

170184
===========================================
171185
Functor definition

test/corpus/type_declarations.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ type t =
136136
| @live("t.D") D
137137
| B(anotherType)
138138
| C({foo: int, bar: string})
139+
| D(module(Foo))
140+
| D(module(Bar.t))
139141

140142
---
141143

@@ -156,8 +158,16 @@ type t =
156158
(record_type_field
157159
(property_identifier) (type_annotation (type_identifier)))
158160
(record_type_field
159-
(property_identifier) (type_annotation (type_identifier)))
160-
))))))
161+
(property_identifier) (type_annotation (type_identifier))))))
162+
(variant_declaration
163+
(variant_identifier)
164+
(variant_parameters
165+
(module_pack_type (module_identifier))))
166+
(variant_declaration
167+
(variant_identifier)
168+
(variant_parameters
169+
(module_pack_type
170+
(type_identifier_path (module_identifier) (type_identifier))))))))
161171

162172
===========================================
163173
Annotated variant

test/highlight/modules.res

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,29 @@ module Belt = {
2222
// ^ keyword
2323
// ^ operator
2424
}
25+
26+
let a = module(
27+
// ^ keyword
28+
{
29+
type t
30+
let hello = "Hello"
31+
}: X
32+
// ^ namespace
33+
)
34+
35+
module B = unpack(a)
36+
// ^ keyword
37+
38+
module type A = {
39+
type t = int
40+
let value: t
41+
}
42+
43+
module A: A = {
44+
type t = int
45+
let value: t = 42
46+
}
47+
48+
let packedA = module(A: A)
49+
// ^ namespace
50+
// ^ namespace

test/highlight/type_declarations.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ type rec t =
88
// ^ type
99
| Terminal
1010
// ^ constant
11+
| Component(module(Foo.t))
12+
// ^ keyword
1113

1214
type obj = {
1315
"x": int,

0 commit comments

Comments
 (0)