UNPKG

ifc-expressions

Version:

Parsing and evaluation of IFC expressions

179 lines (178 loc) 8.34 kB
import { ExprKind } from "./ExprKind.js"; import { isNullish } from "../util/IfcExpressionUtils.js"; export var ExprEvalStatus; (function (ExprEvalStatus) { ExprEvalStatus[ExprEvalStatus["SUCCESS"] = 1000] = "SUCCESS"; ExprEvalStatus[ExprEvalStatus["ERROR"] = 2000] = "ERROR"; ExprEvalStatus[ExprEvalStatus["UNDEFINED_RESULT"] = 2001] = "UNDEFINED_RESULT"; ExprEvalStatus[ExprEvalStatus["REFERENCE_ERROR"] = 2010] = "REFERENCE_ERROR"; ExprEvalStatus[ExprEvalStatus["CONSEQUENTIAL_ERROR"] = 2020] = "CONSEQUENTIAL_ERROR"; ExprEvalStatus[ExprEvalStatus["MATH_ERROR"] = 2030] = "MATH_ERROR"; ExprEvalStatus[ExprEvalStatus["TYPE_ERROR"] = 2040] = "TYPE_ERROR"; ExprEvalStatus[ExprEvalStatus["NOT_FOUND"] = 2050] = "NOT_FOUND"; ExprEvalStatus[ExprEvalStatus["IFC_PROPERTY_NOT_FOUND"] = 2051] = "IFC_PROPERTY_NOT_FOUND"; ExprEvalStatus[ExprEvalStatus["IFC_PROPERTY_SET_NOT_FOUND"] = 2052] = "IFC_PROPERTY_SET_NOT_FOUND"; ExprEvalStatus[ExprEvalStatus["IFC_TYPE_OBJECT_NOT_FOUND"] = 2053] = "IFC_TYPE_OBJECT_NOT_FOUND"; ExprEvalStatus[ExprEvalStatus["PARSE_ERROR"] = 2080] = "PARSE_ERROR"; ExprEvalStatus[ExprEvalStatus["SYNTAX_ERROR"] = 2081] = "SYNTAX_ERROR"; ExprEvalStatus[ExprEvalStatus["VALIDATION_ERROR"] = 2082] = "VALIDATION_ERROR"; ExprEvalStatus[ExprEvalStatus["STATIC_TYPE_ERROR"] = 2083] = "STATIC_TYPE_ERROR"; ExprEvalStatus[ExprEvalStatus["MISSING_OPERAND"] = 2084] = "MISSING_OPERAND"; ExprEvalStatus[ExprEvalStatus["MISSING_REQUIRED_FUNCTION_ARGUMENT"] = 2085] = "MISSING_REQUIRED_FUNCTION_ARGUMENT"; ExprEvalStatus[ExprEvalStatus["UNKNOWN_FUNCTION"] = 2086] = "UNKNOWN_FUNCTION"; ExprEvalStatus[ExprEvalStatus["WRONG_FUNCTION_ARGUMENT_TYPE"] = 2087] = "WRONG_FUNCTION_ARGUMENT_TYPE"; ExprEvalStatus[ExprEvalStatus["SPURIOUS_FUNCTION_ARGUMENT"] = 2088] = "SPURIOUS_FUNCTION_ARGUMENT"; })(ExprEvalStatus || (ExprEvalStatus = {})); export function isExprEvalStatus(candidate) { return (typeof candidate === "number" && typeof ExprEvalStatus[candidate] !== "undefined"); } export function isExprEvalSuccess(arg) { return (isExprEvalStatus(arg.status) && arg.status >= ExprEvalStatus.SUCCESS && arg.status < ExprEvalStatus.ERROR && typeof arg.result !== "undefined"); } export class ExprEvalSuccessObj { constructor(result) { this.result = result; this.status = ExprEvalStatus.SUCCESS; } } export class ExprEvalErrorObj { constructor(exprKind, status = ExprEvalStatus.ERROR, message, textSpan) { if (!isExprEvalStatus(status) || status < ExprEvalStatus.ERROR) { throw new Error(`ExprEvalStatus ${status} is not an error status"`); } this.status = status; this.exprKind = exprKind; if (!isNullish(message)) { this.message = mapErrorObjectToMessage(message); } if (!isNullish(textSpan)) { this.textSpan = textSpan; } } } export function mapErrorObjectToMessage(errorObjectOrMessage) { if (typeof errorObjectOrMessage === "string") { return errorObjectOrMessage; } else if (errorObjectOrMessage instanceof Error) { return errorObjectOrMessage.message; } else if (Array.isArray(errorObjectOrMessage)) { return errorObjectOrMessage .map((m) => this.createErrorMessage(m)) .join(" "); } return JSON.stringify(errorObjectOrMessage); } export class ExprEvalErrorUndefinedResult extends ExprEvalErrorObj { constructor(epxrKind, textSpan) { super(epxrKind, ExprEvalStatus.UNDEFINED_RESULT, "Expression evaluated to undefined or null - this should never happen", textSpan); } } export class ExprEvalError1Obj extends ExprEvalErrorObj { constructor(exprKind, sub, status, message, textSpan) { super(exprKind, status, message, textSpan); this.sub = sub; } } export class ExprEvalError2Obj extends ExprEvalErrorObj { constructor(exprKind, left, right, status, message, textSpan) { super(exprKind, status, message, textSpan); this.left = left; this.right = right; } } export function isExprEvalError(arg) { return isExprEvalStatus(arg.status) && arg.status >= ExprEvalStatus.ERROR; } export class ExprEvalConsequentialError1Obj extends ExprEvalErrorObj { constructor(exprKind, cause, message, textSpan) { super(exprKind, ExprEvalStatus.CONSEQUENTIAL_ERROR, message || "Consequential error", textSpan); this.exprKind = exprKind; this.cause = cause; } } export class ExprEvalFunctionEvaluationErrorObj extends ExprEvalErrorObj { constructor(exprKind, status, message, functionName, textSpan) { super(exprKind, status, message, textSpan); this.functionName = functionName; } } export class ExprEvalFunctionEvaluationObjectNotFoundErrorObj extends ExprEvalFunctionEvaluationErrorObj { constructor(exprKind, status, message, functionName, objectKey, textSpan) { super(exprKind, status, message, functionName, textSpan); this.offendingKey = objectKey; } } export class ExprEvalFunctionEvaluationConsequentialErrorObj extends ExprEvalFunctionEvaluationErrorObj { constructor(exprKind, functionName, cause, textSpan) { super(exprKind, ExprEvalStatus.CONSEQUENTIAL_ERROR, `Error evaluating function ${functionName}`, functionName, textSpan); this.cause = cause; } } export class ExprEvalMissingRequiredFunctionArgumentErrorObj extends ExprEvalFunctionEvaluationErrorObj { constructor(exprKind, message, functionName, argumentName, argumentIndex, textSpan) { super(exprKind, ExprEvalStatus.MISSING_REQUIRED_FUNCTION_ARGUMENT, message, functionName, textSpan); this.argumentName = argumentName; this.argumentIndex = argumentIndex; } } export class ExprEvalValueErrorObj extends ExprEvalErrorObj { constructor(exprKind, status, message, offendingValue, textSpan) { super(exprKind, status, message, textSpan); this.offendingValue = offendingValue; } } export class ExprEvalTypeErrorObj extends ExprEvalValueErrorObj { constructor(exprKind, message, offendingValue, textSpan) { super(exprKind, ExprEvalStatus.TYPE_ERROR, message, offendingValue, textSpan); } } export class ExprEvalParseErrorObj extends ExprEvalErrorObj { constructor(status, message, offendingInput, textSpan) { super(ExprKind.PARSE_ERROR, status, message, textSpan); this.offendingInput = offendingInput; } } export class ExprEvalValidationErrorObj extends ExprEvalErrorObj { constructor(status, message, textSpan) { super(ExprKind.PARSE_ERROR, status, message, textSpan); } } export class ExprEvalUnknownFunctionErrorObj extends ExprEvalValidationErrorObj { constructor(message, functionName, textSpan) { super(ExprEvalStatus.UNKNOWN_FUNCTION, message, textSpan); this.functionName = functionName; } } export class ExprEvalMissingFunctionArgumentErrorObj extends ExprEvalErrorObj { constructor(message, functionName, argumentName, argumentIndex, textSpan) { super(ExprKind.FUNCTION_ARGUMENTS, ExprEvalStatus.MISSING_REQUIRED_FUNCTION_ARGUMENT, message, textSpan); this.functionName = functionName; this.argumentName = argumentName; this.argumentIndex = argumentIndex; } } export class ExprEvalSpuriousFunctionArgumentErrorObj extends ExprEvalErrorObj { constructor(message, functionName, argumentName, argumentIndex, textSpan) { super(ExprKind.FUNCTION_ARGUMENTS, ExprEvalStatus.SPURIOUS_FUNCTION_ARGUMENT, message, textSpan); this.functionName = functionName; this.argumentName = argumentName; this.argumentIndex = argumentIndex; } } export class ExprEvalWrongFunctionArgumentTypeErrorObj extends ExprEvalValidationErrorObj { constructor(message, functionName, argumentName, argumentIndex, expectedType, actualType, textSpan) { super(ExprEvalStatus.WRONG_FUNCTION_ARGUMENT_TYPE, message, textSpan); this.functionName = functionName; this.argumentName = argumentName; this.argumentIndex = argumentIndex; this.expectedType = expectedType; this.actualType = actualType; } } //# sourceMappingURL=ExprEvalResult.js.map