Created
January 7, 2012 14:21
-
-
Save ldfallas/1574860 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
| ScBool Bool | |
| ScQuote Expr | |
deriving Show | |
mydef = emptyDef { | |
commentLine = ";;" | |
, identStart = alphaNum <|> oneOf ":!$%&*+./<=>?@\\^|-~" | |
, opStart = parserZero | |
, opLetter = parserZero | |
} | |
mytparser = makeTokenParser mydef | |
TokenParser { | |
identifier = idParser, | |
reservedOp = opParser, | |
stringLiteral = stringLiteralParser, | |
natural = numParser, | |
parens = parParser, | |
lexeme = lexParser | |
} = mytparser | |
boolLiteral = lexParser ( | |
do | |
char '#' | |
val <- (char 't') <|> (char 'f') | |
return $ ScBool $ val == 't' | |
) | |
quoteParser = lexParser ( | |
do | |
char '\'' | |
val <- scExprParser | |
return $ ScQuote val | |
) | |
-- atom:: Parser Expr | |
atom = | |
(do | |
id <- idParser | |
return $ ScSymbol id) | |
<|> | |
(do str <- stringLiteralParser | |
return $ ScString str) | |
<|> | |
(do num <- numParser | |
return $ ScNumber num) | |
<|> | |
boolLiteral | |
<|> | |
quoteParser | |
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