Skip to content

Commit

Permalink
Policy: add a Map policy, use instead of Proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 18, 2024
1 parent 918174f commit 8624463
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,34 @@ object Policy {
override def toString: String = s"*($policy)$endPolicy"
}

final class Map(
val endPolicy: Policy.End.WithPos,
val noDequeue: Boolean = false,
val rank: Int = 0,
desc: => String = "",
)(pred: Split => Split)(implicit fileLine: FileLine)
extends Policy.Clause {
private object PredicateDecision {
def unapply(d: Decision): Option[Seq[Split]] = {
var replaced = false
def applyMap(s: Split): Option[Split] = Option(pred(s)).filter { ss =>
(s eq ss) || {
replaced = true
!ss.isIgnored
}
}
val splits = d.splits.flatMap(applyMap)
if (replaced) Some(splits) else None
}
}
override val f: Policy.Pf = { case PredicateDecision(ss) => ss }
override def toString: String = {
val evalDesc = desc
val descStr = if (evalDesc.isEmpty) "" else s"[$evalDesc]"
s"MAP$descStr:" + super.toString
}
}

sealed trait End extends (Token => End.WithPos) {
def apply(endPos: Int): End.WithPos
final def apply(token: Token): End.WithPos = apply(token.end)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,11 @@ object PolicyOps {
}
}

private def delayedBreakPolicyFactory(onBreakPolicy: Policy): Policy.Pf = {
object OnBreakDecision {
def unapply(d: Decision): Option[Seq[Split]] = {
var replaced = false
def decisionPf(s: Split): Split =
if (!s.isNL) s
else {
replaced = true
s.orPolicy(onBreakPolicy)
}
val splits = d.splits.map(decisionPf)
if (replaced) Some(splits) else None
}
}
{ case OnBreakDecision(d) => d }
}

def delayedBreakPolicy(end: Policy.End.WithPos)(onBreakPolicy: Policy)(
implicit fileLine: FileLine,
): Policy = Policy.Proxy(onBreakPolicy, end)(delayedBreakPolicyFactory)
): Policy = new Policy.Map(endPolicy = end, desc = onBreakPolicy.toString)({
s => if (s.isNL) s.orPolicy(onBreakPolicy) else s
})

def delayedBreakPolicyBefore(token: T)(onBreakPolicy: Policy)(implicit
fileLine: FileLine,
Expand Down

0 comments on commit 8624463

Please sign in to comment.