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

org.partiql.lang.errors.ProblemHandler.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0-perf.1
Show newest version
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)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy