-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.hs
38 lines (27 loc) · 820 Bytes
/
Parser.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
module Parser where
import Text.Parsec hiding (parse)
import qualified Text.Parsec as P (parse)
type Parser a = Parsec String () a
data Exp = EAtom Atom | EList [Exp]
deriving (Show, Eq)
data Atom = Number Int | Symbol String
deriving (Show, Eq)
parse :: String -> Either ParseError Exp
parse = P.parse (elist <|> eatom) ""
elist :: Parser Exp
elist = (between (char '(') (char ')') (parsecMap EList (sepBy (eatom <|> elist) space)))
pword :: Parser String
pword = do
x <- many1 $ noneOf ['(', ')', ' ']
return x
pnum :: Parser Int
pnum = do
num <- parsecMap read (many1 digit)
notFollowedBy letter
return num
eatom :: Parser Exp
eatom = parsecMap EAtom atom
atom :: Parser Atom
atom = choice [number, symbol]
where number = parsecMap Number $ try pnum
symbol = parsecMap Symbol pword