-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex3 - Json.fan
348 lines (316 loc) · 11.3 KB
/
ex3 - Json.fan
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
const class State {
const Int pos
const Str remaining
Bool empty() { remaining.isEmpty }
new make(Str src, Int pos := 0) { this.pos = pos; remaining = src }
This advance(Int n) { make((n<remaining.size)? remaining.slice(n..-1): "", pos+n) }
override Str toStr() { "@$pos:'$remaining'" }
}
**
** Result is the result of a parse, combining a parsed value and
** the remaining state
**
abstract const class Result
{
Obj? val() { (this as Ok)?.v }
State? rest() { (this as Ok)?.s }
Bool okay() { this is Ok }
Bool error() { this is Error }
**
** construct an 'Ok' result: a result value, and a snapshot of state
**
static Result Ok(Obj? a, State? s) { Ok(a,s) }
**
** construct an 'Error' result with current state.
**
static Result Error(State at) { Error(at) }
}
**
** an Ok result holds a parse result value, and the new state
**
const class Ok : Result {
const Obj? v;
const State? s;
new make(Obj? v, State s) {this.v=v; this.s=s}
override Str toStr() { "Ok [$v, $s]" }
}
**
** an Error result holds the state at the point of the error.
**
const class Error : Result {
const State at
new make(State at) {this.at=at}
override Str toStr() { "Error at $at" }
}
** Parser a = Parser (State -> [a, State])
const mixin ParserMonad {
**
** monadic unit: convert a basic value into a parser-monadic-function
** by wrapping it into a Result along with the then-current state.
**
|State->Result| unit(Obj? v) {/*echo("->$v"); return*/ |State? st->Result|{ Result.Ok(v, st)} }
**
** monadic bind: bind a parser monad to a combining-function.
** return a bound computation which will run the monad in a
** current state; if the parser ('p') fails, immediately return
** the error result; otherwise, pass the resulting value to the
** combining-function to get a new monadic function, then call
** that second function with the new state.
**
|State->Result| bind(|State->Result| p, |Obj?-> |State->Result| | f)
{
//echo(p)
return |State inp->Result|
{
//echo(inp)
r1 := p.call(inp) // do computation m, get a result
if (r1.error) return r1
p2 := f.call(r1.val) // feed the computed val to f,
// and get another parser, p2.
return p2.call(r1.rest) // do the new parse in the new state,
// and return its result
}
}
**
** parser-deconstructor function: apply parser 'p' to state 'st',
** and return the result.
**
Result parse(|State->Result| p, State st) { (p)(st) }
**
** return the parser monad's "zero" function: a parser that
** always immediately fails, capturing the state at the fail point.
**
|State->Result| zero() { |State inp->Result| {Result.Error(inp)} }
**
** "plus" combinator that tries each parser in turn,
** returning the first successful parse
**
|State->Result| or(|State->Result| p, |State->Result| q)
{
|State inp->Result| {
r1 := p(inp); if (r1.okay) return r1
r2 := q(inp); if (r2.okay) return r2
return Result.Error(inp)
}
}
**
** "plus" combinator that tries each parser in turn,
** returning the first successful parse
**
|State->Result| choice(|State->Result|[] ps)
{
|State inp->Result| {
rslt := ps.eachWhile | |State->Result| p ->Obj?| {
//echo(inp);
r := p(inp);
if (r.okay) return r; return null
}
return rslt ?: Result.Error(inp)
}
}
**
** return a parser that executes 'p', followed by 'q', and
** returns their results in a list. Parse succeeds only if
** both 'p' and 'q' succeed.
**
|State->Result| seq(|State->Result| p, |State->Result| q) {
return bind(p, |Obj? x-> |State->Result| | { //echo("x: $x");
return bind(q, |Obj? y-> |State->Result| | { //echo("y: $y");
return unit([x,y])
})})
}
**
** return a parser that executes 'p' as long as it succeeds,
** concatenating the results.
**
|State->Result| many(|State->Result| p) { or(many1(p), unit(null)) }
**
** execute 'p' one or more times, concatenating the results,
** or fail if there is not at least one success.
**
|State->Result| many1(|State->Result| p) {
bind( p , |Obj? x-> |State->Result| | {
bind( many(p), |Obj? y-> |State->Result| | {
unit([x,y])
})})
}
**
**
**
|State->Result| sepby(|State->Result| p, |State->Result| sep) {
or(sepby1(p, sep), unit([,]))
}
|State->Result| sepby1(|State->Result| p, |State->Result| sep) {
bind( p , |Obj? x-> |State->Result| | {
bind( many(
bind(sep, |Obj? _-> |State->Result| | {
bind( p, |Obj? b-> |State->Result| | {
unit(b)
})})
), |Obj? y-> |State->Result| | {
unit([x,y])
})})
}
|State->Result| chainl(|State->Result| p, |State->Result| op, Obj? a) {
or(chainl1(p, op), unit(a))
}
|State->Result| chainr(|State->Result| p, |State->Result| op, Obj? a) {
or(chainr1(p, op), unit(a))
}
|State->Result| chainl1(|State->Result| p, |State->Result| op) {
|Obj? -> |State->Result| |? rest
rest = |Obj? x -> |State->Result| | {
or( bind(op, |Obj? f-> |State->Result| | {
bind( p, |Obj? y-> |State->Result| | {
rest("($f $x,$y)")
})}),
unit(x) )
}
return bind(p, |Obj? x-> |State->Result| | {
rest(x)
})
}
|State->Result| chainleft(|State->Result| p, |State->Result| op) {
|Obj? -> |State->Result| |? rest // forward decl, so we can recurse
rest = |Obj? x -> |State->Result| | {
or( bind(op, |Obj? f-> |State->Result| | {
bind( p, |Obj? y-> |State->Result| | {
func := f as |Int,Int->Int| // typecheck
return rest(func.call(x,y))
})}),
unit(x) )
}
return bind(p, |Obj? x-> |State->Result| | {
rest(x)
})
}
|State->Result| chainr1(|State->Result| p, |State->Result| op) {
bind(p, |Obj? x-> |State->Result| | {
or( bind(op, |Obj? f-> |State->Result| | {
bind(chainr1(p, op), |Obj? y-> |State->Result| | {
unit("($f $x,$y)")
})}),
unit(x) )
})
}
|State->Result| chainright(|State->Result| p, |State->Result| op) {
bind(p, |Obj? x-> |State->Result| | {
or( bind(op, |Obj? f-> |State->Result| | {
bind(chainright(p, op), |Obj? y-> |State->Result| | {
func := f as |Int,Int->Int| // typecheck
return unit(func.call(x,y))
})}),
unit(x) )
})
}
**
** parse a bracketed expression, returning the parse
** being bracketed.
**
|State->Result| bracket(|State->Result| open,
|State->Result| p,
|State->Result| close)
{
bind( open, |Obj? a -> |State->Result| | {
bind( p, |Obj? x -> |State->Result| | {
bind(close, |Obj? b -> |State->Result| | {
unit(x)
})})})
}
}
**
** Parser monad that recognizes Json (json.org) grammar (assuming no extraneous whitespace)
**
const class JsonParser : ParserMonad
{
// parser-func generator methods for Json grammar
|State->Result| object () { bracket(char('{'), members, char('}')) }
|State->Result| members () { sepby(pair, char(',')) }
|State->Result| pair () { seq(str, seq(char(':'), value)) }
|State->Result| array () { bracket(char('['), elements, char(']')) }
|State->Result| elements() { sepby(value, char(',')) }
|State->Result| str () { bracket(char('"'), many(strchar), char('"')) }
|State->Result| strchar () { or(nonCtrl, escaped) }
|State->Result| nonCtrl () { satisfy(|Int c->Bool|{!(c=='"' || c=='\\' || c<' ')}) }
|State->Result| escaped () { seq(char('\\'), escChar) }
|State->Result| escChar () { or(oneOf("\"\\/bfnrt"), seq(char('u'), hex4)) }
|State->Result| number () { seq(int, seq(fracOpt, expOpt)) }
|State->Result| int () { seq( signOpt, or(char('0'), seq(nonzero,digits)) ) }
|State->Result| fracOpt () { or(seq(char('.'), digits), nothing) }
|State->Result| expOpt () { or(seq(e, digits) ,nothing) }
|State->Result| digits () { many(digit) }
|State->Result| hex4 () { seq(seq(seq(hexdigit,hexdigit),hexdigit),hexdigit) }
|State->Result| hexdigit() { oneOf("0123456789abcdefABCDEF") }
|State->Result| digit () { satisfy(|Int c->Bool|{('0'..'9').contains(c)}) }
|State->Result| nonzero () { satisfy(|Int c->Bool|{('1'..'9').contains(c)}) }
|State->Result| signOpt () { or(char('-'), nothing) }
|State->Result| nothing () { unit("") }
** eN, e+N, e-N, EN, E+N, E-N all good; this parses the "e" and optional sign
|State->Result| e() { seq(oneOf("eE"), or(oneOf("+-"), nothing)) }
// generator-methods syntax (above) is convenient, but you have to break the
// mutual recursion somewhere. So 'value' will, instead of immediately creating
// a parser, will instead create a closure that when evaluated during
// parsing, will create a parser and then call it.
//
// Mutual recursion is fine, during a parse, because we're pretty sure that
// the input will terminate eventually, no matter how deep the nested objects
// go. The problem is just in trying to pre-declare the parser structure:
// without this change, it's trying to build an infinite-capacity parser
// before even looking at the input.
/*const |State->Result| value := |State inp->Result| {
or(str,
or(number,
or(object,
or(array,
or(keyword("true"),
or(keyword("false"),
keyword("null")
))))))
.call(inp)
}*/
const |State->Result| value := |State inp->Result| {
choice([
str,
number,
object,
array,
keyword("true"),
keyword("false"),
keyword("null")
]).call(inp)
}
|State->Result| item() {
|State in->Result| {
if (in.empty) return Result.Error(in)
char := in.remaining[0] // grab any char
state := in.advance(1) // move state past it
return Result.Ok(char.toChar, state) // return char and state
}
}
** parse any single character that satisfies the test predicate
|State->Result| satisfy(|Int->Bool| test) {
bind(item, |Str x-> |State->Result| | {
(test(x[0])) ? unit(x) : zero
})
}
|State->Result| oneOf(Str cs) { satisfy(|Int c->Bool|{ cs.any|Int ch->Bool|{c==ch}}) }
|State->Result| noneOf(Str cs) { satisfy(|Int c->Bool|{!cs.any|Int ch->Bool|{c==ch}}) }
|State->Result| char(Int ch) { satisfy(|Int c->Bool|{ch == c}) }
|State->Result| keyword(Str s) {
(s.isEmpty)
? unit("")
: seq(char(s[0]), keyword(s.slice(1..-1)))
}
Void main() {
echo(str.call(State("\"a\"")))
echo(number.call(State("123")))
echo(escaped.call(State("\\uabcd123"))) // parse the unicode, ignore the 123
echo(pair.call(State("\"a\":123")))
echo(sepby(number, char(',')).call(State("1,2,3")))
echo(many(bracket(char('['), sepby(number, char(',')), char(']'))).call(State("[1,2,3]")))
echo(array.call(State("[1,2,3]")))
echo(object.call(State("""{"a":[1,2,3]}""")))
echo(object.call(State("""{"id":12.3,"ints":[1,2,3],"value":456e8}""")))
echo(object.call(State("""{"ident":123,"value":-456e8}""")))
}
}