feat(base-formula): add parse-error and cell-error sentinels

This commit is contained in:
Philipinho
2026-04-23 23:34:05 +01:00
parent 4c2d6772f1
commit 7202e65a07
3 changed files with 42 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
// packages/base-formula/src/error.ts
import type { ErrorCell, ErrorCode } from "./types";
export type ParseErrorCode =
| "UNEXPECTED_TOKEN"
| "UNEXPECTED_EOF"
| "UNKNOWN_PROPERTY"
| "UNKNOWN_FUNCTION"
| "ARITY_MISMATCH"
| "TYPE_MISMATCH"
| "CYCLE";
export type ParseError = {
code: ParseErrorCode;
message: string;
span: { start: number; end: number };
hint?: string;
};
export class FormulaParseError extends Error {
readonly errors: ParseError[];
constructor(errors: ParseError[]) {
super(errors.map((e) => `${e.code}: ${e.message}`).join("; "));
this.errors = errors;
this.name = "FormulaParseError";
}
}
export function makeErrorCell(code: ErrorCode, msg: string): ErrorCell {
return { __err: code, msg, v: 1 };
}
export function isErrorCell(v: unknown): v is ErrorCell {
return (
typeof v === "object" &&
v !== null &&
"__err" in v &&
typeof (v as { __err: unknown }).__err === "string"
);
}
@@ -2,3 +2,4 @@
// Does NOT export eval or the function registry.
export * from "./ast";
export * from "./types";
export * from "./error";
@@ -1,3 +1,4 @@
// Server-side public surface: everything in client + evaluator + registry.
export * from "./ast";
export * from "./types";
export * from "./error";