-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #232 by catching common errors and presenting them to the user in a more accessible way.
- Loading branch information
Showing
9 changed files
with
98 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
parsley/shared/src/main/scala/parsley/exceptions/CorruptedReferenceException.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright 2020 Parsley Contributors <https://github.com/j-mie6/Parsley/graphs/contributors> | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
package parsley.exceptions | ||
|
||
// $COVERAGE-OFF$ | ||
private [parsley] class CorruptedReferenceException | ||
extends ParsleyException("A reference has been used across two different parsers in separate calls to parse, causing it to be misallocated") | ||
// $COVERAGE-ON$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
parsley/shared/src/main/scala/parsley/internal/diagnostics.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2020 Parsley Contributors <https://github.com/j-mie6/Parsley/graphs/contributors> | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
package parsley.internal.diagnostics | ||
|
||
import parsley.exceptions.{BadLazinessException, CorruptedReferenceException, ParsleyException} | ||
|
||
private [parsley] object UserException { | ||
def unapply(e: Throwable): Option[Throwable] = e match { | ||
case _: ParsleyException => None | ||
case e if userStackTrace(e.getStackTrace) => | ||
e.setStackTrace(pruneParsley(e.getStackTrace)) | ||
Some(e) | ||
case _ => None | ||
} | ||
|
||
def userStackTrace(e: Array[StackTraceElement]) = e.view.takeWhile(!_.getClassName.startsWith("parsley.internal")).exists { ste => | ||
!ste.getClassName.startsWith("scala") || !ste.getClassName.startsWith("java") | ||
} | ||
def pruneParsley(e: Array[StackTraceElement]): Array[StackTraceElement] = { | ||
val (userBits, parsleyTrace) = e.span(!_.getClassName.startsWith("parsley.internal")) | ||
userBits ++ parsleyTrace.dropWhile(_.getClassName.startsWith("parsley.internal")) | ||
} | ||
} | ||
|
||
private [parsley] object RegisterOutOfBoundsException { | ||
def unapply(e: Throwable): Option[Throwable] = e match { | ||
case e: ArrayIndexOutOfBoundsException => e.getStackTrace.headOption.collect { | ||
// this exception was thrown plainly during the execution of an instruction | ||
// only register arrays are accessed raw like this: therefore it must be an | ||
// out of bounds register. | ||
case ste if ste.getMethodName == "apply" | ||
&& ste.getClassName.startsWith("parsley.internal.machine.instructions") => | ||
val err = new CorruptedReferenceException | ||
err.addSuppressed(e) | ||
err | ||
} | ||
case _ => None | ||
} | ||
} | ||
|
||
private [parsley] object NullParserException { | ||
def unapply(e: Throwable): Option[Throwable] = e match { | ||
// this should only be true when the null was tripped from within the parsley namespace, | ||
// not the user one | ||
case e: NullPointerException if !UserException.userStackTrace(e.getStackTrace) => Some(new BadLazinessException) | ||
case _ => None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters