Skip to content

Commit c610a12

Browse files
authored
Add bar position to case (#7407)
* Add bar position to case * Add changelog entry
1 parent 974674e commit c610a12

File tree

10 files changed

+22
-7
lines changed

10 files changed

+22
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
- Fix broken `bstracing` CLI location. https://github.com/rescript-lang/rescript/pull/7398
2222

23+
#### :house: Internal
24+
25+
- AST: Add bar location to `case`. https://github.com/rescript-lang/rescript/pull/7407
26+
2327
# 12.0.0-alpha.12
2428

2529
#### :bug: Bug fix

compiler/frontend/bs_ast_mapper.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,9 @@ let default_mapper =
539539
~attrs:(this.attributes this pld_attributes));
540540
cases = (fun this l -> List.map (this.case this) l);
541541
case =
542-
(fun this {pc_lhs; pc_guard; pc_rhs} ->
542+
(fun this {pc_bar; pc_lhs; pc_guard; pc_rhs} ->
543543
{
544+
pc_bar;
544545
pc_lhs = this.pat this pc_lhs;
545546
pc_guard = map_opt (this.expr this) pc_guard;
546547
pc_rhs = this.expr this pc_rhs;

compiler/frontend/bs_builtin_ppx.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
165165
{
166166
e with
167167
pexp_desc =
168-
Pexp_match (pvb_expr, [{pc_lhs = p; pc_guard = None; pc_rhs = body}]);
168+
Pexp_match
169+
( pvb_expr,
170+
[{pc_bar = None; pc_lhs = p; pc_guard = None; pc_rhs = body}] );
169171
pexp_attributes = e.pexp_attributes @ pvb_attributes;
170172
})
171173
(* let [@warning "a"] {a;b} = c in body

compiler/ml/ast_helper.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ module Exp = struct
208208
jsx_container_element_closing_tag = e;
209209
}))
210210

211-
let case lhs ?guard rhs = {pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs}
211+
let case ?bar lhs ?guard rhs =
212+
{pc_bar = bar; pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs}
212213

213214
let make_list_expression loc seq ext_opt =
214215
let rec handle_seq = function

compiler/ml/ast_helper.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ module Exp : sig
231231
Parsetree.jsx_closing_container_tag option ->
232232
expression
233233

234-
val case : pattern -> ?guard:expression -> expression -> case
234+
val case :
235+
?bar:Lexing.position -> pattern -> ?guard:expression -> expression -> case
235236
val await : ?loc:loc -> ?attrs:attrs -> expression -> expression
236237

237238
val make_list_expression :

compiler/ml/ast_mapper.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,9 @@ let default_mapper =
488488
~attrs:(this.attributes this pld_attributes));
489489
cases = (fun this l -> List.map (this.case this) l);
490490
case =
491-
(fun this {pc_lhs; pc_guard; pc_rhs} ->
491+
(fun this {pc_bar; pc_lhs; pc_guard; pc_rhs} ->
492492
{
493+
pc_bar;
493494
pc_lhs = this.pat this pc_lhs;
494495
pc_guard = map_opt (this.expr this) pc_guard;
495496
pc_rhs = this.expr this pc_rhs;

compiler/ml/ast_mapper_from0.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ let default_mapper =
665665
case =
666666
(fun this {pc_lhs; pc_guard; pc_rhs} ->
667667
{
668+
pc_bar = None;
668669
pc_lhs = this.pat this pc_lhs;
669670
pc_guard = map_opt (this.expr this) pc_guard;
670671
pc_rhs = this.expr this pc_rhs;

compiler/ml/parsetree.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ and jsx_closing_container_tag = {
381381

382382
and case = {
383383
(* (P -> E) or (P when E0 -> E) *)
384+
pc_bar: Lexing.position option;
384385
pc_lhs: pattern;
385386
pc_guard: expression option;
386387
pc_rhs: expression;

compiler/ml/printast.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,10 @@ and longident_x_pattern i ppf (li, p, opt) =
681681
line i ppf "%a%s\n" fmt_longident_loc li (if opt then "?" else "");
682682
pattern (i + 1) ppf p
683683

684-
and case i ppf {pc_lhs; pc_guard; pc_rhs} =
684+
and case i ppf {pc_bar; pc_lhs; pc_guard; pc_rhs} =
685685
line i ppf "<case>\n";
686+
pc_bar
687+
|> Option.iter (fun bar -> line i ppf "| %a\n" (fmt_position false) bar);
686688
pattern (i + 1) ppf pc_lhs;
687689
(match pc_guard with
688690
| None -> ()

compiler/syntax/src/res_core.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3543,6 +3543,7 @@ and parse_pattern_match_case p =
35433543
Parser.leave_breadcrumb p Grammar.PatternMatchCase;
35443544
match p.Parser.token with
35453545
| Token.Bar ->
3546+
let bar = p.start_pos in
35463547
Parser.next p;
35473548
Parser.leave_breadcrumb p Grammar.Pattern;
35483549
let lhs = parse_pattern p in
@@ -3556,7 +3557,7 @@ and parse_pattern_match_case p =
35563557
let rhs = parse_expr_block p in
35573558
Parser.end_region p;
35583559
Parser.eat_breadcrumb p;
3559-
Some (Ast_helper.Exp.case lhs ?guard rhs)
3560+
Some (Ast_helper.Exp.case ~bar lhs ?guard rhs)
35603561
| _ ->
35613562
Parser.end_region p;
35623563
Parser.eat_breadcrumb p;

0 commit comments

Comments
 (0)