-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathMain.hs
288 lines (255 loc) · 8.96 KB
/
Main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ViewPatterns #-}
module Main where
import Control.Applicative
import Data.Char
import Numeric
import System.Exit
data Input = Input
{ inputLoc :: Int
, inputStr :: String
} deriving (Show, Eq)
-- | Pull the first character of the input if there is one still input
inputUncons :: Input -- input to check
-> Maybe (Char, Input)
inputUncons (Input _ []) = Nothing
inputUncons (Input loc (x:xs)) = Just (x, Input (loc + 1) xs)
data JsonValue
= JsonNull
| JsonBool Bool
| JsonNumber Double
| JsonString String
| JsonArray [JsonValue]
| JsonObject [(String, JsonValue)]
deriving (Show, Eq)
data ParserError = ParserError Int String deriving (Show)
newtype Parser a = Parser
{ runParser :: Input -> Either ParserError (Input, a)
}
instance Functor Parser where
fmap f (Parser p) =
Parser $ \input -> do
(input', x) <- p input
return (input', f x)
instance Applicative Parser where
pure x = Parser $ \input -> Right (input, x)
(Parser p1) <*> (Parser p2) =
Parser $ \input -> do
(input', f) <- p1 input
(input'', a) <- p2 input'
return (input'', f a)
instance Alternative (Either ParserError) where
empty = Left $ ParserError 0 "empty"
Left _ <|> e2 = e2
e1 <|> _ = e1
instance Alternative Parser where
empty = Parser $ const empty
(Parser p1) <|> (Parser p2) =
Parser $ \input -> p1 input <|> p2 input
-- | Parser for null json
jsonNull :: Parser JsonValue
jsonNull = JsonNull <$ stringP "null"
-- | Create a parser for a single specific character
charP :: Char -- The single character to find in the input
-> Parser Char
charP x = Parser f
where
f input@(inputUncons -> Just (y, ys))
| y == x = Right (ys, x)
| otherwise =
Left $
ParserError
(inputLoc input)
("Expected '" ++ [x] ++ "', but found '" ++ [y] ++ "'")
f input =
Left $
ParserError
(inputLoc input)
("Expected '" ++ [x] ++ "', but reached end of string")
-- | Create a parser for a specific string
stringP :: String -- String to find in the input
-> Parser String
stringP str =
Parser $ \input ->
case runParser (traverse charP str) input of
Left _ ->
Left $
ParserError
(inputLoc input)
("Expected \"" ++ str ++ "\", but found \"" ++ inputStr input ++ "\"")
result -> result
-- | Create a parser for boolean values
jsonBool :: Parser JsonValue
jsonBool = jsonTrue <|> jsonFalse
where
jsonTrue = JsonBool True <$ stringP "true"
jsonFalse = JsonBool False <$ stringP "false"
-- | Parser of strings where all characters satifsfy a predicate
spanP :: String -- description
-> (Char -> Bool) -- predicate
-> Parser String
spanP desc = many . parseIf desc
-- | Parser of a character that satisfies a predicate
parseIf :: String -- Description of the predicate
-> (Char -> Bool) -- predicate
-> Parser Char
parseIf desc f =
Parser $ \input ->
case input of
(inputUncons -> Just (y, ys))
| f y -> Right (ys, y)
| otherwise ->
Left $
ParserError
(inputLoc input)
("Expected " ++ desc ++ ", but found '" ++ [y] ++ "'")
_ ->
Left $
ParserError
(inputLoc input)
("Expected " ++ desc ++ ", but reached end of string")
{-
See page 12 of
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
-}
-- | Parser for doubles
doubleLiteral :: Parser Double
doubleLiteral =
doubleFromParts
<$> (minus <|> pure 1)
<*> (read <$> digits)
<*> ((read <$> (('0':) <$> ((:) <$> charP '.' <*> digits))) <|> pure 0)
<*> ((e *> ((*) <$> (plus <|> minus <|> pure 1) <*> (read <$> digits))) <|> pure 0)
where
digits = some $ parseIf "digit" isDigit
minus = (-1) <$ charP '-'
plus = 1 <$ charP '+'
e = charP 'e' <|> charP 'E'
-- | Build a Double from its parts (sign, integral part, decimal part, exponent)
doubleFromParts :: Integer -- sign
-> Integer -- integral part
-> Double -- decimal part
-> Integer -- exponent
-> Double
doubleFromParts sign int dec expo =
fromIntegral sign * (fromIntegral int + dec) * (10 ^^ expo)
-- | Parser for json number values
jsonNumber :: Parser JsonValue
jsonNumber = JsonNumber <$> doubleLiteral
-- | Parser for characters as unicode in input
escapeUnicode :: Parser Char
escapeUnicode = chr . fst . head . readHex <$> sequenceA (replicate 4 (parseIf "hex digit" isHexDigit))
-- | Parser for characters that are scaped in the input
escapeChar :: Parser Char
escapeChar = ('"' <$ stringP "\\\"") <|>
('\\' <$ stringP "\\\\") <|>
('/' <$ stringP "\\/") <|>
('\b' <$ stringP "\\b") <|>
('\f' <$ stringP "\\f") <|>
('\n' <$ stringP "\\n") <|>
('\r' <$ stringP "\\r") <|>
('\t' <$ stringP "\\t") <|>
(stringP "\\u" *> escapeUnicode)
-- | Parser of a character that is not " or \\
normalChar :: Parser Char
normalChar = parseIf "non-special character" ((&&) <$> (/= '"') <*> (/= '\\'))
-- | Parser of a string that is between double quotes (not considering any double quots that are scaped)
stringLiteral :: Parser String
stringLiteral = charP '"' *> many (normalChar <|> escapeChar) <* charP '"'
-- | Parser of literal json string values
jsonString :: Parser JsonValue
jsonString = JsonString <$> stringLiteral
-- | Parser for white spaces
ws :: Parser String
ws = spanP "whitespace character" isSpace
-- | Creates a parser for a string of type "element1 sep1 element2 sep2 element3"
-- from a parser for separators (sep1, sep2) and and a parser form elements (element1, element2, element3).
sepBy :: Parser a -- Parser for the separators
-> Parser b -- Parser for elements
-> Parser [b]
sepBy sep element = (:) <$> element <*> many (sep *> element) <|> pure []
-- | Parser for json arrays
jsonArray :: Parser JsonValue
jsonArray = JsonArray <$> (charP '[' *> ws *> elements <* ws <* charP ']')
where
elements = sepBy (ws *> charP ',' <* ws) jsonValue
-- | Parser for json objects
jsonObject :: Parser JsonValue
jsonObject =
JsonObject <$>
(charP '{' *> ws *> sepBy (ws *> charP ',' <* ws) pair <* ws <* charP '}')
where
pair = liftA2 (,) (stringLiteral <* ws <* charP ':' <* ws) jsonValue
-- | Parser for any json
jsonValue :: Parser JsonValue
jsonValue =
jsonNull <|> jsonBool <|> jsonNumber <|> jsonString <|> jsonArray <|>
jsonObject
-- | Apply parser to content of file
parseFile :: FilePath -- File path to parse
-> Parser a -- Parser to use
-> IO (Either ParserError a)
parseFile fileName parser = do
input <- readFile fileName
case runParser parser $ Input 0 input of
Left e -> return $ Left e
Right (_, x) -> return $ Right x
-- >>> main
-- [INFO] JSON:
-- {
-- "hello": [false, true, null, 42, "foo\n\u1234\"", [1, -2, 3.1415, 4e-6, 5E6, 0.123e+1]],
-- "world": null
-- }
-- <BLANKLINE>
-- [INFO] Parsed as: JsonObject [("hello",JsonArray [JsonBool False,JsonBool True,JsonNull,JsonNumber 42.0,JsonString "foo\n\4660\"",JsonArray [JsonNumber 1.0,JsonNumber (-2.0),JsonNumber 3.1415,JsonNumber 4.0e-6,JsonNumber 5000000.0,JsonNumber 1.23]]),("world",JsonNull)]
-- [INFO] Remaining input (codes): [10]
-- [SUCCESS] Parser produced expected result.
--
main :: IO ()
main = do
putStrLn "[INFO] JSON:"
putStrLn testJsonText
case runParser jsonValue $ Input 0 testJsonText of
Right (input, actualJsonAst) -> do
putStrLn ("[INFO] Parsed as: " ++ show actualJsonAst)
putStrLn
("[INFO] Remaining input (codes): " ++ show (map ord $ inputStr input))
if actualJsonAst == expectedJsonAst
then putStrLn "[SUCCESS] Parser produced expected result."
else do
putStrLn
("[ERROR] Parser produced unexpected result. Expected result was: " ++
show expectedJsonAst)
exitFailure
Left (ParserError loc msg) -> do
putStrLn $
"[ERROR] Parser failed at character " ++ show loc ++ ": " ++ msg
exitFailure
where
testJsonText =
unlines
[ "{"
, " \"hello\": [false, true, null, 42, \"foo\\n\\u1234\\\"\", [1, -2, 3.1415, 4e-6, 5E6, 0.123e+1]],"
, " \"world\": null"
, "}"
]
expectedJsonAst =
JsonObject
[ ( "hello"
, JsonArray
[ JsonBool False
, JsonBool True
, JsonNull
, JsonNumber 42
, JsonString "foo\n\4660\""
, JsonArray
[ JsonNumber 1.0
, JsonNumber (-2.0)
, JsonNumber 3.1415
, JsonNumber 4e-6
, JsonNumber 5000000
, JsonNumber 1.23
]
])
, ("world", JsonNull)
]