-
Notifications
You must be signed in to change notification settings - Fork 35
/
ParseSQLQueries.lhs
39 lines (33 loc) · 1.22 KB
/
ParseSQLQueries.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
= Parsing a file with SQL queries
This is an example exe which parses a list of SQL queries in a file
separated by semi colons.
> import System.Environment
> import Text.Parsec (ParseError, errorPos, sourceName, sourceLine, sourceColumn)
> import Text.Parsec.String
> import Text.Parsec.String.Char
> import Text.Parsec.String.Combinator
> import Control.Applicative ((<$>),(<*), (*>),(<*>), (<$), (<|>), many)
> import Control.Monad
> import SimpleSQLQueryParser0
> import Data.List
> import qualified PrettyPrinting0 as P
> main :: IO ()
> main = do
> a <- getArgs
> case a of
> [str] -> parseFromFile myParser str
> >>= either (putStrLn . showError)
> (putStrLn . intercalate "\n" . map P.prettyQueryExpr)
> _ -> error "please pass one argument with the file containing the queries to parse"
> myParser :: Parser [QueryExpr]
> myParser = whitespace
> *> sepBy1 queryExpr semi
> <* optional semi
> <* eof
> where semi = void $ lexeme $ char ';'
> showError :: ParseError -> String
> showError e =
> let p = errorPos e
> in sourceName p ++ ":" ++ show (sourceLine p) ++ ":"
> ++ show (sourceColumn p) ++ ":\n"
> ++ show e