Skip to content

Commit

Permalink
added logic to parsley-debug for opaque-only tagging without the core…
Browse files Browse the repository at this point in the history
… hook
  • Loading branch information
j-mie6 committed Apr 24, 2024
1 parent 90e250b commit d6ad26a
Show file tree
Hide file tree
Showing 23 changed files with 167 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ private [parsley] object Renamer {
// This renames the parser if it is present, otherwise gives the default name found earlier.
def nameOf(userAssignedName: Option[String], p: LazyParsley[_]): String = userAssignedName.getOrElse {
val extracted = underlying(p)
collected.getOrElse(extracted, extracted.prettyName)
collected.getOrElse(extracted, extracted.debugName)
}

// Perform the first step of renaming, a partial rename where only the type name is exposed.
@inline def internalName(p: LazyParsley[_]): String = underlying(p).prettyName
@inline def internalName(p: LazyParsley[_]): String = underlying(p).debugName

private [parsley] def addNames(names: Map[LazyParsley[_], String]): Unit = collected ++= names
private [parsley] def addName(par: LazyParsley[_], name: String): Unit = collected.update(par, name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import parsley.internal.deepembedding.frontend.{LazyParsley, LazyParsleyIVisitor

// $COVERAGE-OFF$
// Wrapper parser class indicating explicitly named parsers.
private [parsley] final class Named[A](val par: LazyParsley[A], val name: String) extends Unary[A, A](par) {
XAssert.assert(!par.isInstanceOf[Named[_]], "Named parsers should not be nested within each other directly.")
private [parsley] final class Named[A](_p: LazyParsley[A], val name: String) extends Unary[A, A](_p) {
XAssert.assert(!p.isInstanceOf[Named[_]], "Named parsers should not be nested within each other directly.")
def make(p: StrictParsley[A]): StrictParsley[A] = p
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visitUnknown(this, context)
override private [parsley] def prettyName = name
override private [parsley] def debugName = name
}

private [parsley] object Named {
def apply[A](par: LazyParsley[A], name: String): Named[A] = new Named(par, name)

def unapply[A](p: LazyParsley[A]): Option[(LazyParsley[A], String)] = p match {
case n: Named[A @unchecked] => Some((n.par, n.name))
case n: Named[A @unchecked] => Some((n.p, n.name))
case _ => None
}
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class DebuggerUsageSpec extends ParsleyTest {

it should "preserve the prettified names of the parsers" in {
val factory = new Debugging(new DebugContext())
new TaggedWith(factory)(named(fresh(()), "foo").internal, null, None).prettyName shouldBe "foo"
new TaggedWith(factory)(fresh(()).internal, null, None).prettyName shouldBe "fresh"
new TaggedWith(factory)(fresh(()).internal, null, Some("bar")).prettyName shouldBe "bar"
new TaggedWith(factory)(named(fresh(()), "foo").internal, null, None).debugName shouldBe "foo"
new TaggedWith(factory)(fresh(()).internal, null, None).debugName shouldBe "fresh"
new TaggedWith(factory)(fresh(()).internal, null, Some("bar")).debugName shouldBe "bar"
}

"the debugger runtime" should "preserve the result of parsers" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ private class DummyParser extends LazyParsley[Any] {
override protected def findLetsAux[M[_, +_] : ContOps, R](seen: Set[LazyParsley[_]])(implicit state: LetFinderState): M[R, Unit] = Utils.crash()
override protected def preprocess[M[_, +_] : ContOps, R, A_ >: Any](implicit lets: LetMap): M[R, StrictParsley[A_]] = Utils.crash()
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[Any] = visitor.visitUnknown(this, context)
override private [parsley] def prettyName = "dummyParser"
override private [parsley] def debugName = "dummyParser"
}

private class <**> extends LazyParsley[Any] {
override protected def findLetsAux[M[_, +_] : ContOps, R](seen: Set[LazyParsley[_]])(implicit state: LetFinderState): M[R, Unit] = Utils.crash()
override protected def preprocess[M[_, +_] : ContOps, R, A_ >: Any](implicit lets: LetMap): M[R, StrictParsley[A_]] = Utils.crash()
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[Any] = visitor.visitUnknown(this, context)
override private [parsley] def prettyName = "<**>"
override private [parsley] def debugName = "<**>"
}
6 changes: 6 additions & 0 deletions parsley/shared/src/main/scala/parsley/Parsley.scala
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,12 @@ final class Parsley[+A] private [parsley] (private [parsley] val internal: front
*/
def withFilter(pred: A => Boolean): Parsley[A] = this.filter(pred)
// $COVERAGE-ON$

// hidden methods
private [parsley] def unsafeTransparent(): Parsley[A] = {
internal.transparent()
this
}
}

/** This object contains the core "function-style" combinators: all parsers will likely require something from within!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ private [parsley] final class <|>[A](p: LazyParsley[A], q: LazyParsley[A]) exten
// - "orElse" is too wordy.
// - "or" does not exist as a combinator.
// We cannot think of any better options at the moment.
override private [parsley] def prettyName = "<|>"
override private [parsley] def debugName = "<|>"
// $COVERAGE-ON$
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private [parsley] final class ErrorLabel[A](p: LazyParsley[A], label: String, la
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p, label, labels)

override private [parsley] def prettyName = "label"
override private [parsley] def debugName = "label"
// $COVERAGE-ON$
}
private [parsley] final class ErrorHide[A](p: LazyParsley[A]) extends Unary[A, A](p) {
Expand All @@ -22,7 +22,7 @@ private [parsley] final class ErrorHide[A](p: LazyParsley[A]) extends Unary[A, A
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p)

override private[parsley] def prettyName: String = "errorHide"
override private[parsley] def debugName: String = "errorHide"
// $COVERAGE-ON$
}
private [parsley] final class ErrorExplain[A](p: LazyParsley[A], reason: String) extends Unary[A, A](p) {
Expand All @@ -31,7 +31,7 @@ private [parsley] final class ErrorExplain[A](p: LazyParsley[A], reason: String)
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p, reason)

override private[parsley] def prettyName = "explain"
override private[parsley] def debugName = "explain"
// $COVERAGE-ON$
}

Expand All @@ -41,7 +41,7 @@ private [parsley] final class ErrorAmend[A](p: LazyParsley[A], partial: Boolean)
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p, partial)

override private[parsley] def prettyName = "amend"
override private[parsley] def debugName = "amend"
// $COVERAGE-ON$
}
private [parsley] final class ErrorEntrench[A](p: LazyParsley[A]) extends Unary[A, A](p) {
Expand All @@ -50,7 +50,7 @@ private [parsley] final class ErrorEntrench[A](p: LazyParsley[A]) extends Unary[
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p)

override private[parsley] def prettyName = "entrench"
override private[parsley] def debugName = "entrench"
// $COVERAGE-ON$
}
private [parsley] final class ErrorDislodge[A](n: Int, p: LazyParsley[A]) extends Unary[A, A](p) {
Expand All @@ -59,7 +59,7 @@ private [parsley] final class ErrorDislodge[A](n: Int, p: LazyParsley[A]) extend
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(n, p)

override private[parsley] def prettyName = "dislodge"
override private[parsley] def debugName = "dislodge"
// $COVERAGE-ON$
}

Expand All @@ -69,6 +69,6 @@ private [parsley] final class ErrorLexical[A](p: LazyParsley[A]) extends Unary[A
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p)

override private[parsley] def prettyName = "markAsToken"
override private[parsley] def debugName = "markAsToken"
// $COVERAGE-ON$
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private [parsley] final class Lift2[A, B, C](private val f: (A, B) => C, p: Lazy
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[C] = visitor.visit(this, context)(f, p, q)

override private[parsley] def prettyName = "lift.lift2"
override private[parsley] def debugName = "lift.lift2"
// $COVERAGE-ON$
}
private [parsley] final class Lift3[A, B, C, D](private val f: (A, B, C) => D, p: LazyParsley[A], q: =>LazyParsley[B], r: =>LazyParsley[C])
Expand All @@ -25,7 +25,7 @@ private [parsley] final class Lift3[A, B, C, D](private val f: (A, B, C) => D, p
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[D] = visitor.visit(this, context)(f, p, q, r)

override private[parsley] def prettyName = "lift.lift3"
override private[parsley] def debugName = "lift.lift3"
// $COVERAGE-ON$
}
private [parsley] final class Local[S, A](val ref: Ref[S], p: LazyParsley[S], q: =>LazyParsley[A]) extends Binary[S, A, A](p, q) with UsesRef {
Expand All @@ -34,6 +34,6 @@ private [parsley] final class Local[S, A](val ref: Ref[S], p: LazyParsley[S], q:
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(ref, p, q)

override private[parsley] def prettyName = "local"
override private[parsley] def debugName = "local"
// $COVERAGE-ON$
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private [parsley] final class Many[A, C](p: LazyParsley[A], factory: Factory[A,
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[C] = visitor.visit(this, context)(p, factory)

override private[parsley] def prettyName = "many"
override private[parsley] def debugName = "many"
// $COVERAGE-ON$
}
private [parsley] final class ChainPost[A](p: LazyParsley[A], _op: =>LazyParsley[A => A]) extends Binary[A, A => A, A](p, _op) {
Expand All @@ -25,7 +25,7 @@ private [parsley] final class ChainPost[A](p: LazyParsley[A], _op: =>LazyParsley
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p, _op)

override private[parsley] def prettyName = "chain.postfix"
override private[parsley] def debugName = "chain.postfix"
// $COVERAGE-ON$
}
private [parsley] final class ChainPre[A](p: LazyParsley[A], op: LazyParsley[A => A]) extends LazyParsley[A] {
Expand All @@ -41,7 +41,7 @@ private [parsley] final class ChainPre[A](p: LazyParsley[A], op: LazyParsley[A =
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p, op)

override private[parsley] def prettyName = "chain.prefix"
override private[parsley] def debugName = "chain.prefix"
// $COVERAGE-ON$
}
private [parsley] final class Chainl[A, B](init: LazyParsley[B], p: =>LazyParsley[A], op: =>LazyParsley[(B, A) => B])
Expand All @@ -51,7 +51,7 @@ private [parsley] final class Chainl[A, B](init: LazyParsley[B], p: =>LazyParsle
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[B] = visitor.visit(this, context)(init, p, op)

override private[parsley] def prettyName = "infix.left1"
override private[parsley] def debugName = "infix.left1"
// $COVERAGE-ON$
}
private [parsley] final class Chainr[A, B](p: LazyParsley[A], op: =>LazyParsley[(A, B) => B], private [Chainr] val wrap: A => B)
Expand All @@ -61,7 +61,7 @@ private [parsley] final class Chainr[A, B](p: LazyParsley[A], op: =>LazyParsley[
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[B] = visitor.visit(this, context)(p, op, wrap)

override private[parsley] def prettyName = "infix.right1"
override private[parsley] def debugName = "infix.right1"
// $COVERAGE-ON$
}
private [parsley] final class SepEndBy1[A, C](p: LazyParsley[A], sep: =>LazyParsley[_], factory: Factory[A, C]) extends Binary[A, Any, C](p, sep) {
Expand All @@ -70,7 +70,7 @@ private [parsley] final class SepEndBy1[A, C](p: LazyParsley[A], sep: =>LazyPars
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[C] = visitor.visit(this, context)(p, sep, factory)

override private[parsley] def prettyName = "sepEndBy1"
override private[parsley] def debugName = "sepEndBy1"
// $COVERAGE-ON$
}
private [parsley] final class ManyUntil[A, C](body: LazyParsley[Any], factory: Factory[A, C]) extends Unary[Any, C](body) {
Expand All @@ -79,7 +79,7 @@ private [parsley] final class ManyUntil[A, C](body: LazyParsley[Any], factory: F
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[C] = visitor.visit(this, context)(body, factory)

override private[parsley] def prettyName = "manyUntil"
override private[parsley] def debugName = "manyUntil"
// $COVERAGE-ON$
}
private [parsley] final class SkipManyUntil(body: LazyParsley[Any]) extends Unary[Any, Unit](body) {
Expand All @@ -88,6 +88,6 @@ private [parsley] final class SkipManyUntil(body: LazyParsley[Any]) extends Unar
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[Unit] = visitor.visit(this, context)(body)

override private[parsley] def prettyName = "skipManyUntil"
override private[parsley] def debugName = "skipManyUntil"
// $COVERAGE-ON$
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ private [parsley] abstract class LazyParsley[+A] private [deepembedding] {
def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A]

/** Pretty names for parsers, for internal debugging purposes only. */
private [parsley] def prettyName: String
private [parsley] def debugName: String

// TODO:
private [parsley] def transparent(): Unit = ()
private [parsley] def isOpaque: Boolean = debugName != null

/** Pretty-prints a combinator tree, for internal debugging purposes only. */
final private [internal] def prettyAST: String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private [parsley] final class Attempt[A](p: LazyParsley[A]) extends Unary[A, A](
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p)

override private[parsley] def prettyName = "atomic"
override private[parsley] def debugName = "atomic"
// $COVERAGE-ON$
}
private [parsley] final class Look[A](p: LazyParsley[A]) extends Unary[A, A](p) {
Expand All @@ -26,7 +26,7 @@ private [parsley] final class Look[A](p: LazyParsley[A]) extends Unary[A, A](p)
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p)

override private[parsley] def prettyName = "lookAhead"
override private[parsley] def debugName = "lookAhead"
// $COVERAGE-ON$
}
private [parsley] final class NotFollowedBy[A](p: LazyParsley[A]) extends Unary[A, Unit](p) {
Expand All @@ -35,7 +35,7 @@ private [parsley] final class NotFollowedBy[A](p: LazyParsley[A]) extends Unary[
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[Unit] = visitor.visit(this, context)(p)

override private[parsley] def prettyName = "notFollowedBy"
override private[parsley] def debugName = "notFollowedBy"
// $COVERAGE-ON$
}
private [parsley] final class Put[S](val ref: Ref[S], _p: LazyParsley[S]) extends Unary[S, Unit](_p) with UsesRef {
Expand All @@ -44,7 +44,7 @@ private [parsley] final class Put[S](val ref: Ref[S], _p: LazyParsley[S]) extend
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[Unit] = visitor.visit(this, context)(ref, _p)

override private[parsley] def prettyName = "Ref.set"
override private[parsley] def debugName = "Ref.set"
// $COVERAGE-ON$
}
private [parsley] final class NewReg[S, A](val ref: Ref[S], init: LazyParsley[S], body: =>LazyParsley[A]) extends Binary[S, A, A](init, body) with UsesRef {
Expand All @@ -53,7 +53,7 @@ private [parsley] final class NewReg[S, A](val ref: Ref[S], init: LazyParsley[S]
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(ref, init, body)

override private[parsley] def prettyName = "fillRef"
override private[parsley] def debugName = "fillRef"
// $COVERAGE-ON$
}
private [parsley] final class Span(p: LazyParsley[_]) extends Unary[Any, String](p) {
Expand All @@ -62,7 +62,7 @@ private [parsley] final class Span(p: LazyParsley[_]) extends Unary[Any, String]
// $COVERAGE-OFF$
override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[String] = visitor.visit(this, context)(p)

override private[parsley] def prettyName: String = "span"
override private[parsley] def debugName: String = "span"
// $COVERAGE-ON$
}

Expand All @@ -73,29 +73,29 @@ private [parsley] final class Debug[A](p: LazyParsley[A], name: String, ascii: B

override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p, name, ascii, break, watchedRefs)

override private[parsley] def prettyName = "debug"
override private[parsley] def debugName = "debug"
}
private [parsley] final class DebugError[A](p: LazyParsley[A], name: String, ascii: Boolean, errBuilder: ErrorBuilder[_]) extends Unary[A, A](p) {
override def make(p: StrictParsley[A]): StrictParsley[A] = new backend.DebugError(p, name, ascii, errBuilder)

override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p, name, ascii, errBuilder)

override private[parsley] def prettyName = "debugError"
override private[parsley] def debugName = "debugError"
}

private [parsley] final class Profile[A](p: LazyParsley[A], name: String, profiler: Profiler) extends Unary[A, A](p) {
override def make(p: StrictParsley[A]): StrictParsley[A] = new backend.Profile(p, name, profiler)

override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = visitor.visit(this, context)(p, name, profiler)

override private[parsley] def prettyName: String = "profile"
override private[parsley] def debugName: String = "profile"
}

private [parsley] final class Opaque[A](p: LazyParsley[A]) extends Unary[A, A](p) {
override def make(p: StrictParsley[A]): StrictParsley[A] = new backend.Opaque(p)

override def visit[T, U[+_]](visitor: LazyParsleyIVisitor[T, U], context: T): U[A] = p.visit(visitor, context)

override private[parsley] def prettyName: String = "impure"
override private[parsley] def debugName: String = "impure"
}
// $COVERAGE-ON$
Loading

0 comments on commit d6ad26a

Please sign in to comment.