Skip to content

Commit a58ada0

Browse files
authored
Merge pull request #143 from nkrkv/refactor-simplify-2v2
Simplify grammar
2 parents 2bf1d01 + 6a7a4ba commit a58ada0

File tree

8 files changed

+133
-203
lines changed

8 files changed

+133
-203
lines changed

grammar.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ module.exports = grammar({
2424
$.primary_expression,
2525
$._type,
2626
$.module_expression,
27+
$.module_primary_expression,
2728
],
2829

2930
precedences: $ => [
31+
// + - Operators -> precendence
3032
[
3133
'unary_not',
3234
'member',
3335
'call',
3436
$.spread_element,
3537
$.await_expression,
38+
$.pipe_expression,
3639
'binary_times',
3740
'binary_pow',
3841
'binary_plus',
@@ -48,14 +51,19 @@ module.exports = grammar({
4851
$.function,
4952
$.let_binding,
5053
],
54+
// Nested.Module.Path precendence
55+
[
56+
$.module_primary_expression,
57+
$.value_identifier_path,
58+
$.nested_variant_identifier,
59+
$.module_identifier_path,
60+
],
5161
[$._jsx_attribute_value, $.pipe_expression],
5262
[$.function_type_parameters, $.function_type],
53-
[$.module_identifier_path, $.module_type_of],
5463
],
5564

5665
conflicts: $ => [
5766
[$.unit, $.formal_parameters],
58-
[$.pipe_expression, $.expression],
5967
[$.primary_expression, $._pattern],
6068
[$.primary_expression, $.record_pattern],
6169
[$.primary_expression, $.spread_pattern],
@@ -64,7 +72,6 @@ module.exports = grammar({
6472
[$.tuple_pattern, $.parameter],
6573
[$.primary_expression, $.parameter],
6674
[$.primary_expression, $.record_field],
67-
[$.module_identifier_path, $.module_expression],
6875
[$.tuple_type, $.function_type_parameter],
6976
[$.list, $.list_pattern],
7077
[$.array, $.array_pattern],
@@ -74,10 +81,9 @@ module.exports = grammar({
7481
[$._let_binding],
7582
[$.let_binding, $.ternary_expression],
7683
[$.variant_identifier, $.module_identifier],
77-
[$.variant],
7884
[$.variant, $.variant_pattern],
7985
[$.variant_declaration, $.function_type_parameter],
80-
[$.polyvar],
86+
[$.variant_arguments, $._variant_pattern_parameters],
8187
[$.polyvar, $.polyvar_pattern],
8288
[$._pattern],
8389
[$._record_element, $.jsx_expression],
@@ -93,7 +99,7 @@ module.exports = grammar({
9399
[$.parameter, $._parenthesized_pattern],
94100
[$._switch_value_pattern, $._parenthesized_pattern],
95101
[$.variant_declaration],
96-
[$.unit, $._function_type_parameter_list]
102+
[$.unit, $._function_type_parameter_list],
97103
],
98104

99105
rules: {
@@ -205,7 +211,7 @@ module.exports = grammar({
205211
),
206212

207213
functor_parameter: $ => seq(
208-
$.module_identifier_path,
214+
$.module_identifier,
209215
$.module_type_annotation,
210216
),
211217

@@ -516,7 +522,7 @@ module.exports = grammar({
516522
),
517523

518524
value_identifier_path: $ => seq(
519-
$.module_identifier_path,
525+
$.module_primary_expression,
520526
'.',
521527
$.value_identifier,
522528
),
@@ -863,7 +869,7 @@ module.exports = grammar({
863869

864870
_variant_pattern_parameters: $ => seq(
865871
'(',
866-
commaSep1t($._variant_pattern_parameter),
872+
commaSept($._variant_pattern_parameter),
867873
')',
868874
),
869875

@@ -1204,13 +1210,13 @@ module.exports = grammar({
12041210
')',
12051211
),
12061212

1207-
variant: $ => prec.dynamic(-1, seq(
1213+
variant: $ => prec.right(seq(
12081214
choice($.variant_identifier, $.nested_variant_identifier),
12091215
optional(alias($.variant_arguments, $.arguments)),
12101216
)),
12111217

12121218
nested_variant_identifier: $ => seq(
1213-
$.module_identifier_path,
1219+
$.module_primary_expression,
12141220
'.',
12151221
$.variant_identifier
12161222
),
@@ -1224,10 +1230,10 @@ module.exports = grammar({
12241230
')',
12251231
),
12261232

1227-
polyvar: $ => seq(
1233+
polyvar: $ => prec.right(seq(
12281234
$.polyvar_identifier,
12291235
optional(alias($.variant_arguments, $.arguments)),
1230-
),
1236+
)),
12311237

12321238
_type_identifier: $ =>
12331239
choice(
@@ -1237,22 +1243,27 @@ module.exports = grammar({
12371243
),
12381244

12391245
type_identifier_path: $ => seq(
1240-
$.module_identifier_path,
1246+
$.module_primary_expression,
12411247
'.',
12421248
$.type_identifier
12431249
),
12441250

12451251
module_expression: $ => choice(
1246-
$.module_identifier_path,
1252+
$.module_primary_expression,
12471253
$.type_identifier_path,
12481254
$.module_type_of,
1249-
$.functor_use,
12501255
$.module_type_constraint,
1256+
),
1257+
1258+
module_primary_expression: $ => choice(
1259+
$.module_identifier,
1260+
$.module_identifier_path,
1261+
$.functor_use,
12511262
$.module_unpack,
12521263
),
12531264

12541265
module_identifier_path: $ => path(
1255-
$.module_identifier_path,
1266+
$.module_primary_expression,
12561267
$.module_identifier,
12571268
),
12581269

@@ -1263,28 +1274,28 @@ module.exports = grammar({
12631274
choice($.module_expression, $.block)
12641275
)),
12651276

1266-
_module_type_constraint: $ => seq(
1277+
_module_type_constraint_with: $ => prec.right(seq(
12671278
'with',
12681279
sep1(choice('and', 'with'),
12691280
choice($.constrain_module, $.constrain_type)
12701281
),
1271-
),
1282+
)),
12721283

12731284
module_type_constraint: $ => prec.left(choice(
1274-
seq($.module_identifier_path, $._module_type_constraint),
1285+
seq($.module_expression, $._module_type_constraint_with),
12751286
seq(
12761287
'(',
1277-
$.module_identifier_path, $._module_type_constraint,
1288+
$.module_expression, $._module_type_constraint_with,
12781289
')',
1279-
$._module_type_constraint
1290+
$._module_type_constraint_with
12801291
)
12811292
)),
12821293

12831294
constrain_module: $ => seq(
12841295
'module',
1285-
$.module_identifier_path,
1296+
$.module_identifier,
12861297
choice('=', ':='),
1287-
$.module_identifier_path,
1298+
$.module_primary_expression,
12881299
),
12891300

12901301
constrain_type: $ => seq(
@@ -1295,7 +1306,7 @@ module.exports = grammar({
12951306
),
12961307

12971308
functor_use: $ => seq(
1298-
$.module_identifier_path,
1309+
$.module_primary_expression,
12991310
alias($.functor_arguments, $.arguments),
13001311
),
13011312

test/corpus/decorators.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ let onResult = () => @doesNotRaise Belt.Array.getExn([], 0)
4242
(parenthesized_expression
4343
(decorator (decorator_identifier))
4444
(value_identifier_path
45-
(module_identifier_path
46-
(module_identifier))
45+
(module_identifier)
4746
(value_identifier)))
4847
(arguments
4948
(number)
@@ -54,8 +53,7 @@ let onResult = () => @doesNotRaise Belt.Array.getExn([], 0)
5453
(decorator (decorator_identifier))
5554
(call_expression
5655
(value_identifier_path
57-
(module_identifier_path
58-
(module_identifier))
56+
(module_identifier)
5957
(value_identifier))
6058
(arguments
6159
(number)
@@ -69,8 +67,7 @@ let onResult = () => @doesNotRaise Belt.Array.getExn([], 0)
6967
(call_expression
7068
(value_identifier_path
7169
(module_identifier_path
72-
(module_identifier_path
73-
(module_identifier))
70+
(module_identifier)
7471
(module_identifier))
7572
(value_identifier))
7673
(arguments

0 commit comments

Comments
 (0)