Created
September 12, 2013 09:53
-
-
Save stephenamills/6535198 to your computer and use it in GitHub Desktop.
preliminary parser for Azure.
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 Parser where | |
import Text.ParserCombinators.Parsec hiding (spaces) | |
import System.Environment (getArgs) | |
import Control.Monad (liftM) | |
{- Azure's AST | |
TODO: handle escaped strings, different number bases -} | |
data Clj | |
= Atom String -- symbol/special form | |
| String String -- A string | |
| Number Number -- A number (hex, dec, oct) | |
| List [Clj] -- "proper" list e.g., `(foo bar baz)' | |
| DotList [Clj] Clj -- TODO | |
type Clj = CljVal | |
readClj input = case parse clj "Clojure" input of | |
Left err -> "Parsing Error: " ++ show err | |
Right val -> "Parse Sucess:\n" | |
clj = atom | |
<|> string | |
<|> number | |
<|> sexp | |
atom = do | |
x <- letter <|> symbol | |
xs <- many (letter <|> digit <|> symbol) | |
let atomNode = x:xs | |
return $ case atomNode of | |
"#t" -> Bool True | |
"#f" -> Bool False | |
_ -> Atom atomNode | |
string = do | |
char '"' | |
s <- many (letter <|> escape) | |
char '"' | |
return $ String s | |
number = liftM (Number.read) $ many1 digit | |
sexp = list <|> parens <|> brackets | |
list = (try plainList) <|> dotList <|> quoteList | |
plainList = liftM List $ sepBy clj spaces | |
quoteList = do char '\'' | |
l <- sepBy clj spaces | |
return $ List [Atom "quote", l] | |
dotList = do | |
x <- endBy clj spaces | |
xs <- char '.' >> spaces >> clj | |
return $ DotList x xs | |
parens = do char '(' | |
l <- list | |
char ')' | |
return l | |
brackets = do char '[' | |
l <- list | |
char ']' | |
return l | |
symbol = oneOf "!#$%&|*+-/:<=>?@^_~" | |
escape = oneOf "\n\r\t\"\\" | |
spaces = skipMany1 space |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment