diff --git a/formatTest/unit_tests/expected_output/infix.re b/formatTest/unit_tests/expected_output/infix.re index 839145b60..17af535ea 100644 --- a/formatTest/unit_tests/expected_output/infix.re +++ b/formatTest/unit_tests/expected_output/infix.re @@ -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; diff --git a/formatTest/unit_tests/input/infix.re b/formatTest/unit_tests/input/infix.re index 8ee32f76b..df126fd54 100644 --- a/formatTest/unit_tests/input/infix.re +++ b/formatTest/unit_tests/input/infix.re @@ -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; diff --git a/src/reason-parser/reason_lexer.mll b/src/reason-parser/reason_lexer.mll index d7fb9ef6d..9c26e411c 100644 --- a/src/reason-parser/reason_lexer.mll +++ b/src/reason-parser/reason_lexer.mll @@ -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* diff --git a/src/reason-parser/reason_syntax_util.ml b/src/reason-parser/reason_syntax_util.ml index 2d4456d1e..390277167 100644 --- a/src/reason-parser/reason_syntax_util.ml +++ b/src/reason-parser/reason_syntax_util.ml @@ -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 *) @@ -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 = diff --git a/src/reason-parser/reason_syntax_util.mli b/src/reason-parser/reason_syntax_util.mli index 9f289d17c..f9fb43b05 100644 --- a/src/reason-parser/reason_syntax_util.mli +++ b/src/reason-parser/reason_syntax_util.mli @@ -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 : diff --git a/src/reason-parser/reason_toolchain.ml b/src/reason-parser/reason_toolchain.ml index f60b9fc4e..3e25d0271 100644 --- a/src/reason-parser/reason_toolchain.ml +++ b/src/reason-parser/reason_toolchain.ml @@ -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) diff --git a/src/refmt/reason_implementation_printer.ml b/src/refmt/reason_implementation_printer.ml index 966396aa7..6b6dc99b1 100644 --- a/src/refmt/reason_implementation_printer.ml +++ b/src/refmt/reason_implementation_printer.ml @@ -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),