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
/
Context.scala
218 lines (206 loc) · 7.48 KB
/
Context.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
package org.http4s
package parsley.instructions
import Stack._
import parsley.{Failure, Result, Success, UnsafeOption}
import scala.annotation.tailrec
// Private internals
private [instructions] final class Frame(val ret: Int, val instrs: Array[Instr])
{
override def toString: String = s"[$instrs@$ret]"
}
private [instructions] final class Handler(val depth: Int, val pc: Int, var stacksz: Int)
{
override def toString: String = s"Handler@$depth:$pc(-${stacksz+1})"
}
private [instructions] final class State(val offset: Int, val line: Int, val col: Int, val regs: Array[Any])
{
override def toString: String = s"$offset ($line, $col)"
}
final class Context private [parsley] (private [instructions] var instrs: Array[Instr],
private [instructions] var input: Array[Char])
{
private [instructions] val stack: ArrayStack[Any] = new ArrayStack()
private [instructions] var offset: Int = 0
private [instructions] var inputsz: Int = input.length
private [instructions] var calls: Stack[Frame] = Stack.empty
private [instructions] var states: Stack[State] = Stack.empty
private [instructions] var checkStack: Stack[Int] = Stack.empty
private [instructions] var status: Status = Good
private [instructions] var handlers: Stack[Handler] = Stack.empty
private [instructions] var depth: Int = 0
private [instructions] var pc: Int = 0
private [instructions] var line: Int = 1
private [instructions] var col: Int = 1
private [instructions] var erroffset: Int = -1
private [instructions] var errcol: Int = -1
private [instructions] var errline: Int = -1
private [instructions] var raw: List[String] = Nil
private [instructions] var unexpected: UnsafeOption[String] = _
private [instructions] var expected: List[UnsafeOption[String]] = Nil
private [instructions] var unexpectAnyway: Boolean = false
private [instructions] var errorOverride: UnsafeOption[String] = _
private [instructions] var overrideDepth: Int = 0
private [instructions] var regs: Array[Any] = new Array[Any](4)
private [instructions] var debuglvl: Int = 0
private [instructions] var startline: Int = 1
private [instructions] var startcol: Int = 1
var sourceName: String = "input"
//override def toString: String = pretty
private [instructions] def pretty: String =
{
s"""[
| stack = [${stack.mkString(", ")}]
| instrs = ${instrs.mkString("; ")}
| input = ${input.drop(offset).mkString}
| pos = ($line, $col)
| status = $status
| pc = $pc
| depth = $depth
| rets = ${mkString(map[Frame, Int](calls, _.ret), ", ")}
| handlers = ${mkString(handlers, ":") + "[]"}
| recstates = ${mkString(states, ":") + "[]"}
| checks = ${mkString(checkStack, ":") + "[]"}
| registers = ${regs.zipWithIndex.map{case (r, i) => s"r$i = $r"}.mkString("\n ")}
|]""".stripMargin
}
def pos: (Int, Int) = (startline, startcol)
def pos_=(pos: (Int, Int)): Unit =
{
val (line, col) = pos
startline = line
startcol = col
}
@tailrec @inline private [parsley] def runParser[A](): Result[A] =
{
//println(this)
if (status eq Failed) return Failure(errorMessage)
if (pc < instrs.length)
{
instrs(pc)(this)
runParser[A]()
}
else if (isEmpty(calls)) Success(stack.peek[A])
else
{
ret()
runParser[A]()
}
}
private [instructions] def ret(): Unit =
{
val frame = calls.head
instrs = frame.instrs
calls = calls.tail
pc = frame.ret
depth -= 1
if (depth < overrideDepth)
{
overrideDepth = 0
errorOverride = null
}
}
private [instructions] def fail(e: UnsafeOption[String] = null): Unit =
{
if (isEmpty(handlers))
{
status = Failed
if (erroffset == -1)
{
errcol = col
errline = line
}
}
else
{
status = Recover
val handler = handlers.head
handlers = handlers.tail
val diffdepth = depth - handler.depth - 1
if (diffdepth >= 0)
{
val calls_ = if (diffdepth != 0) drop(calls, diffdepth) else calls
instrs = calls_.head.instrs
calls = calls_.tail
}
pc = handler.pc
val diffstack = stack.usize - handler.stacksz
if (diffstack > 0) stack.drop(diffstack)
depth = handler.depth
}
if (offset > erroffset)
{
erroffset = offset
errcol = col
errline = line
unexpected = if (offset < inputsz) "\"" + nextChar + "\"" else "end of " + sourceName
expected = (if (errorOverride == null) e else errorOverride)::Nil
raw = Nil
unexpectAnyway = false
}
else if (offset == erroffset) expected ::= (if (errorOverride == null) e else errorOverride)
if (depth < overrideDepth)
{
overrideDepth = 0
errorOverride = null
}
}
private def errorMessage: String =
{
val posStr = s"(line $errline, column $errcol):"
val unexpectedStr = Option(unexpected).map(s => s"unexpected $s")
val expectedFlat = expected.flatMap(Option(_))
val expectedFiltered = expectedFlat.filterNot(_.isEmpty)
val rawFiltered = raw.filterNot(_.isEmpty)
val expectedStr = if (expectedFiltered.isEmpty) None else Some(s"expected ${expectedFiltered.distinct.reverse.mkString(" or ")}")
val rawStr = if (rawFiltered.isEmpty) None else Some(rawFiltered.distinct.reverse.mkString(" or "))
unexpectAnyway = unexpectAnyway || expectedFlat.nonEmpty || raw.nonEmpty
if (rawStr.isEmpty && expectedStr.isEmpty && unexpectAnyway)
{
s"$posStr\n ${unexpectedStr.getOrElse("unknown parse error")}"
}
else if (rawStr.isEmpty && expectedStr.isEmpty)
{
s"$posStr\n unknown parse error"
}
else if (expectedStr.isEmpty)
{
s"$posStr\n ${rawStr.get}"
}
else
{
s"$posStr${unexpectedStr.fold("")("\n " + _)}\n ${expectedStr.get}${rawStr.fold("")("\n " + _)}"
}
}
private [instructions] def inc(): Unit = pc += 1
private [instructions] def nextChar: Char = input(offset)
private [instructions] def moreInput: Boolean = offset < inputsz
// Allows us to reuse a context, helpful for benchmarking and potentially user applications
private [parsley] def apply(_instrs: Array[Instr], _input: Array[Char]): Context =
{
instrs = _instrs
input = _input
stack.clear()
offset = 0
inputsz = input.length
calls = Stack.empty
states = Stack.empty
checkStack = Stack.empty
status = Good
handlers = Stack.empty
depth = 0
pc = 0
line = startline
col = startcol
erroffset = -1
errcol = -1
errline = -1
raw = Nil
unexpected = null
expected = Nil
unexpectAnyway = false
errorOverride = null
overrideDepth = 0
debuglvl = 0
this
}
}