-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParsing.hs
95 lines (71 loc) · 2.35 KB
/
Parsing.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module Parsing where
import Data.Char
import Control.Applicative
newtype Parser a = P (String -> [(a, String)])
parse :: Parser a -> String -> [(a, String)]
parse (P p) inp = p inp
item = P (\inp -> case inp of
[] -> []
(x:xs) -> [(x,xs)])
instance Functor Parser where
fmap g p = P (\inp -> case parse p inp of
[] -> []
xs -> map (\(x,s) -> (g x,s)) xs)
instance Applicative Parser where
-- pure :: a -> Parser a
pure x = P (\inp -> [(x,inp)])
-- (<*>) :: Parser (a -> b) -> Parser a -> Parser b
pf <*> px = P (\inp -> case parse pf inp of
[] -> []
xs -> concat $
map (\(f,s) -> parse (fmap f px) s) xs)
instance Alternative Parser where
-- empty :: Parser a
empty = P (\inp -> [])
-- (<|>) :: Parser a -> Parser a -> Parser a
p <|> q = P (\inp -> case parse p inp of
[] -> parse q inp
xs -> xs)
(<+>) :: Parser a -> Parser a -> Parser a
p <+> q = P (\inp -> parse p inp ++ parse q inp)
instance Monad Parser where
p >>= f = P (\inp -> case parse p inp of
[] -> []
xs -> concat $ map (\(x,s) -> parse (f x) s) xs)
sat :: (Char -> Bool) -> Parser Char
sat p = do x <- item
if p x then return x else empty
notP :: Parser a -> Parser ()
notP p = P (\inp -> case parse p inp of
[] -> [((),inp)]
_ -> [])
char x = sat (== x)
digit = sat isDigit
lower = sat isLower
upper = sat isUpper
letter = sat isAlpha
alphanum = sat isAlphaNum
nat :: Parser Integer
nat = do xs <- some digit
return (read xs)
int :: Parser Integer
int = do char '-'
n <- nat
return (-n)
<|> nat
string :: String -> Parser String
string [] = return []
string (x:xs) = do char x
string xs
return (x:xs)
strings :: [String] -> Parser String
strings = foldr1 (<|>) . map string
space :: Parser ()
space = do many (sat isSpace)
return ()
token :: Parser a -> Parser a
token p = do space
x <- p
space
return x
symbol = token . string