@@ -10,6 +10,10 @@ module Parser = Res_parser
10
10
let mk_loc start_loc end_loc =
11
11
Location. {loc_start = start_loc; loc_end = end_loc; loc_ghost = false }
12
12
13
+ type inline_types_context = {
14
+ mutable found_inline_types : (string * Warnings .loc * Parsetree .type_kind ) list ;
15
+ }
16
+
13
17
module Recover = struct
14
18
let default_expr () =
15
19
let id = Location. mknoloc " rescript.exprhole" in
@@ -3978,7 +3982,7 @@ and parse_array_exp p =
3978
3982
3979
3983
(* TODO: check attributes in the case of poly type vars,
3980
3984
* might be context dependend: parseFieldDeclaration (see ocaml) *)
3981
- and parse_poly_type_expr ?current_type_name_path ?inline_types p =
3985
+ and parse_poly_type_expr ?current_type_name_path ?inline_types_context p =
3982
3986
let start_pos = p.Parser. start_pos in
3983
3987
match p.Parser. token with
3984
3988
| SingleQuote -> (
@@ -4004,7 +4008,7 @@ and parse_poly_type_expr ?current_type_name_path ?inline_types p =
4004
4008
Ast_helper.Typ. arrow ~loc ~arity: (Some 1 ) Nolabel typ return_type
4005
4009
| _ -> Ast_helper.Typ. var ~loc: var.loc var.txt)
4006
4010
| _ -> assert false )
4007
- | _ -> parse_typ_expr ?current_type_name_path ?inline_types p
4011
+ | _ -> parse_typ_expr ?current_type_name_path ?inline_types_context p
4008
4012
4009
4013
(* 'a 'b 'c *)
4010
4014
and parse_type_var_list p =
@@ -4032,7 +4036,8 @@ and parse_lident_list p =
4032
4036
in
4033
4037
loop p []
4034
4038
4035
- and parse_atomic_typ_expr ?current_type_name_path ?inline_types ~attrs p =
4039
+ and parse_atomic_typ_expr ?current_type_name_path ?inline_types_context ~attrs p
4040
+ =
4036
4041
Parser. leave_breadcrumb p Grammar. AtomicTypExpr ;
4037
4042
let start_pos = p.Parser. start_pos in
4038
4043
let typ =
@@ -4076,14 +4081,14 @@ and parse_atomic_typ_expr ?current_type_name_path ?inline_types ~attrs p =
4076
4081
| Uident _ | Lident _ ->
4077
4082
let constr = parse_value_path p in
4078
4083
let args =
4079
- parse_type_constructor_args ?inline_types ?current_type_name_path
4080
- ~constr_name: constr p
4084
+ parse_type_constructor_args ?inline_types_context
4085
+ ?current_type_name_path ~constr_name: constr p
4081
4086
in
4082
4087
let number_of_inline_records_in_args =
4083
- match inline_types with
4088
+ match inline_types_context with
4084
4089
| None -> 0
4085
- | Some inline_types ->
4086
- let inline_types = ! inline_types in
4090
+ | Some inline_types_context ->
4091
+ let inline_types = inline_types_context.found_inline_types in
4087
4092
args
4088
4093
|> List. filter (fun (c : Parsetree.core_type ) ->
4089
4094
match c.ptyp_desc with
@@ -4111,7 +4116,8 @@ and parse_atomic_typ_expr ?current_type_name_path ?inline_types ~attrs p =
4111
4116
let loc = mk_loc start_pos p.prev_end_pos in
4112
4117
Ast_helper.Typ. extension ~attrs ~loc extension
4113
4118
| Lbrace ->
4114
- parse_record_or_object_type ?current_type_name_path ?inline_types ~attrs p
4119
+ parse_record_or_object_type ?current_type_name_path ?inline_types_context
4120
+ ~attrs p
4115
4121
| Eof ->
4116
4122
Parser. err p (Diagnostics. unexpected p.Parser. token p.breadcrumbs);
4117
4123
Recover. default_type ()
@@ -4173,7 +4179,8 @@ and parse_package_constraint p =
4173
4179
Some (type_constr, typ)
4174
4180
| _ -> None
4175
4181
4176
- and parse_record_or_object_type ?current_type_name_path ?inline_types ~attrs p =
4182
+ and parse_record_or_object_type ?current_type_name_path ?inline_types_context
4183
+ ~attrs p =
4177
4184
(* for inline record in constructor *)
4178
4185
let start_pos = p.Parser. start_pos in
4179
4186
Parser. expect Lbrace p;
@@ -4187,19 +4194,22 @@ and parse_record_or_object_type ?current_type_name_path ?inline_types ~attrs p =
4187
4194
Asttypes. Closed
4188
4195
| _ -> Asttypes. Closed
4189
4196
in
4190
- match (p.token, inline_types , current_type_name_path) with
4191
- | Lident _ , Some inline_types , Some current_type_name_path ->
4197
+ match (p.token, inline_types_context , current_type_name_path) with
4198
+ | Lident _ , Some inline_types_context , Some current_type_name_path ->
4192
4199
let labels =
4193
4200
parse_comma_delimited_region ~grammar: Grammar. RecordDecl ~closing: Rbrace
4194
4201
~f:
4195
- (parse_field_declaration_region ~current_type_name_path ~inline_types )
4202
+ (parse_field_declaration_region ~current_type_name_path
4203
+ ~inline_types_context )
4196
4204
p
4197
4205
in
4198
4206
Parser. expect Rbrace p;
4199
4207
let loc = mk_loc start_pos p.prev_end_pos in
4200
4208
let inline_type_name = current_type_name_path |> String. concat " ." in
4201
- inline_types :=
4202
- (inline_type_name, loc, Parsetree. Ptype_record labels) :: ! inline_types;
4209
+
4210
+ inline_types_context.found_inline_types < -
4211
+ (inline_type_name, loc, Parsetree. Ptype_record labels)
4212
+ :: inline_types_context.found_inline_types;
4203
4213
4204
4214
let lid = Location. mkloc (Longident. Lident inline_type_name) loc in
4205
4215
Ast_helper.Typ. constr ~loc lid []
@@ -4417,7 +4427,7 @@ and parse_es6_arrow_type ~attrs p =
4417
4427
* | uident.lident
4418
4428
* | uident.uident.lident --> long module path
4419
4429
*)
4420
- and parse_typ_expr ?current_type_name_path ?inline_types ?attrs
4430
+ and parse_typ_expr ?current_type_name_path ?inline_types_context ?attrs
4421
4431
?(es6_arrow = true ) ?(alias = true ) p =
4422
4432
(* Parser.leaveBreadcrumb p Grammar.TypeExpression; *)
4423
4433
let start_pos = p.Parser. start_pos in
@@ -4430,7 +4440,8 @@ and parse_typ_expr ?current_type_name_path ?inline_types ?attrs
4430
4440
if es6_arrow && is_es6_arrow_type p then parse_es6_arrow_type ~attrs p
4431
4441
else
4432
4442
let typ =
4433
- parse_atomic_typ_expr ?current_type_name_path ?inline_types ~attrs p
4443
+ parse_atomic_typ_expr ?current_type_name_path ?inline_types_context
4444
+ ~attrs p
4434
4445
in
4435
4446
parse_arrow_type_rest ~es6_arrow ~start_pos typ p
4436
4447
in
@@ -4470,16 +4481,18 @@ and parse_tuple_type ~attrs ~first ~start_pos p =
4470
4481
let tuple_loc = mk_loc start_pos p.prev_end_pos in
4471
4482
Ast_helper.Typ. tuple ~attrs ~loc: tuple_loc typexprs
4472
4483
4473
- and parse_type_constructor_arg_region ?inline_types ?current_type_name_path p =
4484
+ and parse_type_constructor_arg_region ?inline_types_context
4485
+ ?current_type_name_path p =
4474
4486
if Grammar. is_typ_expr_start p.Parser. token then
4475
- Some (parse_typ_expr ?inline_types ?current_type_name_path p)
4487
+ Some (parse_typ_expr ?inline_types_context ?current_type_name_path p)
4476
4488
else if p.token = LessThan then (
4477
4489
Parser. next p;
4478
- parse_type_constructor_arg_region ?inline_types ?current_type_name_path p)
4490
+ parse_type_constructor_arg_region ?inline_types_context
4491
+ ?current_type_name_path p)
4479
4492
else None
4480
4493
4481
4494
(* Js.Nullable.value<'a> *)
4482
- and parse_type_constructor_args ?inline_types ?current_type_name_path
4495
+ and parse_type_constructor_args ?inline_types_context ?current_type_name_path
4483
4496
~constr_name p =
4484
4497
let opening = p.Parser. token in
4485
4498
let opening_start_pos = p.start_pos in
@@ -4492,7 +4505,7 @@ and parse_type_constructor_args ?inline_types ?current_type_name_path
4492
4505
parse_comma_delimited_region ~grammar: Grammar. TypExprList
4493
4506
~closing: GreaterThan
4494
4507
~f:
4495
- (parse_type_constructor_arg_region ?inline_types
4508
+ (parse_type_constructor_arg_region ?inline_types_context
4496
4509
?current_type_name_path)
4497
4510
p
4498
4511
in
@@ -4578,7 +4591,7 @@ and parse_field_declaration p =
4578
4591
let loc = mk_loc start_pos typ.ptyp_loc.loc_end in
4579
4592
Ast_helper.Type. field ~attrs ~loc ~mut ~optional name typ
4580
4593
4581
- and parse_field_declaration_region ?current_type_name_path ?inline_types
4594
+ and parse_field_declaration_region ?current_type_name_path ?inline_types_context
4582
4595
?found_object_field p =
4583
4596
let start_pos = p.Parser. start_pos in
4584
4597
let attrs = parse_attributes p in
@@ -4614,7 +4627,7 @@ and parse_field_declaration_region ?current_type_name_path ?inline_types
4614
4627
match p.Parser. token with
4615
4628
| Colon ->
4616
4629
Parser. next p;
4617
- parse_poly_type_expr ?current_type_name_path ?inline_types p
4630
+ parse_poly_type_expr ?current_type_name_path ?inline_types_context p
4618
4631
| _ ->
4619
4632
Ast_helper.Typ. constr ~loc: name.loc ~attrs
4620
4633
{name with txt = Lident name.txt}
@@ -4640,12 +4653,14 @@ and parse_field_declaration_region ?current_type_name_path ?inline_types
4640
4653
* | { field-decl, field-decl }
4641
4654
* | { field-decl, field-decl, field-decl, }
4642
4655
*)
4643
- and parse_record_declaration ?current_type_name_path ?inline_types p =
4656
+ and parse_record_declaration ?current_type_name_path ?inline_types_context p =
4644
4657
Parser. leave_breadcrumb p Grammar. RecordDecl ;
4645
4658
Parser. expect Lbrace p;
4646
4659
let rows =
4647
4660
parse_comma_delimited_region ~grammar: Grammar. RecordDecl ~closing: Rbrace
4648
- ~f: (parse_field_declaration_region ?current_type_name_path ?inline_types)
4661
+ ~f:
4662
+ (parse_field_declaration_region ?current_type_name_path
4663
+ ?inline_types_context)
4649
4664
p
4650
4665
in
4651
4666
Parser. expect Rbrace p;
@@ -4889,7 +4904,7 @@ and parse_type_constructor_declarations ?first p =
4889
4904
* ∣ = private record-decl
4890
4905
* | = ..
4891
4906
*)
4892
- and parse_type_representation ?current_type_name_path ?inline_types p =
4907
+ and parse_type_representation ?current_type_name_path ?inline_types_context p =
4893
4908
Parser. leave_breadcrumb p Grammar. TypeRepresentation ;
4894
4909
(* = consumed *)
4895
4910
let private_flag =
@@ -4902,7 +4917,8 @@ and parse_type_representation ?current_type_name_path ?inline_types p =
4902
4917
Parsetree. Ptype_variant (parse_type_constructor_declarations p)
4903
4918
| Lbrace ->
4904
4919
Parsetree. Ptype_record
4905
- (parse_record_declaration ?current_type_name_path ?inline_types p)
4920
+ (parse_record_declaration ?current_type_name_path ?inline_types_context
4921
+ p)
4906
4922
| DotDot ->
4907
4923
Parser. next p;
4908
4924
Ptype_open
@@ -5093,7 +5109,8 @@ and parse_type_equation_or_constr_decl p =
5093
5109
(* TODO: is this a good idea? *)
5094
5110
(None , Asttypes. Public , Parsetree. Ptype_abstract )
5095
5111
5096
- and parse_record_or_object_decl ?current_type_name_path ?inline_types p =
5112
+ and parse_record_or_object_decl ?current_type_name_path ?inline_types_context p
5113
+ =
5097
5114
let start_pos = p.Parser. start_pos in
5098
5115
Parser. expect Lbrace p;
5099
5116
match p.Parser. token with
@@ -5151,7 +5168,7 @@ and parse_record_or_object_decl ?current_type_name_path ?inline_types p =
5151
5168
parse_comma_delimited_region ~grammar: Grammar. RecordDecl ~closing: Rbrace
5152
5169
~f:
5153
5170
(parse_field_declaration_region ?current_type_name_path
5154
- ?inline_types ~found_object_field )
5171
+ ?inline_types_context ~found_object_field )
5155
5172
p
5156
5173
in
5157
5174
Parser. expect Rbrace p;
@@ -5225,7 +5242,7 @@ and parse_record_or_object_decl ?current_type_name_path ?inline_types p =
5225
5242
~closing: Rbrace
5226
5243
~f:
5227
5244
(parse_field_declaration_region ?current_type_name_path
5228
- ?inline_types )
5245
+ ?inline_types_context )
5229
5246
p
5230
5247
| attr :: _ as attrs ->
5231
5248
let first =
@@ -5246,7 +5263,7 @@ and parse_record_or_object_decl ?current_type_name_path ?inline_types p =
5246
5263
~closing: Rbrace
5247
5264
~f:
5248
5265
(parse_field_declaration_region ?current_type_name_path
5249
- ?inline_types )
5266
+ ?inline_types_context )
5250
5267
p
5251
5268
in
5252
5269
Parser. expect Rbrace p;
@@ -5437,16 +5454,17 @@ and parse_polymorphic_variant_type_args p =
5437
5454
| [typ] -> typ
5438
5455
| types -> Ast_helper.Typ. tuple ~loc ~attrs types
5439
5456
5440
- and parse_type_equation_and_representation ?current_type_name_path ? inline_types
5441
- p =
5457
+ and parse_type_equation_and_representation ?current_type_name_path
5458
+ ? inline_types_context p =
5442
5459
match p.Parser. token with
5443
5460
| (Equal | Bar ) as token -> (
5444
5461
if token = Bar then Parser. expect Equal p;
5445
5462
Parser. next p;
5446
5463
match p.Parser. token with
5447
5464
| Uident _ -> parse_type_equation_or_constr_decl p
5448
5465
| Lbrace ->
5449
- parse_record_or_object_decl ?current_type_name_path ?inline_types p
5466
+ parse_record_or_object_decl ?current_type_name_path ?inline_types_context
5467
+ p
5450
5468
| Private -> parse_private_eq_or_repr p
5451
5469
| Bar | DotDot ->
5452
5470
let priv, kind = parse_type_representation p in
@@ -5457,7 +5475,8 @@ and parse_type_equation_and_representation ?current_type_name_path ?inline_types
5457
5475
| Equal ->
5458
5476
Parser. next p;
5459
5477
let priv, kind =
5460
- parse_type_representation ?current_type_name_path ?inline_types p
5478
+ parse_type_representation ?current_type_name_path
5479
+ ?inline_types_context p
5461
5480
in
5462
5481
(manifest, priv, kind)
5463
5482
| _ -> (manifest, Public , Parsetree. Ptype_abstract )))
@@ -5524,12 +5543,12 @@ and parse_type_extension ~params ~attrs ~name p =
5524
5543
let constructors = loop p [first] in
5525
5544
Ast_helper.Te. mk ~attrs ~params ~priv name constructors
5526
5545
5527
- and parse_type_definitions ~current_type_name_path ~inline_types ~attrs ~ name
5528
- ~params ~start_pos p =
5546
+ and parse_type_definitions ~current_type_name_path ~inline_types_context ~attrs
5547
+ ~name ~ params ~start_pos p =
5529
5548
let type_def =
5530
5549
let manifest, priv, kind =
5531
5550
parse_type_equation_and_representation ~current_type_name_path
5532
- ~inline_types p
5551
+ ~inline_types_context p
5533
5552
in
5534
5553
let cstrs = parse_type_constraints p in
5535
5554
let loc = mk_loc start_pos p.prev_end_pos in
@@ -5580,16 +5599,18 @@ and parse_type_definition_or_extension ~attrs p =
5580
5599
|> Diagnostics. message)
5581
5600
in
5582
5601
let current_type_name_path = Longident. flatten name.txt in
5583
- let inline_types = ref [] in
5602
+ let inline_types_context = {found_inline_types = [] } in
5584
5603
let type_defs =
5585
- parse_type_definitions ~inline_types ~current_type_name_path ~attrs ~name
5586
- ~params ~start_pos p
5604
+ parse_type_definitions ~inline_types_context ~current_type_name_path
5605
+ ~attrs ~name ~ params ~start_pos p
5587
5606
in
5588
5607
let rec_flag =
5589
- if List. length ! inline_types > 0 then Asttypes. Recursive else rec_flag
5608
+ if List. length inline_types_context.found_inline_types > 0 then
5609
+ Asttypes. Recursive
5610
+ else rec_flag
5590
5611
in
5591
5612
let inline_types =
5592
- ! inline_types
5613
+ inline_types_context.found_inline_types
5593
5614
|> List. map (fun (inline_type_name , loc , kind ) ->
5594
5615
Ast_helper.Type. mk
5595
5616
~attrs: [(Location. mknoloc " res.inlineRecordDefinition" , PStr [] )]
0 commit comments