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

View file

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

View file

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