All Downloads are FREE. Search and download functionalities are using the official Maven repository.

parsley.internal.diagnostics.scala Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 Parsley 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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy