Skip to content

Commit

Permalink
add simplified parsec modules, checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWheat committed Jan 1, 2014
1 parent 5b8b679 commit 47085c4
Show file tree
Hide file tree
Showing 12 changed files with 738 additions and 435 deletions.
9 changes: 5 additions & 4 deletions FromClause.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ TODO: qualify or add explicit imports

> --import Text.Groom (groom)
> --import qualified Text.Parsec as P
> import qualified Text.Parsec.String as P
> import Text.Parsec (try,optional, option,choice)
> import Text.Parsec.String (Parser)
> import Text.Parsec.String.Parsec (try)
> import Text.Parsec.String.Combinator (optional, option,choice)
> import Control.Applicative ((<$>),(*>),(<*>), (<$))
> --import Control.Monad (void,guard)
> --import Debug.Trace
Expand Down Expand Up @@ -106,7 +107,7 @@ because of all the keywords.

TODO: try and simplify this code some more.

> from :: P.Parser [TableRef]
> from :: Parser [TableRef]
> from = option [] (try (keyword_ "from") *> commaSep1 tref)
> where
> tref = choice [try (JoinQueryExpr <$> parens queryExpr)
Expand Down Expand Up @@ -149,5 +150,5 @@ TODO: try and simplify this code some more.
Here is another small helper parser. Having the arguments in this
order makes it easy to chain using >>=.

> optionSuffix :: (a -> P.Parser a) -> a -> P.Parser a
> optionSuffix :: (a -> Parser a) -> a -> Parser a
> optionSuffix p a = option a (p a)
4 changes: 2 additions & 2 deletions ParseFile.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parser given below.

> import System.Environment
> import Text.Parsec
> import Text.Parsec.String as P
> import Text.Parsec.String
> import Control.Monad

> main :: IO ()
Expand All @@ -18,7 +18,7 @@ parser given below.

This is the parser which you can replace with your own code:

> myParser :: P.Parser ()
> myParser :: Parser ()
> myParser = void $ string "correct"

Here is an example of running this program:
Expand Down
4 changes: 2 additions & 2 deletions ParseString.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ below.

> import System.Environment
> import Text.Parsec
> import Text.Parsec.String as P
> import Text.Parsec.String
> import Control.Monad

> main :: IO ()
Expand All @@ -18,7 +18,7 @@ below.

This is the parser which you can replace with your own code:

> myParser :: P.Parser ()
> myParser :: Parser ()
> myParser = void $ string "correct"

Here is an example of running this program:
Expand Down
29 changes: 16 additions & 13 deletions ParsecExtras.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ means you have to replace it with something more flexible later on.


> --import qualified Text.Parsec as P
> import Text.Parsec (oneOf, many1, letter, char, digit, string)
> import qualified Text.Parsec.String as P (Parser)
> import Text.Parsec.String (Parser)
> import Text.Parsec.String.Combinator (many1)
> import Text.Parsec.String.Char (letter, char, digit, string, oneOf)

> import Control.Applicative ((<$>), (<*>), (<*), (<|>), many)
> import Control.Monad (void)
> import Control.Monad.Identity (Identity)
> import qualified Text.Parsec.Expr as E

> import qualified Text.Parsec.String.Expr as E


= Text.Parsec.Expr
Expand Down Expand Up @@ -127,13 +129,13 @@ Here is the abstract syntax type:

Here is the new expression parser:

> simpleExpr2 :: P.Parser SimpleExpr2
> simpleExpr2 :: Parser SimpleExpr2
> simpleExpr2 = E.buildExpressionParser table term

> term :: P.Parser SimpleExpr2
> term :: Parser SimpleExpr2
> term = var2 <|> num2

> table :: [[E.Operator String () Identity SimpleExpr2]]
> table :: [[E.Operator SimpleExpr2]]
> table = [[prefix "-", prefix "+"]
> ,[binary "^" E.AssocLeft]
> ,[binary "*" E.AssocLeft
Expand All @@ -158,13 +160,14 @@ Here is the new expression parser:

TODO: expand and explain the bits.

> num2 :: P.Parser SimpleExpr2
> num2 :: Parser SimpleExpr2
> num2 = Num2 <$> integer

> var2 :: P.Parser SimpleExpr2
> var2 :: Parser SimpleExpr2
> var2 = Var2 <$> identifier

TODO: write lots of parsing examples, including parse failures with ambiguity.
TODO: write lots of parsing examples, including parse failures with
ambiguity.

issue: double prefix op.

Expand All @@ -181,13 +184,13 @@ parsers instead of writing them by hand.
TODO: examples


> whiteSpace :: P.Parser ()
> whiteSpace :: Parser ()
> whiteSpace = void $ many $ oneOf " \n\t"

> integer :: P.Parser Integer
> integer :: Parser Integer
> integer = read <$> many1 digit <* whiteSpace

> identifier :: P.Parser String
> identifier :: Parser String
> identifier = (:) <$> firstChar <*> many nonFirstChar <* whiteSpace
> where
> firstChar = letter <|> char '_'
Expand Down
Loading

0 comments on commit 47085c4

Please sign in to comment.