Skip to content

Commit

Permalink
Better fix for #143
Browse files Browse the repository at this point in the history
  • Loading branch information
travisbrown committed Jan 4, 2019
1 parent 287ae29 commit e34c6b3
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion parser/src/main/scala/jawn/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ abstract class Parser[J] {
protected[this] def die(i: Int, msg: String): Nothing =
die(i, msg, ErrorContext)

/**
* A version of `at` that returns as much of the indicated substring as
* possible without throwing exceptions.
*
* This method is necessary because `at(i, j)` may fail with an exception even
* when `atEof(i, j)` returns false (see #143 for an example). This method is
* a workaround; it is not optimized or robust against invalid input and
* should not be used outside of the context of `die`.
*/
private[this] def safeAt(i: Int, j: Int): CharSequence =
if (j <= i) "" else {
try at(i, j) catch {
case _: Exception =>
safeAt(i, j - 1)
}
}

/**
* Used to generate error messages with character info and offsets.
*
Expand All @@ -108,7 +125,7 @@ abstract class Parser[J] {
} else {
var offset = 0
while (offset < chars && !atEof(i + offset)) offset += 1
val txt = at(i, i + offset)
val txt = safeAt(i, i + offset)
if (atEof(i + offset)) s"'$txt'" else s"'$txt...'"
}
val s = "%s got %s (line %d, column %d)" format (msg, got, y, x)
Expand Down

0 comments on commit e34c6b3

Please sign in to comment.