From c4706c2c2df9e8be251176e9d51006a287c346e9 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Tue, 14 Mar 2023 09:24:43 +0100 Subject: [PATCH] Add support for `@@infix.add` and `@@infix.remove`. Add support for `@@infix.add` and `@@infix.remove`. --- res_syntax/src/res_core.ml | 11 +++++++---- res_syntax/src/res_custom_infix.ml | 2 ++ res_syntax/src/res_printer.ml | 12 ++++++++---- .../tests/printer/expr/expected/infixAdd.res.txt | 7 +++++++ .../tests/printer/expr/expected/infixRemove.res.txt | 7 +++++++ .../tests/printer/expr/expected/infixScope.res.txt | 7 +++++++ res_syntax/tests/printer/expr/infixAdd.res | 7 +++++++ res_syntax/tests/printer/expr/infixRemove.res | 7 +++++++ res_syntax/tests/printer/expr/infixScope.res | 8 ++++++++ 9 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 res_syntax/tests/printer/expr/expected/infixAdd.res.txt create mode 100644 res_syntax/tests/printer/expr/expected/infixRemove.res.txt create mode 100644 res_syntax/tests/printer/expr/expected/infixScope.res.txt create mode 100644 res_syntax/tests/printer/expr/infixAdd.res create mode 100644 res_syntax/tests/printer/expr/infixRemove.res create mode 100644 res_syntax/tests/printer/expr/infixScope.res diff --git a/res_syntax/src/res_core.ml b/res_syntax/src/res_core.ml index 1ec8050ab7e..221d4795274 100644 --- a/res_syntax/src/res_core.ml +++ b/res_syntax/src/res_core.ml @@ -6420,7 +6420,7 @@ and parseStandaloneAttribute p = p.uncurried_config <- Res_uncurried.Default; attrId | "toUncurried" -> {attrId with txt = "uncurried"} - | "infix" -> ( + | "infix" | "infix.add" | "infix.remove" -> ( match payload with | PStr [ @@ -6441,9 +6441,12 @@ and parseStandaloneAttribute p = _ ); }; ] -> - p.scanner.customInfix <- - p.scanner.customInfix |> Res_custom_infix.addSymbol ~name ~alias; - attrId + if attrId.txt <> "infix.add" then + p.scanner.customInfix <- + p.scanner.customInfix |> Res_custom_infix.addSymbol ~name ~alias; + + if attrId.txt = "infix.remove" then attrId + else {attrId with txt = "infix"} | _ -> Parser.err ~startPos ~endPos:p.startPos p (Diagnostics.message diff --git a/res_syntax/src/res_custom_infix.ml b/res_syntax/src/res_custom_infix.ml index e6cf346324c..8d116269c92 100644 --- a/res_syntax/src/res_custom_infix.ml +++ b/res_syntax/src/res_custom_infix.ml @@ -7,6 +7,8 @@ let addSymbol ~name ~alias x = let findAlias ~alias x = x |> List.find_opt (fun (_, a) -> alias = a) +let removeName ~name x = x |> List.filter (fun (n, _) -> n <> name) + let lookupName ~name ~src ~srcLen ~offset = let nameLen = String.length name in let restLen = srcLen - offset in diff --git a/res_syntax/src/res_printer.ml b/res_syntax/src/res_printer.ml index 9c8fb7367a9..9c95ff5243e 100644 --- a/res_syntax/src/res_printer.ml +++ b/res_syntax/src/res_printer.ml @@ -5279,7 +5279,7 @@ and printAttribute ?(standalone = false) ~state | "toUncurried" -> state.uncurried_config <- Res_uncurried.Default; {id with txt = "uncurried"} - | "infix" -> ( + | "infix" | "infix.add" | "infix.remove" -> ( match payload with | PStr [ @@ -5303,9 +5303,13 @@ and printAttribute ?(standalone = false) ~state _ ); }; ] -> - state.customInfix <- - state.customInfix |> Res_custom_infix.addSymbol ~name ~alias; - id + if id.txt = "infix.remove" then + state.customInfix <- + state.customInfix |> Res_custom_infix.removeName ~name + else + state.customInfix <- + state.customInfix |> Res_custom_infix.addSymbol ~name ~alias; + if id.txt = "infix.add" then {id with txt = "infix"} else id | _ -> (* should never happpen *) id) | _ -> id in diff --git a/res_syntax/tests/printer/expr/expected/infixAdd.res.txt b/res_syntax/tests/printer/expr/expected/infixAdd.res.txt new file mode 100644 index 00000000000..f5dea22dced --- /dev/null +++ b/res_syntax/tests/printer/expr/expected/infixAdd.res.txt @@ -0,0 +1,7 @@ +let plus = (x, y) => x + y +let minus = (x, y) => x - y + +@@infix(("😀", "plus")) +@@infix(("💩💩", "minus")) + +let q = 3 😀 4 💩💩 5 diff --git a/res_syntax/tests/printer/expr/expected/infixRemove.res.txt b/res_syntax/tests/printer/expr/expected/infixRemove.res.txt new file mode 100644 index 00000000000..fef8ff31977 --- /dev/null +++ b/res_syntax/tests/printer/expr/expected/infixRemove.res.txt @@ -0,0 +1,7 @@ +let plus = (x, y) => x + y +let minus = (x, y) => x - y + +@@infix.remove(("😀", "plus")) +@@infix.remove(("💩💩", "minus")) + +let q = minus(plus(3, 4), 5) diff --git a/res_syntax/tests/printer/expr/expected/infixScope.res.txt b/res_syntax/tests/printer/expr/expected/infixScope.res.txt new file mode 100644 index 00000000000..e445eca612b --- /dev/null +++ b/res_syntax/tests/printer/expr/expected/infixScope.res.txt @@ -0,0 +1,7 @@ +module M = { + @@infix(("+++", "+")) + + let z = 3 +++ 4 +} + +let z = 3 + 4 diff --git a/res_syntax/tests/printer/expr/infixAdd.res b/res_syntax/tests/printer/expr/infixAdd.res new file mode 100644 index 00000000000..284d410201e --- /dev/null +++ b/res_syntax/tests/printer/expr/infixAdd.res @@ -0,0 +1,7 @@ +let plus = (x, y) => x + y +let minus = (x, y) => x - y + +@@infix.add(("😀", "plus")) +@@infix.add(("💩💩", "minus")) + +let q = minus(plus(3, 4), 5) diff --git a/res_syntax/tests/printer/expr/infixRemove.res b/res_syntax/tests/printer/expr/infixRemove.res new file mode 100644 index 00000000000..d4a3217bab2 --- /dev/null +++ b/res_syntax/tests/printer/expr/infixRemove.res @@ -0,0 +1,7 @@ +let plus = (x, y) => x + y +let minus = (x, y) => x - y + +@@infix.remove(("😀", "plus")) +@@infix.remove(("💩💩", "minus")) + +let q = 3 😀 4 💩💩 5 diff --git a/res_syntax/tests/printer/expr/infixScope.res b/res_syntax/tests/printer/expr/infixScope.res new file mode 100644 index 00000000000..fef231b6f92 --- /dev/null +++ b/res_syntax/tests/printer/expr/infixScope.res @@ -0,0 +1,8 @@ + +module M = { + @@infix.add(("+++", "+")) + + let z = 3 + 4 +} + +let z = 3 + 4 \ No newline at end of file