Skip to content

Commit 95391e5

Browse files
committed
refactor inline types context
1 parent c33f978 commit 95391e5

File tree

1 file changed

+65
-44
lines changed

1 file changed

+65
-44
lines changed

compiler/syntax/src/res_core.ml

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ module Parser = Res_parser
1010
let mk_loc start_loc end_loc =
1111
Location.{loc_start = start_loc; loc_end = end_loc; loc_ghost = false}
1212

13+
type inline_types_context = {
14+
mutable found_inline_types: (string * Warnings.loc * Parsetree.type_kind) list;
15+
}
16+
1317
module Recover = struct
1418
let default_expr () =
1519
let id = Location.mknoloc "rescript.exprhole" in
@@ -3978,7 +3982,7 @@ and parse_array_exp p =
39783982

39793983
(* TODO: check attributes in the case of poly type vars,
39803984
* 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 =
39823986
let start_pos = p.Parser.start_pos in
39833987
match p.Parser.token with
39843988
| SingleQuote -> (
@@ -4004,7 +4008,7 @@ and parse_poly_type_expr ?current_type_name_path ?inline_types p =
40044008
Ast_helper.Typ.arrow ~loc ~arity:(Some 1) Nolabel typ return_type
40054009
| _ -> Ast_helper.Typ.var ~loc:var.loc var.txt)
40064010
| _ -> 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
40084012

40094013
(* 'a 'b 'c *)
40104014
and parse_type_var_list p =
@@ -4032,7 +4036,8 @@ and parse_lident_list p =
40324036
in
40334037
loop p []
40344038

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+
=
40364041
Parser.leave_breadcrumb p Grammar.AtomicTypExpr;
40374042
let start_pos = p.Parser.start_pos in
40384043
let typ =
@@ -4076,14 +4081,14 @@ and parse_atomic_typ_expr ?current_type_name_path ?inline_types ~attrs p =
40764081
| Uident _ | Lident _ ->
40774082
let constr = parse_value_path p in
40784083
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
40814086
in
40824087
let number_of_inline_records_in_args =
4083-
match inline_types with
4088+
match inline_types_context with
40844089
| 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
40874092
args
40884093
|> List.filter (fun (c : Parsetree.core_type) ->
40894094
match c.ptyp_desc with
@@ -4111,7 +4116,8 @@ and parse_atomic_typ_expr ?current_type_name_path ?inline_types ~attrs p =
41114116
let loc = mk_loc start_pos p.prev_end_pos in
41124117
Ast_helper.Typ.extension ~attrs ~loc extension
41134118
| 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
41154121
| Eof ->
41164122
Parser.err p (Diagnostics.unexpected p.Parser.token p.breadcrumbs);
41174123
Recover.default_type ()
@@ -4173,7 +4179,8 @@ and parse_package_constraint p =
41734179
Some (type_constr, typ)
41744180
| _ -> None
41754181

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 =
41774184
(* for inline record in constructor *)
41784185
let start_pos = p.Parser.start_pos in
41794186
Parser.expect Lbrace p;
@@ -4187,19 +4194,22 @@ and parse_record_or_object_type ?current_type_name_path ?inline_types ~attrs p =
41874194
Asttypes.Closed
41884195
| _ -> Asttypes.Closed
41894196
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 ->
41924199
let labels =
41934200
parse_comma_delimited_region ~grammar:Grammar.RecordDecl ~closing:Rbrace
41944201
~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)
41964204
p
41974205
in
41984206
Parser.expect Rbrace p;
41994207
let loc = mk_loc start_pos p.prev_end_pos in
42004208
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;
42034213

42044214
let lid = Location.mkloc (Longident.Lident inline_type_name) loc in
42054215
Ast_helper.Typ.constr ~loc lid []
@@ -4417,7 +4427,7 @@ and parse_es6_arrow_type ~attrs p =
44174427
* | uident.lident
44184428
* | uident.uident.lident --> long module path
44194429
*)
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
44214431
?(es6_arrow = true) ?(alias = true) p =
44224432
(* Parser.leaveBreadcrumb p Grammar.TypeExpression; *)
44234433
let start_pos = p.Parser.start_pos in
@@ -4430,7 +4440,8 @@ and parse_typ_expr ?current_type_name_path ?inline_types ?attrs
44304440
if es6_arrow && is_es6_arrow_type p then parse_es6_arrow_type ~attrs p
44314441
else
44324442
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
44344445
in
44354446
parse_arrow_type_rest ~es6_arrow ~start_pos typ p
44364447
in
@@ -4470,16 +4481,18 @@ and parse_tuple_type ~attrs ~first ~start_pos p =
44704481
let tuple_loc = mk_loc start_pos p.prev_end_pos in
44714482
Ast_helper.Typ.tuple ~attrs ~loc:tuple_loc typexprs
44724483

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 =
44744486
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)
44764488
else if p.token = LessThan then (
44774489
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)
44794492
else None
44804493

