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
/
OptInstrs.scala
337 lines (317 loc) · 10.3 KB
/
OptInstrs.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
package org.http4s
package parsley.instructions
import parsley.{UnsafeOption, XCompat}
import parsley.instructions.Stack.push
import scala.annotation.{switch, tailrec}
import scala.collection.mutable
private [parsley] final class Perform[-A, +B](f: A => B) extends Instr
{
private [Perform] val g = f.asInstanceOf[Any => B]
override def apply(ctx: Context): Unit =
{
ctx.stack.exchange(g(ctx.stack.upeek))
ctx.inc()
}
override def toString: String = "Perform(?)"
}
private [parsley] final class Exchange[A](private [Exchange] val x: A) extends Instr
{
override def apply(ctx: Context): Unit =
{
ctx.stack.exchange(x)
ctx.inc()
}
override def toString: String = s"Ex($x)"
}
private [parsley] class Newline(_expected: UnsafeOption[String]) extends CharTok('\n', _expected)
{
override def apply(ctx: Context): Unit =
{
if (ctx.moreInput && ctx.nextChar == '\n')
{
ctx.stack.push(ac)
ctx.offset += 1
ctx.col = 1
ctx.line += 1
ctx.inc()
}
else ctx.fail(expected)
}
}
private [parsley] class Tab(_expected: UnsafeOption[String]) extends CharTok('\t', _expected)
{
override def apply(ctx: Context): Unit =
{
if (ctx.moreInput && ctx.nextChar == '\t')
{
ctx.stack.push(ac)
ctx.offset += 1
ctx.col += 4 - ((ctx.col - 1) & 3)
ctx.inc()
}
else ctx.fail(expected)
}
}
private [parsley] class CharTokFastPerform protected (protected final val c: Char, protected final val f: Char => Any, _expected: UnsafeOption[String]) extends Instr
{
protected val expected: String = if (_expected == null) "\"" + c.toString + "\"" else _expected
protected final val fc: Any = f(c)
override def apply(ctx: Context): Unit =
{
if (ctx.moreInput && ctx.nextChar == c)
{
ctx.stack.push(fc)
ctx.offset += 1
ctx.col += 1
ctx.inc()
}
else ctx.fail(expected)
}
override final def toString: String = s"ChrPerform($c, ?)"
}
private [parsley] final class NewlineFastPerform(g: Char => Any, _expected: UnsafeOption[String]) extends CharTokFastPerform('\n', g, _expected)
{
override def apply(ctx: Context): Unit =
{
if (ctx.moreInput && ctx.nextChar == '\n')
{
ctx.stack.push(fc)
ctx.offset += 1
ctx.col = 1
ctx.line += 1
ctx.inc()
}
else ctx.fail(expected)
}
}
private [parsley] final class TabFastPerform(g: Char => Any, _expected: UnsafeOption[String]) extends CharTokFastPerform('\t', g, _expected)
{
override def apply(ctx: Context): Unit =
{
if (ctx.moreInput && ctx.nextChar == '\t')
{
ctx.stack.push(fc)
ctx.offset += 1
ctx.col += 4 - ((ctx.col - 1) & 3)
ctx.inc()
}
else ctx.fail(expected)
}
}
private [parsley] final class StringTokFastPerform(s: String, f: String => Any, _expected: UnsafeOption[String]) extends Instr
{
protected val expected: String = if (_expected == null) "\"" + s + "\"" else _expected
private [this] val cs = s.toCharArray
private [this] val sz = cs.length
private [this] val fs: Any = f(s)
private [this] val adjustAtIndex = new Array[(Int => Int, Int => Int)](s.length + 1)
def makeAdjusters(col: Int, line: Int, tabprefix: Option[Int]): (Int => Int, Int => Int) =
if (line > 0) ((_: Int) => col, (x: Int) => x + line)
else (tabprefix match
{
case Some(prefix) =>
val outer = 4 + col + prefix
val inner = prefix - 1
(x: Int) => outer + x - ((x + inner) & 3)
case None => (x: Int) => x + col
}, (x: Int) => x)
@tailrec def compute(cs: Array[Char], i: Int = 0, col: Int = 0, line: Int = 0)(implicit tabprefix: Option[Int] = None): Unit =
{
adjustAtIndex(i) = makeAdjusters(col, line, tabprefix)
if (i < cs.length) (cs(i): @switch) match
{
case '\n' => compute(cs, i+1, 1, line + 1)(Some(0))
case '\t' if tabprefix.isEmpty => compute(cs, i+1, 0, line)(Some(col))
case '\t' => compute(cs, i+1, col + 4 - ((col-1) & 3), line)
case _ => compute(cs, i+1, col + 1, line)
}
}
compute(cs)
override def apply(ctx: Context): Unit =
{
val strsz = this.sz
val inputsz = ctx.inputsz
val input = ctx.input
var i = ctx.offset
var j = 0
val cs = this.cs
if (inputsz != i)
{
while (j < strsz)
{
val c = cs(j)
if (i == inputsz || input(i) != c)
{
ctx.offset = i
val (colAdjust, lineAdjust) = adjustAtIndex(j)
ctx.col = colAdjust(ctx.col)
ctx.line = lineAdjust(ctx.line)
ctx.fail(expected)
return
}
i += 1
j += 1
}
val (colAdjust, lineAdjust) = adjustAtIndex(j)
ctx.col = colAdjust(ctx.col)
ctx.line = lineAdjust(ctx.line)
ctx.offset = i
ctx.stack.push(fs)
ctx.inc()
}
else ctx.fail(expected)
}
override def toString: String = s"StrPerform($s, ?)"
}
private [parsley] final class SatisfyExchange[A](f: Char => Boolean, x: A, expected: UnsafeOption[String]) extends Instr
{
override def apply(ctx: Context): Unit =
{
if (ctx.moreInput)
{
val c = ctx.nextChar
if (f(ctx.nextChar))
{
ctx.stack.push(x)
ctx.offset += 1
(c: @switch) match
{
case '\n' => ctx.line += 1; ctx.col = 1
case '\t' => ctx.col += 4 - ((ctx.col - 1) & 3)
case _ => ctx.col += 1
}
ctx.inc()
}
else ctx.fail(expected)
}
else ctx.fail(expected)
}
override def toString: String = s"SatEx(?, $x)"
}
private [parsley] final class JumpGoodAttempt(var label: Int) extends JumpInstr
{
override def apply(ctx: Context): Unit =
{
if (ctx.status eq Good)
{
ctx.states = ctx.states.tail
ctx.handlers = ctx.handlers.tail
ctx.pc = label
}
else
{
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
ctx.status = Good
ctx.inc()
}
}
override def toString: String = s"JumpGood'($label)"
}
private [parsley] final class RecoverWith[A](x: A) extends Instr
{
override def apply(ctx: Context): Unit =
{
if (ctx.offset != ctx.checkStack.head) ctx.fail()
else
{
ctx.status = Good
ctx.stack.push(x)
ctx.inc()
}
ctx.checkStack = ctx.checkStack.tail
}
override def toString: String = s"Recover($x)"
}
private [parsley] final class AlwaysRecoverWith[A](x: A) extends Instr
{
override def apply(ctx: Context): Unit =
{
if (ctx.status eq Good)
{
ctx.states = ctx.states.tail
ctx.handlers = ctx.handlers.tail
ctx.inc()
}
else
{
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
ctx.status = Good
ctx.stack.push(x)
ctx.inc()
}
}
override def toString: String = s"AlwaysRecover($x)"
}
private [parsley] final class JumpTable(prefixes: List[Char], labels: List[Int], private [this] var default: Int, _expecteds: List[UnsafeOption[String]]) extends Instr
{
private [this] var defaultPreamble: Int = _
private [this] val jumpTable = mutable.LongMap(prefixes.map(_.toLong).zip(labels): _*)
val expecteds = prefixes.zip(_expecteds).map{case (c, expected) => if (expected == null) "\"" + c + "\"" else expected}
override def apply(ctx: Context): Unit =
{
if (ctx.moreInput)
{
val dest = jumpTable.getOrElseUpdate(ctx.nextChar, default)
ctx.pc = dest
if (dest == default) addErrors(ctx)
else
{
ctx.checkStack = push(ctx.checkStack, ctx.offset)
ctx.handlers = push(ctx.handlers, new Handler(ctx.depth, defaultPreamble, ctx.stack.usize))
}
}
else
{
addErrors(ctx)
ctx.pc = default
}
}
private def addErrors(ctx: Context): Unit =
{
if (ctx.offset > ctx.erroffset)
{
ctx.erroffset = ctx.offset
ctx.errcol = ctx.col
ctx.errline = ctx.line
ctx.unexpected = if (ctx.offset < ctx.inputsz) "\"" + ctx.nextChar + "\"" else "end of input"
ctx.expected = if (ctx.errorOverride == null) expecteds else ctx.errorOverride::Nil
ctx.raw = Nil
ctx.unexpectAnyway = false
}
else if (ctx.offset == ctx.erroffset)
{
if (ctx.errorOverride == null) ctx.expected = ctx.expected reverse_::: expecteds
else ctx.expected ::= ctx.errorOverride
}
}
private [parsley] def relabel(labels: Array[Int]): Unit =
{
XCompat.mapValuesInPlace(jumpTable)((_, v) => labels(v))
default = labels(default)
defaultPreamble = default - 1
}
override def toString: String = s"JumpTable(${jumpTable.map{case (k, v) => k.toChar -> v}.mkString(", ")}, _ -> $default)"
}
private [parsley] object CharTokFastPerform
{
def apply[A >: Char, B](c: Char, f: A => B, expected: UnsafeOption[String]): CharTokFastPerform = (c: @switch) match
{
case '\n' => new NewlineFastPerform(f, expected)
case '\t' => new TabFastPerform(f, expected)
case _ => new CharTokFastPerform(c, f, expected)
}
}
private [parsley] object Exchange
{
def unapply[A](ex: Exchange[A]): Option[A] = Some(ex.x)
}