Skip to content

Commit 62b7637

Browse files
committed
docs: add JSDoc for exported API
1 parent f3c5bb4 commit 62b7637

5 files changed

Lines changed: 85 additions & 3 deletions

File tree

mod.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type { Expr, NodeBase, Span } from "./src/ast/mod.ts";
1+
export type { BinaryOp, Expr, NodeBase, Span, UnaryOp } from "./src/ast/mod.ts";
22
export {
33
formatCaret,
44
type FormatCaretOptions,
@@ -24,5 +24,9 @@ export {
2424
evaluateExpression,
2525
type EvaluateExpressionOptions,
2626
ExpEvalError,
27+
type RuntimeArray,
28+
type RuntimeFunction,
29+
type RuntimeObject,
30+
type RuntimePrimitive,
2731
type RuntimeValue,
2832
} from "./src/eval.ts";

src/ast/mod.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1+
/**
2+
* Byte span (half-open) into the original input.
3+
*
4+
* - `start` is inclusive
5+
* - `end` is exclusive
6+
*/
17
export type Span = Readonly<{
28
start: number;
39
end: number;
410
}>;
511

12+
/** Base fields common to all AST nodes. */
613
export type NodeBase = Readonly<{
714
span: Span;
815
}>;
916

17+
/** Unary operator tokens supported by the expression language. */
1018
export type UnaryOp = "!" | "-" | "+";
1119

20+
/** Binary operator tokens supported by the expression language. */
1221
export type BinaryOp =
1322
| "+"
1423
| "-"
@@ -24,6 +33,11 @@ export type BinaryOp =
2433
| "&&"
2534
| "||";
2635

36+
/**
37+
* Expression AST node.
38+
*
39+
* Every node includes a `span` into the original input for diagnostics.
40+
*/
2741
export type Expr =
2842
| (NodeBase & { kind: "number"; value: number })
2943
| (NodeBase & { kind: "string"; value: string })
@@ -42,6 +56,7 @@ export type Expr =
4256
alternate: Expr;
4357
});
4458

59+
/** Create a unary expression node. */
4560
export const mkUnary = (op: UnaryOp, start: number, expr: Expr): Expr => {
4661
return {
4762
kind: "unary",
@@ -51,6 +66,7 @@ export const mkUnary = (op: UnaryOp, start: number, expr: Expr): Expr => {
5166
};
5267
};
5368

69+
/** Create a binary expression node. */
5470
export const mkBinary = (left: Expr, op: BinaryOp, right: Expr): Expr => {
5571
return {
5672
kind: "binary",
@@ -61,6 +77,7 @@ export const mkBinary = (left: Expr, op: BinaryOp, right: Expr): Expr => {
6177
};
6278
};
6379

80+
/** Create a member access node (`object.property`). */
6481
export const mkMember = (
6582
object: Expr,
6683
property: { value: string; end: number },
@@ -73,6 +90,7 @@ export const mkMember = (
7390
};
7491
};
7592

93+
/** Create a call node (`callee(args...)`). */
7694
export const mkCall = (callee: Expr, args: Expr[], end: number): Expr => {
7795
return {
7896
kind: "call",

src/diagnostics.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { Span } from "./ast/mod.ts";
22

3+
/** Options for `formatCaret` / `formatSpanCaret`. */
34
export type FormatCaretOptions = Readonly<{
45
/** Max characters to include before the index. Default: 40 */
56
before?: number;
67
/** Max characters to include after the index. Default: 40 */
78
after?: number;
89
}>;
910

11+
/** Options for `formatDiagnosticReport`. */
1012
export type FormatReportOptions = Readonly<{
1113
/** Include N lines before/after the error line. Default: 0 */
1214
contextLines?: number;
@@ -74,6 +76,7 @@ export const formatCaret = (
7476
return `${snippet}\n${" ".repeat(caretPos)}^`;
7577
};
7678

79+
/** Format a caret snippet from an AST `span` (uses `span.start`). */
7780
export const formatSpanCaret = (
7881
input: string,
7982
span: Span,
@@ -82,6 +85,7 @@ export const formatSpanCaret = (
8285
return formatCaret(input, span.start, opts);
8386
};
8487

88+
/** Input for `formatDiagnosticCaret`. */
8589
export type FormatDiagnosticCaretSource = Readonly<{
8690
index?: number;
8791
span?: Span;
@@ -103,6 +107,7 @@ export const formatDiagnosticCaret = (
103107
return formatCaret(input, 0, opts);
104108
};
105109

110+
/** Input for `formatDiagnosticReport`. */
106111
export type FormatDiagnosticReportSource = Readonly<{
107112
index?: number;
108113
span?: Span;

src/eval.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
import type { Expr, Span } from "./ast/mod.ts";
22
import { parseExpression } from "./parse.ts";
33

4+
/** Primitive runtime values supported by the evaluator. */
45
export type RuntimePrimitive = undefined | null | boolean | number | string;
56

7+
/** A function callable from expressions (must accept/return `RuntimeValue`). */
68
export type RuntimeFunction = (...args: RuntimeValue[]) => RuntimeValue;
79

10+
/** A `RuntimeValue` array. */
811
export interface RuntimeArray extends Array<RuntimeValue> {}
912

13+
/** A plain object mapping string keys to `RuntimeValue`. */
1014
export interface RuntimeObject {
15+
/** Own enumerable properties (prototype is ignored by the evaluator). */
1116
[key: string]: RuntimeValue;
1217
}
1318

19+
/**
20+
* Allowed runtime data model for evaluation.
21+
*
22+
* Values are validated at runtime when present in `env`, and function return
23+
* values are also validated.
24+
*/
1425
export type RuntimeValue =
1526
| RuntimePrimitive
1627
| RuntimeArray
1728
| RuntimeObject
1829
| RuntimeFunction;
1930

31+
/** Options for `evaluateAst` and `evaluateExpression`. */
2032
export type EvalOptions = Readonly<{
2133
/**
2234
* Identifier bindings available to the expression.
@@ -50,6 +62,12 @@ export type EvalOptions = Readonly<{
5062
throwOnError?: boolean;
5163
}>;
5264

65+
/**
66+
* An evaluation failure.
67+
*
68+
* - `span` is present for errors tied to a specific AST node.
69+
* - `index` is present when evaluation failed because parsing failed.
70+
*/
5371
export type EvalError = Readonly<{
5472
message: string;
5573
span?: Span;
@@ -58,11 +76,20 @@ export type EvalError = Readonly<{
5876
index?: number;
5977
}>;
6078

79+
/**
80+
* Thrown evaluation error (default mode).
81+
*
82+
* Carries either `span` (eval failures) and/or `index` (parse failures).
83+
*/
6184
export class ExpEvalError extends Error {
85+
/** AST span for eval errors tied to a node. */
6286
readonly span?: Span;
87+
/** Step counter at the time of failure (useful with budgets). */
6388
readonly steps?: number;
89+
/** Byte index into input when the failure originated from parsing. */
6490
readonly index?: number;
6591

92+
/** Create an `ExpEvalError` from an `EvalError` payload. */
6693
constructor(error: EvalError) {
6794
super(error.message);
6895
this.name = "ExpEvalError";
@@ -72,6 +99,7 @@ export class ExpEvalError extends Error {
7299
}
73100
}
74101

102+
/** Result returned by `evaluateAst` / `evaluateExpression` in non-throwing mode. */
75103
export type EvalResult =
76104
| Readonly<{ success: true; value: RuntimeValue }>
77105
| Readonly<{ success: false; error: EvalError }>;
@@ -416,6 +444,7 @@ const evalExpr = (expr: Expr, ctx: Ctx): EvalResult => {
416444
}
417445
};
418446

447+
/** Evaluate a pre-parsed AST. */
419448
export function evaluateAst(expr: Expr, opts: EvalOptions = {}): EvalResult {
420449
const throwOnError = opts.throwOnError ?? true;
421450

@@ -442,14 +471,20 @@ export function evaluateAst(expr: Expr, opts: EvalOptions = {}): EvalResult {
442471
return res;
443472
}
444473

474+
/** Options for `evaluateExpression` (includes all `EvalOptions`). */
445475
export type EvaluateExpressionOptions =
446476
& EvalOptions
447477
& Readonly<{
448478
/** When true, throw on parse failure. Default: true */
449479
throwOnParseError?: boolean;
450480
}>;
451481

452-
/** Parse + evaluate a single expression. */
482+
/**
483+
* Parse + evaluate a single expression.
484+
*
485+
* If `throwOnParseError: false`, parse failures return an `EvalError` that
486+
* includes `index` so callers can render diagnostics.
487+
*/
453488
export function evaluateExpression(
454489
input: string,
455490
opts: EvaluateExpressionOptions = {},

src/parse.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,37 @@ import {
3030
} from "./ast/mod.ts";
3131
import { createStringSpan } from "./string_literal.ts";
3232

33+
/** Options for `parseExpression`. */
3334
export type ParseOptions = Readonly<{
3435
/** When true, throw on parse failure. Default: true */
3536
throwOnError?: boolean;
3637
}>;
3738

39+
/**
40+
* A parse failure.
41+
*
42+
* `index` is a byte index into the input string.
43+
*/
3844
export type ParseError = Readonly<{
3945
message: string;
4046
index: number;
4147
}>;
4248

49+
/** Result of `parseExpression`. */
4350
export type ParseResult =
4451
| Readonly<{ success: true; value: Expr }>
4552
| Readonly<{ success: false; error: ParseError }>;
4653

54+
/**
55+
* Thrown parse error (default mode).
56+
*
57+
* Carries the byte `index` into the original input.
58+
*/
4759
export class ExpParseError extends Error {
60+
/** Byte index into the input string where parsing failed. */
4861
readonly index: number;
4962

63+
/** Create an `ExpParseError` from a `ParseError` payload. */
5064
constructor(error: ParseError) {
5165
super(error.message);
5266
this.name = "ExpParseError";
@@ -370,7 +384,13 @@ const ExpressionLang: ExprLang = createLanguage<ExprLang>({
370384
File: (s) => map(seq(lx.trivia, s.Expression, eof()), ([, e]) => e),
371385
});
372386

373-
/** Parse a single expression. */
387+
/**
388+
* Parse a single expression into an AST.
389+
*
390+
* - On success: returns `{ success: true, value }`.
391+
* - On failure: throws `ExpParseError` by default.
392+
* Set `throwOnError: false` to get `{ success: false, error }`.
393+
*/
374394
export function parseExpression(
375395
input: string,
376396
opts: ParseOptions = {},

0 commit comments

Comments
 (0)