git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1226 from purescript/binders

Simplify pretty printer for binders

commit190b129305bcd8c035c314e0b47a38760d64915e authorPhil Freeman <paf31@cantab.net> dateFri 3 Jul 2015, 13:17 (11 years ago) parented5cc1875bc13f
1 file changed+32−55
src/Language/PureScript/Pretty/Values.hs+32−55
src/Language/PureScript/Pretty/Values.hs+32−55
@@ -95,7 +95,7 @@ prettyPrintDeclaration _ = error "Invalid argument to prettyPrintDeclaration"
9595prettyPrintCaseAlternative :: CaseAlternative -> StateT PrinterState Maybe String
9696prettyPrintCaseAlternative (CaseAlternative binders result) =
9797 concat <$> sequence
98− [ intercalate ", " <$> forM binders prettyPrintBinder'
98+ [ return (unwords (map prettyPrintBinderAtom binders))
9999 , prettyPrintResult result
100100 ]
101101 where
@@ -118,7 +118,7 @@ prettyPrintDoNotationElement (DoNotationValue val) =
118118 prettyPrintValue' val
119119prettyPrintDoNotationElement (DoNotationBind binder val) =
120120 concat <$> sequence
121− [ prettyPrintBinder' binder
121+ [ return (prettyPrintBinder binder)
122122 , return " <- "
123123 , prettyPrintValue' val
124124 ]
@@ -131,13 +131,10 @@ prettyPrintDoNotationElement (PositionedDoNotationElement _ _ el) = prettyPrintD
131131
132132prettyPrintObject' :: [(String, Maybe Expr)] -> StateT PrinterState Maybe String
133133prettyPrintObject' [] = return "{}"
134−prettyPrintObject' ps = concat <$> sequence
135− [ return "{\n"
136− , withIndent $ prettyPrintMany prettyPrintObjectProperty ps
137− , return "\n"
138− , currentIndent
139− , return "}"
140− ]
134+prettyPrintObject' ps = return $ "{ " ++ intercalate ", " (map prettyPrintObjectProperty ps) ++ "}"
135+ where
136+ prettyPrintObjectProperty :: (String, Maybe Expr) -> String
137+ prettyPrintObjectProperty (key, value) = prettyPrintObjectKey key ++ ": " ++ maybe "_" prettyPrintValue value
141138
142139ifThenElse :: Pattern PrinterState Expr ((Expr, Expr), Expr)
143140ifThenElse = mkPattern match
@@ -190,55 +187,35 @@ prettyPrintValue' = runKleisli $ runPattern matchValue
190187 , [ Wrap ifThenElse $ \(th, el) cond -> "if " ++ cond ++ " then " ++ prettyPrintValue th ++ " else " ++ prettyPrintValue el ]
191188 ]
192189
193−prettyPrintBinderAtom :: Pattern PrinterState Binder String
194−prettyPrintBinderAtom = mkPattern' match
190+prettyPrintBinderAtom :: Binder -> String
191+prettyPrintBinderAtom NullBinder = "_"
192+prettyPrintBinderAtom (StringBinder str) = show str
193+prettyPrintBinderAtom (CharBinder c) = show c
194+prettyPrintBinderAtom (NumberBinder num) = either show show num
195+prettyPrintBinderAtom (BooleanBinder True) = "true"
196+prettyPrintBinderAtom (BooleanBinder False) = "false"
197+prettyPrintBinderAtom (VarBinder ident) = show ident
198+prettyPrintBinderAtom (ConstructorBinder ctor []) = show ctor
199+prettyPrintBinderAtom (ObjectBinder bs) =
200+ "{ "
201+ ++ intercalate ", " (map prettyPrintObjectPropertyBinder bs)
202+ ++ " }"
195203 where
196− match :: Binder -> StateT PrinterState Maybe String
197− match NullBinder = return "_"
198− match (StringBinder str) = return $ show str
199− match (CharBinder c) = return $ show c
200− match (NumberBinder num) = return $ either show show num
201− match (BooleanBinder True) = return "true"
202− match (BooleanBinder False) = return "false"
203− match (VarBinder ident) = return $ show ident
204− match (ConstructorBinder ctor args) = concat <$> sequence
205− [ return $ show ctor ++ " "
206− , unwords <$> forM args match
207− ]
208− match (ObjectBinder bs) = concat <$> sequence
209− [ return "{\n"
210− , withIndent $ prettyPrintMany prettyPrintObjectPropertyBinder bs
211− , currentIndent
212− , return "}"
213− ]
214− match (ArrayBinder bs) = concat <$> sequence
215− [ return "["
216− , unwords <$> mapM prettyPrintBinder' bs
217− , return "]"
218− ]
219− match (NamedBinder ident binder) = ((show ident ++ "@") ++) <$> prettyPrintBinder' binder
220− match (PositionedBinder _ _ binder) = prettyPrintBinder' binder
204+ prettyPrintObjectPropertyBinder :: (String, Binder) -> String
205+ prettyPrintObjectPropertyBinder (key, binder) = prettyPrintObjectKey key ++ ": " ++ prettyPrintBinder binder
206+prettyPrintBinderAtom (ArrayBinder bs) =
207+ "[ "
208+ ++ intercalate ", " (map prettyPrintBinder bs)
209+ ++ " ]"
210+prettyPrintBinderAtom (NamedBinder ident binder) = show ident ++ "@" ++ prettyPrintBinder binder
211+prettyPrintBinderAtom (PositionedBinder _ _ binder) = prettyPrintBinderAtom binder
212+prettyPrintBinderAtom b = parens (prettyPrintBinder b)
221213
222214-- |
223215-- Generate a pretty-printed string representing a Binder
224216--
225217prettyPrintBinder :: Binder -> String
226−prettyPrintBinder = fromMaybe (error "Incomplete pattern") . flip evalStateT (PrinterState 0) . prettyPrintBinder'
227−
228−prettyPrintBinder' :: Binder -> StateT PrinterState Maybe String
229−prettyPrintBinder' = runKleisli $ runPattern matchBinder
230− where
231− matchBinder :: Pattern PrinterState Binder String
232− matchBinder = prettyPrintBinderAtom <+> fmap parens matchBinder
233−
234−prettyPrintObjectPropertyBinder :: (String, Binder) -> StateT PrinterState Maybe String
235−prettyPrintObjectPropertyBinder (key, binder) = concat <$> sequence
236− [ return $ prettyPrintObjectKey key ++ ": "
237− , prettyPrintBinder' binder
238− ]
239−
240−prettyPrintObjectProperty :: (String, Maybe Expr) -> StateT PrinterState Maybe String
241−prettyPrintObjectProperty (key, value) = concat <$> sequence
242− [ return $ prettyPrintObjectKey key ++ ": "
243− , maybe (pure "_") prettyPrintValue' value
244− ]
218+prettyPrintBinder (ConstructorBinder ctor []) = show ctor
219+prettyPrintBinder (ConstructorBinder ctor args) = show ctor ++ " " ++ unwords (map prettyPrintBinderAtom args)
220+prettyPrintBinder (PositionedBinder _ _ binder) = prettyPrintBinder binder
221+prettyPrintBinder b = prettyPrintBinderAtom b