-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.hs
63 lines (52 loc) · 1.37 KB
/
parse.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
import Text.ParserCombinators.Parsec hiding ((<|>), many)
import Control.Applicative
import Control.Monad
ws :: Parser String
ws = many space
lexeme p = p <* ws
data JSONValue =
B Bool
| S String
| A [JSONValue]
| O [(String, JSONValue)]
deriving Show
stringLiteral :: Parser String
stringLiteral = char '"' *> many (noneOf ['"']) <* char '"'
bool :: Parser Bool
bool = (string "true" *> pure True )
<|>(string "false" *> pure False)
array :: Parser [JSONValue]
array =
(lexeme $ char '[')
*>
((lexeme jsonValue) `sepBy` (lexeme $ char ','))
<*
(lexeme $ char ']')
jsonObject :: Parser [(String, JSONValue)]
jsonObject =
(lexeme $ char '{')
*>
((lexeme objectEntry) `sepBy` (lexeme $ char ','))
<*
(lexeme $ char '}')
objectEntry :: Parser (String, JSONValue)
objectEntry =
do key <- lexeme stringLiteral
lexeme $ char ':'
value <- lexeme jsonValue
return (key, value)
jsonValue :: Parser JSONValue
jsonValue = (B <$> bool)
<|> (S <$> stringLiteral)
<|> (A <$> array)
<|> (O <$> jsonObject)
(<||>) :: Parser a -> Parser a -> Parser a
p <||> q = (try p) <|> q
day :: Parser Int
day = (string "Monday" *> pure 1)
<||> (string "Tuesday" *> pure 2)
<||> (string "Wednesday" *> pure 3)
<||> (string "Thursday" *> pure 4)
<||> (string "Friday" *> pure 5)
<||> (string "Saturday" *> pure 5)
<||> (string "Sunday" *> pure 5)