Skip to content

Commit 30350ad

Browse files
committed
better support for module
1 parent 652c0af commit 30350ad

File tree

8 files changed

+245
-122
lines changed

8 files changed

+245
-122
lines changed

grammar.js

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,7 @@ module.exports = grammar({
278278
$.object_type,
279279
$.generic_type,
280280
$.unit_type,
281-
$.module_pack_type,
282-
),
283-
284-
module_pack_type: $ => seq(
285-
'module',
286-
'(',
287-
choice($.module_identifier, $._type_identifier),
288-
optional(
289-
seq('with', sep1('and', seq('type', $._type_identifier, '=', $._type_identifier)))
290-
),
291-
')'
281+
$.module_pack,
292282
),
293283

294284
tuple_type: $ => prec.dynamic(-1, seq(
@@ -489,7 +479,7 @@ module.exports = grammar({
489479
$.pipe_expression,
490480
$.subscript_expression,
491481
$.member_expression,
492-
$.module_pack_expression,
482+
$.module_pack,
493483
$.extension_expression,
494484
),
495485

@@ -502,7 +492,8 @@ module.exports = grammar({
502492
),
503493

504494
value_identifier_path: $ => seq(
505-
repeat1(seq($.module_identifier, '.')),
495+
$.module_identifier_path,
496+
'.',
506497
$.value_identifier,
507498
),
508499

@@ -716,7 +707,7 @@ module.exports = grammar({
716707
),
717708
)),
718709

719-
module_pack_expression: $ => seq(
710+
module_pack: $ => seq(
720711
'module',
721712
'(',
722713
choice($.module_expression, $.block),
@@ -811,7 +802,8 @@ module.exports = grammar({
811802
$._destructuring_pattern,
812803
$.polyvar_type_pattern,
813804
$.unit,
814-
$._parenthesized_pattern
805+
$.module_pack,
806+
$._parenthesized_pattern,
815807
)),
816808
optional($.type_annotation),
817809
optional($.as_aliasing),
@@ -1194,7 +1186,8 @@ module.exports = grammar({
11941186
)),
11951187

11961188
nested_variant_identifier: $ => seq(
1197-
repeat1(seq($.module_identifier, '.')),
1189+
$.module_identifier_path,
1190+
'.',
11981191
$.variant_identifier
11991192
),
12001193

@@ -1220,54 +1213,59 @@ module.exports = grammar({
12201213
),
12211214

12221215
type_identifier_path: $ => seq(
1223-
repeat1(seq($.module_identifier, '.')),
1216+
$.module_identifier_path,
1217+
'.',
12241218
$.type_identifier
12251219
),
12261220

12271221
module_expression: $ => choice(
1228-
$.module_identifier,
12291222
$.module_identifier_path,
1223+
$.type_identifier_path,
12301224
$.module_type_of,
12311225
$.functor_use,
12321226
$.module_type_constraint,
12331227
$.module_unpack,
12341228
),
12351229

1236-
module_identifier_path: $ => prec.left(seq(
1237-
$.module_expression,
1238-
'.',
1230+
module_identifier_path: $ => path(
1231+
$.module_identifier_path,
12391232
$.module_identifier,
1240-
)),
1233+
),
12411234

1242-
module_type_of: $ => prec.dynamic(-1, seq(
1235+
module_type_of: $ => prec.left(seq(
12431236
'module',
12441237
'type',
12451238
'of',
1246-
choice($.module_expression, $.block),
1239+
choice($.module_expression, $.block)
12471240
)),
12481241

12491242
module_type_constraint: $ => seq(
1250-
'(',
1251-
choice($.module_identifier, $.module_identifier_path),
1252-
':',
1253-
$.module_type_of,
1243+
$.module_expression,
1244+
optional(':'),
1245+
$.module_expression,
12541246
'with',
12551247
sep1('and',
1256-
seq(
1257-
'module',
1258-
$.module_expression,
1259-
choice('=', ':='),
1260-
$.module_expression
1261-
)
1248+
choice($.constrain_module, $.constrain_type)
12621249
),
1263-
')'
1250+
),
1251+
1252+
constrain_module: $ => seq(
1253+
'module',
1254+
$.module_identifier_path,
1255+
choice('=', ':='),
1256+
$.module_identifier_path,
1257+
),
1258+
1259+
// TODO: use $.type_declaration rule
1260+
constrain_type: $ => seq(
1261+
'type',
1262+
$._type_identifier,
1263+
'=',
1264+
$._type_identifier
12641265
),
12651266

12661267
functor_use: $ => seq(
1267-
choice(
1268-
$.module_identifier,
1269-
$.module_identifier_path,
1270-
),
1268+
$.module_identifier_path,
12711269
alias($.functor_arguments, $.arguments),
12721270
),
12731271

@@ -1466,3 +1464,7 @@ function commaSept(rule) {
14661464
function sep1(delimiter, rule) {
14671465
return seq(rule, repeat(seq(delimiter, rule)))
14681466
}
1467+
1468+
function path(prefix, final) {
1469+
return choice(final, seq(prefix, '.', final))
1470+
}

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)