Skip to content

Commit

Permalink
removed internal Map use, the keys aren't needed
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Jan 3, 2025
1 parent 74d27a2 commit 915bd14
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 67 deletions.

This file was deleted.

This file was deleted.

20 changes: 5 additions & 15 deletions parsley-debug/shared/src/main/scala/parsley/debug/DebugTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,17 @@ private [debug] abstract class DebugTree extends Iterable[DebugTree] {

/** What are the child debug nodes for this node?
*
* The map provided by the implementation should be a linked map in order to preserve the
* order of child parser occurrences within each parser.
*
* Internally, child nodes are given an arbitrary numeric suffix to disambiguate them in the map
* if multiple child nodes have the same parser name. This also allows one to traverse the tree
* down specific nodes via keys.
*
* Those internal names are not represented if checking [[parserName]].
* To preserve the order of child parser occurrences within each parser, this necessarily
* must be a sequence: List is convenient for processing.
*/
def nodeChildren: List[DebugTree]

// $COVERAGE-OFF$
/** Provides a depth-first view of the tree as an iterator. */
override def iterator: Iterator[DebugTree] = Iterator(this) ++ nodeChildren.flatMap(_.iterator)
override def iterator: Iterator[DebugTree] = Iterator(this) ++ nodeChildren.iterator.flatMap(_.iterator)

// this isn't needed, but apparently the only place where the keys were referenced in the internal
// maps...
/*override def toString: String = {
val possibleChildNumber = childNumber.map(", " + _.toString).getOrElse("")
val hasSuccess = parseResults.exists(_.success)
Expand All @@ -65,10 +61,4 @@ private [debug] abstract class DebugTree extends Iterable[DebugTree] {
s"DebugTree { name: $parserName ($internalName$possibleChildNumber), success: $hasSuccess, children: $keys }"
}*/
}

/*private [debug] object DebugTree {
def unapply(dt: DebugTree): Some[(String, String, Option[Long], String, Option[ParseAttempt], Map[String, DebugTree])] = {
Some((dt.parserName, dt.internalName, dt.childNumber, dt.fullInput, dt.parseResults, dt.nodeChildren))
}
}*/
// $COVERAGE-ON$
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private [parsley] class DebugContext(private val toStringRules: PartialFunction[
XAssert.assert(!(ch.size > 1), s"The root tree has somehow gained multiple children. (${ch.size})")

// This should never fail.
ch.valuesIterator.next()
ch.head
}

// Add an attempt of parsing at the current stack point.
Expand All @@ -69,20 +69,21 @@ private [parsley] class DebugContext(private val toStringRules: PartialFunction[
}

// Unique parser IDs.
private var uid = -1L
/*private var uid = -1L
private def nextUid(): Long = {
uid += 1L
uid
}
}*/

// Push a new parser onto the parser callstack.
def push(fullInput: String, parser: LazyParsley[_], userAssignedName: Option[String]): Unit = {
val newTree = new TransientDebugTree(fullInput = fullInput)
newTree.name = Renamer.nameOf(userAssignedName, parser)
newTree.internal = Renamer.internalName(parser)

val uid = nextUid()
builderStack.head.children(s"${newTree.name}-#$uid") = newTree
//val uid = nextUid()
//builderStack.head.children(s"${newTree.name}-#$uid") = newTree
builderStack.head.children += newTree
builderStack.prepend(newTree)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
*/
package parsley.debug.internal

import scala.collection.immutable.ListMap
import scala.collection.mutable

import parsley.debug.{DebugTree, ParseAttempt}
import parsley.debug.util.XMap

/** A mutable implementation of [[DebugTree]], used when constructing the tree as a parser is
* running.
Expand All @@ -23,7 +21,7 @@ import parsley.debug.util.XMap
*/
private [parsley] class TransientDebugTree(var name: String = "", var internal: String = "", val fullInput: String,
var parse: Option[ParseAttempt] = None, var cNumber: Option[Long] = None,
val children: mutable.LinkedHashMap[String, TransientDebugTree] = mutable.LinkedHashMap.empty) extends DebugTree {
val children: mutable.ListBuffer[TransientDebugTree] = mutable.ListBuffer.empty) extends DebugTree {
// These are user-facing, and will depend heavily on what the parser looks like.
// $COVERAGE-OFF$
override def parserName: String = name
Expand All @@ -35,20 +33,7 @@ private [parsley] class TransientDebugTree(var name: String = "", var internal:
// The pair stores the input the parser attempted to parse and its success.
override def parseResults: Option[ParseAttempt] = parse

// FIXME: doesn't this repeatedly remake the tree? when is this called?
// TODO: I'm pretty sure nothing ever uses this as a map! this could be a list
// which would be far more efficient (it also appears that nothing ever uses the keys?)
// TODO: move the name into the DebugTree or remove them entirely...
override def nodeChildren: List[DebugTree] = new XMap[String, DebugTree] {
// We'll use a copy-on-write methodology for these two -- remember, ordering is important!
override def removed(key: String): Map[String, DebugTree] = ListMap.empty ++ children - key
override def updated[V1 >: DebugTree](key: String, value: V1): Map[String, V1] = (ListMap.empty ++ children).updated(key, value)

// For get, size and iterator, we'll just use the mutable map.
override def get(key: String): Option[DebugTree] = children.get(key)
override def iterator: Iterator[(String, DebugTree)] = children.iterator
override def size: Int = children.size
}.values.toList
override def nodeChildren: List[DebugTree] = children.toList

// Factors out inputs or results for parsers with children.
private type Augment = (Long, (Int, Int))
Expand All @@ -74,7 +59,7 @@ private [parsley] class TransientDebugTree(var name: String = "", var internal:
// don't augment input when the consumption was rolled-back
case ((aid, (_, aen)), st) if aen > p.toOffset =>
// remove the UID from the child (ew gross, there has to be a better way to do this?!)
children.valuesIterator.find(_.cNumber.contains(aid)).foreach { t =>
children.find(_.cNumber.contains(aid)).foreach { t =>
t.cNumber = None
}
st
Expand Down

0 comments on commit 915bd14

Please sign in to comment.