Skip to content

Commit

Permalink
Improved tabilification
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Aug 11, 2023
1 parent 4578ad4 commit a886bce
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import parsley.XAssert._
import parsley.internal.collection.mutable.SinglyLinkedList, SinglyLinkedList.LinkedListIterator
import parsley.internal.deepembedding.ContOps, ContOps.{result, suspend, ContAdapter}
import parsley.internal.deepembedding.singletons._
import parsley.internal.errors.ExpectItem
import parsley.internal.errors.{ExpectDesc, ExpectItem}
import parsley.internal.machine.instructions

// scalastyle:off underscore.import
import Choice._
import StrictParsley.InstrBuffer
// scalastyle:on underscore.import

// TODO: can we tabilify across a Let?
// FIXME: It's annoying this doesn't work if the first thing is not tablable: let's make it more fine-grained to create groupings?
private [deepembedding] final class Choice[A](private [backend] val alt1: StrictParsley[A],
private [backend] var alt2: StrictParsley[A],
private [backend] var alts: SinglyLinkedList[StrictParsley[A]]) extends StrictParsley[A] {
Expand Down Expand Up @@ -222,7 +224,7 @@ private [backend] object Choice {
expecteds, expectedss.zip(leads.toList.reverseIterator.map(backtracking(_)).toList))
}

@tailrec private def tablable(p: StrictParsley[_], backtracks: Boolean): Option[(Char, Iterable[ExpectItem], Int, Boolean)] = p match {
private def tablable(p: StrictParsley[_], backtracks: Boolean): Option[(Char, Iterable[ExpectItem], Int, Boolean)] = p match {
// CODO: Numeric parsers by leading digit (This one would require changing the foldTablified function a bit)
case ct@CharTok(c) => Some((c, ct.expected.asExpectItems(c), 1, backtracks))
case ct@SupplementaryCharTok(c) => Some((Character.highSurrogate(c), ct.expected.asExpectItems(Character.toChars(c).mkString), 1, backtracks))
Expand All @@ -233,6 +235,13 @@ private [backend] object Choice {
case t@token.SoftKeyword(s) if t.caseSensitive => Some((s.head, t.expected.asExpectDescs(s), s.codePointCount(0, s.length), backtracks))
case t@token.SoftOperator(s) => Some((s.head, t.expected.asExpectDescs(s), s.codePointCount(0, s.length), backtracks))
case Attempt(t) => tablable(t, backtracks = true)
case ErrorLabel(t, labels) => tablable(t, backtracks).map {
case (c, _, width, backtracks) => (c, labels.map(new ExpectDesc(_)), width, backtracks)
}
case ErrorHide(t) => tablable(t, backtracks).map {
case (c, _, _, backtracks) => (c, None, 0, backtracks)
}
case TablableErrors(t) => tablable(t, backtracks)
case (_: Pure[_]) <*> t => tablable(t, backtracks)
case Lift2(_, t, _) => tablable(t, backtracks)
case Lift3(_, t, _, _) => tablable(t, backtracks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ private [backend] object ErrorLabel {
def apply[A](p: StrictParsley[A], labels: scala.Seq[String]): ErrorLabel[A] = new ErrorLabel(p, labels)
def unapply[A](self: ErrorLabel[A]): Some[(StrictParsley[A], scala.Seq[String])] = Some((self.p, self.labels))
}
private [backend] object ErrorHide {
def unapply[A](self: ErrorHide[A]): Some[StrictParsley[A]] = Some(self.p)
}
private [backend] object ErrorExplain {
def apply[A](p: StrictParsley[A], reason: String): ErrorExplain[A] = new ErrorExplain(p, reason)
}

private [backend] object TablableErrors {
def unapply[A](self: StrictParsley[A]): Option[StrictParsley[A]] = self match {
case self: ErrorAmend[A] => Some(self.p)
case self: ErrorLexical[A] => Some(self.p) // is this correct?
case _ => None
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ private [deepembedding] final class Seq[A](private [backend] var before: DoublyL
}

// TODO: Get behaves much like pure except for shifting positions
// TODO: can this be optimised to reduce repeated matching?
override def optimise: StrictParsley[A] = this match {
// Assume that this is eliminated first, so not other before or afters
case (_: Pure[_]) **> u => u
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private [internal] final class EscapeAtMost(n: Int, radix: Int) extends EscapeSo
}

// $COVERAGE-OFF$
override def toString: String = "EscapeAtMost"
override def toString: String = s"EscapeAtMost(n = $n, radix = $radix)"
// $COVERAGE-ON$
}

Expand Down Expand Up @@ -153,6 +153,6 @@ private [internal] final class EscapeOneOfExactly(radix: Int, ns: List[Int], ine
}

// $COVERAGE-OFF$
override def toString: String = "EscapeOneOfExactly"
override def toString: String = s"EscapeOneOfExactly(ns = $ns, radix = $radix)"
// $COVERAGE-ON$
}

0 comments on commit a886bce

Please sign in to comment.