1
Fork 0

add parsing for unary + -

This commit is contained in:
Jake Wheat 2013-12-13 20:01:57 +02:00
parent ed47656a0c
commit 182526d1fc
2 changed files with 38 additions and 24 deletions

View file

@ -13,6 +13,7 @@
> import qualified Language.Haskell.Exts.Syntax as HSE > import qualified Language.Haskell.Exts.Syntax as HSE
> import qualified Language.Haskell.Exts.Fixity as HSE > import qualified Language.Haskell.Exts.Fixity as HSE
> import Data.Maybe > import Data.Maybe
> import Data.List
> import Language.SQL.SimpleSQL.Syntax > import Language.SQL.SimpleSQL.Syntax
@ -37,19 +38,20 @@
> setPos :: FilePath -> Maybe (Int,Int) -> P () > setPos :: FilePath -> Maybe (Int,Int) -> P ()
> setPos f p = do > setPos f p = do
> sp <- getPosition > sp <- getPosition
> let sp' = setSourceName sp f > let sp' = setSourceName sp f
> sp'' = maybe sp' > sp'' = maybe sp'
> (\(l,c) -> flip setSourceColumn c > (\(l,c) -> flip setSourceColumn c
> $ setSourceLine sp' l) > $ setSourceLine sp' l)
> p > p
> setPosition sp'' > setPosition sp''
> data ParseError = ParseError > data ParseError = ParseError
> {peErrorString :: String > {peErrorString :: String
> ,peFilename :: FilePath > ,peFilename :: FilePath
> ,pePosition :: (Int,Int) > ,pePosition :: (Int,Int)
> ,peFormattedError :: String} > ,peFormattedError :: String
> } deriving (Eq,Show)
> convParseError :: String -> P.ParseError -> ParseError > convParseError :: String -> P.ParseError -> ParseError
> convParseError src e = > convParseError src e =
@ -79,11 +81,8 @@ format the error more nicely: emacs format for positioning, plus context
> _ -> "" > _ -> ""
> p = errorPos e > p = errorPos e
Language/SQL/SimpleSQL/Parser.lhs:54:3:
> type P a = ParsecT String () Identity a > type P a = ParsecT String () Identity a
------------------------------------------------ ------------------------------------------------
= scalar expressions = scalar expressions
@ -174,9 +173,20 @@ to be.
> binOpKeywordNames :: [String] > binOpKeywordNames :: [String]
> binOpKeywordNames = ["and", "or", "like"] > binOpKeywordNames = ["and", "or", "like"]
> unOpKeywordNames :: [String]
> unOpKeywordNames = ["not"]
> unOpSymbolNames :: [String]
> unOpSymbolNames = ["+", "-"]
> unaryOp :: P ScalarExpr > unaryOp :: P ScalarExpr
> unaryOp = makeOp <$> (try (keyword_ "not") *> scalarExpr) > unaryOp =
> where makeOp e = Op "not" [e] > makeOp <$> opSymbol <*> scalarExpr
> where
> makeOp nm e = Op nm [e]
> opSymbol = choice (map (try . symbol) unOpSymbolNames
> ++ map (try . keyword) unOpKeywordNames)
> scalarExpr' :: P ScalarExpr > scalarExpr' :: P ScalarExpr
> scalarExpr' = factor >>= trysuffix > scalarExpr' = factor >>= trysuffix
@ -206,7 +216,7 @@ to be.
> 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)
> (toHaskell e1) > (toHaskell e1)
> Op "not" [e0] -> toHaskell $ App "not" [e0] > Op o [e0] -> toHaskell $ App ("unary:" ++ o) [e0]
> Op {} -> error $ "bad args to operator " ++ groom e > Op {} -> error $ "bad args to operator " ++ groom e
> Star -> HSE.Var $ HSE.UnQual $ HSE.Ident "*" > Star -> HSE.Var $ HSE.UnQual $ HSE.Ident "*"
> Iden2 a b -> HSE.Var $ HSE.Qual (HSE.ModuleName a) (HSE.Ident b) > Iden2 a b -> HSE.Var $ HSE.Qual (HSE.ModuleName a) (HSE.Ident b)
@ -230,8 +240,9 @@ to be.
> HSE.Lit (HSE.String ('n':l)) -> NumLit l > HSE.Lit (HSE.String ('n':l)) -> NumLit 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 x)))
> (HSE.List [ea]) -> Op "not" [toSql ea] > (HSE.List [ea]) | "unary:" `isPrefixOf` x ->
> Op (drop 6 x) [toSql ea]
> HSE.App (HSE.Var (HSE.UnQual (HSE.Ident i))) > HSE.App (HSE.Var (HSE.UnQual (HSE.Ident i)))
> (HSE.List es) -> App i $ map toSql es > (HSE.List es) -> App i $ map toSql es
> HSE.InfixApp e0 (HSE.QVarOp (HSE.UnQual (HSE.Ident n))) e1 -> > HSE.InfixApp e0 (HSE.QVarOp (HSE.UnQual (HSE.Ident n))) e1 ->

19
TODO
View file

@ -1,14 +1,15 @@
first release first release:
1. utility function to parse some sql and print the context of the
error using the position
2. complete the parsing for the tests in the Tests.lhs
3. get tpch parsing
4. add tests to cabal
5. do code documentation and haddock
complete the parsing for the tests in the Tests.lhs
case insensivity
get tpch parsing
check the pretty printer on the tpch queries
add automated tests to cabal
do code documentation and haddock
do some tests for parse errors?
website with haddock and table of parsing tests
---- ----
@ -29,6 +30,8 @@ position annotation
= sql support = sql support
case insensitivity
scalar function syntax: scalar function syntax:
standard interval literal standard interval literal