-
Notifications
You must be signed in to change notification settings - Fork 5
/
ParserTests.scala
498 lines (416 loc) · 14.6 KB
/
ParserTests.scala
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package lms.verify
// inspired by http://manojo.github.io/2015/09/04/staged-parser-combinators-recursion
trait StagedParser extends Dsl with Reader {
// Parser Result
abstract class ParseResultCPS[T: Typ] { self =>
def apply[X: Typ](
success: (Rep[T], Rep[Input]) => Rep[X],
failure: Rep[Input] => Rep[X]
): Rep[X]
def map[U: Typ](f: Rep[T] => Rep[U]) = new ParseResultCPS[U] {
def apply[X: Typ](
success: (Rep[U], Rep[Input]) => Rep[X],
failure: Rep[Input] => Rep[X]
): Rep[X] = self.apply(
(t: Rep[T], in: Rep[Input]) => success(f(t), in),
failure
)
}
def flatMapWithNext[U: Typ](f: (Rep[T], Rep[Input]) => ParseResultCPS[U])
= new ParseResultCPS[U] {
def apply[X: Typ](
success: (Rep[U], Rep[Input]) => Rep[X],
failure: Rep[Input] => Rep[X]
): Rep[X] = {
var isEmpty = unit(true); var value = zeroVal[T]; var rdr = zeroVal[Input]
self.apply[Unit](
(x, next) => { isEmpty = unit(false); value = x; rdr = next },
next => rdr = next
)
if (isEmpty) failure(rdr) else f(value, rdr).apply(success, failure)
}
}
def orElse(that: => ParseResultCPS[T]) = new ParseResultCPS[T] {
def apply[X: Typ](
success: (Rep[T], Rep[Input]) => Rep[X],
failure: Rep[Input] => Rep[X]
): Rep[X] = {
var isEmpty = unit(true); var value = zeroVal[T]; var rdr = zeroVal[Input]
self.apply[Unit](
(x, next) => { isEmpty = unit(false); value = x; rdr = next },
next => ()
)
if (isEmpty) that.apply(success, failure) else success(value, rdr)
}
}
def toResult(default: Rep[T]): Rep[T] = {
var value = default
self.apply(
(t, _) => value = t,
_ => unit(())
)
value
}
}
case class ParseResultCPSCond[T: Typ](
cond: Rep[Boolean],
t: ParseResultCPS[T],
e: ParseResultCPS[T]
) extends ParseResultCPS[T] { self =>
def apply[X: Typ](
success: (Rep[T], Rep[Input]) => Rep[X],
failure: Rep[Input] => Rep[X]
): Rep[X] = if (cond) t(success, failure) else e(success, failure)
override def map[U: Typ](f: Rep[T] => Rep[U]) = new ParseResultCPS[U] {
def apply[X: Typ](
success: (Rep[U], Rep[Input]) => Rep[X],
failure: Rep[Input] => Rep[X]
): Rep[X] = {
var isEmpty = unit(true); var value = zeroVal[T]; var rdr = zeroVal[Input]
self.apply[Unit](
(x, next) => { isEmpty = unit(false); value = x; rdr = next },
next => rdr = next
)
if (isEmpty) failure(rdr) else success(f(value), rdr)
}
}
override def orElse(that: => ParseResultCPS[T]): ParseResultCPS[T] = {
var isEmpty = unit(true); var value = zeroVal[T]; var rdr = zeroVal[Input]
self.apply[Unit](
(x, next) => { isEmpty = unit(false); value = x; rdr = next },
next => ()
)
conditional(isEmpty, that, ParseResultCPS.Success(value, rdr))
}
}
object ParseResultCPS {
def Success[T: Typ](t: Rep[T], next: Rep[Input]) = new ParseResultCPS[T] {
def apply[X: Typ](
success: (Rep[T], Rep[Input]) => Rep[X],
failure: (Rep[Input]) => Rep[X]
): Rep[X] = success(t, next)
}
def Failure[T: Typ](next: Rep[Input]) = new ParseResultCPS[T] {
def apply[X: Typ](
success: (Rep[T], Rep[Input]) => Rep[X],
failure: (Rep[Input]) => Rep[X]
): Rep[X] = failure(next)
}
}
def conditional[T: Typ](
cond: Rep[Boolean],
thenp: => ParseResultCPS[T],
elsep: => ParseResultCPS[T]
): ParseResultCPS[T] = ParseResultCPSCond(cond, thenp, elsep)
// Parser
abstract class Parser[T: Typ]
extends (Rep[Input] => ParseResultCPS[T]) {
private def flatMap[U: Typ](f: Rep[T] => Parser[U]) = Parser[U] { input =>
this(input) flatMapWithNext { (res, rdr) => f(res)(rdr) }
}
def >>[U: Typ](f: Rep[T] => Parser[U]) = flatMap(f)
def ~[U: Typ](that: => Parser[U]): Parser[(T, U)] =
for (l <- this; r <- that) yield make_tuple2(l, r)
def ~>[U: Typ](that: => Parser[U]): Parser[U] =
this flatMap { l => that }
def <~[U: Typ](that: => Parser[U]): Parser[T] =
for (l <- this; r <- that) yield l
def map[U: Typ](f: Rep[T] => Rep[U]) = Parser[U] { input =>
this(input) map f
}
def ^^[U: Typ](f: Rep[T] => Rep[U]) = map(f)
def ^^^[U: Typ](u: Rep[U]) = map(x => u)
def | (that: => Parser[T]) = Parser[T] { input =>
this(input) orElse that(input)
}
}
def __ifThenElse[A: Typ](
cond: Rep[Boolean],
thenp: => Parser[A],
elsep: => Parser[A]
): Parser[A] = Parser[A] { input => conditional(cond, thenp(input), elsep(input)) }
object Parser {
def apply[T: Typ](f: Rep[Input] => ParseResultCPS[T]) = new Parser[T] {
def apply(in: Rep[Input]) = f(in)
}
def phrase[T: Typ](p: => Parser[T], in: Rep[Input], default: Rep[T]): Rep[T] =
p(in).toResult(default)
}
// CharParsers
def acceptIf(p: Rep[Elem] => Rep[Boolean]) = Parser[Elem] { in =>
conditional(
in.atEnd,
ParseResultCPS.Failure[Elem](in),
conditional(
p(in.first),
ParseResultCPS.Success(in.first, in.rest),
ParseResultCPS.Failure[Elem](in)
)
)
}
def accept(e: Rep[Elem]): Parser[Elem] = acceptIf(_ == e)
def isLetter(c: Rep[Char]): Rep[Boolean] =
(c >= unit('a') && c <= unit('z')) ||
(c >= unit('A') && c <= unit('Z'))
def letter: Parser[Char] = acceptIf(isLetter)
def isDigit(c: Rep[Char]): Rep[Boolean] =
c >= unit('0') && c <= unit('9')
def digit: Parser[Char] = acceptIf(isDigit)
def digit2Int: Parser[Int] = digit map (c => (c - unit('0')).asInstanceOf[Rep[Int]])
def rep[T: Typ, R: Typ](p: Parser[T], z: Rep[R], f: (Rep[R], Rep[T]) => Rep[R], pz: Option[Rep[R] => Rep[Boolean]] = None) = Parser[R] { input =>
var in = input
var c = unit(true); var a = z
loop (valid_input(in) && (pz.map(_(a)).getOrElse(true)), List[Any](in, c, a), 0) {
while (c) {
p(in).apply[Unit](
(x, next) => { a = f(a, x); in = next },
next => { c = false })
}}
ParseResultCPS.Success(a, in)
}
def accept(cs: List[Char]): Parser[Unit] = cs match {
case Nil => Parser { i => ParseResultCPS.Success((), i) }
case x :: xs => accept(x) ~> accept(xs)
}
def accept(s: String): Parser[Unit] = accept(s.toList)
/*
Alternative definition of `accept`. Generates less code, but arguably
more low-level.
def accept(s: String) = Parser[Unit] { input =>
var in = input
var ok = unit(true)
for (i <- (0 until s.length):Range) {
if (ok) {
if (in.atEnd) ok = false
else if (in.first != s(i)) ok = false
else in = in.rest
}
}
conditional(
ok,
ParseResultCPS.Success((), in),
ParseResultCPS.Failure(input))
}
*/
def repUnit[T: Typ](p: Parser[T]) =
rep(p, (), { (a: Rep[Unit], x: Rep[T]) => a })
def repN[T: Typ](p: Parser[T], n: Rep[Int]) = Parser[Unit] { input =>
var ok = unit(true)
var in = input
loop(
{ i: Rep[Int] => 0<=i && valid_input(in) },
{ i: Rep[Int] => List(i, ok, in) },
{ i: Rep[Int] => n-i }) {
for (i <- 0 until n)
if (ok)
p(in).apply[Unit](
(_, next) => { in = next },
next => { ok = false })
}
conditional(ok, ParseResultCPS.Success((), in), ParseResultCPS.Failure(input))
}
}
// toy HTTP parser inspired by http://dl.acm.org/authorize?N80783
// also see https://github.com/manojo/experiments/blob/simple/src/main/scala/lms/parsing/examples/HttpParser.scala
//
// to avoid dealing with data structures, just returns
// the content length of the payload if parse successful
// -1 otherwise
trait HttpParser extends StagedParser { import Parser._
val OVERFLOW = -1
def overflowOrPos: Option[Rep[Int] => Rep[Boolean]] =
Some({ a: Rep[Int] => (a == OVERFLOW) || (0 <= a) })
def numAcc(b: Int) = { (a: Rep[Int], x: Rep[Int]) =>
if (a<0) a
else if (a>Int.MaxValue / b - b) OVERFLOW
else a*b+x
}
def num(c: Parser[Int], b: Int): Parser[Int] =
c >> { z: Rep[Int] =>
rep(c, z, numAcc(b), overflowOrPos)
}
def nat: Parser[Int] = num(digit2Int, 10)
def acceptNat: Parser[Unit] =
digit >> { z: Rep[Char] => repUnit(digit) }
def ignoredNat: Parser[Unit] = acceptNat
def anyChar: Parser[Char] = acceptIf(c => true)
def wildChar: Parser[Char] = acceptIf(c => c != '\r')
def acceptNewline: Parser[Unit] = accept("\r\n") ^^^ unit(())
def acceptLine: Parser[Unit] = repUnit(wildChar) ~> acceptNewline
def whitespaces: Parser[Unit] = repUnit(accept(' '))
def status: Parser[Int] =
(accept("HTTP/") ~> ignoredNat ~> accept('.') ~> ignoredNat ~> whitespaces) ~>
nat <~ acceptLine
val CONTENT_LENGTH = 1
def headerMap: List[(String, Int)] = ("Content-Length", CONTENT_LENGTH)::Nil
val OTHER_HEADER = 0
def headerName: Parser[Int] =
((for ((h,i) <- headerMap) yield (accept(h) ^^^ i)).reduce(_ | _)) |
//(repUnit(letter | accept('-') | accept('$')) ^^^ OTHER_HEADER)
(repUnit(acceptIf{c => c != ':' && c != ' '}) ^^^ OTHER_HEADER)
val NO_VALUE = -2
def headerValue(h: Rep[Int]) =
if (h==CONTENT_LENGTH) (nat <~ whitespaces <~ acceptNewline)
else (acceptLine ^^^ NO_VALUE)
def header: Parser[Int] =
(headerName <~ whitespaces <~ accept(':') <~ whitespaces) >> headerValue
def headers: Parser[Int] =
rep(header, 0, { (a: Rep[Int], x: Rep[Int]) => if (x==NO_VALUE) a else x })
def acceptBody(n: Rep[Int]): Parser[Int] =
if (n<0) Parser[Int] { input => ParseResultCPS.Failure(input) }
else (repN(anyChar, n) ^^^ n)// <~ acceptNewline
def http: Parser[Int] =
(status ~> headers <~ acceptNewline) >> acceptBody
def top = toplevel("p",
{ in: Rep[Input] =>
var r = unit(-1)
http(in).apply(
(v, next) => if (next.atEnd) r = v,
_ => unit(()))
r: Rep[Int]
},
{ in: Rep[Input] => valid_input(in) },
{ in: Rep[Input] => result: Rep[Int] => unit(true) })
}
trait ChunkedHttpParser extends HttpParser { import Parser._
val TRANSFER_ENCODING = 2
override def headerMap = super.headerMap :+ ("Transfer-Encoding", TRANSFER_ENCODING)
val CHUNKED = -3
override def headerValue(h: Rep[Int]) =
if (h==TRANSFER_ENCODING) (accept("chunked") ^^^ CHUNKED) <~ whitespaces <~ acceptNewline
else super.headerValue(h)
override def acceptBody(n: Rep[Int]): Parser[Int] =
if (n==CHUNKED) chunkedBody else super.acceptBody(n)
def hexDigit2Int: Parser[Int] =
digit2Int |
(acceptIf(c => c >= unit('a') && c <= unit('f')) ^^
(c => 10+(c - unit('a')).asInstanceOf[Rep[Int]]))
def hex: Parser[Int] = num(hexDigit2Int, 16)
def acceptChunk: Parser[Int] =
(hex <~ acceptNewline) >> super.acceptBody
def chunkedBody: Parser[Int] =
rep(acceptChunk, 0, { (a: Rep[Int], x: Rep[Int]) =>
if (a<0) a
else if (a>Int.MaxValue - x) OVERFLOW
else a+x
}, overflowOrPos)
}
trait ToplevelAcceptParser extends StagedParser { import Parser._
type ToplevelParser = Rep[Input] => Rep[Input]
val ps = new scala.collection.mutable.LinkedHashMap[String,ToplevelParser]
def toplevel_parser(s: String, p: Parser[Unit]): ToplevelParser =
ps.getOrElseUpdate(s, toplevel("p_"+s.replaceAll("[^A-Za-z0-9]", ""),
{ in: Rep[Input] =>
var out = in
p(in).apply(
(_, next) => out = next,
next => out = unit(0).asInstanceOf[Rep[Input]]) // ignore next on failure
out: Rep[Input]
},
{ in: Rep[Input] => valid_input(in) },
{ in: Rep[Input] => out: Rep[Input] => unit(0) == out || valid_input(out) }))
override def accept(s: String): Parser[Unit] = {
val f = toplevel_parser(s, super.accept(s))
Parser[Unit] { in =>
val out = f(in)
conditional(
unit(0) != out,
ParseResultCPS.Success(unit(()), out),
ParseResultCPS.Failure(in))
}
}
}
trait TweakParser extends StagedParser { import Parser._
override def repUnit[T: Typ](p: Parser[T]) = Parser[Unit] { input =>
var in = input
var c = unit(true)
loop (valid_input(in), List[Any](in, c), 0) {
while (c) {
p(in).apply[Unit](
(_, next) => { in = next },
next => { c = false })
}}
ParseResultCPS.Success((), in)
}
}
class ParserTests extends TestSuite {
val under = "parse"
test("0") {
trait P0 extends StagedParser { import Parser._
val p = toplevel("p",
{ in: Rep[Input] => phrase(digit2Int, in, -1) },
{ in: Rep[Input] => valid_input(in) },
{ in: Rep[Input] => result: Rep[Int] =>
result == -1 || (0 <= result && result <= 9)
})
}
check("0", (new P0 with Impl).code)
}
// overflow failures
test("1") {
trait P1 extends StagedParser { import Parser._
val p = toplevel("p",
{ in: Rep[Input] =>
phrase(rep(digit2Int, 0, { (a: Rep[Int], x: Rep[Int]) => a*10+x }), in, -1)
},
{ in: Rep[Input] => valid_input(in) },
{ in: Rep[Input] => result: Rep[Int] => unit(true) })
}
check("1", (new P1 with Impl).code)
}
// overflow verifies
test("2") {
trait P2 extends StagedParser { import Parser._
val p = toplevel("p",
{ in: Rep[Input] =>
val m = Int.MaxValue / 10 - 10
phrase(
rep(digit2Int, 0,
{ (a: Rep[Int], x: Rep[Int]) =>
if (a<0) a
else if (a>m) -1
else a*10+x
},
Some({ a: Rep[Int] => (a == -1) || (0 <= a) })),
in, -1)
},
{ in: Rep[Input] => valid_input(in) },
{ in: Rep[Input] => result: Rep[Int] =>
(result == -1) || (0 <= result)
})
}
check("2", (new P2 with Impl).code)
}
test("3a") {
trait P3a extends HttpParser {
override def http = headers
val p = top
}
check("3a", (new P3a with Impl).code)
}
test("3") {
trait P3 extends HttpParser with ToplevelAcceptParser with TweakParser {
val p = top
}
check("3", (new P3 with Impl).code)
}
test("4") {
trait P4 extends ChunkedHttpParser {
override def http = chunkedBody
val p = top
}
check("4", (new P4 with Impl).code)
}
test("5") {
trait P5 extends ChunkedHttpParser with ToplevelAcceptParser with TweakParser {
val p = top
// Variations
// still parse ignored nat
//override def ignoredNat = nat ^^^ ()
// no overflow check
//override def overflowOrPos = None
//override def numAcc(b: Int) = { (a: Rep[Int], x: Rep[Int]) => a*b+x }
}
check("5", (new P5 with Impl).code)
}
}