org.partiql.lang.errors.ProblemHandler.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of partiql-lang-kotlin Show documentation
Show all versions of partiql-lang-kotlin Show documentation
An implementation of PartiQL for the JVM written in Kotlin.
package org.partiql.lang.errors
import org.partiql.lang.ast.passes.SemanticException
/** Handles the encountered problem. */
interface ProblemHandler {
/** Handles a [problem] */
fun handleProblem(problem: Problem)
}
/**
* A [ProblemHandler] that collects all of the encountered [Problem]s without throwing.
*
* This is intended to be used when wanting to collect multiple problems that may be encountered (e.g. a static type
* inference pass that can result in multiple errors and/or warnings). This handler does not collect other exceptions
* that may be thrown.
*/
internal class ProblemCollector : ProblemHandler {
private val problemList = mutableListOf()
val problems: List
get() = problemList
val hasErrors: Boolean
get() = problemList.any { it.details.severity == ProblemSeverity.ERROR }
val hasWarnings: Boolean
get() = problemList.any { it.details.severity == ProblemSeverity.WARNING }
override fun handleProblem(problem: Problem) {
problemList.add(problem)
}
}
/**
* A [ProblemHandler] that throws the first [Problem] that has a [ProblemSeverity] of [ProblemSeverity.ERROR] as a
* [SemanticException].
*
* This is intended to support existing internal code (e.g. CompilerPipeline, StaticTypeInferenceVisitorTransform)
* behavior that expects the first encountered problem to be thrown. Once multiple problem handling is supported
* in that code, this class can be removed.
*
* @throws SemanticException on the first [Problem] logged with severity of [ProblemSeverity.ERROR]
*/
internal class ProblemThrower : ProblemHandler {
override fun handleProblem(problem: Problem) {
if (problem.details.severity == ProblemSeverity.ERROR) {
throw SemanticException(problem)
}
}
}