Skip to content

Commit 2bf1d01

Browse files
authored
Merge pull request #137 from aspeddro/fix-fcm
Better support for modules
2 parents 0ad57c4 + c9aacd0 commit 2bf1d01

File tree

8 files changed

+329
-131
lines changed

8 files changed

+329
-131
lines changed

grammar.js

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ module.exports = grammar({
153153

154154
include_statement: $ => seq(
155155
'include',
156-
choice($.module_expression, seq('(', $.module_expression, ')')),
156+
choice(
157+
$.module_expression,
158+
seq('(', choice($.module_expression, $.functor_parameter), ')')
159+
),
157160
),
158161

159162
declaration: $ => choice(
@@ -202,14 +205,15 @@ module.exports = grammar({
202205
),
203206

204207
functor_parameter: $ => seq(
205-
$.module_identifier,
208+
$.module_identifier_path,
206209
$.module_type_annotation,
207210
),
208211

209212
module_type_annotation: $ => seq(
210213
':',
211214
choice(
212215
$.module_expression,
216+
seq('(', $.module_expression, ')'),
213217
$.block,
214218
)
215219
),
@@ -291,18 +295,8 @@ module.exports = grammar({
291295
$.object_type,
292296
$.generic_type,
293297
$.unit_type,
298+
$.module_pack,
294299
$.unit,
295-
$.module_pack_type,
296-
),
297-
298-
module_pack_type: $ => seq(
299-
'module',
300-
'(',
301-
choice($.module_identifier, $._type_identifier),
302-
optional(
303-
seq('with', sep1('and', seq('type', $._type_identifier, '=', $._type_identifier)))
304-
),
305-
')'
306300
),
307301

308302
tuple_type: $ => prec.dynamic(-1, seq(
@@ -509,7 +503,7 @@ module.exports = grammar({
509503
$.pipe_expression,
510504
$.subscript_expression,
511505
$.member_expression,
512-
$.module_pack_expression,
506+
$.module_pack,
513507
$.extension_expression,
514508
),
515509

@@ -522,7 +516,8 @@ module.exports = grammar({
522516
),
523517

524518
value_identifier_path: $ => seq(
525-
repeat1(seq($.module_identifier, '.')),
519+
$.module_identifier_path,
520+
'.',
526521
$.value_identifier,
527522
),
528523

@@ -736,11 +731,11 @@ module.exports = grammar({
736731
),
737732
)),
738733

739-
module_pack_expression: $ => seq(
734+
module_pack: $ => seq(
740735
'module',
741736
'(',
742-
choice($.module_expression, $.block),
743-
optional(seq(':', $.module_expression)),
737+
$._module_definition,
738+
optional($.module_type_annotation),
744739
')'
745740
),
746741

@@ -831,7 +826,8 @@ module.exports = grammar({
831826
$._destructuring_pattern,
832827
$.polyvar_type_pattern,
833828
$.unit,
834-
$._parenthesized_pattern
829+
$.module_pack,
830+
$._parenthesized_pattern,
835831
)),
836832
optional($.type_annotation),
837833
optional($.as_aliasing),
@@ -1214,7 +1210,8 @@ module.exports = grammar({
12141210
)),
12151211

12161212
nested_variant_identifier: $ => seq(
1217-
repeat1(seq($.module_identifier, '.')),
1213+
$.module_identifier_path,
1214+
'.',
12181215
$.variant_identifier
12191216
),
12201217

@@ -1240,54 +1237,65 @@ module.exports = grammar({
12401237
),
12411238

12421239
type_identifier_path: $ => seq(
1243-
repeat1(seq($.module_identifier, '.')),
1240+
$.module_identifier_path,
1241+
'.',
12441242
$.type_identifier
12451243
),
12461244

12471245
module_expression: $ => choice(
1248-
$.module_identifier,
12491246
$.module_identifier_path,
1247+
$.type_identifier_path,
12501248
$.module_type_of,
12511249
$.functor_use,
12521250
$.module_type_constraint,
12531251
$.module_unpack,
12541252
),
12551253

1256-
module_identifier_path: $ => prec.left(seq(
1257-
$.module_expression,
1258-
'.',
1254+
module_identifier_path: $ => path(
1255+
$.module_identifier_path,
12591256
$.module_identifier,
1260-
)),
1257+
),
12611258

1262-
module_type_of: $ => prec.dynamic(-1, seq(
1259+
module_type_of: $ => prec.left(seq(
12631260
'module',
12641261
'type',
12651262
'of',
1266-
choice($.module_expression, $.block),
1263+
choice($.module_expression, $.block)
12671264
)),
12681265

1269-
module_type_constraint: $ => seq(
1270-
'(',
1271-
choice($.module_identifier, $.module_identifier_path),
1272-
':',
1273-
$.module_type_of,
1266+
_module_type_constraint: $ => seq(
12741267
'with',
1275-
sep1('and',
1276-
seq(
1277-
'module',
1278-
$.module_expression,
1279-
choice('=', ':='),
1280-
$.module_expression
1281-
)
1268+
sep1(choice('and', 'with'),
1269+
choice($.constrain_module, $.constrain_type)
12821270
),
1283-
')'
1271+
),
1272+
1273+
module_type_constraint: $ => prec.left(choice(
1274+
seq($.module_identifier_path, $._module_type_constraint),
1275+
seq(
1276+
'(',
1277+
$.module_identifier_path, $._module_type_constraint,
1278+
')',
1279+
$._module_type_constraint
1280+
)
1281+
)),
1282+
1283+
constrain_module: $ => seq(
1284+
'module',
1285+
$.module_identifier_path,
1286+
choice('=', ':='),
1287+
$.module_identifier_path,
1288+
),
1289+
1290+
constrain_type: $ => seq(
1291+
'type',
1292+
$._type,
1293+
choice('=', ':='),
1294+
$._type,
12841295
),
12851296

12861297
functor_use: $ => seq(
1287-
choice(
1288-
$.module_identifier,
1289-
$.module_identifier_path,
1290-
),
1298+
$.module_identifier_path,
12911299
alias($.functor_arguments, $.arguments),
12921300
),
12931301

@@ -1486,3 +1494,7 @@ function commaSept(rule) {
14861494
function sep1(delimiter, rule) {
14871495
return seq(rule, repeat(seq(delimiter, rule)))
14881496
}
1497+
1498+
function path(prefix, final) {
1499+
return choice(final, seq(prefix, '.', final))
1500+
}

test/corpus/decorators.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ let onResult = () => @doesNotRaise Belt.Array.getExn([], 0)
4242
(parenthesized_expression
4343
(decorator (decorator_identifier))
4444
(value_identifier_path
45-
(module_identifier) (value_identifier)))
45+
(module_identifier_path
46+
(module_identifier))
47+
(value_identifier)))
4648
(arguments
4749
(number)
4850
(character))))
@@ -51,7 +53,10 @@ let onResult = () => @doesNotRaise Belt.Array.getExn([], 0)
5153
(value_identifier)
5254
(decorator (decorator_identifier))
5355
(call_expression
54-
(value_identifier_path (module_identifier) (value_identifier))
56+
(value_identifier_path
57+
(module_identifier_path
58+
(module_identifier))
59+
(value_identifier))
5560
(arguments
5661
(number)
5762
(character))))
@@ -62,7 +67,12 @@ let onResult = () => @doesNotRaise Belt.Array.getExn([], 0)
6267
(formal_parameters)
6368
(decorator (decorator_identifier))
6469
(call_expression
65-
(value_identifier_path (module_identifier) (module_identifier) (value_identifier))
70+
(value_identifier_path
71+
(module_identifier_path
72+
(module_identifier_path
73+
(module_identifier))
74+
(module_identifier))
75+
(value_identifier))
6676
(arguments
6777
(array)
6878
(number))))))

0 commit comments

Comments
 (0)