44814494
(* 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
44834496
~constr_name p =
44844497
let opening = p.Parser.token in
44854498
let opening_start_pos = p.start_pos in
@@ -4492,7 +4505,7 @@ and parse_type_constructor_args ?inline_types ?current_type_name_path
44924505
parse_comma_delimited_region ~grammar:Grammar.TypExprList
44934506
~closing:GreaterThan
44944507
~f:
4495-
(parse_type_constructor_arg_region ?inline_types
4508+
(parse_type_constructor_arg_region ?inline_types_context
44964509
?current_type_name_path)
44974510
p
44984511
in
@@ -4578,7 +4591,7 @@ and parse_field_declaration p =
45784591
let loc = mk_loc start_pos typ.ptyp_loc.loc_end in
45794592
Ast_helper.Type.field ~attrs ~loc ~mut ~optional name typ
45804593

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
45824595
?found_object_field p =
45834596
let start_pos = p.Parser.start_pos in
45844597
let attrs = parse_attributes p in
@@ -4614,7 +4627,7 @@ and parse_field_declaration_region ?current_type_name_path ?inline_types
46144627
match p.Parser.token with
46154628
| Colon ->
46164629
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
46184631
| _ ->
46194632
Ast_helper.Typ.constr ~loc:name.loc ~attrs
46204633
{name with txt = Lident name.txt}
@@ -4640,12 +4653,14 @@ and parse_field_declaration_region ?current_type_name_path ?inline_types
46404653
* | { field-decl, field-decl }
46414654
* | { field-decl, field-decl, field-decl, }
46424655
*)
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 =
46444657
Parser.leave_breadcrumb p Grammar.RecordDecl;
46454658
Parser.expect Lbrace p;
46464659
let rows =
46474660
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)
46494664
p
46504665
in
46514666
Parser.expect Rbrace p;
@@ -4889,7 +4904,7 @@ and parse_type_constructor_declarations ?first p =
48894904
* ∣ = private record-decl
48904905
* | = ..
48914906
*)
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 =
48934908
Parser.leave_breadcrumb p Grammar.TypeRepresentation;
48944909
(* = consumed *)
48954910
let private_flag =
@@ -4902,7 +4917,8 @@ and parse_type_representation ?current_type_name_path ?inline_types p =
49024917
Parsetree.Ptype_variant (parse_type_constructor_declarations p)
49034918
| Lbrace ->
49044919
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)
49064922
| DotDot ->
49074923
Parser.next p;
49084924
Ptype_open
@@ -5093,7 +5109,8 @@ and parse_type_equation_or_constr_decl p =
50935109
(* TODO: is this a good idea? *)
50945110
(None, Asttypes.Public, Parsetree.Ptype_abstract)
50955111

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+
=
50975114
let start_pos = p.Parser.start_pos in
50985115
Parser.expect Lbrace p;
50995116
match p.Parser.token with
@@ -5151,7 +5168,7 @@ and parse_record_or_object_decl ?current_type_name_path ?inline_types p =
51515168
parse_comma_delimited_region ~grammar:Grammar.RecordDecl ~closing:Rbrace
51525169
~f:
51535170
(parse_field_declaration_region ?current_type_name_path
5154-
?inline_types ~found_object_field)
5171+
?inline_types_context ~found_object_field)
51555172
p
51565173
in
51575174
Parser.expect Rbrace p;
@@ -5225,7 +5242,7 @@ and parse_record_or_object_decl ?current_type_name_path ?inline_types p =
52255242
~closing:Rbrace
52265243
~f:
52275244
(parse_field_declaration_region ?current_type_name_path
5228-
?inline_types)
5245+
?inline_types_context)
52295246
p
52305247
| attr :: _ as attrs ->
52315248
let first =
@@ -5246,7 +5263,7 @@ and parse_record_or_object_decl ?current_type_name_path ?inline_types p =
52465263
~closing:Rbrace
52475264
~f:
52485265
(parse_field_declaration_region ?current_type_name_path
5249-
?inline_types)
5266+
?inline_types_context)
52505267
p
52515268
in
52525269
Parser.expect Rbrace p;
@@ -5437,16 +5454,17 @@ and parse_polymorphic_variant_type_args p =
54375454
| [typ] -> typ
54385455
| types -> Ast_helper.Typ.tuple ~loc ~attrs types
54395456

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 =
54425459
match p.Parser.token with
54435460
| (Equal | Bar) as token -> (
54445461
if token = Bar then Parser.expect Equal p;
54455462
Parser.next p;
54465463
match p.Parser.token with
54475464
| Uident _ -> parse_type_equation_or_constr_decl p
54485465
| 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
54505468
| Private -> parse_private_eq_or_repr p
54515469
| Bar | DotDot ->
54525470
let priv, kind = parse_type_representation p in
@@ -5457,7 +5475,8 @@ and parse_type_equation_and_representation ?current_type_name_path ?inline_types
54575475
| Equal ->
54585476
Parser.next p;
54595477
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
54615480
in
54625481
(manifest, priv, kind)
54635482
| _ -> (manifest, Public, Parsetree.Ptype_abstract)))
@@ -5524,12 +5543,12 @@ and parse_type_extension ~params ~attrs ~name p =
55245543
let constructors = loop p [first] in
55255544
Ast_helper.Te.mk ~attrs ~params ~priv name constructors
55265545

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 =
55295548
let type_def =
55305549
let manifest, priv, kind =
55315550
parse_type_equation_and_representation ~current_type_name_path
5532-
~inline_types p
5551+
~inline_types_context p
55335552
in
55345553
let cstrs = parse_type_constraints p in
55355554
let loc = mk_loc start_pos p.prev_end_pos in
@@ -5580,16 +5599,18 @@ and parse_type_definition_or_extension ~attrs p =
55805599
|> Diagnostics.message)
55815600
in
55825601
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
55845603
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
55875606
in
55885607
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
55905611
in
55915612
let inline_types =
5592-
!inline_types
5613+
inline_types_context.found_inline_types
55935614
|> List.map (fun (inline_type_name, loc, kind) ->
55945615
Ast_helper.Type.mk
55955616
~attrs:[(Location.mknoloc "res.inlineRecordDefinition", PStr [])]

0 commit comments

Comments
 (0)