Skip to content
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

Missing => infix operator escape #1944

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions formatTest/unit_tests/expected_output/infix.re
Original file line number Diff line number Diff line change
Expand Up @@ -1311,3 +1311,10 @@ let not = x => !x;
let other = x => not(x);

let derefInsideArray = [|a^|];

/* #1941: infix `=>` */
let (\=>) = (a, b) => a + b;

let x = a \=> b;

let (=>>) = (a, b) => a + b;
7 changes: 7 additions & 0 deletions formatTest/unit_tests/input/infix.re
Original file line number Diff line number Diff line change
Expand Up @@ -1004,3 +1004,10 @@ let not = (x) => !x;
let other = (x) => not(x);

let derefInsideArray = [|a^|];

/* #1941: infix `=>` */
let (\=>) = (a, b) => a + b;

let x = a \=> b;

let (=>>) = (a, b) => a + b;
4 changes: 1 addition & 3 deletions src/reason-parser/reason_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,7 @@ rule token = parse
| '\\'? ['~' '?' '!'] operator_chars+
{ PREFIXOP(lexeme_operator lexbuf) }
| '\\'? ['=' '<' '>' '|' '&' '$'] operator_chars*
{
INFIXOP0(lexeme_operator lexbuf)
}
{ INFIXOP0(lexeme_operator lexbuf) }
| '\\'? '@' operator_chars*
{ INFIXOP1(lexeme_operator lexbuf) }
| '\\'? '^' ('\\' '.')? operator_chars*
Expand Down
7 changes: 7 additions & 0 deletions src/reason-parser/reason_syntax_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ let escape_string str =
) str;
Buffer.contents buf

let remove_backslashes_for_ml s =
if s.[0] == '\\' then
String.sub s 1 (String.length s - 1)
else s

(* the stuff below contains side-effects and are not used by BuckleScript's
vendored version of reason_syntax_util.ml. So we can neglect it *)

Expand Down Expand Up @@ -393,6 +398,8 @@ let remove_literal_attrs_mapper_maker super =
let remove_literal_attrs_mapper =
remove_literal_attrs_mapper_maker Ast_mapper.default_mapper

let remove_backslashes_for_ml_mapper = identifier_mapper remove_backslashes_for_ml

(** escape_stars_slashes_mapper escapes all stars and slashes in an AST *)
let escape_stars_slashes_mapper =
let escape_stars_slashes str =
Expand Down
3 changes: 3 additions & 0 deletions src/reason-parser/reason_syntax_util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ val reason_to_ml_swap_operator_mapper :
val ml_to_reason_swap_operator_mapper :
Ast_404.Ast_mapper.mapper -> Ast_404.Ast_mapper.mapper

val remove_backslashes_for_ml_mapper :
Ast_404.Ast_mapper.mapper -> Ast_404.Ast_mapper.mapper

val attribute_exists : 'a -> ('a Ast_404.Asttypes.loc * 'b) list -> bool

val attributes_conflicted :
Expand Down
4 changes: 3 additions & 1 deletion src/reason-parser/reason_toolchain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,10 @@ module OCaml_syntax = struct
Pprintast.signature formatter
(To_current.copy_signature signature)
let format_implementation_with_comments (structure, _) formatter =
let mapper = Reason_syntax_util.(remove_literal_attrs_mapper |> remove_backslashes_for_ml_mapper)
in
let structure =
Reason_syntax_util.(apply_mapper_to_structure structure remove_literal_attrs_mapper)
Reason_syntax_util.(apply_mapper_to_structure structure mapper)
in
Pprintast.structure formatter
(To_current.copy_structure structure)
Expand Down
4 changes: 3 additions & 1 deletion src/refmt/reason_implementation_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ let print printtype filename parsedAsML output_chan output_formatter =
);
)
| `Binary -> fun (ast, _) ->
let mapper = Reason_syntax_util.(remove_literal_attrs_mapper |> remove_backslashes_for_ml_mapper)
in
let ast =
Reason_syntax_util.(apply_mapper_to_structure ast remove_literal_attrs_mapper)
Reason_syntax_util.(apply_mapper_to_structure ast mapper)
in
Ast_io.to_channel output_chan filename
(Ast_io.Impl ((module OCaml_current),
Expand Down