This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
forked from j-mie6/parsley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntrinsicInstrs.scala
464 lines (446 loc) · 15.7 KB
/
IntrinsicInstrs.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
package org.http4s
package parsley.instructions
import parsley.{DeepEmbedding, UnsafeOption}
import Stack.isEmpty
import scala.collection.mutable.ListBuffer
private [parsley] final class Lift2[A, B, C](f: (A, B) => C) extends Instr
{
private [this] val g = f.asInstanceOf[(Any, Any) => C]
override def apply(ctx: Context): Unit =
{
val y = ctx.stack.upop()
ctx.stack.exchange(g(ctx.stack.peek, y))
ctx.inc()
}
override def toString: String = "Lift2(f)"
}
private [parsley] final class Lift3[A, B, C, D](f: (A, B, C) => D) extends Instr
{
private [this] val g = f.asInstanceOf[(Any, Any, Any) => D]
override def apply(ctx: Context): Unit =
{
val z = ctx.stack.upop()
val y = ctx.stack.upop()
ctx.stack.exchange(g(ctx.stack.peek, y, z))
ctx.inc()
}
override def toString: String = "Lift3(f)"
}
private [parsley] final class Many(var label: Int) extends JumpInstr with Stateful
{
private [this] val acc: ListBuffer[Any] = ListBuffer.empty
override def apply(ctx: Context): Unit =
{
if (ctx.status eq Good)
{
acc += ctx.stack.upop()
ctx.checkStack.head = ctx.offset
ctx.pc = label
}
// If the head of input stack is not the same size as the head of check stack, we fail to next handler
else if (ctx.offset != ctx.checkStack.head) { ctx.checkStack = ctx.checkStack.tail; acc.clear(); ctx.fail() }
else
{
ctx.stack.push(acc.toList)
acc.clear()
ctx.checkStack = ctx.checkStack.tail
ctx.status = Good
ctx.inc()
}
}
override def toString: String = s"Many($label)"
override def copy: Many = new Many(label)
}
private [parsley] final class SkipMany(var label: Int) extends JumpInstr
{
override def apply(ctx: Context): Unit =
{
if (ctx.status eq Good)
{
ctx.stack.pop_()
ctx.checkStack.head = ctx.offset
ctx.pc = label
}
// If the head of input stack is not the same size as the head of check stack, we fail to next handler
else if (ctx.offset != ctx.checkStack.head) { ctx.checkStack = ctx.checkStack.tail; ctx.fail() }
else
{
ctx.checkStack = ctx.checkStack.tail
ctx.status = Good
ctx.stack.push(())
ctx.inc()
}
}
override def toString: String = s"SkipMany($label)"
}
private [parsley] final class ChainPost(var label: Int) extends JumpInstr with Stateful
{
private [this] var acc: Any = _
override def apply(ctx: Context): Unit =
{
if (ctx.status eq Good)
{
// When acc is null, we are entering the instruction for the first time, a p will be on the stack
if (acc == null)
{
// after this point, the inputCheck will roll back one too many items on the stack, because this item
// was consumed. It should be adjusted
val op = ctx.stack.upop()
acc = ctx.stack.upeek
ctx.stack.exchange(op)
ctx.handlers.head.stacksz -= 1
}
acc = ctx.stack.pop[Any => Any]()(acc)
ctx.checkStack.head = ctx.offset
ctx.pc = label
}
// If the head of input stack is not the same size as the head of check stack, we fail to next handler
else if (ctx.offset != ctx.checkStack.head) { ctx.checkStack = ctx.checkStack.tail; acc = null; ctx.fail() }
else
{
// When acc is null, we have entered for first time but the op failed, so the result is already on the stack
if (acc != null)
{
ctx.stack.push(acc)
acc = null
}
ctx.checkStack = ctx.checkStack.tail
ctx.status = Good
ctx.inc()
}
}
override def toString: String = s"ChainPost($label)"
override def copy: ChainPost = new ChainPost(label)
}
private [parsley] final class ChainPre(var label: Int) extends JumpInstr with Stateful
{
private var acc: Any => Any = _
override def apply(ctx: Context): Unit =
{
if (ctx.status eq Good)
{
// If acc is null we are entering the instruction, so nothing to compose, this saves on an identity call
if (acc == null) acc = ctx.stack.pop[Any => Any]()
// We perform the acc after the tos function; the tos function is "closer" to the final p
else acc = ctx.stack.pop[Any => Any]().andThen(acc)
ctx.checkStack.head = ctx.offset
ctx.pc = label
}
// If the head of input stack is not the same size as the head of check stack, we fail to next handler
else if (ctx.offset != ctx.checkStack.head) { ctx.checkStack = ctx.checkStack.tail; acc = null; ctx.fail() }
else
{
ctx.stack.push(if (acc == null) identity[Any] _ else acc)
acc = null
ctx.checkStack = ctx.checkStack.tail
ctx.status = Good
ctx.inc()
}
}
override def toString: String = s"ChainPre($label)"
override def copy: ChainPre = new ChainPre(label)
}
private [parsley] final class Chainl[A, B](var label: Int, _wrap: A => B) extends JumpInstr with Stateful
{
private [this] val wrap: Any => B = _wrap.asInstanceOf[Any => B]
private [this] var acc: Any = _
override def apply(ctx: Context): Unit =
{
if (ctx.status eq Good)
{
val y = ctx.stack.upop()
val op = ctx.stack.pop[(Any, Any) => Any]()
// When acc is null, we are entering the instruction for the first time, a p will be on the stack
if (acc == null)
{
// after this point, the inputCheck will roll back one too many items on the stack, because this item
// was consumed. It should be adjusted
acc = op(wrap(ctx.stack.upop()), y)
ctx.handlers.head.stacksz -= 1
}
else acc = op(acc, y)
ctx.checkStack.head = ctx.offset
ctx.pc = label
}
// If the head of input stack is not the same size as the head of check stack, we fail to next handler
else if (ctx.offset != ctx.checkStack.head)
{
ctx.checkStack = ctx.checkStack.tail
acc = null
ctx.fail()
}
else
{
ctx.checkStack = ctx.checkStack.tail
// if acc is null this is first entry, p already on the stack!
if (acc != null) ctx.stack.push(acc)
acc = null
ctx.status = Good
ctx.inc()
}
}
override def toString: String = s"Chainl($label)"
override def copy: Chainl[A, B] = new Chainl(label, wrap)
}
private [parsley] final class Chainr[A, B](var label: Int, _wrap: A => B) extends JumpInstr with Stateful
{
private [this] val wrap: Any => B = _wrap.asInstanceOf[Any => B]
private [this] var acc: Any => Any = _
override def apply(ctx: Context): Unit =
{
if (ctx.status eq Good)
{
val f = ctx.stack.pop[(Any, Any) => Any]()
val x = ctx.stack.upop()
// If acc is null we are entering the instruction, so nothing to compose, this saves on an identity call
if (acc == null) acc = (y: Any) => f(x, y)
// We perform the acc after the tos function; the tos function is "closer" to the final p
else
{
val acc_ = acc
acc = (y: Any) => acc_(f(x, y))
}
// Pop H2 off the stack
ctx.handlers = ctx.handlers.tail
ctx.checkStack = ctx.checkStack.tail
ctx.checkStack.head = ctx.offset
ctx.pc = label
}
// If the head of input stack is not the same size as the head of check stack, we fail to next handler
else if (ctx.offset != ctx.checkStack.head)
{
// H1 might still be on the stack
if (!isEmpty(ctx.handlers) && ctx.handlers.head.pc == ctx.pc)
{
ctx.handlers = ctx.handlers.tail
ctx.checkStack = ctx.checkStack.tail.tail
}
else ctx.checkStack = ctx.checkStack.tail
acc = null
ctx.fail()
}
else
{
// H1 is on the stack, so p succeeded, just not op
if (!isEmpty(ctx.handlers) && ctx.handlers.head.pc == ctx.pc)
{
if (acc != null) ctx.stack.exchange(acc(wrap(ctx.stack.upeek)))
else ctx.stack.exchange(wrap(ctx.stack.upeek))
ctx.checkStack = ctx.checkStack.tail.tail
ctx.handlers = ctx.handlers.tail
ctx.inc()
ctx.status = Good
}
// p did not succeed and hence neither did op
else
{
ctx.checkStack = ctx.checkStack.tail
ctx.fail()
}
acc = null
}
}
override def toString: String = s"Chainr($label)"
override def copy: Chainr[A, B] = new Chainr(label, wrap)
}
private [parsley] final class SepEndBy1(var label: Int) extends JumpInstr with Stateful
{
private [this] val acc: ListBuffer[Any] = ListBuffer.empty
override def apply(ctx: Context): Unit =
{
if (ctx.status eq Good)
{
ctx.stack.pop_()
acc += ctx.stack.upop()
// Pop H2 off the stack
ctx.handlers = ctx.handlers.tail
// Pop a check off the stack and edit the other
ctx.checkStack = ctx.checkStack.tail
ctx.checkStack.head = ctx.offset
ctx.pc = label
}
// If the head of input stack is not the same size as the head of check stack, we fail to next handler
else if (ctx.offset != ctx.checkStack.head)
{
// H1 might still be on the stack
if (!isEmpty(ctx.handlers) && ctx.handlers.head.pc == ctx.pc)
{
ctx.handlers = ctx.handlers.tail
ctx.checkStack = ctx.checkStack.tail.tail
}
else ctx.checkStack = ctx.checkStack.tail
acc.clear()
ctx.fail()
}
else
{
// H1 is on the stack, so p succeeded, just not sep
if (!isEmpty(ctx.handlers) && ctx.handlers.head.pc == ctx.pc)
{
acc += ctx.stack.upop()
ctx.checkStack = ctx.checkStack.tail.tail
ctx.handlers = ctx.handlers.tail
}
else ctx.checkStack = ctx.checkStack.tail
if (acc.isEmpty) ctx.fail()
else
{
ctx.stack.push(acc.toList)
acc.clear()
ctx.status = Good
ctx.inc()
}
}
}
override def toString: String = s"SepEndBy1($label)"
override def copy: SepEndBy1 = new SepEndBy1(label)
}
private [parsley] final class ManyUntil(var label: Int) extends JumpInstr with Stateful
{
private [this] val acc: ListBuffer[Any] = ListBuffer.empty
override def apply(ctx: Context): Unit =
{
if (ctx.status eq Good)
{
val x = ctx.stack.upop()
if (x == DeepEmbedding.ManyUntil.Stop)
{
ctx.stack.push(acc.toList)
acc.clear()
ctx.handlers = ctx.handlers.tail
ctx.inc()
}
else
{
acc += x
ctx.pc = label
}
}
// ManyUntil is a fallthrough handler, it must be visited during failure, but does nothing to the external state
else { acc.clear(); ctx.fail() }
}
override def toString: String = s"ManyUntil($label)"
override def copy: ManyUntil = new ManyUntil(label)
}
private [parsley] final class If(var label: Int) extends JumpInstr
{
override def apply(ctx: Context): Unit =
{
if (ctx.stack.pop()) ctx.pc = label
else ctx.inc()
}
override def toString: String = s"If(true: $label)"
}
private [parsley] final class Ensure[A](pred: A=>Boolean, expected: UnsafeOption[String]) extends Instr
{
private [this] val pred_ = pred.asInstanceOf[Any=>Boolean]
override def apply(ctx: Context): Unit =
{
if (pred_(ctx.stack.upeek)) ctx.inc()
else
{
val strip = ctx.expected.isEmpty
ctx.fail(expected)
if (strip) ctx.unexpected = null
}
}
override def toString: String = "Ensure(?)"
}
private [parsley] final class Guard[A](pred: A=>Boolean, msg: String, expected: UnsafeOption[String]) extends Instr
{
private [this] val pred_ = pred.asInstanceOf[Any=>Boolean]
override def apply(ctx: Context): Unit =
{
if (pred_(ctx.stack.upeek)) ctx.inc()
else
{
ctx.stack.pop_() //this might not be needed? drop handles in fail
ctx.fail(expected)
ctx.raw ::= msg
}
}
override def toString: String = s"Guard(?, $msg)"
}
private [parsley] final class FastGuard[A](pred: A=>Boolean, msggen: A=>String, expected: UnsafeOption[String]) extends Instr
{
private [this] val pred_ = pred.asInstanceOf[Any=>Boolean]
private [this] val msggen_ = msggen.asInstanceOf[Any=>String]
override def apply(ctx: Context): Unit =
{
if (pred_(ctx.stack.upeek)) ctx.inc()
else
{
val msg = msggen_(ctx.stack.upop())
ctx.fail(expected)
ctx.raw ::= msg
}
}
override def toString: String = "FastGuard(?, ?)"
}
private [parsley] final class FastFail[A](msggen: A=>String, expected: UnsafeOption[String]) extends Instr
{
private [this] val msggen_ = msggen.asInstanceOf[Any => String]
override def apply(ctx: Context): Unit =
{
val msg = msggen_(ctx.stack.upop())
ctx.fail(expected)
ctx.raw ::= msg
}
override def toString: String = "FastFail(?)"
}
private [parsley] final class FastUnexpected[A](msggen: A=>String, expected: UnsafeOption[String]) extends Instr
{
private [this] val msggen_ = msggen.asInstanceOf[Any => String]
override def apply(ctx: Context): Unit =
{
val msg = msggen_(ctx.stack.upop())
ctx.fail(expected)
ctx.unexpected = msg
ctx.unexpectAnyway = true
}
override def toString: String = "FastUnexpected(?)"
}
private [parsley] final class NotFollowedBy(expected: UnsafeOption[String]) extends Instr
{
override def apply(ctx: Context): Unit =
{
// Recover the previous state; notFollowedBy NEVER consumes input
val state = ctx.states.head
ctx.states = ctx.states.tail
ctx.offset = state.offset
ctx.line = state.line
ctx.col = state.col
ctx.regs = state.regs
// A previous success is a failure
if (ctx.status eq Good)
{
ctx.handlers = ctx.handlers.tail
val x = ctx.stack.upop()
ctx.fail(expected)
ctx.unexpected = "\"" + x.toString + "\""
ctx.unexpectAnyway = true
}
// A failure is what we wanted
else
{
ctx.status = Good
ctx.stack.push(())
ctx.inc()
}
}
override def toString: String = "NotFollowedBy"
}
private [parsley] class Eof(_expected: UnsafeOption[String]) extends Instr
{
val expected: String = if (_expected == null) "end of input" else _expected
override def apply(ctx: Context): Unit =
{
if (ctx.offset == ctx.inputsz)
{
ctx.stack.push(())
ctx.inc()
}
else ctx.fail(expected)
}
override final def toString: String = "Eof"
}