-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchemeParser.hs
79 lines (65 loc) · 2.18 KB
/
SchemeParser.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{-# LANGUAGE ExistentialQuantification #-}
------------------------------------Parsing the Lisp Style Syntax ---------------------------
module SchemeParser where
import LispVal
import Control.Monad (liftM)
import Control.Monad.Error
import Data.Array (Array (..), listArray)
import Data.Char (toLower)
import Data.Complex (Complex (..))
import Data.IORef
import Data.Ratio (Rational (..), (%))
import System.IO hiding (try)
import Numeric (readOct, readHex)
import System.Environment
import Text.ParserCombinators.Parsec hiding (spaces)
readOrThrow :: Parser a -> String -> ThrowsError a
readOrThrow parser input = case parse parser "lisp" input of
Left err -> throwError $ Parser err
Right val -> return val
--readExpr :: String -> ThrowsError LispVal
readExpr = readOrThrow parseExpr
readExprList = readOrThrow (endBy parseExpr spaces)
symbol :: Parser Char -- parser that recognises the symbol
symbol = oneOf "&!$%|*+-/:<=?>@^_~#"
spaces :: Parser () -- additonal features to recognize spaces
spaces = skipMany1 space
parseString :: Parser LispVal -- parses String
parseString = do
char '"'
s <- many (noneOf "\"")
char '"'
(return . String) s
parseNumber :: Parser LispVal
parseNumber = liftM ( Number . read ) $ many1 digit -- to parse number
parseAtom :: Parser LispVal
parseAtom = do
first <- letter <|> symbol
rest <- many (letter <|> digit <|> symbol)
let atom = [first] ++ rest
return $ case atom of
"#t" -> Bool True
"#f" -> Bool False
otherwise -> Atom atom
parseList :: Parser LispVal
parseList = liftM List $ sepBy parseExpr spaces
parseDottedList :: Parser LispVal -- definitions to define special characters inside the quotes
parseDottedList = do
head <- endBy parseExpr spaces
tail <- char '.' >> spaces >> parseExpr
return $ DottedList head tail
parseQuoted :: Parser LispVal -- defintion to allow nested quotes"
parseQuoted = do
char '\''
x <- parseExpr
return $ List [ Atom "quote", x]
parseExpr :: Parser LispVal -- to parse according to data type Here the order in which I have mentioned things matter
parseExpr = parseAtom
<|> parseString
<|> parseNumber
<|> parseQuoted
<|> do
char '('
x <- (try parseList) <|> parseDottedList
char ')'
return x