-
Notifications
You must be signed in to change notification settings - Fork 14
/
ConfigImplTyped.scala
210 lines (197 loc) · 7.93 KB
/
ConfigImplTyped.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
/* SPDX-FileCopyrightText: © 2023 Parsley Contributors <https://github.com/j-mie6/Parsley/graphs/contributors>
* SPDX-License-Identifier: BSD-3-Clause
*/
package parsley.token.errors
import parsley.Parsley, Parsley.pure
import parsley.errors.combinator, combinator.ErrorMethods
import parsley.position
private [parsley] object FilterOps {
def amendThenDislodge[A](full: Boolean)(p: Parsley[A]): Parsley[A] = {
if (full) combinator.amendThenDislodge(p)
else p
}
def amendThenDislodgeOrPartial[A](full: Boolean)(p: Parsley[A]): Parsley[A] = {
if (full) combinator.amendThenDislodge(p)
else combinator.partialAmendThenDislodge(p)
}
def entrench[A](full: Boolean)(p: Parsley[A]): Parsley[A] = {
if (full) combinator.entrench(p)
else p
}
}
/** This trait, and its subclasses, can be used to configure how filters should be used within the `Lexer`.
* @since 4.1.0
* @group filters
*/
trait FilterConfig[A] {
private [parsley] def filter(p: Parsley[A])(f: A => Boolean): Parsley[A]
// $COVERAGE-OFF$
private [parsley] def collect[B](p: Parsley[A])(f: PartialFunction[A, B]): Parsley[B] = this.filter(p)(f.isDefinedAt).map(f)
private [parsley] def injectLeft[B]: FilterConfig[Either[A, B]]
private [parsley] def injectRight[B]: FilterConfig[Either[B, A]]
// $COVERAGE-ON$
}
/** This subtrait of `FilterConfig` specifies that only filters generating ''specialised'' errors may be used.
* @since 4.1.0
* @group filters
*/
trait SpecialisedFilterConfig[A] extends FilterConfig[A]
/** This subtrait of `FilterConfig` specifies that only filters generating ''vanilla'' errors may be used.
* @since 4.1.0
* @group filters
*/
trait VanillaFilterConfig[A] extends FilterConfig[A]
/** This class ensures that the filter will generate ''specialised'' messages for the given failing parse.
* @since 4.1.0
* @param fullAmend filters usually have partial amend semantics: should this instead do a full amend?
* @group filters
*/
abstract class SpecialisedMessage[A](fullAmend: Boolean) extends SpecialisedFilterConfig[A] { self =>
/** This method produces the messages for the given value.
* @since 4.1.0
* @group badchar
*/
def message(x: A): Seq[String]
private [parsley] final override def filter(p: Parsley[A])(f: A => Boolean) = FilterOps.amendThenDislodge(fullAmend) {
FilterOps.entrench(fullAmend)(p).guardAgainst {
case x if !f(x) => message(x)
}
}
private [parsley] final override def collect[B](p: Parsley[A])(f: PartialFunction[A, B]) = FilterOps.amendThenDislodge(fullAmend) {
FilterOps.entrench(fullAmend)(p).collectMsg(message(_))(f)
}
// $COVERAGE-OFF$
private [parsley] final override def injectLeft[B] = new SpecialisedMessage[Either[A, B]](fullAmend) {
def message(xy: Either[A, B]) = {
val Left(x) = xy
self.message(x)
}
}
private [parsley] final override def injectRight[B] = new SpecialisedMessage[Either[B, A]](fullAmend) {
def message(xy: Either[B, A]) = {
val Right(y) = xy
self.message(y)
}
}
// $COVERAGE-ON$
}
/** This class ensures that the filter will generate a ''vanilla'' unexpected item for the given failing parse.
* @since 4.1.0
* @param fullAmend filters usually have partial amend semantics: should this instead do a full amend?
* @group filters
*/
abstract class Unexpected[A](fullAmend: Boolean) extends VanillaFilterConfig[A] { self =>
/** This method produces the unexpected label for the given value.
* @since 4.1.0
* @group badchar
*/
def unexpected(x: A): String
private [parsley] final override def filter(p: Parsley[A])(f: A => Boolean) = FilterOps.amendThenDislodge(fullAmend) {
FilterOps.entrench(fullAmend)(p).unexpectedWhen {
case x if !f(x) => unexpected(x)
}
}
// $COVERAGE-OFF$
private [parsley] final override def injectLeft[B] = new Unexpected[Either[A, B]](fullAmend) {
def unexpected(xy: Either[A, B]) = {
val Left(x) = xy
self.unexpected(x)
}
}
private [parsley] final override def injectRight[B] = new Unexpected[Either[B, A]](fullAmend) {
def unexpected(xy: Either[B, A]) = {
val Right(y) = xy
self.unexpected(y)
}
}
// $COVERAGE-ON$
}
/** This class ensures that the filter will generate a ''vanilla'' reason for the given failing parse.
* @since 4.1.0
* @param fullAmend filters usually have partial amend semantics: should this instead do a full amend?
* @group filters
*/
abstract class Because[A](fullAmend: Boolean) extends VanillaFilterConfig[A] { self =>
/** This method produces the reason for the given value.
* @since 4.1.0
* @group badchar
*/
def reason(x: A): String
private [parsley] final override def filter(p: Parsley[A])(f: A => Boolean) = FilterOps.amendThenDislodge(fullAmend) {
FilterOps.entrench(fullAmend)(p).filterOut {
case x if !f(x) => reason(x)
}
}
// $COVERAGE-OFF$
private [parsley] final override def injectLeft[B] = new Because[Either[A, B]](fullAmend) {
def reason(xy: Either[A, B]) = {
val Left(x) = xy
self.reason(x)
}
}
private [parsley] final override def injectRight[B] = new Because[Either[B, A]](fullAmend) {
def reason(xy: Either[B, A]) = {
val Right(y) = xy
self.reason(y)
}
}
// $COVERAGE-ON$
}
/** This class ensures that the filter will generate a ''vanilla'' unexpected item and a reason for the given failing parse.
* @since 4.1.0
* @param fullAmend filters usually have partial amend semantics: should this instead do a full amend?
* @group filters
*/
abstract class UnexpectedBecause[A](fullAmend: Boolean) extends VanillaFilterConfig[A] { self =>
/** This method produces the unexpected label for the given value.
* @since 4.1.0
* @group badchar
*/
def unexpected(x: A): String
/** This method produces the reason for the given value.
* @since 4.1.0
* @group badchar
*/
def reason(x: A): String
// TODO: factor this combinator out with the "Great Move" in 4.2
private [parsley] final override def filter(p: Parsley[A])(f: A => Boolean) = FilterOps.amendThenDislodgeOrPartial(fullAmend) {
position.internalOffsetSpan(combinator.entrench(p)).flatMap { case (os, x, oe) =>
if (f(x)) combinator.unexpected(oe - os, this.unexpected(x)).explain(reason(x))
else pure(x)
}
}
// $COVERAGE-OFF$
private [parsley] final override def injectLeft[B] = new UnexpectedBecause[Either[A, B]](fullAmend) {
def unexpected(xy: Either[A, B]) = {
val Left(x) = xy
self.unexpected(x)
}
def reason(xy: Either[A, B]) = {
val Left(x) = xy
self.reason(x)
}
}
private [parsley] final override def injectRight[B] = new UnexpectedBecause[Either[B, A]](fullAmend) {
def unexpected(xy: Either[B, A]) = {
val Right(y) = xy
self.unexpected(y)
}
def reason(xy: Either[B, A]) = {
val Right(x) = xy
self.reason(x)
}
}
// $COVERAGE-ON$
}
/** This class can be used to not specify an error configuration for the filter, a regular `filter` is used instead.
* @since 4.1.0
* @group filters
*/
final class BasicFilter[A] extends SpecialisedFilterConfig[A] with VanillaFilterConfig[A] {
private [parsley] final override def filter(p: Parsley[A])(f: A => Boolean) = p.filter(f)
private [parsley] final override def collect[B](p: Parsley[A])(f: PartialFunction[A, B]) = p.collect(f)
// $COVERAGE-OFF$
private [parsley] final override def injectLeft[B] = new BasicFilter[Either[A, B]]
private [parsley] final override def injectRight[B] = new BasicFilter[Either[B, A]]
// $COVERAGE-ON$
}