1
Fork 0

add support for multiple case when expressions

This commit is contained in:
Jake Wheat 2013-12-17 18:42:00 +02:00
parent ebe522b21d
commit 7d094182b7
6 changed files with 28 additions and 31 deletions

View file

@ -102,7 +102,7 @@ the fixity code.
> -- map the two maybes to lists with either 0 or 1 element
> Case v ts el -> HSE.App (var "$case")
> (HSE.List [ltoh $ maybeToList v
> ,HSE.List $ map (ltoh . (\(a,b) -> [a,b])) ts
> ,HSE.List $ map (ltoh . (\(a,b) -> b:a)) ts
> ,ltoh $ maybeToList el])
> Cast e0 tn -> HSE.App (str ('c':show tn)) $ toHaskell e0
> TypedLit {} -> str ('v':show e)
@ -158,7 +158,7 @@ the fixity code.
> SpecialOp (unname nm) $ map toSql es
> HSE.App (HSE.Var (HSE.UnQual (HSE.Ident "$case")))
> (HSE.List [v,ts,el]) ->
> Case (ltom v) (pairs ts) (ltom el)
> Case (ltom v) (whens ts) (ltom el)
> HSE.App (HSE.Lit (HSE.String ('c':nm))) e0 ->
> Cast (toSql e0) (read nm)
> HSE.App (HSE.Lit (HSE.String ('i':nm)))
@ -173,8 +173,8 @@ the fixity code.
> ltom (HSE.List []) = Nothing
> ltom (HSE.List [ex]) = Just $ toSql ex
> ltom ex = err ex
> pairs (HSE.List l) = map (\(HSE.List [a,b]) -> (toSql a, toSql b)) l
> pairs ex = err ex
> whens (HSE.List l) = map (\(HSE.List (t:ws)) -> (map toSql ws, toSql t)) l
> whens ex = err ex
> err :: Show a => a -> e
> err a = error $ "simple-sql-parser: internal fixity error " ++ show a
> unname ('"':nm) = QName nm

View file

@ -222,7 +222,8 @@ always used with the optionSuffix combinator.
> <* keyword_ "end"
> where
> swhen = keyword_ "when" *>
> ((,) <$> scalarExpr' <*> (keyword_ "then" *> scalarExpr'))
> ((,) <$> commaSep1 scalarExpr'
> <*> (keyword_ "then" *> scalarExpr'))
== miscellaneous keyword operators

View file

@ -124,7 +124,7 @@
> ++ [text "end"]
> where
> w (t0,t1) =
> text "when" <+> nest 5 (scalarExpr t0)
> text "when" <+> nest 5 (commaSep $ map scalarExpr t0)
> <+> text "then" <+> nest 5 (scalarExpr t1)
> e el = text "else" <+> nest 5 (scalarExpr el)
> scalarExpr (Parens e) = parens $ scalarExpr e

View file

@ -99,7 +99,7 @@
> -- a=4,b=5 then x end)
> | Case
> {caseTest :: Maybe ScalarExpr -- ^ test value
> ,caseWhens :: [(ScalarExpr,ScalarExpr)] -- ^ when branches
> ,caseWhens :: [([ScalarExpr],ScalarExpr)] -- ^ when branches
> ,caseElse :: Maybe ScalarExpr -- ^ else value
> }
> | Parens ScalarExpr