-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONparser.fsx
More file actions
212 lines (145 loc) · 4.27 KB
/
JSONparser.fsx
File metadata and controls
212 lines (145 loc) · 4.27 KB
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
#load "ParserLib.fsx"
open System
open ParserLib
type JValue =
| JString of string
| JNumber of float
| JBool of bool
| JNull
| JObject of Map<string, JValue>
| JArray of JValue list
let createParserForwardedToRef<'a>() =
let dummyParser : Parser<'a> =
let innerFn _ =
failwith "unfixed forwarded parser"
{parseFn=innerFn; label="unknown"}
let parserRef = ref dummyParser
let innerFn input =
runOnInput !parserRef input
let wrapperParser = {parseFn=innerFn; label="unknown"}
wrapperParser, parserRef
let jValue, jValueRef = createParserForwardedToRef<JValue>()
let ( >>% ) p x =
p |>> (fun _ -> x)
let jNull =
pstring "null"
>>% JNull
<?> "null"
let jBool =
let jtrue =
pstring "true"
>>% JBool true
let jfalse =
pstring "false"
>>% JBool false
jtrue <|> jfalse
<?> "bool"
let jUnescapedChar =
let label = "char"
satisfy (fun ch -> ch <> '\\' && ch <> '\"') label
let jEscapedChar =
[
//(stingToMatch, resultChar)
("\\\"", '\"')
("\\\\", '\\')
("\\/", '/')
("\\b",'\b')
("\\f",'\f')
("\\n",'\n')
("\\r",'\r')
("\\t",'\t')
]
|> List.map (fun (toMatch, result) ->
pstring toMatch >>% result)
|> choice
<?> "escaped char"
let jUnicodeChar =
let backslash = pchar '\\'
let uChar = pchar 'u'
let hexdigit =
anyOf (['0'..'9'] @ ['A'..'F'] @ ['a'..'f'])
let fourHexDigits =
hexdigit .>>. hexdigit .>>. hexdigit .>>. hexdigit
let convertToChar (((h1,h2),h3),h4) =
let str = sprintf "%c%c%c%c" h1 h2 h3 h3
Int32.Parse(str,Globalization.NumberStyles.HexNumber) |> char
backslash >>. uChar >>. fourHexDigits
|>> convertToChar
let quotedString =
let quote = pchar '\"' <?> "quote"
let jchar = jUnescapedChar <|> jEscapedChar <|> jUnicodeChar
quote >>. manyChars jchar .>> quote
let jString =
quotedString
|>> JString
<?> "quoted string"
let jNumber =
let optSign = opt (pchar '-')
let zero = pstring "0"
let digitOneNine =
satisfy (fun ch -> Char.IsDigit ch && ch <> '0') "1-9"
let digit =
satisfy (fun ch -> Char.IsDigit ch) "digit"
let point = pchar '.'
let e = pchar 'e' <|> pchar 'E'
let optPlusMinus = opt (pchar '-' <|> pchar '+')
let nonZeroInt =
digitOneNine .>>. manyChars digit
|>> fun (first,rest) -> string first + rest
let intPart = zero <|> nonZeroInt
let fractionPart = point >>. manyChars1 digit
let exponentPart = e >>. optPlusMinus .>>. manyChars1 digit
let ( |>? ) opt f =
match opt with
| None -> ""
| Some x -> f x
let convertToJNumber (((optSign,intPart), fractionPart),expPart) =
let signStr =
optSign
|>? string
let fractionPartStr =
fractionPart
|>? (fun digits -> "." + digits)
let expPartStr =
expPart
|>? fun (optSign, digits) ->
let sign = optSign |>? string
"e" + sign + digits
(signStr + intPart + fractionPartStr + expPartStr)
|> float
|> JNumber
optSign .>>. intPart .>>. opt fractionPart .>>. opt exponentPart
|>> convertToJNumber
<?> "number"
let jNumber_ = jNumber .>> spaces1
let jArray =
let left = pchar '[' .>> spaces
let right = pchar ']' .>> spaces
let comma = pchar ',' .>> spaces
let value = jValue .>> spaces
let values = sepBy value comma
between left values right
|>> JArray
<?> "array"
let jObject =
let left = spaces >>. pchar '{' .>> spaces
let right = pchar '}' .>> spaces
let colon = pchar ':' .>> spaces
let comma = pchar ',' .>> spaces
let key = quotedString .>> spaces
let value = jValue .>> spaces
let keyValue = (key .>> colon) .>>. value
let keyValues = sepBy keyValue comma
between left keyValues right
|>> Map.ofList
|>> JObject
<?> "object"
jValueRef.Value <- choice
[
jNull
jBool
jNumber
jString
jArray
jObject
]