@@ -1,4 +1,5 @@
11{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
23module Nitpick.Pattern
34 ( Pattern(..)
45 , fromCanonicalPattern
@@ -7,6 +8,9 @@ module Nitpick.Pattern
78 where
89
910import qualified Data.List as List
11+import Data.Monoid ((<>))
12+import qualified Data.Text.Lazy as LazyText
13+import Data.Text.Lazy.Builder (Builder, fromText, toLazyText)
1014
1115import qualified AST.Literal as L
1216import qualified AST.Pattern as P
@@ -28,7 +32,7 @@ data Pattern
2832fromCanonicalPattern :: P.Canonical -> Pattern
2933fromCanonicalPattern (A.A _ pattern) =
3034 case pattern of
31− P.Data name patterns ->
35+ P.Ctor name patterns ->
3236 Ctor name (map fromCanonicalPattern patterns)
3337
3438 P.Record _ ->
@@ -57,34 +61,39 @@ fromCanonicalPattern (A.A _ pattern) =
5761-- TO STRING
5862
5963
60−toString :: Bool -> Pattern -> String
61−toString needsParens pattern =
64+toString :: Pattern -> String
65+toString pattern =
66+ LazyText.unpack (toLazyText (toBuilder False pattern))
67+
68+
69+toBuilder :: Bool -> Pattern -> Builder
70+toBuilder needsParens pattern =
6271 case pattern of
63− Ctor tag [first,rest] | Var.toString tag == "::" ->
64− toString (isCons first) first
65− ++ " :: "
66− ++ toString False rest
72+ Ctor tag [first,rest] | Var.toText tag == "::" ->
73+ toBuilder (isCons first) first <> " :: " <> toBuilder False rest
6774
6875 Ctor tag args ->
6976 if Var.isTuple tag then
7077 if null args then "()" else
71− "( " ++ List.intercalate ", " (map (toString False) args) ++ " )"
78+ "( "
79+ <> mconcat (List.intersperse ", " (map (toBuilder False) args))
80+ <> " )"
7281
7382 else
7483 addParensIf (needsParens && not (null args)) $
75− List.intercalate " " (Var.toString tag : map (toString True) args)
84+ mconcat (List.intersperse " " (fromText (Var.toText tag) : map (toBuilder True) args))
7685
7786 Anything ->
7887 "_"
7988
8089 Literal literal ->
81− L.toString literal
90+ L.toBuilder literal
8291
8392
84−addParensIf :: Bool -> String -> String
93+addParensIf :: Bool -> Builder -> Builder
8594addParensIf needsParens str =
8695 if needsParens then
87− "(" ++ str ++ ")"
96+ "(" <> str <> ")"
8897
8998 else
9099 str
@@ -94,7 +103,7 @@ isCons :: Pattern -> Bool
94103isCons pattern =
95104 case pattern of
96105 Ctor tag [_,_] ->
97− Var.toString tag == "::"
106+ Var.toText tag == "::"
98107
99108 _ ->
100109 False