Skip to content

Instantly share code, notes, and snippets.

@ldfallas
Created January 7, 2012 14:21
Show Gist options
  • Save ldfallas/1574860 to your computer and use it in GitHub Desktop.
Save ldfallas/1574860 to your computer and use it in GitHub Desktop.
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