prefix-safe-json 0.0.1-alpha.0
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/LICENSE-APACHE +114 -0
- package/LICENSE-MIT +21 -0
- package/README.md +169 -0
- package/dist/coordinator/coordinator.d.ts +43 -0
- package/dist/coordinator/coordinator.d.ts.map +1 -0
- package/dist/coordinator/coordinator.js +407 -0
- package/dist/coordinator/coordinator.js.map +1 -0
- package/dist/coordinator/protocol.d.ts +61 -0
- package/dist/coordinator/protocol.d.ts.map +1 -0
- package/dist/coordinator/protocol.js +2 -0
- package/dist/coordinator/protocol.js.map +1 -0
- package/dist/coordinator/types.d.ts +84 -0
- package/dist/coordinator/types.d.ts.map +1 -0
- package/dist/coordinator/types.js +7 -0
- package/dist/coordinator/types.js.map +1 -0
- package/dist/diagnostics/codes.d.ts +24 -0
- package/dist/diagnostics/codes.d.ts.map +1 -0
- package/dist/diagnostics/codes.js +28 -0
- package/dist/diagnostics/codes.js.map +1 -0
- package/dist/diagnostics/factory.d.ts +8 -0
- package/dist/diagnostics/factory.d.ts.map +1 -0
- package/dist/diagnostics/factory.js +17 -0
- package/dist/diagnostics/factory.js.map +1 -0
- package/dist/grammar/frame.d.ts +58 -0
- package/dist/grammar/frame.d.ts.map +1 -0
- package/dist/grammar/frame.js +87 -0
- package/dist/grammar/frame.js.map +1 -0
- package/dist/grammar/pointer.d.ts +10 -0
- package/dist/grammar/pointer.d.ts.map +1 -0
- package/dist/grammar/pointer.js +20 -0
- package/dist/grammar/pointer.js.map +1 -0
- package/dist/grammar/stack.d.ts +73 -0
- package/dist/grammar/stack.d.ts.map +1 -0
- package/dist/grammar/stack.js +168 -0
- package/dist/grammar/stack.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/lexer/scanner.d.ts +108 -0
- package/dist/lexer/scanner.d.ts.map +1 -0
- package/dist/lexer/scanner.js +676 -0
- package/dist/lexer/scanner.js.map +1 -0
- package/dist/lexer/states.d.ts +35 -0
- package/dist/lexer/states.d.ts.map +1 -0
- package/dist/lexer/states.js +39 -0
- package/dist/lexer/states.js.map +1 -0
- package/dist/lexer/tokens.d.ts +22 -0
- package/dist/lexer/tokens.d.ts.map +1 -0
- package/dist/lexer/tokens.js +22 -0
- package/dist/lexer/tokens.js.map +1 -0
- package/dist/limits.d.ts +14 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +23 -0
- package/dist/limits.js.map +1 -0
- package/dist/parser.d.ts +6 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +949 -0
- package/dist/parser.js.map +1 -0
- package/dist/providers/adapter.d.ts +10 -0
- package/dist/providers/adapter.d.ts.map +1 -0
- package/dist/providers/adapter.js +2 -0
- package/dist/providers/adapter.js.map +1 -0
- package/dist/providers/anthropic.d.ts +45 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +112 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/gemini.d.ts +31 -0
- package/dist/providers/gemini.d.ts.map +1 -0
- package/dist/providers/gemini.js +94 -0
- package/dist/providers/gemini.js.map +1 -0
- package/dist/providers/openai-compatible.d.ts +33 -0
- package/dist/providers/openai-compatible.d.ts.map +1 -0
- package/dist/providers/openai-compatible.js +160 -0
- package/dist/providers/openai-compatible.js.map +1 -0
- package/dist/providers/openai.d.ts +48 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +205 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/openrouter.d.ts +35 -0
- package/dist/providers/openrouter.d.ts.map +1 -0
- package/dist/providers/openrouter.js +55 -0
- package/dist/providers/openrouter.js.map +1 -0
- package/dist/semantic/builder.d.ts +55 -0
- package/dist/semantic/builder.d.ts.map +1 -0
- package/dist/semantic/builder.js +140 -0
- package/dist/semantic/builder.js.map +1 -0
- package/dist/semantic/events.d.ts +2 -0
- package/dist/semantic/events.d.ts.map +1 -0
- package/dist/semantic/events.js +5 -0
- package/dist/semantic/events.js.map +1 -0
- package/dist/semantic/snapshot.d.ts +49 -0
- package/dist/semantic/snapshot.d.ts.map +1 -0
- package/dist/semantic/snapshot.js +228 -0
- package/dist/semantic/snapshot.js.map +1 -0
- package/dist/types.d.ts +156 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/utf8/decoder.d.ts +53 -0
- package/dist/utf8/decoder.d.ts.map +1 -0
- package/dist/utf8/decoder.js +318 -0
- package/dist/utf8/decoder.js.map +1 -0
- package/package.json +79 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Diagnostic } from "../types.js";
|
|
2
|
+
type Severity = Diagnostic["severity"];
|
|
3
|
+
/**
|
|
4
|
+
* Creates a Diagnostic object.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createDiagnostic(code: string, severity: Severity, byteOffset: number, message: string, recoverable: boolean, path?: string): Diagnostic;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../src/diagnostics/factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,KAAK,QAAQ,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC;AAEvC;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,WAAW,EAAE,OAAO,EACpB,IAAI,CAAC,EAAE,MAAM,GACZ,UAAU,CAYZ"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a Diagnostic object.
|
|
3
|
+
*/
|
|
4
|
+
export function createDiagnostic(code, severity, byteOffset, message, recoverable, path) {
|
|
5
|
+
const diag = {
|
|
6
|
+
code,
|
|
7
|
+
severity,
|
|
8
|
+
byteOffset,
|
|
9
|
+
message,
|
|
10
|
+
recoverable,
|
|
11
|
+
};
|
|
12
|
+
if (path !== undefined) {
|
|
13
|
+
diag.path = path;
|
|
14
|
+
}
|
|
15
|
+
return diag;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../src/diagnostics/factory.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAC9B,IAAY,EACZ,QAAkB,EAClB,UAAkB,EAClB,OAAe,EACf,WAAoB,EACpB,IAAa;IAEb,MAAM,IAAI,GAAe;QACvB,IAAI;QACJ,QAAQ;QACR,UAAU;QACV,OAAO;QACP,WAAW;KACZ,CAAC;IACF,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expectation states for object parsing.
|
|
3
|
+
*/
|
|
4
|
+
export type ObjectExpectation = "first_key_or_end" | "key_after_comma" | "colon" | "value" | "comma_or_end";
|
|
5
|
+
/**
|
|
6
|
+
* Expectation states for array parsing.
|
|
7
|
+
*/
|
|
8
|
+
export type ArrayExpectation = "first_value_or_end" | "value_after_comma" | "comma_or_end";
|
|
9
|
+
/**
|
|
10
|
+
* A frame on the grammar stack representing a JSON container.
|
|
11
|
+
*/
|
|
12
|
+
export interface GrammarFrame {
|
|
13
|
+
/** The type of container. */
|
|
14
|
+
containerType: "object" | "array";
|
|
15
|
+
/**
|
|
16
|
+
* The JSON Pointer path up to this container.
|
|
17
|
+
* Backed by a lazily-computed, memoized getter (see attachLazyPath) —
|
|
18
|
+
* reads exactly like a plain string field everywhere else in the codebase.
|
|
19
|
+
*/
|
|
20
|
+
path: string;
|
|
21
|
+
/** Current expectation for object parsing. */
|
|
22
|
+
objectExpectation?: ObjectExpectation;
|
|
23
|
+
/** Current key being processed. */
|
|
24
|
+
currentKey?: string;
|
|
25
|
+
/** Set of keys already seen (for duplicate detection). */
|
|
26
|
+
seenKeys?: Set<string>;
|
|
27
|
+
/** Current expectation for array parsing. */
|
|
28
|
+
arrayExpectation?: ArrayExpectation;
|
|
29
|
+
/** Index of the next element. */
|
|
30
|
+
nextArrayIndex: number;
|
|
31
|
+
/** Byte offset where this container started. */
|
|
32
|
+
byteStart: number;
|
|
33
|
+
/**
|
|
34
|
+
* @internal Lazy-path bookkeeping. Do not read directly — use `.path`.
|
|
35
|
+
* A frame's own path never changes after creation (only descendants'
|
|
36
|
+
* currentKey/nextArrayIndex mutate), so it's safe to resolve on demand.
|
|
37
|
+
*/
|
|
38
|
+
_parent?: GrammarFrame | null;
|
|
39
|
+
/** @internal The key/index linking `_parent` to this frame, captured at creation time. */
|
|
40
|
+
_segment?: string | number | null;
|
|
41
|
+
/** @internal Memoized resolved path, set the first time `.path` is read. */
|
|
42
|
+
_cachedPath?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Create a new object frame.
|
|
46
|
+
* `parent` is the frame this one is nested inside (or null at the root);
|
|
47
|
+
* `segment` is the key/index leading from parent to this frame (or null at
|
|
48
|
+
* the root) — the same value `GrammarStack.currentValuePath()` would have
|
|
49
|
+
* used, captured eagerly since it reflects the parent's mutable state at
|
|
50
|
+
* this exact moment. Only the (expensive, O(depth)) string flattening is
|
|
51
|
+
* deferred.
|
|
52
|
+
*/
|
|
53
|
+
export declare function createObjectFrame(parent: GrammarFrame | null, segment: string | number | null, byteStart: number): GrammarFrame;
|
|
54
|
+
/**
|
|
55
|
+
* Create a new array frame. See createObjectFrame for `parent`/`segment`.
|
|
56
|
+
*/
|
|
57
|
+
export declare function createArrayFrame(parent: GrammarFrame | null, segment: string | number | null, byteStart: number): GrammarFrame;
|
|
58
|
+
//# sourceMappingURL=frame.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frame.d.ts","sourceRoot":"","sources":["../../src/grammar/frame.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,MAAM,MAAM,iBAAiB,GACzB,kBAAkB,GAClB,iBAAiB,GACjB,OAAO,GACP,OAAO,GACP,cAAc,CAAC;AAEnB;;GAEG;AACH,MAAM,MAAM,gBAAgB,GACxB,oBAAoB,GACpB,mBAAmB,GACnB,cAAc,CAAC;AAEnB;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,6BAA6B;IAC7B,aAAa,EAAE,QAAQ,GAAG,OAAO,CAAC;IAElC;;;;OAIG;IACH,IAAI,EAAE,MAAM,CAAC;IAGb,8CAA8C;IAC9C,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IAEtC,mCAAmC;IACnC,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,0DAA0D;IAC1D,QAAQ,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAGvB,6CAA6C;IAC7C,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IAEpC,iCAAiC;IACjC,cAAc,EAAE,MAAM,CAAC;IAEvB,gDAAgD;IAChD,SAAS,EAAE,MAAM,CAAC;IAElB;;;;OAIG;IACH,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,CAAC;IAC9B,0FAA0F;IAC1F,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAClC,4EAA4E;IAC5E,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AA0DD;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,YAAY,GAAG,IAAI,EAC3B,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,EAC/B,SAAS,EAAE,MAAM,GAChB,YAAY,CAWd;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,YAAY,GAAG,IAAI,EAC3B,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,EAC/B,SAAS,EAAE,MAAM,GAChB,YAAY,CAUd"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Grammar stack frame — tracks state of a JSON container
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
import { appendPointer } from "./pointer.js";
|
|
5
|
+
/**
|
|
6
|
+
* Resolve (and memoize) a frame's full JSON Pointer path.
|
|
7
|
+
*
|
|
8
|
+
* Iterative, not recursive: walks up `_parent` references collecting frames
|
|
9
|
+
* until it finds one whose path is already cached (or the root), then walks
|
|
10
|
+
* back down building and caching each frame's path exactly once. This keeps
|
|
11
|
+
* per-frame cost amortized O(1) regardless of nesting depth, and avoids
|
|
12
|
+
* both the call-stack growth a recursive walk would have and the O(depth)
|
|
13
|
+
* eager string concatenation this replaces (which made opening N nested
|
|
14
|
+
* containers cost O(N^2) total, regardless of whether any value inside them
|
|
15
|
+
* was ever committed).
|
|
16
|
+
*/
|
|
17
|
+
function resolveFramePath(frame) {
|
|
18
|
+
if (frame._cachedPath !== undefined)
|
|
19
|
+
return frame._cachedPath;
|
|
20
|
+
const chain = [];
|
|
21
|
+
let cur = frame;
|
|
22
|
+
while (cur !== null && cur._cachedPath === undefined) {
|
|
23
|
+
chain.push(cur);
|
|
24
|
+
cur = cur._parent === undefined ? null : cur._parent;
|
|
25
|
+
}
|
|
26
|
+
let basePath = cur !== null ? cur._cachedPath : "";
|
|
27
|
+
// chain[0] is always `frame` itself (pushed in the first loop iteration),
|
|
28
|
+
// and this loop runs in reverse, so the last iteration (i === 0) sets
|
|
29
|
+
// basePath to frame's own resolved path — returning it directly avoids
|
|
30
|
+
// re-reading frame._cachedPath, which TS can't prove was set here since
|
|
31
|
+
// the mutation happens through the aliased `f` reference.
|
|
32
|
+
for (let i = chain.length - 1; i >= 0; i--) {
|
|
33
|
+
const f = chain[i];
|
|
34
|
+
const seg = f._segment;
|
|
35
|
+
basePath =
|
|
36
|
+
seg === null || seg === undefined ? basePath : appendPointer(basePath, seg);
|
|
37
|
+
f._cachedPath = basePath;
|
|
38
|
+
}
|
|
39
|
+
return basePath;
|
|
40
|
+
}
|
|
41
|
+
function attachLazyPath(frame, parent, segment) {
|
|
42
|
+
frame._parent = parent;
|
|
43
|
+
frame._segment = segment;
|
|
44
|
+
Object.defineProperty(frame, "path", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
get() {
|
|
48
|
+
return resolveFramePath(frame);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Create a new object frame.
|
|
54
|
+
* `parent` is the frame this one is nested inside (or null at the root);
|
|
55
|
+
* `segment` is the key/index leading from parent to this frame (or null at
|
|
56
|
+
* the root) — the same value `GrammarStack.currentValuePath()` would have
|
|
57
|
+
* used, captured eagerly since it reflects the parent's mutable state at
|
|
58
|
+
* this exact moment. Only the (expensive, O(depth)) string flattening is
|
|
59
|
+
* deferred.
|
|
60
|
+
*/
|
|
61
|
+
export function createObjectFrame(parent, segment, byteStart) {
|
|
62
|
+
const frame = {
|
|
63
|
+
containerType: "object",
|
|
64
|
+
path: "",
|
|
65
|
+
objectExpectation: "first_key_or_end",
|
|
66
|
+
seenKeys: new Set(),
|
|
67
|
+
nextArrayIndex: 0,
|
|
68
|
+
byteStart,
|
|
69
|
+
};
|
|
70
|
+
attachLazyPath(frame, parent, segment);
|
|
71
|
+
return frame;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create a new array frame. See createObjectFrame for `parent`/`segment`.
|
|
75
|
+
*/
|
|
76
|
+
export function createArrayFrame(parent, segment, byteStart) {
|
|
77
|
+
const frame = {
|
|
78
|
+
containerType: "array",
|
|
79
|
+
path: "",
|
|
80
|
+
arrayExpectation: "first_value_or_end",
|
|
81
|
+
nextArrayIndex: 0,
|
|
82
|
+
byteStart,
|
|
83
|
+
};
|
|
84
|
+
attachLazyPath(frame, parent, segment);
|
|
85
|
+
return frame;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=frame.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frame.js","sourceRoot":"","sources":["../../src/grammar/frame.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,yDAAyD;AACzD,8EAA8E;AAE9E,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAkE7C;;;;;;;;;;;GAWG;AACH,SAAS,gBAAgB,CAAC,KAAmB;IAC3C,IAAI,KAAK,CAAC,WAAW,KAAK,SAAS;QAAE,OAAO,KAAK,CAAC,WAAW,CAAC;IAE9D,MAAM,KAAK,GAAmB,EAAE,CAAC;IACjC,IAAI,GAAG,GAAwB,KAAK,CAAC;IACrC,OAAO,GAAG,KAAK,IAAI,IAAI,GAAG,CAAC,WAAW,KAAK,SAAS,EAAE,CAAC;QACrD,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAChB,GAAG,GAAG,GAAG,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC;IACvD,CAAC;IAED,IAAI,QAAQ,GAAG,GAAG,KAAK,IAAI,CAAC,CAAC,CAAE,GAAG,CAAC,WAAsB,CAAC,CAAC,CAAC,EAAE,CAAC;IAE/D,0EAA0E;IAC1E,sEAAsE;IACtE,uEAAuE;IACvE,wEAAwE;IACxE,0DAA0D;IAC1D,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3C,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAiB,CAAC;QACnC,MAAM,GAAG,GAAG,CA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AE,MAAM,EAAE,OAAO,CAAC,CAAC;IACvC,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escape a JSON Pointer reference token per RFC 6901.
|
|
3
|
+
* ~ → ~0, / → ~1
|
|
4
|
+
*/
|
|
5
|
+
export declare function escapePointerToken(token: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* Build a JSON Pointer path from parent path and a key or index.
|
|
8
|
+
*/
|
|
9
|
+
export declare function appendPointer(parent: string, token: string | number): string;
|
|
10
|
+
//# sourceMappingURL=pointer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pointer.d.ts","sourceRoot":"","sources":["../../src/grammar/pointer.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAExD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAK5E"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// JSON Pointer utilities (RFC 6901)
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
/**
|
|
5
|
+
* Escape a JSON Pointer reference token per RFC 6901.
|
|
6
|
+
* ~ → ~0, / → ~1
|
|
7
|
+
*/
|
|
8
|
+
export function escapePointerToken(token) {
|
|
9
|
+
return token.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Build a JSON Pointer path from parent path and a key or index.
|
|
13
|
+
*/
|
|
14
|
+
export function appendPointer(parent, token) {
|
|
15
|
+
if (typeof token === "number") {
|
|
16
|
+
return `${parent}/${token}`;
|
|
17
|
+
}
|
|
18
|
+
return `${parent}/${escapePointerToken(token)}`;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=pointer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pointer.js","sourceRoot":"","sources":["../../src/grammar/pointer.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,oCAAoC;AACpC,8EAA8E;AAE9E;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAa;IAC9C,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AACxD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,KAAsB;IAClE,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,GAAG,MAAM,IAAI,KAAK,EAAE,CAAC;IAC9B,CAAC;IACD,OAAO,GAAG,MAAM,IAAI,kBAAkB,CAAC,KAAK,CAAC,EAAE,CAAC;AAClD,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { GrammarFrame } from "./frame.js";
|
|
2
|
+
import type { Diagnostic } from "../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Manages the stack of open JSON containers.
|
|
5
|
+
* Handles object/array nesting, expectations, and duplicate key detection.
|
|
6
|
+
*/
|
|
7
|
+
export declare class GrammarStack {
|
|
8
|
+
private stack;
|
|
9
|
+
private diagnostics;
|
|
10
|
+
private hasDuplicateKey;
|
|
11
|
+
private maxDepth;
|
|
12
|
+
constructor(maxDepth: number);
|
|
13
|
+
/** Current depth (number of open containers). */
|
|
14
|
+
get depth(): number;
|
|
15
|
+
/** Whether any duplicate key has been seen. */
|
|
16
|
+
get hasDuplicate(): boolean;
|
|
17
|
+
/** Get the current (top) frame, or undefined if empty. */
|
|
18
|
+
get current(): GrammarFrame | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Push a new object container.
|
|
21
|
+
* Returns the frame, or null if depth limit exceeded.
|
|
22
|
+
*/
|
|
23
|
+
pushObject(byteStart: number): GrammarFrame | null;
|
|
24
|
+
/**
|
|
25
|
+
* Push a new array container.
|
|
26
|
+
* Returns the frame, or null if depth limit exceeded.
|
|
27
|
+
*/
|
|
28
|
+
pushArray(byteStart: number): GrammarFrame | null;
|
|
29
|
+
/**
|
|
30
|
+
* Pop the current container.
|
|
31
|
+
* Returns the popped frame.
|
|
32
|
+
*/
|
|
33
|
+
pop(): GrammarFrame | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Register an object key. Returns true if the key is new.
|
|
36
|
+
* If duplicate, emits diagnostic and returns false.
|
|
37
|
+
*/
|
|
38
|
+
registerObjectKey(key: string, byteOffset: number): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Get the JSON Pointer path for where the next value will go.
|
|
41
|
+
*/
|
|
42
|
+
currentValuePath(): string;
|
|
43
|
+
/**
|
|
44
|
+
* The key/index that would lead from the current frame to its next child,
|
|
45
|
+
* without flattening it into a full path string. Same underlying value
|
|
46
|
+
* currentValuePath() uses; kept as a raw segment so a newly-pushed child
|
|
47
|
+
* frame can capture it in O(1) and defer the expensive path-string join
|
|
48
|
+
* (see grammar/frame.ts) until something actually reads `.path`.
|
|
49
|
+
*/
|
|
50
|
+
private currentSegment;
|
|
51
|
+
/**
|
|
52
|
+
* Advance the array index after an element value is committed.
|
|
53
|
+
*/
|
|
54
|
+
advanceArrayIndex(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Take accumulated diagnostics and clear.
|
|
57
|
+
*/
|
|
58
|
+
takeDiagnostics(): Diagnostic[];
|
|
59
|
+
/**
|
|
60
|
+
* Check if the stack is empty (no open containers).
|
|
61
|
+
*/
|
|
62
|
+
isEmpty(): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Get all frames (for snapshot/pending info).
|
|
65
|
+
*/
|
|
66
|
+
getFrames(): readonly GrammarFrame[];
|
|
67
|
+
/**
|
|
68
|
+
* Determine if all open containers can be safely closed by just appending '}' or ']'.
|
|
69
|
+
* This is true if no container is waiting for a colon or a value.
|
|
70
|
+
*/
|
|
71
|
+
canSafelyCloseAll(): boolean;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=stack.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stack.d.ts","sourceRoot":"","sources":["../../src/grammar/stack.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAI9C;;;GAGG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,WAAW,CAAoB;IACvC,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAS;gBAEb,QAAQ,EAAE,MAAM;IAI5B,iDAAiD;IACjD,IAAI,KAAK,IAAI,MAAM,CAElB;IAED,+CAA+C;IAC/C,IAAI,YAAY,IAAI,OAAO,CAE1B;IAED,0DAA0D;IAC1D,IAAI,OAAO,IAAI,YAAY,GAAG,SAAS,CAEtC;IAED;;;OAGG;IACH,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI;IAmBlD;;;OAGG;IACH,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI;IAmBjD;;;OAGG;IACH,GAAG,IAAI,YAAY,GAAG,SAAS;IAI/B;;;OAGG;IACH,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO;IA0B3D;;OAEG;IACH,gBAAgB,IAAI,MAAM;IAc1B;;;;;;OAMG;IACH,OAAO,CAAC,cAAc;IAUtB;;OAEG;IACH,iBAAiB,IAAI,IAAI;IAOzB;;OAEG;IACH,eAAe,IAAI,UAAU,EAAE;IAM/B;;OAEG;IACH,OAAO,IAAI,OAAO;IAIlB;;OAEG;IACH,SAAS,IAAI,SAAS,YAAY,EAAE;IAIpC;;;OAGG;IACH,iBAAiB,IAAI,OAAO;CAkB7B"}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Grammar Stack — tracks nested JSON container state
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
import { createObjectFrame, createArrayFrame } from "./frame.js";
|
|
5
|
+
import { appendPointer } from "./pointer.js";
|
|
6
|
+
import { DiagnosticCode } from "../diagnostics/codes.js";
|
|
7
|
+
import { createDiagnostic } from "../diagnostics/factory.js";
|
|
8
|
+
/**
|
|
9
|
+
* Manages the stack of open JSON containers.
|
|
10
|
+
* Handles object/array nesting, expectations, and duplicate key detection.
|
|
11
|
+
*/
|
|
12
|
+
export class GrammarStack {
|
|
13
|
+
stack = [];
|
|
14
|
+
diagnostics = [];
|
|
15
|
+
hasDuplicateKey = false;
|
|
16
|
+
maxDepth;
|
|
17
|
+
constructor(maxDepth) {
|
|
18
|
+
this.maxDepth = maxDepth;
|
|
19
|
+
}
|
|
20
|
+
/** Current depth (number of open containers). */
|
|
21
|
+
get depth() {
|
|
22
|
+
return this.stack.length;
|
|
23
|
+
}
|
|
24
|
+
/** Whether any duplicate key has been seen. */
|
|
25
|
+
get hasDuplicate() {
|
|
26
|
+
return this.hasDuplicateKey;
|
|
27
|
+
}
|
|
28
|
+
/** Get the current (top) frame, or undefined if empty. */
|
|
29
|
+
get current() {
|
|
30
|
+
return this.stack[this.stack.length - 1];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Push a new object container.
|
|
34
|
+
* Returns the frame, or null if depth limit exceeded.
|
|
35
|
+
*/
|
|
36
|
+
pushObject(byteStart) {
|
|
37
|
+
if (this.stack.length >= this.maxDepth) {
|
|
38
|
+
this.diagnostics.push(createDiagnostic(DiagnosticCode.E_LIMIT_DEPTH, "fatal", byteStart, `Maximum nesting depth ${this.maxDepth} exceeded`, false));
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const parent = this.current ?? null;
|
|
42
|
+
const frame = createObjectFrame(parent, this.currentSegment(), byteStart);
|
|
43
|
+
this.stack.push(frame);
|
|
44
|
+
return frame;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Push a new array container.
|
|
48
|
+
* Returns the frame, or null if depth limit exceeded.
|
|
49
|
+
*/
|
|
50
|
+
pushArray(byteStart) {
|
|
51
|
+
if (this.stack.length >= this.maxDepth) {
|
|
52
|
+
this.diagnostics.push(createDiagnostic(DiagnosticCode.E_LIMIT_DEPTH, "fatal", byteStart, `Maximum nesting depth ${this.maxDepth} exceeded`, false));
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const parent = this.current ?? null;
|
|
56
|
+
const frame = createArrayFrame(parent, this.currentSegment(), byteStart);
|
|
57
|
+
this.stack.push(frame);
|
|
58
|
+
return frame;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Pop the current container.
|
|
62
|
+
* Returns the popped frame.
|
|
63
|
+
*/
|
|
64
|
+
pop() {
|
|
65
|
+
return this.stack.pop();
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Register an object key. Returns true if the key is new.
|
|
69
|
+
* If duplicate, emits diagnostic and returns false.
|
|
70
|
+
*/
|
|
71
|
+
registerObjectKey(key, byteOffset) {
|
|
72
|
+
const frame = this.current;
|
|
73
|
+
if (!frame || frame.containerType !== "object" || !frame.seenKeys) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (frame.seenKeys.has(key)) {
|
|
77
|
+
this.hasDuplicateKey = true;
|
|
78
|
+
this.diagnostics.push(createDiagnostic(DiagnosticCode.E_DUPLICATE_KEY, "error", byteOffset, `Duplicate object key: ${JSON.stringify(key)}`, false, frame.path));
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
frame.seenKeys.add(key);
|
|
82
|
+
frame.currentKey = key;
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Get the JSON Pointer path for where the next value will go.
|
|
87
|
+
*/
|
|
88
|
+
currentValuePath() {
|
|
89
|
+
const frame = this.current;
|
|
90
|
+
if (!frame)
|
|
91
|
+
return "";
|
|
92
|
+
if (frame.containerType === "object") {
|
|
93
|
+
if (frame.currentKey !== undefined) {
|
|
94
|
+
return appendPointer(frame.path, frame.currentKey);
|
|
95
|
+
}
|
|
96
|
+
return frame.path;
|
|
97
|
+
}
|
|
98
|
+
// array
|
|
99
|
+
return appendPointer(frame.path, frame.nextArrayIndex);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The key/index that would lead from the current frame to its next child,
|
|
103
|
+
* without flattening it into a full path string. Same underlying value
|
|
104
|
+
* currentValuePath() uses; kept as a raw segment so a newly-pushed child
|
|
105
|
+
* frame can capture it in O(1) and defer the expensive path-string join
|
|
106
|
+
* (see grammar/frame.ts) until something actually reads `.path`.
|
|
107
|
+
*/
|
|
108
|
+
currentSegment() {
|
|
109
|
+
const frame = this.current;
|
|
110
|
+
if (!frame)
|
|
111
|
+
return null;
|
|
112
|
+
if (frame.containerType === "object") {
|
|
113
|
+
return frame.currentKey !== undefined ? frame.currentKey : null;
|
|
114
|
+
}
|
|
115
|
+
return frame.nextArrayIndex;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Advance the array index after an element value is committed.
|
|
119
|
+
*/
|
|
120
|
+
advanceArrayIndex() {
|
|
121
|
+
const frame = this.current;
|
|
122
|
+
if (frame && frame.containerType === "array") {
|
|
123
|
+
frame.nextArrayIndex++;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Take accumulated diagnostics and clear.
|
|
128
|
+
*/
|
|
129
|
+
takeDiagnostics() {
|
|
130
|
+
const result = this.diagnostics;
|
|
131
|
+
this.diagnostics = [];
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Check if the stack is empty (no open containers).
|
|
136
|
+
*/
|
|
137
|
+
isEmpty() {
|
|
138
|
+
return this.stack.length === 0;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get all frames (for snapshot/pending info).
|
|
142
|
+
*/
|
|
143
|
+
getFrames() {
|
|
144
|
+
return this.stack;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Determine if all open containers can be safely closed by just appending '}' or ']'.
|
|
148
|
+
* This is true if no container is waiting for a colon or a value.
|
|
149
|
+
*/
|
|
150
|
+
canSafelyCloseAll() {
|
|
151
|
+
for (const frame of this.stack) {
|
|
152
|
+
if (frame.containerType === "object") {
|
|
153
|
+
if (frame.objectExpectation === "colon" ||
|
|
154
|
+
frame.objectExpectation === "value" ||
|
|
155
|
+
frame.objectExpectation === "key_after_comma") {
|
|
156
|
+
return false; // Missing colon or value or key
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
if (frame.arrayExpectation === "value_after_comma") {
|
|
161
|
+
return false; // Missing value after comma
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
//# sourceMappingURL=stack.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stack.js","sourceRoot":"","sources":["../../src/grammar/stack.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,qDAAqD;AACrD,8EAA8E;AAG9E,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAE7D;;;GAGG;AACH,MAAM,OAAO,YAAY;IACf,KAAK,GAAmB,EAAE,CAAC;IAC3B,WAAW,GAAiB,EAAE,CAAC;IAC/B,eAAe,GAAG,KAAK,CAAC;IACxB,Q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;OAGG;IACH,iBAAiB;QACf,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAC/B,IAAI,KAAK,CAAC,aAAa,KAAK,QAAQ,EAAE,CAAC;gBACrC,IACE,KAAK,CAAC,iBAAiB,KAAK,OAAO;oBACnC,KAAK,CAAC,iBAAiB,KAAK,OAAO;oBACnC,KAAK,CAAC,iBAAiB,KAAK,iBAAiB,EAC7C,CAAC;oBACD,OAAO,KAAK,CAAC,CAAC,gCAAgC;gBAChD,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,IAAI,KAAK,CAAC,gBAAgB,KAAK,mBAAmB,EAAE,CAAC;oBACnD,OAAO,KAAK,CAAC,CAAC,4BAA4B;gBAC5C,CAAC;YACH,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @public (Stable)
|
|
3
|
+
* Creates a new Incremental JSON Parser instance.
|
|
4
|
+
*/
|
|
5
|
+
export { createParser } from "./parser.js";
|
|
6
|
+
export type { IncrementalJsonParser, ParserOptions, PushResult, ParserSnapshot, FinalResult, ParserEvent, ValueCommittedEvent, ContainerClosedEvent, RepairAppliedEvent, DiagnosticEvent, DocumentCompleteEvent, StreamFinishedEvent, JsonValue, JsonObject, JsonArray, SyntaxStatus, StreamEndReason, PendingToken, RepairAction, Diagnostic, ParserLimits, } from "./types.js";
|
|
7
|
+
export { DEFAULT_LIMITS } from "./limits.js";
|
|
8
|
+
export { DiagnosticCode } from "./diagnostics/codes.js";
|
|
9
|
+
/**
|
|
10
|
+
* @public (Stable)
|
|
11
|
+
* Creates a stream coordinator for mapping overlapping multi-tool streams.
|
|
12
|
+
*/
|
|
13
|
+
export { createToolCallStreamCoordinator } from "./coordinator/coordinator.js";
|
|
14
|
+
export type { ToolCallStreamCoordinator } from "./coordinator/types.js";
|
|
15
|
+
export type { NormalizedToolStreamEvent } from "./coordinator/protocol.js";
|
|
16
|
+
export type { ProviderStreamAdapter } from "./providers/adapter.js";
|
|
17
|
+
/**
|
|
18
|
+
* @public (Experimental)
|
|
19
|
+
* Provider adapters for various LLM network shapes.
|
|
20
|
+
* These are experimental and may change if upstream APIs change.
|
|
21
|
+
*/
|
|
22
|
+
export { OpenAIStreamAdapter } from "./providers/openai.js";
|
|
23
|
+
export { OpenAICompatibleStreamAdapter } from "./providers/openai-compatible.js";
|
|
24
|
+
export { AnthropicStreamAdapter } from "./providers/anthropic.js";
|
|
25
|
+
export { GeminiStreamAdapter } from "./providers/gemini.js";
|
|
26
|
+
export { OpenRouterStreamAdapter } from "./providers/openrouter.js";
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,YAAY,EAEV,qBAAqB,EACrB,aAAa,EAGb,UAAU,EACV,cAAc,EACd,WAAW,EAGX,WAAW,EACX,mBAAmB,EACnB,oBAAoB,EACpB,kBAAkB,EAClB,eAAe,EACf,qBAAqB,EACrB,mBAAmB,EAGnB,SAAS,EACT,UAAU,EACV,SAAS,EACT,YAAY,EACZ,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,YAAY,GACb,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAGxD;;;GAGG;AACH,OAAO,EAAE,+BAA+B,EAAE,MAAM,8BAA8B,CAAC;AAC/E,YAAY,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAC;AACxE,YAAY,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAG3E,YAAY,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAEpE;;;;GAIG;AACH,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,6BAA6B,EAAE,MAAM,kCAAkC,CAAC;AACjF,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Public API — prefix-safe-json
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
/**
|
|
5
|
+
* @public (Stable)
|
|
6
|
+
* Creates a new Incremental JSON Parser instance.
|
|
7
|
+
*/
|
|
8
|
+
export { createParser } from "./parser.js";
|
|
9
|
+
export { DEFAULT_LIMITS } from "./limits.js";
|
|
10
|
+
export { DiagnosticCode } from "./diagnostics/codes.js";
|
|
11
|
+
// Coordinator API
|
|
12
|
+
/**
|
|
13
|
+
* @public (Stable)
|
|
14
|
+
* Creates a stream coordinator for mapping overlapping multi-tool streams.
|
|
15
|
+
*/
|
|
16
|
+
export { createToolCallStreamCoordinator } from "./coordinator/coordinator.js";
|
|
17
|
+
/**
|
|
18
|
+
* @public (Experimental)
|
|
19
|
+
* Provider adapters for various LLM network shapes.
|
|
20
|
+
* These are experimental and may change if upstream APIs change.
|
|
21
|
+
*/
|
|
22
|
+
export { OpenAIStreamAdapter } from "./providers/openai.js";
|
|
23
|
+
export { OpenAICompatibleStreamAdapter } from "./providers/openai-compatible.js";
|
|
24
|
+
export { AnthropicStreamAdapter } from "./providers/anthropic.js";
|
|
25
|
+
export { GeminiStreamAdapter } from "./providers/gemini.js";
|
|
26
|
+
export { OpenRouterStreamAdapter } from "./providers/openrouter.js";
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,gCAAgC;AAChC,8EAA8E;AAE9E;;;GAGG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAiC3C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAExD,kBAAkB;AAClB;;;GAGG;AACH,OAAO,EAAE,+BAA+B,EAAE,MAAM,8BAA8B,CAAC;AAO/E;;;;GAIG;AACH,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,6BAA6B,EAAE,MAAM,kCAAkC,CAAC;AACjF,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { ScannerState } from "./states.js";
|
|
2
|
+
import type { Token } from "./tokens.js";
|
|
3
|
+
import type { Diagnostic, RepairAction, ParserOptions } from "../types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Incremental JSON scanner.
|
|
6
|
+
* Feed characters one at a time via feedChar().
|
|
7
|
+
* Collect tokens via takeTokens().
|
|
8
|
+
*/
|
|
9
|
+
export declare class Scanner {
|
|
10
|
+
private options;
|
|
11
|
+
private maxStringBytes;
|
|
12
|
+
constructor(options?: ParserOptions);
|
|
13
|
+
private state;
|
|
14
|
+
private tokens;
|
|
15
|
+
private diagnostics;
|
|
16
|
+
private repairs;
|
|
17
|
+
private stringBuffer;
|
|
18
|
+
private stringIsKey;
|
|
19
|
+
private stringByteStart;
|
|
20
|
+
private unicodeHexBuffer;
|
|
21
|
+
private highSurrogate;
|
|
22
|
+
/** Tracks position in surrogate pair sequence: 0=initial, 1=saw backslash, 2=saw 'u' */
|
|
23
|
+
private surrogatePhase;
|
|
24
|
+
private numberBuffer;
|
|
25
|
+
private numberByteStart;
|
|
26
|
+
private numberHasDigit;
|
|
27
|
+
private literalTarget;
|
|
28
|
+
private literalIndex;
|
|
29
|
+
private literalByteStart;
|
|
30
|
+
/** Byte offset of the current character being fed. Set by the caller. */
|
|
31
|
+
private currentByteOffset;
|
|
32
|
+
/** Whether we're in a key position for string parsing. */
|
|
33
|
+
private nextStringIsKey;
|
|
34
|
+
get currentState(): ScannerState;
|
|
35
|
+
/**
|
|
36
|
+
* Feed a single character to the scanner.
|
|
37
|
+
* @param ch - The character to process.
|
|
38
|
+
* @param byteOffset - Absolute byte offset of this character in the input stream.
|
|
39
|
+
* @param charByteLength - Number of bytes this character occupies in UTF-8.
|
|
40
|
+
*/
|
|
41
|
+
feedChar(ch: string, byteOffset: number, charByteLength: number): void;
|
|
42
|
+
/**
|
|
43
|
+
* Take all tokens accumulated since the last call.
|
|
44
|
+
* Clears the internal token buffer.
|
|
45
|
+
*/
|
|
46
|
+
takeTokens(): Token[];
|
|
47
|
+
/**
|
|
48
|
+
* Take all diagnostics accumulated since the last call.
|
|
49
|
+
* Clears the internal diagnostic buffer.
|
|
50
|
+
*/
|
|
51
|
+
takeDiagnostics(): Diagnostic[];
|
|
52
|
+
takeRepairs(): RepairAction[];
|
|
53
|
+
/**
|
|
54
|
+
* Set whether the next string to be scanned should be treated as an object key.
|
|
55
|
+
*/
|
|
56
|
+
setNextStringIsKey(isKey: boolean): void;
|
|
57
|
+
/**
|
|
58
|
+
* Get info about any pending (incomplete) value.
|
|
59
|
+
*/
|
|
60
|
+
getPendingInfo(): {
|
|
61
|
+
type: "string" | "number" | "literal" | "unicode_escape" | "surrogate" | "none";
|
|
62
|
+
buffer: string;
|
|
63
|
+
byteStart: number;
|
|
64
|
+
isKey: boolean;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Check numberBuffer against the parts of RFC 8259's number grammar that
|
|
68
|
+
* aren't already enforced by the state machine's character-by-character
|
|
69
|
+
* transitions: no leading zero in the integer part, and no trailing
|
|
70
|
+
* "." or exponent marker with no digit following it. Returns an error
|
|
71
|
+
* message if invalid, or null if the buffer is a well-formed number.
|
|
72
|
+
*/
|
|
73
|
+
private numberGrammarError;
|
|
74
|
+
/**
|
|
75
|
+
* Attempt to finalize a pending number (at end of input or finish()).
|
|
76
|
+
* - "finalized": a valid number token was emitted.
|
|
77
|
+
* - "invalid": there was a complete-looking buffer, but it violates JSON
|
|
78
|
+
* number grammar (leading zero, trailing "." or exponent marker with
|
|
79
|
+
* no digit after it). Nothing is emitted; numberBuffer is left as-is.
|
|
80
|
+
* - "not_ready": scanner isn't in a finalizable number state at all
|
|
81
|
+
* (e.g. buffer is just "-", or ends right at a bare "e"/"E" with no
|
|
82
|
+
* sign or digit yet) — unchanged pre-existing behavior, callers should
|
|
83
|
+
* not newly diagnose this case.
|
|
84
|
+
*/
|
|
85
|
+
finalizeNumber(): "finalized" | "invalid" | "not_ready";
|
|
86
|
+
/**
|
|
87
|
+
* Attempt to finalize a pending literal (at end of input or finish).
|
|
88
|
+
* Returns true if a literal token was emitted.
|
|
89
|
+
*/
|
|
90
|
+
finalizeLiteral(): boolean;
|
|
91
|
+
private processStructural;
|
|
92
|
+
private processString;
|
|
93
|
+
private processEscape;
|
|
94
|
+
private processUnicodeEscape;
|
|
95
|
+
private processSurrogatePending;
|
|
96
|
+
private processNumber;
|
|
97
|
+
private processLiteral;
|
|
98
|
+
private processTrailingWhitespace;
|
|
99
|
+
private beginString;
|
|
100
|
+
private beginNumber;
|
|
101
|
+
private beginLiteral;
|
|
102
|
+
private commitNumber;
|
|
103
|
+
private isValueTerminator;
|
|
104
|
+
private reprocessChar;
|
|
105
|
+
private emitToken;
|
|
106
|
+
private emitDiagnostic;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=scanner.d.ts.map
|