Skip to content

Instantly share code, notes, and snippets.

@chrisdone
Created February 19, 2011 11:52
Show Gist options
  • Save chrisdone/835021 to your computer and use it in GitHub Desktop.
Save chrisdone/835021 to your computer and use it in GitHub Desktop.
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