1
Fork 0

split number literals and string literals

This commit is contained in:
Jake Wheat 2013-12-13 16:44:00 +02:00
parent afc6933f64
commit 63fe9778f7
4 changed files with 29 additions and 24 deletions

View file

@ -31,10 +31,10 @@
= scalar expressions
> estring :: P ScalarExpr
> estring = Literal <$> (symbol_ "'" *> manyTill anyChar (symbol_ "'"))
> estring = StringLiteral <$> (symbol_ "'" *> manyTill anyChar (symbol_ "'"))
> integer :: P ScalarExpr
> integer = Literal <$> (many1 digit <* whiteSpace)
> integer = NumLiteral <$> (many1 digit <* whiteSpace)
> literal :: P ScalarExpr
> literal = integer <|> estring
@ -119,7 +119,8 @@ to be.
> toHaskell :: ScalarExpr -> HSE.Exp
> toHaskell e = case e of
> Identifier i -> HSE.Var $ HSE.UnQual $ HSE.Ident i
> Literal l -> HSE.Lit $ HSE.String l
> StringLiteral l -> HSE.Lit $ HSE.String $ 's':l
> NumLiteral l -> HSE.Lit $ HSE.String $ 'n':l
> App n es -> HSE.App (toHaskell $ Identifier n) $ ltoh es
> Op n [e0,e1] -> HSE.InfixApp (toHaskell e0)
> (HSE.QVarOp $ HSE.UnQual $ HSE.Ident n)
@ -144,7 +145,8 @@ to be.
> HSE.Var (HSE.Qual (HSE.ModuleName q) (HSE.Ident "*")) -> Star2 q
> HSE.Var (HSE.Qual (HSE.ModuleName a) (HSE.Ident b)) -> Identifier2 a b
> HSE.Var (HSE.UnQual (HSE.Ident i)) -> Identifier i
> HSE.Lit (HSE.String l) -> Literal l
> HSE.Lit (HSE.String ('s':l)) -> StringLiteral l
> HSE.Lit (HSE.String ('n':l)) -> NumLiteral l
> HSE.App (HSE.Var (HSE.UnQual (HSE.Ident "$case"))) (HSE.List [v,ts,el]) ->
> Case (ltom v) (pairs ts) (ltom el)
> HSE.App (HSE.Var (HSE.UnQual (HSE.Ident "not")))

View file

@ -21,7 +21,8 @@ back into SQL source text. It attempts to format the output nicely.
= scalar expressions
> scalarExpr :: ScalarExpr -> Doc
> scalarExpr (Literal s) = quotes $ text s
> scalarExpr (StringLiteral s) = quotes $ text s
> scalarExpr (NumLiteral s) = text s
> scalarExpr (Identifier i) = text i
> scalarExpr (Identifier2 q i) = text q <> text "." <> text i
> scalarExpr Star = text "*"

View file

@ -9,7 +9,8 @@
> ) where
> data ScalarExpr = Literal String
> data ScalarExpr = NumLiteral String
> | StringLiteral String
> | Identifier String
> | Identifier2 String String
> | Star

View file

@ -25,8 +25,8 @@
> literals :: TestItem
> literals = Group "literals" $ map (uncurry TestScalarExpr)
> [("3", Literal "3")
> ,("'string'", Literal "string")
> [("3", NumLiteral "3")
> ,("'string'", StringLiteral "string")
> ]
> identifiers :: TestItem
@ -51,17 +51,18 @@
> caseexp :: TestItem
> caseexp = Group "caseexp" $ map (uncurry TestScalarExpr)
> [("case a when 1 then 2 end"
> ,Case (Just $ Identifier "a") [(Literal "1", Literal "2")] Nothing)
> ,Case (Just $ Identifier "a") [(NumLiteral "1"
> ,NumLiteral "2")] Nothing)
> ,("case a when 1 then 2 when 3 then 4 end"
> ,Case (Just $ Identifier "a") [(Literal "1", Literal "2")
> ,(Literal "3", Literal "4")] Nothing)
> ,Case (Just $ Identifier "a") [(NumLiteral "1", NumLiteral "2")
> ,(NumLiteral "3", NumLiteral "4")] Nothing)
> ,("case a when 1 then 2 when 3 then 4 else 5 end"
> ,Case (Just $ Identifier "a") [(Literal "1", Literal "2")
> ,(Literal "3", Literal "4")] (Just $ Literal "5"))
> ,Case (Just $ Identifier "a") [(NumLiteral "1", NumLiteral "2")
> ,(NumLiteral "3", NumLiteral "4")] (Just $ NumLiteral "5"))
> ,("case when a=1 then 2 when a=3 then 4 else 5 end"
> ,Case Nothing [(Op "=" [Identifier "a", Literal "1"], Literal "2")
> ,(Op "=" [Identifier "a", Literal "3"], Literal "4")]
> (Just $ Literal "5"))
> ,Case Nothing [(Op "=" [Identifier "a", NumLiteral "1"], NumLiteral "2")
> ,(Op "=" [Identifier "a",NumLiteral "3"], NumLiteral "4")]
> (Just $ NumLiteral "5"))
> ]
> operators :: TestItem
@ -90,7 +91,7 @@
> selectLists :: TestItem
> selectLists = Group "selectLists" $ map (uncurry TestQueryExpr)
> [("select 1",
> makeSelect {qeSelectList = [(Nothing,Literal "1")]})
> makeSelect {qeSelectList = [(Nothing,NumLiteral "1")]})
> ,("select a"
> ,makeSelect {qeSelectList = [(Nothing,Identifier "a")]})
> ,("select a,b"
@ -98,8 +99,8 @@
> ,(Nothing,Identifier "b")]})
> ,("select 1+2,3+4"
> ,makeSelect {qeSelectList =
> [(Nothing,Op "+" [Literal "1",Literal "2"])
> ,(Nothing,Op "+" [Literal "3",Literal "4"])]})
> [(Nothing,Op "+" [NumLiteral "1",NumLiteral "2"])
> ,(Nothing,Op "+" [NumLiteral "3",NumLiteral "4"])]})
> ,("select a as a, /*comment*/ b as b"
> ,makeSelect {qeSelectList = [(Just "a", Identifier "a")
> ,(Just "b", Identifier "b")]})
@ -154,7 +155,7 @@
> [("select a from t where a = 5"
> ,makeSelect {qeSelectList = [(Nothing,Identifier "a")]
> ,qeFrom = [SimpleTableRef "t"]
> ,qeWhere = Just $ Op "=" [Identifier "a", Literal "5"]})
> ,qeWhere = Just $ Op "=" [Identifier "a", NumLiteral "5"]})
> ]
> groupByClause :: TestItem
@ -181,7 +182,7 @@
> ,(Nothing, App "sum" [Identifier "b"])]
> ,qeFrom = [SimpleTableRef "t"]
> ,qeGroupBy = [Identifier "a"]
> ,qeHaving = Just $ Op ">" [App "sum" [Identifier "b"], Literal "5"]
> ,qeHaving = Just $ Op ">" [App "sum" [Identifier "b"], NumLiteral "5"]
> })
> ]
@ -216,10 +217,10 @@
> ,(Just "s", App "sum" [Op "+" [Identifier "c"
> ,Identifier "d"]])]
> ,qeFrom = [SimpleTableRef "t", SimpleTableRef "u"]
> ,qeWhere = Just $ Op ">" [Identifier "a", Literal "5"]
> ,qeWhere = Just $ Op ">" [Identifier "a", NumLiteral "5"]
> ,qeGroupBy = [Identifier "a"]
> ,qeHaving = Just $ Op ">" [App "count" [Literal "1"]
> ,Literal "5"]
> ,qeHaving = Just $ Op ">" [App "count" [NumLiteral "1"]
> ,NumLiteral "5"]
> ,qeOrderBy = [Identifier "s"]
> }
> )