Skip to content

Instantly share code, notes, and snippets.

@ldfallas
Created January 6, 2012 12:26
Show Gist options
  • Save ldfallas/1570370 to your computer and use it in GitHub Desktop.
Save ldfallas/1570370 to your computer and use it in GitHub Desktop.
Little Parsec experiment
module Experiment where
import Text.Parsec
import Text.Parsec.Token
import Text.Parsec.Language
data Expr = ScSymbol String
| ScString String
| ScNumber Integer
| ScCons Expr Expr
| ScNil
deriving Show
mydef = emptyDef {
commentLine = ";;"
, reservedOpNames = []}
mytparser = makeTokenParser mydef
TokenParser {
identifier = idParser,
reservedOp = opParser,
stringLiteral = stringLiteralParser,
natural = numParser,
parens = parParser
} = mytparser
-- atom:: Parser Expr
atom =
(do
id <- idParser
return $ ScSymbol id)
<|>
(do str <- stringLiteralParser
return $ ScString str)
<|>
(do num <- numParser
return $ ScNumber num)
comp =
do exprs <- parParser (many scExprParser)
return $ foldr ScCons ScNil exprs
scExprParser =
atom <|> comp
doIt input = parse scExprParser "" input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment