@@ -14,6 +14,7 @@ import Bag (bagToList)
14import BasicTypes
14import BasicTypes
15import Control.Monad
15import Control.Monad
16import Data.Bool (bool)
16import Data.Bool (bool)
17+import Data.Char (isAlphaNum)
17import Data.Data hiding (Infix, Prefix)
18import Data.Data hiding (Infix, Prefix)
18import Data.List (intersperse, sortOn)
19import Data.List (intersperse, sortOn)
19import Data.Text (Text)
20import Data.Text (Text)
@@ -803,7 +804,15 @@ p_hsBracket = \case
803 TypBr NoExt ty -> quote "t" (located ty p_hsType)
804 TypBr NoExt ty -> quote "t" (located ty p_hsType)
804 VarBr NoExt isSingleQuote name -> do
805 VarBr NoExt isSingleQuote name -> do
805 txt (bool "''" "'" isSingleQuote)
806 txt (bool "''" "'" isSingleQuote)
807+ -- HACK As you can see we use 'noLoc' here to be able to pass name into
808+ -- 'p_rdrName' since the latter expects a "located" thing. The problem
809+ -- is that 'VarBr' doesn't provide us with location of the name. This in
810+ -- turn makes it impossible to detect if there are parentheses around
811+ -- it, etc. So we have to add parentheses manually assuming they are
812+ -- necessary for all operators.
813+ let isOperator = not (any isAlphaNum (showOutputable name))
814+ wrapper = if isOperator then parens else id
806− p_rdrName (noLoc name)
815+ wrapper $ p_rdrName (noLoc name)
807 TExpBr NoExt expr -> do
816 TExpBr NoExt expr -> do
808 txt "[||"
817 txt "[||"
809 breakpoint'
818 breakpoint'