-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSeqPattern.scala
380 lines (321 loc) · 12.6 KB
/
SeqPattern.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
// Copyright 2024-2025 DCal Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package distcompiler
import cats.syntax.all.given
import dsl.*
import scala.collection.IndexedSeqView
final class SeqPattern[+T](val manip: Manip[SeqPattern.Result[T]])
extends AnyVal:
import SeqPattern.*
def |[U >: T](other: SeqPattern[U]): SeqPattern[U] =
SeqPattern(manip.combineK(other.manip))
def productAtRightSibling[U](other: SeqPattern[U]): SeqPattern[(T, U)] =
SeqPattern:
manip.lookahead.flatMap:
case Result.Top(_, _) => backtrack
case Result.Look(_, idx, t) =>
atIdx(idx)(other.map((t, _)).manip)
case Result.Match(_, idx, t) =>
atIdx(idx + 1)(other.map((t, _)).manip)
def restrict[U](fn: PartialFunction[T, U]): SeqPattern[U] =
SeqPattern:
manip.restrict:
case Result.Top(top, fn(u)) => Result.Top(top, u)
case Result.Look(parent, idx, fn(u)) => Result.Look(parent, idx, u)
case Result.Match(parent, idx, fn(u)) => Result.Match(parent, idx, u)
def filter(pred: T => Boolean): SeqPattern[T] =
SeqPattern(manip.filter(res => pred(res.value)))
def asManip: Manip[T] =
manip.map(_.value)
@scala.annotation.targetName("fieldsConcat")
def ~[Tpl1 <: Tuple, Tpl2 <: Tuple](using
T <:< Fields[Tpl1]
)(using
DebugInfo
)(
other: SeqPattern[Fields[Tpl2]]
): SeqPattern[Fields[Tuple.Concat[Tpl1, Tpl2]]] =
this
.productAtRightSibling(other)
.map: (flds1, flds2) =>
Fields(flds1.fields ++ flds2.fields)
@scala.annotation.targetName("fieldsEnd")
def ~[Tpl <: Tuple, T2](using
T <:< Fields[Tpl]
)(using
maybeStrip: Fields.MaybeStripTuple1[Tpl, T2]
)(using DebugInfo)(other: eof.type): SeqPattern[T2] =
this.productAtRightSibling(atEnd).map(flds => maybeStrip(flds._1.fields))
@scala.annotation.targetName("fieldsTrailing")
def ~[Tpl <: Tuple, T2](using
T <:< Fields[Tpl]
)(using
maybeStrip: Fields.MaybeStripTuple1[Tpl, T2]
)(
other: trailing.type
): SeqPattern[T2] =
this.map(flds => maybeStrip(flds.fields))
def stripFields[TT](using T <:< Fields[TT]): SeqPattern[TT] =
this.map(t => t.fields)
@scala.annotation.targetName("logicalAnd")
def &&(other: SeqPattern[?]): SeqPattern[Unit] =
this *> other.void
object SeqPattern:
export applicative.{pure, unit}
given applicative: cats.Applicative[SeqPattern] with
override val unit: SeqPattern[Unit] = pure(())
def pure[A](x: A): SeqPattern[A] = SeqPattern:
getHandle.map:
case Manip.Handle.AtTop(top) =>
Result.Top(top, x)
case handle: (Manip.Handle.Sentinel | Manip.Handle.AtChild) =>
Result.Look(handle.parent, handle.idx, x)
override def map[A, B](fa: SeqPattern[A])(f: A => B): SeqPattern[B] =
SeqPattern:
fa.manip.map: result =>
result.withValue(f(result.value))
def ap[A, B](ff: SeqPattern[A => B])(fa: SeqPattern[A]): SeqPattern[B] =
SeqPattern:
(ff.manip, fa.manip).mapN: (result1, result2) =>
result1.combine(result2)(_.apply(_))
given semigroupk: cats.SemigroupK[SeqPattern] with
def combineK[A](x: SeqPattern[A], y: SeqPattern[A]): SeqPattern[A] =
SeqPattern(x.manip | y.manip)
enum Result[+T]:
val value: T
case Top(top: Node.Top, value: T)
case Look(parent: Node.Parent, idx: Int, value: T)
case Match(parent: Node.Parent, idx: Int, value: T)
def isLook: Boolean =
this match
case _: Look[?] => true
case _ => false
def isMatch: Boolean =
this match
case _: Match[?] => true
case _ => false
def withValue[U](value: U): Result[U] =
if this.value.asInstanceOf[AnyRef] eq value.asInstanceOf[AnyRef]
then this.asInstanceOf
else
this match
case Top(top, _) => Top(top, value)
case Look(parent, idx, _) => Look(parent, idx, value)
case Match(parent, idx, _) => Match(parent, idx, value)
def combine[U, V](other: Result[U])(fn: (T, U) => V): Result[V] =
def result = fn(value, other.value)
(this, other) match
case (Top(top1, _), Top(top2, _)) =>
assert(top1 eq top2)
Top(top1, result)
case (Top(_, _), _) | (_, Top(_, _)) =>
throw NodeError(
"one part of the same pattern matched top while the other did not"
)
case (lhs: (Look[T] | Match[T]), rhs: (Look[U] | Match[U])) =>
assert(rhs.parent eq rhs.parent)
val parent = lhs.parent
def mkLook(idx: Int): Result[V] =
Look(parent, idx, result)
def mkMatch(idx: Int): Result[V] =
Match(parent, idx, result)
(lhs, rhs) match
case (Look(_, idx1, _), Look(_, idx2, _)) =>
mkLook(idx1.max(idx2))
case (Look(_, idx1, _), Match(_, idx2, _)) if idx1 <= idx2 =>
mkMatch(idx2)
case (Look(_, idx1, _), Match(_, idx2, _)) =>
assert(idx1 > idx2)
mkLook(idx1)
case (Match(_, idx1, _), Look(_, idx2, _)) if idx1 < idx2 =>
mkLook(idx2)
case (Match(_, idx1, _), Look(_, idx2, _)) =>
assert(idx1 >= idx2)
mkMatch(idx1)
case (Match(_, idx1, _), Match(_, idx2, _)) =>
mkMatch(idx1.max(idx2))
object Result:
extension [T](result: Result.Look[T] | Result.Match[T])
def parent: Node.Parent =
result match
case Result.Look(parent, _, _) => parent
case Result.Match(parent, _, _) => parent
def idx: Int =
result match
case Result.Look(_, idx, _) => idx
case Result.Match(_, idx, _) => idx
import scala.language.implicitConversions
implicit def tokenAsTok(using DebugInfo)(token: Token): SeqPattern[Node] =
tok(token)
final class Fields[T](val fields: T) extends AnyVal
object Fields:
sealed trait MaybeStripTuple1[Tpl <: Tuple, T]:
def apply(tpl: Tpl): T
given stripTuple1[T]: MaybeStripTuple1[Tuple1[T], T] with
def apply(tpl: Tuple1[T]): T = tpl._1
given notTuple1[Tpl <: Tuple](using
scala.util.NotGiven[Tpl <:< Tuple1[?]]
): MaybeStripTuple1[Tpl, Tpl] with
def apply(tpl: Tpl): Tpl = tpl
case object FieldsEndMarker
case object FieldsTrailingMarker
object ops:
def refine[T](manip: Manip[T]): SeqPattern[T] =
SeqPattern:
(SeqPattern.unit.manip, manip).mapN(_.withValue(_))
@scala.annotation.targetName("deferSeqPattern")
def defer[T](pattern: => SeqPattern[T]): SeqPattern[T] =
SeqPattern(Manip.ops.defer(pattern.manip))
def field[T](pattern: SeqPattern[T]): SeqPattern[Fields[Tuple1[T]]] =
pattern.map(t => Fields(Tuple1(t)))
def fields[Tpl <: Tuple](
pattern: SeqPattern[Tpl]
): SeqPattern[Fields[Tpl]] =
pattern.map(Fields(_))
def skip[T](pattern: SeqPattern[T]): SeqPattern[Fields[EmptyTuple]] =
pattern.as(Fields(EmptyTuple))
def anyNode(using DebugInfo): SeqPattern[Node] =
SeqPattern:
getNode.restrict:
case node: Node =>
Result.Match(node.parent.get, node.idxInParent, node)
def anyChild(using DebugInfo): SeqPattern[Node.Child] =
SeqPattern:
getNode.restrict:
case child: Node.Child =>
Result.Match(child.parent.get, child.idxInParent, child)
def tok(using DebugInfo)(tokens: Token*): SeqPattern[Node] =
SeqPattern:
val tokenSet = tokens.toSet
getNode.restrict:
case node: Node if tokenSet(node.token) =>
Result.Match(node.parent.get, node.idxInParent, node)
def atEnd(using DebugInfo): SeqPattern[Unit] =
SeqPattern:
getHandle.restrict:
case Manip.Handle.Sentinel(parent, idx) =>
Result.Look(parent, idx, ())
def atBegin(using DebugInfo): SeqPattern[Unit] =
SeqPattern:
getHandle.restrict:
case Manip.Handle.Sentinel(parent, 0) =>
Result.Look(parent, 0, ())
case Manip.Handle.AtChild(parent, 0, _) =>
Result.Look(parent, 0, ())
def nodeSpanMatchedBy(using
DebugInfo
)(
pattern: SeqPattern[?]
): SeqPattern[IndexedSeqView[Node.Child]] =
SeqPattern:
getHandle
.restrict:
case handle: (Manip.Handle.Sentinel | Manip.Handle.AtChild) =>
(handle.parent, handle.idx)
.product(pattern.void.manip)
.restrict:
case (
(parent, startIdx),
patResult: (Result.Look[Unit] | Result.Match[Unit])
) =>
assert(patResult.parent eq parent)
patResult.withValue:
parent.children.view.slice(
from = startIdx,
until =
// Endpoint is exclusive.
// If we matched idx, add 1 to include it.
// Otherwise, default is fine.
if patResult.isMatch
then patResult.idx + 1
else patResult.idx
)
export SeqPattern.{FieldsEndMarker as eof, FieldsTrailingMarker as trailing}
def not[T](using DebugInfo)(pattern: SeqPattern[T]): SeqPattern[Unit] =
SeqPattern(Manip.Negated(pattern.manip, summon[DebugInfo]) *> unit.manip)
def optional[T](using
DebugInfo
)(
pattern: SeqPattern[T]
): SeqPattern[Option[T]] =
pattern.map(Some(_)) | pure(None)
def repeated[T](using
DebugInfo
)(
pattern: SeqPattern[T]
): SeqPattern[List[T]] =
lazy val impl: SeqPattern[List[T]] =
(field(pattern) ~ field(defer(impl)) ~ trailing)
.map(_ :: _)
| pure(Nil)
impl
def repeated1[T](using
DebugInfo
)(
pattern: SeqPattern[T]
): SeqPattern[List[T]] =
(field(pattern) ~ field(repeated(pattern)) ~ trailing).map(_ :: _)
def repeatedSepBy1[T](using
DebugInfo
)(sep: SeqPattern[?])(pattern: SeqPattern[T]): SeqPattern[List[T]] =
(field(pattern) ~ field(
repeated(skip(sep) ~ field(pattern) ~ trailing)
) ~ trailing)
.map(_ :: _)
def repeatedSepBy[T](using
DebugInfo
)(sep: SeqPattern[?])(
pattern: SeqPattern[T]
): SeqPattern[List[T]] =
repeatedSepBy1(sep)(pattern)
| pure(Nil)
def theTop(using DebugInfo): SeqPattern[Node.Top] =
SeqPattern:
getHandle.restrict:
case Manip.Handle.AtTop(top) =>
Result.Top(top, top)
def theFirstChild(using DebugInfo): SeqPattern[Node.Child] =
SeqPattern:
getHandle.restrict:
case Manip.Handle.AtChild(parent, 0, child) =>
Result.Match(parent, 0, child)
def children[T](using DebugInfo)(pattern: SeqPattern[T]): SeqPattern[T] =
refine(atFirstChild(pattern.asManip))
def onlyChild[T](using DebugInfo)(pattern: SeqPattern[T]): SeqPattern[T] =
refine(atFirstChild((field(pattern) ~ eof).asManip))
def parent[T](using DebugInfo)(pattern: SeqPattern[T]): SeqPattern[T] =
refine(atParent(on(pattern).value))
def ancestor[T](using DebugInfo)(pattern: SeqPattern[T]): SeqPattern[T] =
refine(atAncestor(on(pattern).value))
def lastChild[T](using DebugInfo)(pattern: SeqPattern[T]): SeqPattern[T] =
refine(atLastChild(on(pattern).value))
extension [P <: Node.Parent](parentPattern: SeqPattern[P])
def withChildren[T](using
DebugInfo
)(
pattern: SeqPattern[T]
): SeqPattern[T] =
SeqPattern:
parentPattern.manip.lookahead.flatMap: result =>
val parent = result.value
atNode(parent)(atFirstChild(pattern.asManip))
.map(result.withValue)
extension (nodePattern: SeqPattern[Node])
def src(using DebugInfo)(sourceRange: SourceRange): SeqPattern[Node] =
nodePattern.filter(_.sourceRange == sourceRange)
def src(using DebugInfo)(str: String): SeqPattern[Node] =
src(SourceRange.entire(Source.fromString(str)))
extension [T](hdPattern: SeqPattern[T])
def *:[Tpl <: Tuple](tlPattern: SeqPattern[Tpl]): SeqPattern[T *: Tpl] =
(hdPattern, tlPattern).mapN(_ *: _)