Skip to content

Commit d93f5cb

Browse files
authored
Fix parsing of ReScript compiler syntax examples (#309)
ReScript compiler printer fixtures exposed missing grammar rules and premature automatic semicolon insertion. This PR allows those constructs to parse without `ERROR` or `MISSING` nodes and adds 28 regression tests organized by syntax category. ### Declaration continuations Newlines and comments before `constraint` or a binding's `=` no longer terminate the declaration: ```rescript type result<'good, 'bad> = | Good('good) | Bad('bad) constraint 'good = bool constraint 'bad = float let identity: int => int /* The initializer belongs to the same binding. */ = value => value ``` ### Extensible types and constructor rebindings Support private extensible types and constructor aliases, including qualified aliases and decorated constructors: ```rescript module Private = {type t = private ..} type event = .. type event += Original(int) type event += Alias = Original ``` ### Functors Support bare and anonymous functor parameters, unit parameters, and type annotations on functor arguments: ```rescript module type Input = {type t} module type Output = {type t} module type Build = Input => Output module type Anonymous = (_: Input) => Output module Make = (Config: Input, ()) => {type t = Config.t} module Config = {type t = int} module Result = Make(Config: Input, ()) ``` ### Expression receivers and labeled arguments Braced expressions can be used as call, member-access, and subscript receivers. Labeled arguments now accept punned type annotations and combined source-type/coercion annotations: ```rescript let add = (a, b) => a + b let sum = {add}(1, 2) let values = [1, 2] let first = {values}[{0}] let call = (~value: int) => value let value = 1 let typed = call(~value: int) let coerced = call(~value=value : int :> int) ``` ## Additional Coverage - Polymorphic variant conjunctions (`&`) and escaped type variables. - Repeated type constraints in module type refinements. - Explicit exception result types and extensions in type/pattern positions. - Type annotations in collection patterns and optional parenthesized record patterns. - Adjacent decorators and decorator payloads containing patterns, guards, or declarations. - Signature payloads in extensions. - Regression cases for tuple callbacks, decorator statement boundaries, and continuations through comments. The generated parser and node metadata are updated. The ReScript development dependency is updated to `13.0.0-alpha.7` for compiler validation.
1 parent cc6a15e commit d93f5cb

15 files changed

Lines changed: 205617 additions & 172314 deletions

‎AGENTS.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ npm run parse path/to/file.res
2020
# Compare with ReScript compiler's AST (useful for debugging grammar issues)
2121
npx bsc -dparsetree -only-parse -ignore-parse-errors path/to/file.res
2222

23+
# To check if has syntax error or not
24+
npx bsc path/to/file.res
25+
2326
# Launch interactive playground (builds WASM first)
2427
npm start
2528

‎grammar.js‎

Lines changed: 157 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ export default grammar({
130130
[$._record_element, $._record_single_field],
131131
[$._record_pun_field, $._record_single_pun_field],
132132
[$._record_field_name, $.record_pattern],
133+
[$.primary_expression, $._record_field_name],
134+
[$.primary_expression, $._record_field_name, $.record_pattern],
135+
[$.primary_expression, $.object_field],
133136
[$._statement, $._switch_body],
134137
[$._inline_type, $.function_type_parameters],
135138
[$.primary_expression, $.parameter, $._pattern],
@@ -150,6 +153,44 @@ export default grammar({
150153
[$._record_type_member, $._object_type_member],
151154
[$._non_function_inline_type, $.generic_type],
152155
[$._type_identifier, $.polymorphic_type],
156+
[$.functor, $.module_expression],
157+
[$.call_expression, $._jsx_child],
158+
[$.subscript_expression, $._jsx_child],
159+
[$._labeled_parameter_default_value, $.coercion_expression],
160+
[$.await_expression, $.coercion_expression],
161+
[$.lazy_expression, $.coercion_expression],
162+
[$.mutation_expression, $.coercion_expression],
163+
// A ternary's ':' can also start a coercion's source type annotation.
164+
[$.type_identifier, $.value_identifier],
165+
[$._non_function_inline_type, $.primary_expression],
166+
[$.block, $.record_type],
167+
[$.record_type_field, $.primary_expression, $._record_field_name],
168+
[$.record_type_field, $.primary_expression],
169+
[$.object_type_field, $.primary_expression, $.object_field],
170+
[$._function_type_parameter_list, $.formal_parameters, $.unit],
171+
[$._non_function_inline_type, $.primary_expression, $._pattern],
172+
[$._non_function_inline_type, $._pattern],
173+
[$.function_type_parameter, $.parameter],
174+
[$.variant_declaration, $.variant_pattern, $.variant],
175+
[$.variant_declaration, $.variant_pattern],
176+
[$.variant_declaration, $.variant],
177+
[$.polyvar_declaration, $.polyvar],
178+
[$.polyvar_declaration, $.polyvar_pattern],
179+
[$.polyvar_declaration, $.polyvar_pattern, $.polyvar],
180+
[$.object_type, $.object],
181+
[
182+
$.record_type_field,
183+
$.primary_expression,
184+
$._record_field_name,
185+
$.record_pattern,
186+
],
187+
[
188+
$._non_function_inline_type,
189+
$.primary_expression,
190+
$.parameter,
191+
$._pattern,
192+
],
193+
[$._non_function_inline_type, $.parameter, $._pattern],
153194
],
154195

155196
rules: {
@@ -253,7 +294,13 @@ export default grammar({
253294

254295
functor: ($) =>
255296
seq(
256-
field('parameters', $.functor_parameters),
297+
field(
298+
'parameters',
299+
choice(
300+
$.functor_parameters,
301+
alias($.module_primary_expression, $.functor_parameter),
302+
),
303+
),
257304
optional(field('return_module_type', $.module_type_annotation)),
258305
'=>',
259306
field('body', $._module_definition),
@@ -263,10 +310,14 @@ export default grammar({
263310
seq('(', optional(commaSep1t($.functor_parameter)), ')'),
264311

265312
functor_parameter: ($) =>
266-
seq($.module_identifier, optional($.module_type_annotation)),
313+
choice(
314+
seq($.module_identifier, optional($.module_type_annotation)),
315+
seq('_', $.module_type_annotation),
316+
$.unit,
317+
),
267318

268319
module_type_annotation: ($) =>
269-
seq(':', choice($.module_expression, $.block)),
320+
seq(':', choice($.module_expression, $.block, $.functor)),
270321

271322
external_declaration: ($) =>
272323
seq('external', $.value_identifier, $.type_annotation, '=', $.string),
@@ -276,6 +327,7 @@ export default grammar({
276327
'exception',
277328
$.variant_identifier,
278329
optional($.variant_parameters),
330+
optional($.type_annotation),
279331
optional(
280332
seq('=', choice($.variant_identifier, $.nested_variant_identifier)),
281333
),
@@ -295,7 +347,12 @@ export default grammar({
295347
optional($.type_parameters),
296348
optional(
297349
choice(
298-
seq('=', $.extensible_type),
350+
seq(
351+
'=',
352+
optional('private'),
353+
$.extensible_type,
354+
repeat($.type_constraint),
355+
),
299356
seq(
300357
optional(seq('=', $._non_function_inline_type)),
301358
optional(
@@ -339,6 +396,7 @@ export default grammar({
339396
$.module_pack,
340397
$.unit,
341398
$.polymorphic_type,
399+
$.extension_expression,
342400
alias($._as_aliasing_non_function_inline_type, $.as_aliasing_type),
343401
),
344402

@@ -366,7 +424,15 @@ export default grammar({
366424
seq(
367425
$.variant_identifier,
368426
optional($.variant_parameters),
369-
optional($.type_annotation),
427+
optional(
428+
choice(
429+
$.type_annotation,
430+
seq(
431+
'=',
432+
choice($.variant_identifier, $.nested_variant_identifier),
433+
),
434+
),
435+
),
370436
),
371437
),
372438

@@ -387,7 +453,11 @@ export default grammar({
387453
polyvar_declaration: ($) =>
388454
prec.right(
389455
choice(
390-
seq($.polyvar_identifier, optional($.polyvar_parameters)),
456+
seq(
457+
$.polyvar_identifier,
458+
optional($.polyvar_parameters),
459+
repeat(seq('&', $._type)),
460+
),
391461
$._inline_type,
392462
),
393463
),
@@ -676,7 +746,7 @@ export default grammar({
676746
prec(
677747
'call',
678748
seq(
679-
field('function', $.primary_expression),
749+
field('function', choice($.primary_expression, $.block)),
680750
field('arguments', alias($.call_arguments, $.arguments)),
681751
),
682752
),
@@ -720,6 +790,7 @@ export default grammar({
720790
optional(
721791
choice(
722792
'?',
793+
$.type_annotation,
723794
seq(
724795
'=',
725796
optional('?'),
@@ -792,11 +863,13 @@ export default grammar({
792863
seq(
793864
choice(
794865
seq(optional('?'), $.value_identifier),
866+
seq('?', $.parenthesized_pattern),
795867
$._literal_pattern,
796868
$._destructuring_pattern,
797869
$.polyvar_type_pattern,
798870
$.unit,
799871
$.module_pack,
872+
$.extension_expression,
800873
$.lazy_pattern,
801874
$.parenthesized_pattern,
802875
$.or_pattern,
@@ -896,7 +969,11 @@ export default grammar({
896969
dict_pattern_entry: ($) => seq($.string, ':', $._pattern),
897970

898971
_collection_element_pattern: ($) =>
899-
seq(choice($._pattern, $.spread_pattern), optional($.as_aliasing)),
972+
seq(
973+
choice($._pattern, $.spread_pattern),
974+
optional($.type_annotation),
975+
optional($.as_aliasing),
976+
),
900977

901978
spread_pattern: ($) =>
902979
seq('...', choice($.value_identifier, $.list_pattern, $.array_pattern)),
@@ -1004,13 +1081,22 @@ export default grammar({
10041081
),
10051082

10061083
decorator_arguments: ($) =>
1007-
seq('(', choice(commaSept($.expression), $.type_annotation), ')'),
1084+
seq(
1085+
'(',
1086+
choice(
1087+
commaSept($.expression),
1088+
seq('?', $._pattern, optional($.guard)),
1089+
$.declaration,
1090+
$.type_annotation,
1091+
),
1092+
')',
1093+
),
10081094

10091095
subscript_expression: ($) =>
10101096
prec.right(
10111097
'member',
10121098
seq(
1013-
field('object', $.primary_expression),
1099+
field('object', choice($.primary_expression, $.block)),
10141100
'[',
10151101
field('index', $.expression),
10161102
']',
@@ -1021,7 +1107,7 @@ export default grammar({
10211107
prec(
10221108
'member',
10231109
seq(
1024-
field('record', $.primary_expression),
1110+
field('record', choice($.primary_expression, $.block)),
10251111
'.',
10261112
optional(
10271113
seq(
@@ -1107,21 +1193,37 @@ export default grammar({
11071193
),
11081194

11091195
coercion_expression: ($) =>
1110-
prec.left(
1111-
'coercion_relation',
1112-
choice(
1196+
choice(
1197+
prec.left(
1198+
'coercion_relation',
11131199
seq(
11141200
field('left', $.expression),
11151201
field('operator', ':>'),
11161202
field('right', $._type),
11171203
),
1204+
),
1205+
prec.dynamic(
1206+
1,
1207+
prec.left(
1208+
'coercion_relation',
1209+
seq(
1210+
'(',
1211+
field('left', $.expression),
1212+
field('source_type', $.type_annotation),
1213+
field('operator', ':>'),
1214+
field('right', $._type),
1215+
')',
1216+
),
1217+
),
1218+
),
1219+
// Do not force ':' to shift before a nested ternary can reduce.
1220+
// Right associativity still preserves the parenthesized coercion form.
1221+
prec.right(
11181222
seq(
1119-
'(',
11201223
field('left', $.expression),
11211224
field('source_type', $.type_annotation),
11221225
field('operator', ':>'),
11231226
field('right', $._type),
1124-
')',
11251227
),
11261228
),
11271229
),
@@ -1153,7 +1255,20 @@ export default grammar({
11531255
),
11541256

11551257
_extension_expression_payload: ($) =>
1156-
seq('(', choice($._one_or_more_statements, $.type_annotation), ')'),
1258+
seq(
1259+
'(',
1260+
choice(
1261+
$._one_or_more_statements,
1262+
$.type_annotation,
1263+
seq(
1264+
':',
1265+
repeat(seq($.let_declaration, $._semicolon)),
1266+
$.let_declaration,
1267+
optional($._semicolon),
1268+
),
1269+
),
1270+
')',
1271+
),
11571272

11581273
variant: ($) =>
11591274
prec.right(
@@ -1243,15 +1358,27 @@ export default grammar({
12431358
),
12441359

12451360
constrain_type: ($) =>
1246-
seq('type', $._type, choice('=', ':='), optional('private'), $._type),
1361+
seq(
1362+
'type',
1363+
$._type,
1364+
choice('=', ':='),
1365+
optional('private'),
1366+
$._type,
1367+
repeat($.type_constraint),
1368+
),
12471369

12481370
functor_use: ($) =>
12491371
seq($.module_primary_expression, alias($.functor_arguments, $.arguments)),
12501372

12511373
functor_arguments: ($) =>
12521374
seq('(', optional(commaSep1t($._functor_argument)), ')'),
12531375

1254-
_functor_argument: ($) => choice($.module_expression, $.block),
1376+
_functor_argument: ($) =>
1377+
choice(
1378+
seq($.module_expression, optional($.module_type_annotation)),
1379+
$.block,
1380+
$.unit,
1381+
),
12551382

12561383
variant_identifier: ($) => /[A-Z][a-zA-Z0-9_']*/,
12571384

@@ -1265,7 +1392,12 @@ export default grammar({
12651392
),
12661393

12671394
type_identifier: ($) =>
1268-
choice($._identifier, /'[a-zA-Z0-9_']*/, $._escape_identifier),
1395+
choice(
1396+
$._identifier,
1397+
/'[a-zA-Z0-9_']*/,
1398+
seq('\u0027', $._escape_identifier),
1399+
$._escape_identifier,
1400+
),
12691401

12701402
_identifier: (_) => /[a-z_][a-zA-Z0-9_']*/,
12711403

@@ -1434,8 +1566,11 @@ export default grammar({
14341566
choice(seq('$', $.value_identifier), seq('${', $.expression, '}')),
14351567

14361568
character: ($) =>
1437-
// prettier-ignore
1438-
seq('\'', repeat(choice(/[^\\']/, $.escape_sequence)), '\''),
1569+
choice(
1570+
// Match the whole literal before a type variable can consume its quotes.
1571+
token(prec(1, seq('\u0027', /[^\\']/, '\u0027'))),
1572+
seq('\u0027', repeat(choice(/[^\\']/, $.escape_sequence)), '\u0027'),
1573+
),
14391574

14401575
lparen: ($) => alias($._lparen, '('),
14411576
rparen: ($) => alias($._rparen, ')'),

0 commit comments

Comments
 (0)