Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Policy: add a Map policy, use instead of Proxy #4011

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,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
Loading