stringent 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -73
- package/dist/context.d.ts +20 -2
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +1 -0
- package/dist/context.js.map +1 -0
- package/dist/createParser.d.ts +109 -26
- package/dist/createParser.d.ts.map +1 -0
- package/dist/createParser.js +80 -19
- package/dist/createParser.js.map +1 -0
- package/dist/errors.d.ts +121 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +186 -0
- package/dist/errors.js.map +1 -0
- package/dist/grammar/index.d.ts +19 -14
- package/dist/grammar/index.d.ts.map +1 -0
- package/dist/grammar/index.js +4 -3
- package/dist/grammar/index.js.map +1 -0
- package/dist/index.d.ts +19 -11
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -7
- package/dist/index.js.map +1 -0
- package/dist/parse/index.d.ts +101 -27
- package/dist/parse/index.d.ts.map +1 -0
- package/dist/parse/index.js +1 -0
- package/dist/parse/index.js.map +1 -0
- package/dist/performance.bench.d.ts +10 -0
- package/dist/performance.bench.d.ts.map +1 -0
- package/dist/performance.bench.js +379 -0
- package/dist/performance.bench.js.map +1 -0
- package/dist/primitive/index.d.ts +27 -35
- package/dist/primitive/index.d.ts.map +1 -0
- package/dist/primitive/index.js +22 -17
- package/dist/primitive/index.js.map +1 -0
- package/dist/runtime/eval.d.ts +157 -0
- package/dist/runtime/eval.d.ts.map +1 -0
- package/dist/runtime/eval.js +206 -0
- package/dist/runtime/eval.js.map +1 -0
- package/dist/runtime/infer.d.ts +2 -1
- package/dist/runtime/infer.d.ts.map +1 -0
- package/dist/runtime/infer.js +3 -2
- package/dist/runtime/infer.js.map +1 -0
- package/dist/runtime/parser.d.ts +92 -11
- package/dist/runtime/parser.d.ts.map +1 -0
- package/dist/runtime/parser.js +522 -47
- package/dist/runtime/parser.js.map +1 -0
- package/dist/schema/index.d.ts +230 -27
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +54 -28
- package/dist/schema/index.js.map +1 -0
- package/dist/static/infer.d.ts +4 -3
- package/dist/static/infer.d.ts.map +1 -0
- package/dist/static/infer.js +1 -0
- package/dist/static/infer.js.map +1 -0
- package/package.json +35 -4
- package/dist/combinators/index.d.ts +0 -57
- package/dist/combinators/index.js +0 -104
- package/dist/static/parser.d.ts +0 -7
- package/dist/static/parser.js +0 -6
package/dist/runtime/infer.js
CHANGED
|
@@ -22,13 +22,14 @@
|
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
24
|
export function infer(ast, _context) {
|
|
25
|
-
if (typeof ast !==
|
|
25
|
+
if (typeof ast !== 'object' || ast === null) {
|
|
26
26
|
throw new Error(`Invalid AST node: ${ast}`);
|
|
27
27
|
}
|
|
28
28
|
const node = ast;
|
|
29
29
|
// Nodes with outputSchema (new architecture)
|
|
30
|
-
if (
|
|
30
|
+
if ('outputSchema' in node && typeof node.outputSchema === 'string') {
|
|
31
31
|
return node.outputSchema;
|
|
32
32
|
}
|
|
33
33
|
throw new Error(`AST node has no outputSchema: ${JSON.stringify(ast)}`);
|
|
34
34
|
}
|
|
35
|
+
//# sourceMappingURL=infer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"infer.js","sourceRoot":"","sources":["../../src/runtime/infer.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,KAAK,CAAC,GAAY,EAAE,QAAiB;IACnD,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QAC5C,MAAM,IAAI,KAAK,CAAC,qBAAqB,GAAG,EAAE,CAAC,CAAC;IAC9C,CAAC;IAED,MAAM,IAAI,GAAG,GAA8B,CAAC;IAE5C,6CAA6C;IAC7C,IAAI,cAAc,IAAI,IAAI,IAAI,OAAO,IAAI,CAAC,YAAY,KAAK,QAAQ,EAAE,CAAC;QACpE,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,iCAAiC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AAC1E,CAAC"}
|
package/dist/runtime/parser.d.ts
CHANGED
|
@@ -7,23 +7,49 @@
|
|
|
7
7
|
* 2. Fall back to next level (higher precedence)
|
|
8
8
|
* 3. Base case: try atoms (last level)
|
|
9
9
|
*/
|
|
10
|
-
import type { Context } from
|
|
11
|
-
import type { ComputeGrammar, Grammar } from
|
|
12
|
-
import type { Parse } from
|
|
13
|
-
import type { NodeSchema } from
|
|
14
|
-
import { ASTNode } from
|
|
10
|
+
import type { Context } from '../context.js';
|
|
11
|
+
import type { ComputeGrammar, Grammar } from '../grammar/index.js';
|
|
12
|
+
import type { Parse } from '../parse/index.js';
|
|
13
|
+
import type { NodeSchema, StringSchema, ConstSchema, ExprSchema } from '../schema/index.js';
|
|
14
|
+
import { ASTNode } from '../primitive/index.js';
|
|
15
|
+
/** All built-in atoms, used as the last level of the grammar */
|
|
16
|
+
export declare const BUILT_IN_ATOMS: readonly [NodeSchema<"numberLiteral", readonly [import("../schema/index.js").NumberSchema & {
|
|
17
|
+
as<TName extends string>(name: TName): import("../schema/index.js").NamedSchema<import("../schema/index.js").NumberSchema, TName>;
|
|
18
|
+
}], 0, "number">, NodeSchema<"stringLiteral", readonly [StringSchema<readonly ["\"", "'"]> & {
|
|
19
|
+
as<TName extends string>(name: TName): import("../schema/index.js").NamedSchema<StringSchema<readonly ["\"", "'"]>, TName>;
|
|
20
|
+
}], 0, "string">, NodeSchema<"nullLiteral", readonly [import("../schema/index.js").NullSchema & {
|
|
21
|
+
as<TName extends string>(name: TName): import("../schema/index.js").NamedSchema<import("../schema/index.js").NullSchema, TName>;
|
|
22
|
+
}], 0, "null">, NodeSchema<"booleanLiteral", readonly [import("../schema/index.js").BooleanSchema & {
|
|
23
|
+
as<TName extends string>(name: TName): import("../schema/index.js").NamedSchema<import("../schema/index.js").BooleanSchema, TName>;
|
|
24
|
+
}], 0, "boolean">, NodeSchema<"undefinedLiteral", readonly [import("../schema/index.js").UndefinedSchema & {
|
|
25
|
+
as<TName extends string>(name: TName): import("../schema/index.js").NamedSchema<import("../schema/index.js").UndefinedSchema, TName>;
|
|
26
|
+
}], 0, "undefined">, NodeSchema<"identifier", readonly [import("../schema/index.js").IdentSchema & {
|
|
27
|
+
as<TName extends string>(name: TName): import("../schema/index.js").NamedSchema<import("../schema/index.js").IdentSchema, TName>;
|
|
28
|
+
}], 0, "unknown">, NodeSchema<"parentheses", readonly [ConstSchema<"("> & {
|
|
29
|
+
as<TName extends string>(name: TName): import("../schema/index.js").NamedSchema<ConstSchema<"(">, TName>;
|
|
30
|
+
}, import("../schema/index.js").NamedSchema<ExprSchema<string, "expr">, "inner">, ConstSchema<")"> & {
|
|
31
|
+
as<TName extends string>(name: TName): import("../schema/index.js").NamedSchema<ConstSchema<")">, TName>;
|
|
32
|
+
}], 0, "unknown">];
|
|
15
33
|
/** Parse result: empty = no match, [node, rest] = matched */
|
|
16
|
-
export type ParseResult<T extends ASTNode<
|
|
34
|
+
export type ParseResult<T extends ASTNode<string, unknown> = ASTNode<string, unknown>> = [] | [T & {}, string];
|
|
17
35
|
/**
|
|
18
|
-
*
|
|
36
|
+
* Process escape sequences in a string.
|
|
37
|
+
* Supports: \n, \t, \r, \\, \", \', \0, \b, \f, \v, \xHH, \uHHHH
|
|
38
|
+
*
|
|
39
|
+
* @param str - The raw string with escape sequences
|
|
40
|
+
* @returns The processed string with escape sequences converted
|
|
41
|
+
*/
|
|
42
|
+
export declare function processEscapeSequences(str: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Build runtime grammar from operator schemas.
|
|
19
45
|
*
|
|
20
46
|
* Returns a flat tuple of levels:
|
|
21
|
-
* [[ops@prec1], [ops@prec2], ..., [
|
|
47
|
+
* [[ops@prec1], [ops@prec2], ..., [builtInAtoms]]
|
|
22
48
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
49
|
+
* Operators are sorted by precedence ascending (lowest first).
|
|
50
|
+
* Built-in atoms are always appended as the last level.
|
|
25
51
|
*/
|
|
26
|
-
export declare function buildGrammar(
|
|
52
|
+
export declare function buildGrammar(operators: readonly NodeSchema[]): Grammar;
|
|
27
53
|
/**
|
|
28
54
|
* Parse input string using node schemas.
|
|
29
55
|
*
|
|
@@ -32,3 +58,58 @@ export declare function buildGrammar(nodes: readonly NodeSchema[]): Grammar;
|
|
|
32
58
|
* parsing stay in sync.
|
|
33
59
|
*/
|
|
34
60
|
export declare function parse<const TNodes extends readonly NodeSchema[], const TInput extends string, const TContext extends Context>(nodes: TNodes, input: TInput, context: TContext): Parse<ComputeGrammar<TNodes>, TInput, TContext>;
|
|
61
|
+
import { type RichParseError, type ParseResultWithErrors } from '../errors.js';
|
|
62
|
+
export type { RichParseError, ParseResultWithErrors };
|
|
63
|
+
/**
|
|
64
|
+
* Result from parseWithErrors - includes rich error information on failure.
|
|
65
|
+
*/
|
|
66
|
+
export interface ParseWithErrorsResult<T extends ASTNode<string, unknown> = ASTNode<string, unknown>> {
|
|
67
|
+
/** Whether parsing was successful */
|
|
68
|
+
readonly success: boolean;
|
|
69
|
+
/** The parsed AST node (only present if success is true) */
|
|
70
|
+
readonly ast?: T;
|
|
71
|
+
/** Remaining unparsed input (only present if success is true) */
|
|
72
|
+
readonly remaining?: string;
|
|
73
|
+
/** Parse error information (only present if success is false) */
|
|
74
|
+
readonly error?: RichParseError;
|
|
75
|
+
/** Original input string */
|
|
76
|
+
readonly input: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Parse input with rich error information.
|
|
80
|
+
*
|
|
81
|
+
* Unlike `parse()` which returns an empty array on failure, this function
|
|
82
|
+
* returns detailed error information including:
|
|
83
|
+
* - Position (line, column, offset)
|
|
84
|
+
* - Error message
|
|
85
|
+
* - Source snippet showing where the error occurred
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const result = parseWithErrors([add], "1 + ", context);
|
|
90
|
+
* if (!result.success) {
|
|
91
|
+
* console.log(result.error.message);
|
|
92
|
+
* // "No grammar rule matched at position 1:5: """
|
|
93
|
+
* console.log(result.error.snippet);
|
|
94
|
+
* // "1 + →"
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function parseWithErrors<const TNodes extends readonly NodeSchema[], const TInput extends string, const TContext extends Context>(nodes: TNodes, input: TInput, context: TContext): ParseWithErrorsResult;
|
|
99
|
+
/**
|
|
100
|
+
* Format a parse error for display.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* const result = parseWithErrors([add], "1 + ", context);
|
|
105
|
+
* if (!result.success) {
|
|
106
|
+
* console.log(formatParseError(result.error));
|
|
107
|
+
* // Error at line 1, column 5:
|
|
108
|
+
* // No grammar rule matched at position 1:5: ""
|
|
109
|
+
* //
|
|
110
|
+
* // 1 + →
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export declare function formatParseError(error: RichParseError): string;
|
|
115
|
+
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/runtime/parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EACV,UAAU,EAEV,YAAY,EACZ,WAAW,EACX,UAAU,EAEX,MAAM,oBAAoB,CAAC;AAa5B,OAAO,EACL,OAAO,EAOR,MAAM,uBAAuB,CAAC;AA2E/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