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
|
module JSON.Parser ( parseJSON ) where
import JSON.Data
import Data.Char
import DetParse
import Test.Prop
import Prelude hiding (some, empty, (<|>), (<$>), (<*>), (<*), (*>))
parseJSON :: String -> Maybe JValue
parseJSON = parse (pWhitespace *> pJValue)
pJValue :: Parser JValue
pJValue = ( pTrue
<!> pFalse
<!> pNull
<!> pJString
<!> pJNumber
<!> pArray
<!> pObject ) <* pWhitespace
pObject :: Parser JValue
pObject = char '{' *> pWhitespace *> pObjectNB
pObjectNB :: Parser JValue
pObjectNB =
JObject . toJObject
<$> ((char '}' *> yield []) <!> (pObjectFields <* char '}'))
pObjectFields :: Parser [(String, JValue)]
pObjectFields = (:) <$>
pKeyValuePair <*> (char ',' *> pWhitespace *> pObjectFields <!> yield [])
pKeyValuePair :: Parser (String, JValue)
pKeyValuePair =
(,) <$> pString <*> (pWhitespace *> char ':' *> pWhitespace *> pJValue)
test_pObject_empty :: Prop
test_pObject_empty = parse pObject "{}" -=- Just (JObject $ toJObject [])
test_pObject_onlyStringKeys :: Prop
test_pObject_onlyStringKeys = parse pObject "{1: 2}" -=- Nothing
test_pObject_simple :: Prop
test_pObject_simple =
parse pObject "{\"test\": 1, \"test2\": false}"
-=- Just (JObject $ toJObject [("test", JInt 1), ("test2", JBool False)])
test_pObject_whitespace :: Prop
test_pObject_whitespace =
parse pObject "{\n \"test\": 1,\n \"test2\": false\n}"
-=- Just (JObject $ toJObject [("test", JInt 1), ("test2", JBool False)])
test_pObject_nested :: Prop
test_pObject_nested =
parse pObject "{\"test\": {\"hello\": \"world\"}}" -=-
Just (JObject $ toJObject
[("test", JObject $ toJObject [("hello", JString "world")])])
pArray :: Parser JValue
pArray = char '[' *> pWhitespace *> pArrayNB
pArrayNB :: Parser JValue
pArrayNB = JArray <$> ((char ']' *> yield []) <!> (pArrayElems <* char ']'))
pArrayElems :: Parser [JValue]
pArrayElems = (:) <$>
pJValue <*> ((char ',' *> pWhitespace *> pArrayElems) <!> yield [])
test_pArray_empty :: Prop
test_pArray_empty = parse pArray "[]" -=- Just (JArray [])
test_pArray_single1 :: Prop
test_pArray_single1 = parse pArray "[1]" -=- Just (JArray [JInt 1])
test_pArray_single2 :: Prop
test_pArray_single2 = parse pArray "[2.0]" -=- Just (JArray [JNumber 2.0])
test_pArray_multi :: Prop
test_pArray_multi =
parse pArray "[true, false, null]"
-=- Just (JArray [JBool True, JBool False, JNull])
test_pArray_nested :: Prop
test_pArray_nested =
parse pArray "[true, [false], [[null]]]"
-=- Just (JArray [JBool True, JArray [JBool False], JArray [JArray [JNull]]])
pWhitespace :: Parser ()
pWhitespace [] = [((),"")]
pWhitespace s@(c:cs) | c `elem` [' ','\n','\r','\t'] = pWhitespace cs
| otherwise = [((),s)]
pTrue :: Parser JValue
pTrue = word "true" *> yield (JBool True)
pFalse :: Parser JValue
pFalse = word "false" *> yield (JBool False)
pNull :: Parser JValue
pNull = word "null" *> yield JNull
pJString :: Parser JValue
pJString = JString <$> pString
pString :: Parser String
pString = char '"' *> pCharSequence <* char '"'
pCharSequence :: Parser String
pCharSequence =
(++) <$> (char '\\' *> (pEscaped <!> failure)) <*> pCharSequence
<!> (:) <$> check (\c -> c /= '"' && c /= '\\') anyChar <*> pCharSequence
<!> yield ""
pEscaped :: Parser String
pEscaped = char '"' *> yield "\""
<!> char '\\' *> yield "\\"
<!> char '/' *> yield "/"
<!> char 'b' *> yield "\b"
<!> char 'f' *> yield "\f"
<!> char 'n' *> yield "\n"
<!> char 'r' *> yield "\r"
<!> char 't' *> yield "\t"
<!> ((:[]) . chr) <$> (char 'u' *> pTwoByteHex)
pTwoByteHex :: Parser Int
pTwoByteHex =
hexToInt <$>
((:) <$> pHexDigit <*> ((:) <$> pHexDigit <*> ((:)
<$> pHexDigit <*> ((:[]) <$> pHexDigit))))
where pHexDigit = check isHexDigit anyChar
hexToInt :: String -> Int
hexToInt s = foldl1 ((+) . (16*)) (map digitToInt s)
test_pCharSequence_simple :: Prop
test_pCharSequence_simple = parse pCharSequence "test" -=- Just "test"
test_pCharSequence_noDoubleQuote :: Prop
test_pCharSequence_noDoubleQuote = parse pCharSequence "te\"st" -=- Nothing
test_pCharSequence_noStandaloneBackslash :: Prop
test_pCharSequence_noStandaloneBackslash =
parse pCharSequence "He\\world" -=- Nothing
test_pCharSequence_escapedDoubleQuote :: Prop
test_pCharSequence_escapedDoubleQuote =
parse pCharSequence "Hello \\\"World\\\"" -=- Just "Hello \"World\""
test_pCharSequence_escapedBackslash :: Prop
test_pCharSequence_escapedBackslash =
parse pCharSequence "He\\\\world" -=- Just "He\\world"
test_pCharSequence_escapedSlash :: Prop
test_pCharSequence_escapedSlash =
parse pCharSequence "He\\/world" -=- Just "He/world"
test_pCharSequence_escapedBackspace :: Prop
test_pCharSequence_escapedBackspace =
parse pCharSequence "He\\bworld" -=- Just "He\bworld"
test_pCharSequence_escapedFormFeed :: Prop
test_pCharSequence_escapedFormFeed =
parse pCharSequence "He\\fworld" -=- Just "He\fworld"
test_pCharSequence_escapedNewline :: Prop
test_pCharSequence_escapedNewline =
parse pCharSequence "He\\nworld" -=- Just "He\nworld"
test_pCharSequence_escapedCarriageReturn :: Prop
test_pCharSequence_escapedCarriageReturn =
parse pCharSequence "He\\rworld" -=- Just "He\rworld"
test_pCharSequence_escapedTab :: Prop
test_pCharSequence_escapedTab =
parse pCharSequence "He\\tworld" -=- Just "He\tworld"
test_pCharSequence_twoEscapes :: Prop
test_pCharSequence_twoEscapes =
parse pCharSequence "He\\r\\nWorld" -=- Just "He\r\nWorld"
test_pCharSequence_escapedUnicodeChar :: Prop
test_pCharSequence_escapedUnicodeChar =
parse pCharSequence "Hello \\u2603 World" -=- Just "Hello \9731 World"
test_pCharSequence_escapedUnicodeRequiresFourDigits :: Prop
test_pCharSequence_escapedUnicodeRequiresFourDigits =
parse pCharSequence "Hello \\u26 World" -=- Nothing
test_pString_simple :: Prop
test_pString_simple =
parse pString "\"Hello, World\"" -=- Just "Hello, World"
test_pString_complex :: Prop
test_pString_complex =
parse pString "\"Hello \\r\\n \\u2603 World\"" -=- Just "Hello \r\n \9731 World"
pJNumber :: Parser JValue
pJNumber = (char '-' *> pPositiveFloat True)
<!> pPositiveFloat False
pPositiveFloat :: Bool -> Parser JValue
pPositiveFloat neg = (uncurry toJNum) <$> pWithOptDecimalPoint <*> pExponent
where
toJNum n Nothing Nothing = JInt (if neg then negate n else n)
toJNum n Nothing (Just e) = toJNumber (fromInt n * (exp 10 e))
toJNum n (Just d) Nothing = toJNumber (fromInt n * (exp 10 d))
toJNum n (Just d) (Just e) = toJNumber ((fromInt n) * (exp 10 (d + e)))
toJNumber x = JNumber (if neg then negate x else x)
exp x y = if y < 0 then 1 / (x ^ (0 - y)) else (x ^ y)
pExponent :: Parser (Maybe Int)
pExponent =
(char 'e' <!> char 'E') *>
(Just <$>
((char '-' *> yield negate <!> char '+' *> yield id <!> yield id)
<*> pInt))
<!> yield Nothing
pWithOptDecimalPoint :: Parser (Int, Maybe Int)
pWithOptDecimalPoint =
combine <$> some pDigit <*> (char '.' *> some pDigit <!> yield "")
where
s2i cs = foldl1 ((+).(10*)) (map (\c' -> ord c' - ord '0') cs)
combine n d = (s2i (n ++ d),
if null d then Nothing else Just (negate $ length d))
pInt :: Parser Int
pInt =
(\cs -> foldl1 ((+).(10*)) (map (\c' -> ord c' - ord '0') cs)) <$> some pDigit
pDigit :: Parser Char
pDigit = check isDigit anyChar
|