Skip to content

Commit

Permalink
lazy and stripped private, which seems to help nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Apr 25, 2024
1 parent 06bbb96 commit 2b088c7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
11 changes: 5 additions & 6 deletions parsley-debug/shared/src/main/scala-2/parsley/debuggable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import scala.reflect.macros.blackbox
/** This annotation can be applied to an object or class to record their
* names for the debugging/diagnostic combinators.
*
* @note BE WARNED: this annotation crashes the compiler for objects/classes nested within another type -- this is ok for Scala 3
* @note It requires that the object/class is type-checkable, which due to Scala 2 macro limitations
* involves stripping away the enclosing type itself. This might lead to
* weird edge-cases: if parsley reports that type-checking failed, you
Expand Down Expand Up @@ -52,13 +51,13 @@ private object debuggable {
// can't typecheck constructors in a stand-alone block
val noConDefs = defs.flatMap {
case DefDef(_, name, _, _, _, _) if name == TermName("<init>") => None
// if the type tree is not empty, then we might as well scratch out the body -- helps remove problem values!
case DefDef(mods, name, tyArgs, args, tpt, _) if tpt.nonEmpty => Some(DefDef(mods, name, tyArgs, args, tpt, q"???"))
// in addition, on 2.12, we need to remove `paramaccessor` modifiers on constructor arguments
// but due to API incompatibility, we have to use NoFlags...
case ValDef(Modifiers(_, privateWithin, annotations), name, tpt, x) =>
case ValDef(Modifiers(_, _, annotations), name, tpt, x) =>
// if the type tree is not empty, then we might as well scratch out the body -- helps remove problem values!
Some(ValDef(Modifiers(NoFlags, privateWithin, annotations), name, tpt, if (tpt.nonEmpty) q"???" else x))
// just to be safe, we'll also mark them as LAZY to avoid "dead code following construct"
Some(ValDef(Modifiers(Flag.LAZY, typeNames.EMPTY, annotations), name, tpt, if (tpt.nonEmpty) q"???" else x))
// if the type tree is not empty, then we might as well scratch out the body -- helps remove problem values!
case DefDef(mods, name, tyArgs, args, tpt, _) if tpt.nonEmpty => Some(DefDef(mods, name, tyArgs, args, tpt, q"???"))
case dfn => Some(dfn)
}
val typeMap = c.typecheck(q"..${noConDefs}", c.TERMmode, silent = true) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package parsley.debugger
import scala.annotation.experimental

// scalastyle:off underscore.import
//import DebuggerUsageSpec.Arithmetic
import org.typelevel.scalaccompat.annotation.unused
import parsley.Parsley, Parsley._
import parsley.ParsleyTest
Expand All @@ -22,6 +21,7 @@ import parsley.internal.deepembedding.backend.debugger.Debugging

@experimental
class DebuggerUsageSpec extends ParsleyTest {
import DebuggerUsageSpec.Arithmetic
"the Debugged internal frontend class" should "not allow nesting of Debugged nodes" in {
val factory = new Debugging(new DebugContext())
try {
Expand Down Expand Up @@ -91,7 +91,7 @@ class DebuggerUsageSpec extends ParsleyTest {
}

// Look, for some reason the annotation dies if it's nested, I don't know why, it's impossible to diagnose
//object DebuggerUsageSpec {
object DebuggerUsageSpec {
@experimental @parsley.debuggable
private [parsley] object Arithmetic {
val int: Parsley[BigInt] = satisfy(_.isDigit).foldLeft1(BigInt(0))((acc, c) => acc * 10 + c.asDigit)
Expand All @@ -102,4 +102,4 @@ class DebuggerUsageSpec extends ParsleyTest {
)
lazy val prog: Parsley[List[BigInt]] = many(many(satisfy("\r\n".contains(_))) ~> expr)
}
//}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scala.annotation.experimental

import org.scalatest.Assertions.fail
import parsley.ParsleyTest
import parsley.debugger.Arithmetic
import parsley.debugger.DebuggerUsageSpec
import parsley.debugger.internal.{DebugContext, Renamer}
import parsley.debugger.util.Collector
import parsley.internal.deepembedding.backend.StrictParsley
Expand Down Expand Up @@ -54,12 +54,11 @@ class RenameSpec extends ParsleyTest {

"the Collector implementations" should "collect names of parsers from objects (on supported platforms)" in {
if (Collector.isSupported) {
//Collector.names(Arithmetic)
Renamer.nameOf(None, Arithmetic.prog.internal) shouldBe "prog"
Renamer.nameOf(None, DebuggerUsageSpec.Arithmetic.prog.internal) shouldBe "prog"

info("it should also allow overriding the name")
Collector.assignName(Arithmetic.prog, "foo")
Renamer.nameOf(None, Arithmetic.prog.internal) shouldBe "foo"
Collector.assignName(DebuggerUsageSpec.Arithmetic.prog, "foo")
Renamer.nameOf(None, DebuggerUsageSpec.Arithmetic.prog.internal) shouldBe "foo"
} else alert("the current platform does not support Collector")
}

Expand Down

0 comments on commit 2b088c7

Please sign in to comment.