Skip to content

Add bar position to case #7407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

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

#### :house: Internal

- AST: Add bar location to `case`. https://github.com/rescript-lang/rescript/pull/7407

# 12.0.0-alpha.12

#### :bug: Bug fix
Expand Down
3 changes: 2 additions & 1 deletion compiler/frontend/bs_ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,9 @@ let default_mapper =
~attrs:(this.attributes this pld_attributes));
cases = (fun this l -> List.map (this.case this) l);
case =
(fun this {pc_lhs; pc_guard; pc_rhs} ->
(fun this {pc_bar; pc_lhs; pc_guard; pc_rhs} ->
{
pc_bar;
pc_lhs = this.pat this pc_lhs;
pc_guard = map_opt (this.expr this) pc_guard;
pc_rhs = this.expr this pc_rhs;
Expand Down
4 changes: 3 additions & 1 deletion compiler/frontend/bs_builtin_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
{
e with
pexp_desc =
Pexp_match (pvb_expr, [{pc_lhs = p; pc_guard = None; pc_rhs = body}]);
Pexp_match
( pvb_expr,
[{pc_bar = None; pc_lhs = p; pc_guard = None; pc_rhs = body}] );
pexp_attributes = e.pexp_attributes @ pvb_attributes;
})
(* let [@warning "a"] {a;b} = c in body
Expand Down
3 changes: 2 additions & 1 deletion compiler/ml/ast_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ module Exp = struct
jsx_container_element_closing_tag = e;
}))

let case lhs ?guard rhs = {pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs}
let case ?bar lhs ?guard rhs =
{pc_bar = bar; pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs}

let make_list_expression loc seq ext_opt =
let rec handle_seq = function
Expand Down
3 changes: 2 additions & 1 deletion compiler/ml/ast_helper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ module Exp : sig
Parsetree.jsx_closing_container_tag option ->
expression

val case : pattern -> ?guard:expression -> expression -> case
val case :
?bar:Lexing.position -> pattern -> ?guard:expression -> expression -> case
val await : ?loc:loc -> ?attrs:attrs -> expression -> expression

val make_list_expression :
Expand Down
3 changes: 2 additions & 1 deletion compiler/ml/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,9 @@ let default_mapper =
~attrs:(this.attributes this pld_attributes));
cases = (fun this l -> List.map (this.case this) l);
case =
(fun this {pc_lhs; pc_guard; pc_rhs} ->
(fun this {pc_bar; pc_lhs; pc_guard; pc_rhs} ->
{
pc_bar;
pc_lhs = this.pat this pc_lhs;
pc_guard = map_opt (this.expr this) pc_guard;
pc_rhs = this.expr this pc_rhs;
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/ast_mapper_from0.ml
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ let default_mapper =
case =
(fun this {pc_lhs; pc_guard; pc_rhs} ->
{
pc_bar = None;
pc_lhs = this.pat this pc_lhs;
pc_guard = map_opt (this.expr this) pc_guard;
pc_rhs = this.expr this pc_rhs;
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/parsetree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ and jsx_closing_container_tag = {

and case = {
(* (P -> E) or (P when E0 -> E) *)
pc_bar: Lexing.position option;
pc_lhs: pattern;
pc_guard: expression option;
pc_rhs: expression;
Expand Down
4 changes: 3 additions & 1 deletion compiler/ml/printast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,10 @@ and longident_x_pattern i ppf (li, p, opt) =
line i ppf "%a%s\n" fmt_longident_loc li (if opt then "?" else "");
pattern (i + 1) ppf p

and case i ppf {pc_lhs; pc_guard; pc_rhs} =
and case i ppf {pc_bar; pc_lhs; pc_guard; pc_rhs} =
line i ppf "<case>\n";
pc_bar
|> Option.iter (fun bar -> line i ppf "| %a\n" (fmt_position false) bar);
pattern (i + 1) ppf pc_lhs;
(match pc_guard with
| None -> ()
Expand Down
3 changes: 2 additions & 1 deletion compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3543,6 +3543,7 @@ and parse_pattern_match_case p =
Parser.leave_breadcrumb p Grammar.PatternMatchCase;
match p.Parser.token with
| Token.Bar ->
let bar = p.start_pos in
Parser.next p;
Parser.leave_breadcrumb p Grammar.Pattern;
let lhs = parse_pattern p in
Expand All @@ -3556,7 +3557,7 @@ and parse_pattern_match_case p =
let rhs = parse_expr_block p in
Parser.end_region p;
Parser.eat_breadcrumb p;
Some (Ast_helper.Exp.case lhs ?guard rhs)
Some (Ast_helper.Exp.case ~bar lhs ?guard rhs)
| _ ->
Parser.end_region p;
Parser.eat_breadcrumb p;
Expand Down