From 4db58148cf9f942b0783b2728261df2098958877 Mon Sep 17 00:00:00 2001 From: piegames Date: Thu, 14 Mar 2024 17:44:15 +0100 Subject: [PATCH] Parser: Fix operator parsing Closes #122 --- src/Nixfmt/Parser.hs | 16 ++++++++++++---- test/correct/operator-after-operator.nix | 3 +++ 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 test/correct/operator-after-operator.nix diff --git a/src/Nixfmt/Parser.hs b/src/Nixfmt/Parser.hs index 647283d9..92ca7622 100644 --- a/src/Nixfmt/Parser.hs +++ b/src/Nixfmt/Parser.hs @@ -342,12 +342,20 @@ list = List <$> symbol TBrackOpen <*> items term <*> symbol TBrackClose -- OPERATORS -opChars :: [Char] -opChars = "<>=+*/." - operator :: Token -> Parser Leaf operator t = label "operator" $ try $ lexeme $ - rawSymbol t <* notFollowedBy (oneOf opChars) + rawSymbol t <* notFollowedBy (oneOf ( + -- Resolve ambiguities between operators which are prefixes of others + case t of + TPlus -> "+" :: [Char] + TMinus -> ">" + TMul -> "/" + TDiv -> "/*" + TLess -> "=" + TGreater -> "=" + TNot -> "=" + _ -> "" + )) opCombiner :: Operator -> MPExpr.Operator Parser Expression opCombiner Apply = MPExpr.InfixL $ return Application diff --git a/test/correct/operator-after-operator.nix b/test/correct/operator-after-operator.nix new file mode 100644 index 00000000..d4ba6740 --- /dev/null +++ b/test/correct/operator-after-operator.nix @@ -0,0 +1,3 @@ +# https://github.com/NixOS/nixfmt/issues/122 +(1+/**/1) +(1+.4)