-- Everything is an expression; no recursion required
data Expr
= Create Table [Column]
| Drop Table
| Select FieldSelect Table [Condition]
| Insert Table [FieldValue]
| Delete Table [Condition]
deriving (Show, Eq)
exprP :: Parser Expr -- Type signature
exprP = -- Function definition
try selectP <|> -- Alternative
try dropP <|>
try createP <|>
try insertP <|>
try deleteP
parseExpr :: String -> Either ParseError Expr
parseExpr string = parse exprP "" string
-- Insert Into myTable [1, True, "name"];
insertP :: Parser Expr
insertP = do
L.reserved "Insert" -- Match "insert" and return nothing
L.reserved "Into" -- Match "into" and return nothing
name <- L.identifier -- Match a string and save it to name
fvs <- L.brackets $ L.commaSep fieldValue -- Combine parsers
L.semi -- Match semicolun and return nothing
return $ Insert name fvs -- Construct Insert with parsed values
fvs <- L.brackets $ L.commaSep fieldValue
L.brackets: match brackets and spaces, return inner
L.commaSep: match commas and spaces, return list
fieldValue: another parser combinator
fieldValue :: Parser FieldValue
fieldValue = boolValue <|> intValue <|> textValue
...
intValue :: Parser FieldValue
intValue = do
n <- L.integer
return $ FvInt (fromInteger n)