/
Filters.scala
78 lines (58 loc) · 2.04 KB
/
Filters.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
package com.codecommit.gll.ast
import com.codecommit.gll._
trait Filters {
def prec(order: Symbol*): Filter[Node] = new PrecedenceFilter(order)
implicit def liftFilter[A](f: Filter[A])(str: Stream[Result[A]]): Stream[Result[A]] = {
str filter {
case Success(node, _) => f(node)
case _ => true
}
}
implicit def liftPredicate[A](pred: A => Boolean): Filter[A] = new Filter[A] {
def apply(n: A) = pred(n)
}
implicit def symbolSyntax(sym: Symbol): RichSymbol = new RichSymbol(sym)
class RichSymbol(sym: Symbol) {
def <(): Filter[Node] = new AssocFilter(sym, true)
def <>(): Filter[Node] = (this <) & (this >)
def >(): Filter[Node] = new AssocFilter(sym, false)
}
private class AssocFilter(sym: Symbol, isLeft: Boolean) extends Filter[Node] {
def apply(n: Node): Boolean = n match {
case n: BinaryNode if n.label == sym =>
(if (isLeft) n.right.label else n.left.label) != sym
case n => true
}
}
private class PrecedenceFilter(order: Seq[Symbol]) extends Filter[Node] {
val forbidden = {
val sets = for (i <- 0 until order.length)
yield (order(i) -> Set(order drop (i + 1): _*))
Map(sets: _*)
}
def apply(n: Node): Boolean = {
if (forbidden contains n.label) {
lazy val fallback = !(n.children map { _.label } exists forbidden(n.label))
n match {
case bn: BinaryNode => {
lazy val leftCheck = bn.left match {
case un: UnaryNode if !un.isPrefix =>
true
case n => !forbidden(bn.label)(n.label)
}
lazy val rightCheck = bn.right match {
case un: UnaryNode if un.isPrefix =>
true
case n => !forbidden(bn.label)(n.label)
}
leftCheck && rightCheck
}
case _ => fallback
}
} else {
true
}
}
}
}
object Filters extends Filters