Created
February 19, 2011 11:52
-
-
Save chrisdone/835021 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 Language.Lisk.Parser2 where | |
------------------------------------------------------------------------------ | |
-- S-expression parser (Lisk tokenizer) | |
-- | |
import Control.Applicative | |
import Text.Parsec hiding ((<|>),many,token,optional,spaces) | |
import Text.Parsec.Combinator hiding (optional) | |
data Expr = Atom Atom | List [Expr] | Quote Expr | UnQuote Expr | Splice Expr | |
deriving Show | |
data Atom = Var String | Cons String | |
deriving Show | |
expr :: Parsec String () Expr | |
expr = atom <|> list <|> quote <|> try splice <|> unquote where | |
list = fmap List $ parens $ sepBy expr spaces1 | |
spaces1 = space *> many space | |
atom = Atom <$> (var <|> cons) | |
var = Var <$> atomStr | |
cons = Cons <$> (char '\'' *> atomStr) | |
quote = Quote <$> (char '`' *> expr) | |
unquote = UnQuote <$> (char ',' *> expr) | |
splice = Splice <$> (string ",@" *> expr) | |
atomStr = ((:) <$> atomCharFirst <*> many atomChar) | |
atomChar = letter <|> digit <|> oneOf "_'" | |
atomCharFirst = letter <|> digit <|> oneOf "_" | |
parens = between (char '(') (char ')') | |
------------------------------------------------------------------------------ | |
-- | |
-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment