Skip to content

Commit

Permalink
Backport 4.1/various error related bugs (#179)
Browse files Browse the repository at this point in the history
* Fixed overwide carets in trivial errors, hints from adjusting unexpected size to 0, and hints from getting reborn during an iterative combinator

* version bump to 4.1.8
  • Loading branch information
j-mie6 authored Apr 30, 2023
1 parent beebbff commit 2922e83
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Parsley is a fast and modern parser combinator library for Scala based loosely o
Parsley is distributed on Maven Central, and can be added to your project via:

```scala
libraryDependencies += "com.github.j-mie6" %% "parsley" % "4.1.7"
libraryDependencies += "com.github.j-mie6" %% "parsley" % "4.1.8"
```

Documentation can be found [**here**](https://javadoc.io/doc/com.github.j-mie6/parsley_2.13/latest/index.html)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private [internal] case class TrivialError(offset: Int, line: Int, col: Int,
builder.unexpected(unexpectedTok.toOption.map(_._1)),
builder.expected(builder.combineExpectedItems(expecteds.map(_.formatExpect))),
builder.combineMessages(reasons.map(builder.reason(_)).toSeq),
builder.lineInfo(line, beforeLines, afterLines, caret, caretSize))
builder.lineInfo(line, beforeLines, afterLines, caret, math.min(caretSize, line.length-caret)))
}
}
private [internal] case class FancyError(offset: Int, line: Int, col: Int, msgs: List[String], caretWidth: Int, lexicalError: Boolean) extends ParseError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private [parsley] final class Context(private [machine] var instrs: Array[Instr]

private [machine] def updateCheckOffsetAndHints() = {
this.checkStack.offset = this.offset
this.hintsValidOffset = this.offset
//this.hintsValidOffset = this.offset // FIXME: verify that this is ok to remove, it seems stupid now that I think about it
}

// $COVERAGE-OFF$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,19 @@ private [errors] final class HintCollector(hints: mutable.Set[ExpectItem]) {
/** Creates a new collector that starts empty */
def this() = this(mutable.Set.empty)

private var width = 0
private var width = Option.empty[Int]

/** Adds a new hint into the collector. */
def +=(hint: ExpectItem): Unit = this.hints += hint
/** Adds several hints into the collector. */
def ++=(hints: Iterable[ExpectItem]): Unit = this.hints ++= hints

/** Gets the width of the unexpected token that may have accompanied the original hints */
def unexpectWidth: Int = width
def unexpectWidth: Option[Int] = width
/** Updates the width of the unexpected token that may have accompanied these hints: this
* can only get wider.
*/
def updateWidth(sz: Int): Unit = width = Math.max(width, sz)
def updateWidth(sz: Int): Unit = width = Some(Math.max(width.getOrElse(0), sz))

/** Generates an immutable snapshot of this collector */
def mkSet: Set[ExpectItem] = this.hints.toSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,7 @@ private [errors] final class WithHints private [errors] (val err: TrivialDefuncE
override def makeTrivial(builder: TrivialErrorBuilder): Unit = {
err.makeTrivial(builder)
builder.whenAcceptingExpected {
val size = hints.updateExpectedsAndGetSize(builder)
builder.updateUnexpected(size)
for (size <- hints.updateExpectedsAndGetSize(builder)) builder.updateUnexpected(size)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private [machine] sealed abstract class DefuncHints {
/** This function evaluates this `DefuncHints` structure into the actual set of
* error items it represents and adds this directly into the provided `TrivialErrorBuilder`
*/
private [machine] def updateExpectedsAndGetSize(builder: TrivialErrorBuilder): Int = {
private [machine] def updateExpectedsAndGetSize(builder: TrivialErrorBuilder): Option[Int] = {
val hintCollector = builder.makeHintCollector
collect(hintCollector)
hintCollector.unexpectWidth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import scala.collection.mutable
import parsley.internal.machine.Context
import parsley.internal.machine.XAssert._

// TODO: Now PushHandlerAndCheck(label, false), so could be removed again!
private [internal] final class PushHandlerIterative(var label: Int) extends InstrWithLabel {
override def apply(ctx: Context): Unit = {
ensureRegularInstruction(ctx)
// This is used for iterative parsers, which must ensure that invalidated hints are invalided _now_
ctx.invalidateHints()
//ctx.invalidateHints() // FIXME: This has been removed because hint setting in updateCheckOffsetAndHints has been disabled, pending deep thought
ctx.pushCheck()
ctx.pushHandler(label)
ctx.inc()
Expand Down

0 comments on commit 2922e83

Please sign in to comment.