Skip to content
Closed
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
11 changes: 7 additions & 4 deletions res_syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
[
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions res_syntax/src/res_custom_infix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions res_syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
[
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions res_syntax/tests/printer/expr/expected/infixAdd.res.txt
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions res_syntax/tests/printer/expr/expected/infixRemove.res.txt
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions res_syntax/tests/printer/expr/expected/infixScope.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module M = {
@@infix(("+++", "+"))

let z = 3 +++ 4
}

let z = 3 + 4
7 changes: 7 additions & 0 deletions res_syntax/tests/printer/expr/infixAdd.res
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions res_syntax/tests/printer/expr/infixRemove.res
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions res_syntax/tests/printer/expr/infixScope.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

module M = {
@@infix.add(("+++", "+"))

let z = 3 + 4
}

let z = 3 + 4