@@ -20,89 +20,133 @@ module Language.PureScript.Pretty.Values (
21import Data.Maybe (fromMaybe)
21import Data.Maybe (fromMaybe)
22import Data.List (intercalate)
22import Data.List (intercalate)
23−import Control.Arrow ((<+>))
24+import Control.Arrow ((<+>), runKleisli)
24import Control.PatternArrows
25import Control.PatternArrows
26+import Control.Monad.State
27+import Control.Applicative
26import Language.PureScript.Types
29import Language.PureScript.Types
27import Language.PureScript.Declarations
30import Language.PureScript.Declarations
28import Language.PureScript.Pretty.Common
31import Language.PureScript.Pretty.Common
29import Language.PureScript.Pretty.Types
32import Language.PureScript.Pretty.Types
31−literals :: Pattern () Value String
34+literals :: Pattern PrinterState Value String
32−literals = mkPattern match
35+literals = mkPattern' match
37+ match :: Value -> StateT PrinterState Maybe String
34− match (NumericLiteral n) = Just $ either show show n
38+ match (NumericLiteral n) = return $ either show show n
35− match (StringLiteral s) = Just $ show s
39+ match (StringLiteral s) = return $ show s
36− match (BooleanLiteral True) = Just "true"
40+ match (BooleanLiteral True) = return "true"
37− match (BooleanLiteral False) = Just "false"
41+ match (BooleanLiteral False) = return "false"
38− match (ArrayLiteral xs) = Just $ "[" ++ intercalate ", " (map prettyPrintValue xs) ++ "]"
42+ match (ArrayLiteral xs) = fmap concat $ sequence
44+ , withIndent $ prettyPrintMany prettyPrintValue' xs
47+ match (ObjectLiteral ps) = fmap concat $ sequence
39− match (ObjectLiteral ps) = Just $ "{" ++ intercalate ", " (map (uncurry prettyPrintObjectProperty) ps) ++ "}"
49+ , withIndent $ prettyPrintMany prettyPrintObjectProperty ps
40− match (Constructor name) = Just $ show name
53+ match (Constructor name) = return $ show name
41− match (Case values binders) = Just $ "case " ++ unwords (map prettyPrintValue values) ++
54+ match (Case values binders) = fmap concat $ sequence
56+ , unwords <$> forM values prettyPrintValue'
42− " of { " ++ intercalate " ; " (map prettyPrintCaseAlternative binders) ++ " }"
58+ , withIndent $ prettyPrintMany prettyPrintCaseAlternative binders
61+ match (Let _ val) = fmap concat $ sequence
62+ [ return "let ... in " -- TODO
43− match (Let _ val) = Just $ "let ... in " ++ prettyPrintValue val
63+ , prettyPrintValue' val
44− match (Var ident) = Just $ show ident
65+ match (Var ident) = return $ show ident
66+ match (Do els) = fmap concat $ sequence
45− match (Do els) = Just $ " do { " ++ intercalate "; " (map prettyPrintDoNotationElement els) ++ " }"
68+ , withIndent $ prettyPrintMany prettyPrintDoNotationElement els
46 match (TypeClassDictionary _ _) = error "Type class dictionary was not replaced"
71 match (TypeClassDictionary _ _) = error "Type class dictionary was not replaced"
47− match (PositionedValue _ val) = Just $ prettyPrintValue val
72+ match (PositionedValue _ val) = prettyPrintValue' val
48− match _ = Nothing
73+ match _ = mzero
50−prettyPrintCaseAlternative :: CaseAlternative -> String
75+prettyPrintCaseAlternative :: CaseAlternative -> StateT PrinterState Maybe String
51prettyPrintCaseAlternative (CaseAlternative binders grd val) =
76prettyPrintCaseAlternative (CaseAlternative binders grd val) =
77+ fmap concat $ sequence
52− "(" ++ intercalate ", " (map prettyPrintBinder binders) ++ ") " ++
78+ [ intercalate ", " <$> forM binders prettyPrintBinder'
53− maybe "" (("| " ++) . prettyPrintValue) grd ++ " -> " ++ prettyPrintValue val
79+ , maybe (return "") (fmap ("| " ++) . prettyPrintValue') grd
81+ , prettyPrintValue' val
84+prettyPrintDoNotationElement :: DoNotationElement -> StateT PrinterState Maybe String
85+prettyPrintDoNotationElement (DoNotationValue val) =
86+ prettyPrintValue' val
87+prettyPrintDoNotationElement (DoNotationBind binder val) =
88+ fmap concat $ sequence
89+ [ prettyPrintBinder' binder
91+ , prettyPrintValue' val
93+prettyPrintDoNotationElement (DoNotationLet binder val) =
94+ fmap concat $ sequence
96+ , prettyPrintBinder' binder
98+ , prettyPrintValue' val
100+prettyPrintDoNotationElement (PositionedDoNotationElement _ el) = prettyPrintDoNotationElement el
55−ifThenElse :: Pattern () Value ((Value, Value), Value)
102+ifThenElse :: Pattern PrinterState Value ((Value, Value), Value)
56ifThenElse = mkPattern match
103ifThenElse = mkPattern match
58 match (IfThenElse cond th el) = Just ((th, el), cond)
105 match (IfThenElse cond th el) = Just ((th, el), cond)
59 match _ = Nothing
106 match _ = Nothing
61−accessor :: Pattern () Value (String, Value)
108+accessor :: Pattern PrinterState Value (String, Value)
62accessor = mkPattern match
109accessor = mkPattern match
64 match (Accessor prop val) = Just (prop, val)
111 match (Accessor prop val) = Just (prop, val)
65 match _ = Nothing
112 match _ = Nothing
67−objectUpdate :: Pattern () Value ([String], Value)
114+objectUpdate :: Pattern PrinterState Value ([String], Value)
68objectUpdate = mkPattern match
115objectUpdate = mkPattern match
70 match (ObjectUpdate o ps) = Just (flip map ps $ \(key, val) -> key ++ " = " ++ prettyPrintValue val, o)
117 match (ObjectUpdate o ps) = Just (flip map ps $ \(key, val) -> key ++ " = " ++ prettyPrintValue val, o)
71 match _ = Nothing
118 match _ = Nothing
73−app :: Pattern () Value (String, Value)
120+app :: Pattern PrinterState Value (String, Value)
74app = mkPattern match
121app = mkPattern match
76 match (App val arg) = Just (prettyPrintValue arg, val)
123 match (App val arg) = Just (prettyPrintValue arg, val)
77 match _ = Nothing
124 match _ = Nothing
79−lam :: Pattern () Value (String, Value)
126+lam :: Pattern PrinterState Value (String, Value)
80lam = mkPattern match
127lam = mkPattern match
82 match (Abs (Left arg) val) = Just (show arg, val)
129 match (Abs (Left arg) val) = Just (show arg, val)
83 match _ = Nothing
130 match _ = Nothing
85−typed :: Pattern () Value (Type, Value)
132+typed :: Pattern PrinterState Value (Type, Value)
86typed = mkPattern match
133typed = mkPattern match
88 match (TypedValue _ val ty) = Just (ty, val)
135 match (TypedValue _ val ty) = Just (ty, val)
89 match _ = Nothing
136 match _ = Nothing
91−prettyPrintDoNotationElement :: DoNotationElement -> String
92−prettyPrintDoNotationElement (DoNotationValue val) = prettyPrintValue val
93−prettyPrintDoNotationElement (DoNotationBind binder val) = prettyPrintBinder binder ++ " <- " ++ prettyPrintValue val
94−prettyPrintDoNotationElement (DoNotationLet binder val) = "let " ++ prettyPrintBinder binder ++ " = " ++ prettyPrintValue val
95−prettyPrintDoNotationElement (PositionedDoNotationElement _ el) = prettyPrintDoNotationElement el
98−-- Generate a pretty-printed string representing a Value
139+-- Generate a pretty-printed string representing an expression
100prettyPrintValue :: Value -> String
141prettyPrintValue :: Value -> String
101−prettyPrintValue = fromMaybe (error "Incomplete pattern") . pattern matchValue ()
142+prettyPrintValue = fromMaybe (error "Incomplete pattern") . flip evalStateT (PrinterState 0) . prettyPrintValue'
144+prettyPrintValue' :: Value -> StateT PrinterState Maybe String
145+prettyPrintValue' = runKleisli $ runPattern matchValue
103− matchValue :: Pattern () Value String
147+ matchValue :: Pattern PrinterState Value String
104 matchValue = buildPrettyPrinter operators (literals <+> fmap parens matchValue)
148 matchValue = buildPrettyPrinter operators (literals <+> fmap parens matchValue)
105− operators :: OperatorTable () Value String
149+ operators :: OperatorTable PrinterState Value String
106 operators =
150 operators =
107 OperatorTable [ [ Wrap accessor $ \prop val -> val ++ "." ++ prop ]
151 OperatorTable [ [ Wrap accessor $ \prop val -> val ++ "." ++ prop ]
108 , [ Wrap objectUpdate $ \ps val -> val ++ "{ " ++ intercalate ", " ps ++ " }" ]
152 , [ Wrap objectUpdate $ \ps val -> val ++ "{ " ++ intercalate ", " ps ++ " }" ]
@@ -112,43 +156,64 @@ prettyPrintValue = fromMaybe (error "Incomplete pattern") . pattern matchValue (
112 , [ Wrap typed $ \ty val -> val ++ " :: " ++ prettyPrintType ty ]
156 , [ Wrap typed $ \ty val -> val ++ " :: " ++ prettyPrintType ty ]
115−prettyPrintBinderAtom :: Pattern () Binder String
159+prettyPrintBinderAtom :: Pattern PrinterState Binder String
116−prettyPrintBinderAtom = mkPattern match
160+prettyPrintBinderAtom = mkPattern' match
118− match :: Binder -> Maybe String
162+ match :: Binder -> StateT PrinterState Maybe String
119− match NullBinder = Just "_"
163+ match NullBinder = return "_"
120− match (StringBinder str) = Just $ show str
164+ match (StringBinder str) = return $ show str
121− match (NumberBinder num) = Just $ either show show num
165+ match (NumberBinder num) = return $ either show show num
122− match (BooleanBinder True) = Just "true"
166+ match (BooleanBinder True) = return "true"
123− match (BooleanBinder False) = Just "false"
167+ match (BooleanBinder False) = return "false"
124− match (VarBinder ident) = Just $ show ident
168+ match (VarBinder ident) = return $ show ident
125− match (ConstructorBinder ctor args) = Just $ show ctor ++ " " ++ unwords (map (parens . prettyPrintBinder) args)
169+ match (ConstructorBinder ctor args) = fmap concat $ sequence
170+ [ return $ show ctor ++ " "
171+ , unwords <$> forM args match
173+ match (ObjectBinder bs) = fmap concat $ sequence
126− match (ObjectBinder bs) = Just $ "{ " ++ intercalate ", " (map (uncurry prettyPrintObjectPropertyBinder) bs) ++ " }"
175+ , withIndent $ prettyPrintMany prettyPrintObjectPropertyBinder bs
179+ match (ArrayBinder bs) = fmap concat $ sequence
127− match (ArrayBinder bs) = Just $ "[ " ++ intercalate ", " (map prettyPrintBinder bs) ++ " ]"
181+ , unwords <$> mapM prettyPrintBinder' bs
128− match (NamedBinder ident binder) = Just $ show ident ++ "@" ++ prettyPrintBinder binder
184+ match (NamedBinder ident binder) = ((show ident ++ "@") ++) <$> prettyPrintBinder' binder
129− match (PositionedBinder _ binder) = Just $ prettyPrintBinder binder
185+ match (PositionedBinder _ binder) = prettyPrintBinder' binder
130− match _ = Nothing
186+ match _ = mzero
133-- Generate a pretty-printed string representing a Binder
189-- Generate a pretty-printed string representing a Binder
135prettyPrintBinder :: Binder -> String
191prettyPrintBinder :: Binder -> String
136−prettyPrintBinder = fromMaybe (error "Incomplete pattern") . pattern matchBinder ()
192+prettyPrintBinder = fromMaybe (error "Incomplete pattern") . flip evalStateT (PrinterState 0) . prettyPrintBinder'
194+prettyPrintBinder' :: Binder -> StateT PrinterState Maybe String
195+prettyPrintBinder' = runKleisli $ runPattern matchBinder
138− matchBinder :: Pattern () Binder String
197+ matchBinder :: Pattern PrinterState Binder String
139 matchBinder = buildPrettyPrinter operators (prettyPrintBinderAtom <+> fmap parens matchBinder)
198 matchBinder = buildPrettyPrinter operators (prettyPrintBinderAtom <+> fmap parens matchBinder)
140− operators :: OperatorTable () Binder String
199+ operators :: OperatorTable PrinterState Binder String
141 operators =
200 operators =
142 OperatorTable [ [ AssocR matchConsBinder (\b1 b2 -> b1 ++ " : " ++ b2) ] ]
201 OperatorTable [ [ AssocR matchConsBinder (\b1 b2 -> b1 ++ " : " ++ b2) ] ]
144−matchConsBinder :: Pattern () Binder (Binder, Binder)
203+matchConsBinder :: Pattern PrinterState Binder (Binder, Binder)
145matchConsBinder = mkPattern match'
204matchConsBinder = mkPattern match'
147 match' (ConsBinder b1 b2) = Just (b1, b2)
206 match' (ConsBinder b1 b2) = Just (b1, b2)
148 match' _ = Nothing
207 match' _ = Nothing
150−prettyPrintObjectPropertyBinder :: String -> Binder -> String
209+prettyPrintObjectPropertyBinder :: (String, Binder) -> StateT PrinterState Maybe String
151−prettyPrintObjectPropertyBinder key binder = key ++ ": " ++ prettyPrintBinder binder
210+prettyPrintObjectPropertyBinder (key, binder) = fmap concat $ sequence
152−
211+ [ return $ key ++ ": "
212+ , prettyPrintBinder' binder
153−prettyPrintObjectProperty :: String -> Value -> String
215+prettyPrintObjectProperty :: (String, Value) -> StateT PrinterState Maybe String
154−prettyPrintObjectProperty key value = key ++ ": " ++ prettyPrintValue value
216+prettyPrintObjectProperty (key, value) = fmap concat $ sequence
217+ [ return $ key ++ ": "
218+ , prettyPrintValue' value