@oh-my-pi/omptype 18.16.0 → 18.18.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/CHANGELOG.md +18 -0
- package/README.md +33 -0
- package/dist/js/compile.js +25 -3
- package/dist/js/from-json-schema.js +61 -216
- package/dist/js/interp.js +2 -0
- package/dist/js/jsonschema/codegen.js +514 -0
- package/dist/js/jsonschema/interpret.js +729 -0
- package/dist/js/jsonschema/pattern.js +221 -0
- package/dist/js/jsonschema/plan.js +448 -0
- package/dist/js/jsonschema/resolve.js +321 -0
- package/dist/js/jsonschema/runtime.js +129 -0
- package/dist/js/jsonschema/validator.js +106 -0
- package/dist/js/typebox.js +5 -5
- package/dist/types/from-json-schema.d.ts +8 -3
- package/dist/types/ir.d.ts +2 -0
- package/dist/types/jsonschema/codegen.d.ts +6 -0
- package/dist/types/jsonschema/interpret.d.ts +48 -0
- package/dist/types/jsonschema/pattern.d.ts +17 -0
- package/dist/types/jsonschema/plan.d.ts +128 -0
- package/dist/types/jsonschema/resolve.d.ts +43 -0
- package/dist/types/jsonschema/runtime.d.ts +30 -0
- package/dist/types/jsonschema/validator.d.ts +33 -0
- package/package.json +2 -5
- package/src/compile.ts +24 -3
- package/src/from-json-schema.ts +64 -213
- package/src/interp.ts +1 -0
- package/src/ir.ts +2 -0
- package/src/jsonschema/codegen.ts +495 -0
- package/src/jsonschema/interpret.ts +724 -0
- package/src/jsonschema/pattern.ts +202 -0
- package/src/jsonschema/plan.ts +538 -0
- package/src/jsonschema/resolve.ts +333 -0
- package/src/jsonschema/runtime.ts +117 -0
- package/src/jsonschema/validator.ts +113 -0
- package/src/typebox.ts +9 -5
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiles simple anchored `pattern`s into char-code checks for generated
|
|
3
|
+
* validators: `^[0-9a-f]{8}-…$`, `^\d{4}-\d{2}-\d{2}$`, `^[A-Za-z0-9._-]+$`.
|
|
4
|
+
*
|
|
5
|
+
* Supported: ASCII literals and escaped punctuation, `\d`, `\w`, non-negated
|
|
6
|
+
* ASCII classes, and quantifiers, with at most one variable-length segment.
|
|
7
|
+
* Fixed segments then pin every position (from the start before the variable
|
|
8
|
+
* segment, from the end after it), so the check is exact without
|
|
9
|
+
* backtracking. ASCII-only classes reject every UTF-16 surrogate, so counting
|
|
10
|
+
* code units agrees with the `u` flag's code points. Anything else returns
|
|
11
|
+
* `undefined` and stays a `RegExp`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
type Range = readonly [number, number];
|
|
15
|
+
|
|
16
|
+
interface Segment {
|
|
17
|
+
readonly ranges: readonly Range[];
|
|
18
|
+
readonly min: number;
|
|
19
|
+
readonly max: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const DIGIT: Range[] = [[48, 57]];
|
|
23
|
+
const WORD: Range[] = [
|
|
24
|
+
[48, 57],
|
|
25
|
+
[65, 90],
|
|
26
|
+
[95, 95],
|
|
27
|
+
[97, 122],
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
function isPunctuation(code: number): boolean {
|
|
31
|
+
return code < 128 && !(code >= 48 && code <= 57) && !(code >= 65 && code <= 90) && !(code >= 97 && code <= 122);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Ranges of an escape (`\d`, `\w`, `\.`), or `undefined` when unsupported. */
|
|
35
|
+
function escapeRanges(code: number | undefined): Range[] | undefined {
|
|
36
|
+
if (code === undefined) return undefined;
|
|
37
|
+
if (code === 100) return DIGIT;
|
|
38
|
+
if (code === 119) return WORD;
|
|
39
|
+
return isPunctuation(code) ? [[code, code]] : undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function parseClass(body: string, start: number): { ranges: Range[]; end: number } | undefined {
|
|
43
|
+
const ranges: Range[] = [];
|
|
44
|
+
let index = start;
|
|
45
|
+
if (body.charCodeAt(index) === 94) return undefined; // negated
|
|
46
|
+
const single = (): number | undefined => {
|
|
47
|
+
const code = body.charCodeAt(index);
|
|
48
|
+
if (code === 92) {
|
|
49
|
+
const escaped = body.charCodeAt(index + 1);
|
|
50
|
+
if (!isPunctuation(escaped)) return undefined;
|
|
51
|
+
index += 2;
|
|
52
|
+
return escaped;
|
|
53
|
+
}
|
|
54
|
+
if (Number.isNaN(code) || code >= 128 || code === 91 || code === 93) return undefined;
|
|
55
|
+
index++;
|
|
56
|
+
return code;
|
|
57
|
+
};
|
|
58
|
+
while (index < body.length && body.charCodeAt(index) !== 93) {
|
|
59
|
+
if (body.charCodeAt(index) === 92) {
|
|
60
|
+
const named = body.charCodeAt(index + 1);
|
|
61
|
+
if (named === 100 || named === 119) {
|
|
62
|
+
ranges.push(...escapeRanges(named)!);
|
|
63
|
+
index += 2;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const low = single();
|
|
68
|
+
if (low === undefined) return undefined;
|
|
69
|
+
if (body.charCodeAt(index) === 45 && index + 1 < body.length && body.charCodeAt(index + 1) !== 93) {
|
|
70
|
+
index++;
|
|
71
|
+
const high = single();
|
|
72
|
+
if (high === undefined || high < low) return undefined;
|
|
73
|
+
ranges.push([low, high]);
|
|
74
|
+
} else {
|
|
75
|
+
ranges.push([low, low]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (index >= body.length || ranges.length === 0) return undefined;
|
|
79
|
+
return { ranges, end: index + 1 };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function parseQuantifier(body: string, start: number): { min: number; max: number; end: number } | undefined {
|
|
83
|
+
const code = body.charCodeAt(start);
|
|
84
|
+
let result: { min: number; max: number; end: number };
|
|
85
|
+
if (code === 43) result = { min: 1, max: Infinity, end: start + 1 };
|
|
86
|
+
else if (code === 42) result = { min: 0, max: Infinity, end: start + 1 };
|
|
87
|
+
else if (code === 63) result = { min: 0, max: 1, end: start + 1 };
|
|
88
|
+
else if (code === 123) {
|
|
89
|
+
const match = /^\{(\d+)(,(\d*))?\}/.exec(body.slice(start));
|
|
90
|
+
if (match === null) return undefined;
|
|
91
|
+
const min = Number(match[1]);
|
|
92
|
+
const max = match[2] === undefined ? min : match[3] === "" ? Infinity : Number(match[3]);
|
|
93
|
+
if (max < min) return undefined;
|
|
94
|
+
result = { min, max, end: start + match[0].length };
|
|
95
|
+
} else {
|
|
96
|
+
return { min: 1, max: 1, end: start };
|
|
97
|
+
}
|
|
98
|
+
// Lazy and possessive-looking suffixes change nothing here but are rare; keep them as regexes.
|
|
99
|
+
return body.charCodeAt(result.end) === 63 || body.charCodeAt(result.end) === 43 ? undefined : result;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function parse(source: string): Segment[] | undefined {
|
|
103
|
+
if (source.length < 2 || source.charCodeAt(0) !== 94 || source.charCodeAt(source.length - 1) !== 36) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
const body = source.slice(1, -1);
|
|
107
|
+
// `\$` at the end is a literal dollar followed by no anchor.
|
|
108
|
+
let slashes = 0;
|
|
109
|
+
for (let index = body.length - 1; index >= 0 && body.charCodeAt(index) === 92; index--) slashes++;
|
|
110
|
+
if (slashes % 2 === 1) return undefined;
|
|
111
|
+
const segments: Segment[] = [];
|
|
112
|
+
let index = 0;
|
|
113
|
+
while (index < body.length) {
|
|
114
|
+
const code = body.charCodeAt(index);
|
|
115
|
+
let ranges: Range[] | undefined;
|
|
116
|
+
if (code === 91) {
|
|
117
|
+
const parsed = parseClass(body, index + 1);
|
|
118
|
+
if (parsed === undefined) return undefined;
|
|
119
|
+
ranges = parsed.ranges;
|
|
120
|
+
index = parsed.end;
|
|
121
|
+
} else if (code === 92) {
|
|
122
|
+
ranges = escapeRanges(body.charCodeAt(index + 1));
|
|
123
|
+
index += 2;
|
|
124
|
+
} else if (code < 128 && !"()|.*+?{}^$]".includes(body[index])) {
|
|
125
|
+
ranges = [[code, code]];
|
|
126
|
+
index++;
|
|
127
|
+
}
|
|
128
|
+
if (ranges === undefined) return undefined;
|
|
129
|
+
const quantifier = parseQuantifier(body, index);
|
|
130
|
+
if (quantifier === undefined) return undefined;
|
|
131
|
+
index = quantifier.end;
|
|
132
|
+
segments.push({ ranges, min: quantifier.min, max: quantifier.max });
|
|
133
|
+
}
|
|
134
|
+
let variable = 0;
|
|
135
|
+
for (const segment of segments) if (segment.min !== segment.max) variable++;
|
|
136
|
+
return variable <= 1 ? segments : undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function rangeTest(ranges: readonly Range[]): string {
|
|
140
|
+
const sorted = [...ranges].sort((a, b) => a[0] - b[0]);
|
|
141
|
+
const merged: [number, number][] = [];
|
|
142
|
+
for (const [low, high] of sorted) {
|
|
143
|
+
const last = merged[merged.length - 1];
|
|
144
|
+
if (last !== undefined && low <= last[1] + 1) last[1] = Math.max(last[1], high);
|
|
145
|
+
else merged.push([low, high]);
|
|
146
|
+
}
|
|
147
|
+
return merged.map(([low, high]) => (low === high ? `c===${low}` : `(c>=${low}&&c<=${high})`)).join("||");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Statements checking positions `[from, to)` (`to` may be an expression) against `ranges`. */
|
|
151
|
+
function positions(ranges: readonly Range[], from: string, to: string, count: number | undefined): string {
|
|
152
|
+
const test = rangeTest(ranges);
|
|
153
|
+
if (count !== undefined && count <= 4) {
|
|
154
|
+
let out = "";
|
|
155
|
+
for (let offset = 0; offset < count; offset++) {
|
|
156
|
+
out += `c=s.charCodeAt(${from}+${offset});if(!(${test}))return false;`;
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
return `for(let i=${from};i<${to};i++){c=s.charCodeAt(i);if(!(${test}))return false;}`;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Source of `function name(s)` testing a string against `source` with the
|
|
165
|
+
* `u` flag's semantics, or `undefined` when the pattern is not simple.
|
|
166
|
+
*/
|
|
167
|
+
export function compileSimplePattern(source: string, name: string): string | undefined {
|
|
168
|
+
const segments = parse(source);
|
|
169
|
+
if (segments === undefined) return undefined;
|
|
170
|
+
let fixed = 0;
|
|
171
|
+
let variable: Segment | undefined;
|
|
172
|
+
for (const segment of segments) {
|
|
173
|
+
if (segment.min === segment.max) fixed += segment.min;
|
|
174
|
+
else variable = segment;
|
|
175
|
+
}
|
|
176
|
+
let body = "const n=s.length;let c;";
|
|
177
|
+
if (variable === undefined) body += `if(n!==${fixed})return false;`;
|
|
178
|
+
else {
|
|
179
|
+
body += `if(n<${fixed + variable.min})return false;`;
|
|
180
|
+
if (variable.max !== Infinity) body += `if(n>${fixed + variable.max})return false;`;
|
|
181
|
+
}
|
|
182
|
+
// Positions before the variable segment count from 0, after it from the end.
|
|
183
|
+
let position = 0;
|
|
184
|
+
let fromEnd = false;
|
|
185
|
+
let suffix = 0;
|
|
186
|
+
if (variable !== undefined) {
|
|
187
|
+
for (let index = segments.length - 1; segments[index] !== variable; index--) suffix += segments[index].min;
|
|
188
|
+
}
|
|
189
|
+
for (const segment of segments) {
|
|
190
|
+
if (segment === variable) {
|
|
191
|
+
body += positions(segment.ranges, String(position), `n-${suffix}`, undefined);
|
|
192
|
+
fromEnd = true;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (segment.min === 0) continue;
|
|
196
|
+
const start = fromEnd ? `n-${suffix}` : String(position);
|
|
197
|
+
body += positions(segment.ranges, start, `${start}+${segment.min}`, segment.min);
|
|
198
|
+
if (fromEnd) suffix -= segment.min;
|
|
199
|
+
else position += segment.min;
|
|
200
|
+
}
|
|
201
|
+
return `function ${name}(s){${body}return true}`;
|
|
202
|
+
}
|