@swaggerexpert/arazzo-criterion 1.0.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.
Files changed (37) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +20 -0
  3. package/README.md +406 -0
  4. package/SECURITY.md +15 -0
  5. package/cjs/apg-lite.cjs +1223 -0
  6. package/cjs/errors/ArazzoCriterionError.cjs +46 -0
  7. package/cjs/errors/ArazzoCriterionEvaluateError.cjs +10 -0
  8. package/cjs/errors/ArazzoCriterionParseError.cjs +10 -0
  9. package/cjs/evaluate/index.cjs +139 -0
  10. package/cjs/grammar.cjs +1111 -0
  11. package/cjs/index.cjs +83 -0
  12. package/cjs/parse/callbacks/cst.cjs +30 -0
  13. package/cjs/parse/index.cjs +42 -0
  14. package/cjs/parse/trace/Expectations.cjs +12 -0
  15. package/cjs/parse/trace/Trace.cjs +37 -0
  16. package/cjs/parse/translators/ASTTranslator/index.cjs +17 -0
  17. package/cjs/parse/translators/ASTTranslator/transformers.cjs +203 -0
  18. package/cjs/parse/translators/CSTTranslator.cjs +49 -0
  19. package/cjs/parse/translators/XMLTranslator.cjs +14 -0
  20. package/cjs/test/index.cjs +20 -0
  21. package/es/errors/ArazzoCriterionError.mjs +40 -0
  22. package/es/errors/ArazzoCriterionEvaluateError.mjs +3 -0
  23. package/es/errors/ArazzoCriterionParseError.mjs +3 -0
  24. package/es/evaluate/index.mjs +132 -0
  25. package/es/grammar.mjs +1105 -0
  26. package/es/index.mjs +11 -0
  27. package/es/parse/callbacks/cst.mjs +24 -0
  28. package/es/parse/index.mjs +35 -0
  29. package/es/parse/trace/Expectations.mjs +6 -0
  30. package/es/parse/trace/Trace.mjs +30 -0
  31. package/es/parse/translators/ASTTranslator/index.mjs +9 -0
  32. package/es/parse/translators/ASTTranslator/transformers.mjs +195 -0
  33. package/es/parse/translators/CSTTranslator.mjs +42 -0
  34. package/es/parse/translators/XMLTranslator.mjs +7 -0
  35. package/es/test/index.mjs +13 -0
  36. package/package.json +95 -0
  37. package/types/index.d.ts +283 -0
@@ -0,0 +1,283 @@
1
+ import type { ASTNode as RuntimeExpressionAST } from '@swaggerexpert/arazzo-runtime-expression';
2
+
3
+ /**
4
+ * CST Node - Concrete Syntax Tree node
5
+ * https://spec.openapis.org/arazzo/v1.1.0.html#criterion-object
6
+ */
7
+ export interface CSTNode {
8
+ readonly type: string;
9
+ readonly text: string;
10
+ readonly start: number;
11
+ readonly length: number;
12
+ readonly children: CSTNode[];
13
+ }
14
+
15
+ /**
16
+ * Translator base interface
17
+ */
18
+ export interface Translator {
19
+ getTree(): CSTNode | ConditionAST | string | undefined;
20
+ }
21
+
22
+ /**
23
+ * CSTTranslator - Produces Concrete Syntax Tree
24
+ */
25
+ export class CSTTranslator implements Translator {
26
+ constructor();
27
+ getTree(): CSTNode;
28
+ }
29
+
30
+ /**
31
+ * XMLTranslator - Produces an XML string representation of the parse tree
32
+ */
33
+ export class XMLTranslator implements Translator {
34
+ constructor();
35
+ getTree(): string;
36
+ }
37
+
38
+ /**
39
+ * AST Node Types for the "simple" criterion condition.
40
+ * https://spec.openapis.org/arazzo/v1.1.0.html#criterion-object
41
+ */
42
+
43
+ export type LiteralValueType = 'number' | 'string' | 'boolean' | 'null';
44
+
45
+ /**
46
+ * A literal operand: number, single-quoted string, boolean, or null.
47
+ */
48
+ export interface Literal {
49
+ readonly type: 'Literal';
50
+ readonly valueType: LiteralValueType;
51
+ readonly value: number | string | boolean | null;
52
+ }
53
+
54
+ /**
55
+ * A runtime expression operand, delegated to
56
+ * @swaggerexpert/arazzo-runtime-expression for its sub-AST.
57
+ */
58
+ export interface RuntimeExpression {
59
+ readonly type: 'RuntimeExpression';
60
+ readonly text: string;
61
+ readonly expression: RuntimeExpressionAST;
62
+ }
63
+
64
+ /**
65
+ * Property de-reference accessor - ".name"
66
+ */
67
+ export interface MemberAccess {
68
+ readonly type: 'MemberAccess';
69
+ readonly name: string;
70
+ }
71
+
72
+ /**
73
+ * Index accessor - "[n]" (0-based)
74
+ */
75
+ export interface IndexAccess {
76
+ readonly type: 'IndexAccess';
77
+ readonly value: number;
78
+ }
79
+
80
+ export type Accessor = MemberAccess | IndexAccess;
81
+
82
+ /**
83
+ * A runtime expression followed by one or more property/index accessors.
84
+ * A runtime expression with no accessors is represented as RuntimeExpression directly.
85
+ */
86
+ export interface RuntimeExpressionNavigation {
87
+ readonly type: 'RuntimeExpressionNavigation';
88
+ readonly expression: RuntimeExpression;
89
+ readonly navigation: Accessor[];
90
+ }
91
+
92
+ export type ComparisonOperator = '==' | '!=' | '<' | '<=' | '>' | '>=';
93
+
94
+ /**
95
+ * A comparison between two comparables.
96
+ */
97
+ export interface BinaryExpression {
98
+ readonly type: 'BinaryExpression';
99
+ readonly operator: ComparisonOperator;
100
+ readonly left: Comparable;
101
+ readonly right: Comparable;
102
+ }
103
+
104
+ /**
105
+ * A logical AND / OR composition.
106
+ */
107
+ export interface LogicalExpression {
108
+ readonly type: 'LogicalExpression';
109
+ readonly operator: '&&' | '||';
110
+ readonly left: ConditionAST;
111
+ readonly right: ConditionAST;
112
+ }
113
+
114
+ /**
115
+ * A logical NOT.
116
+ */
117
+ export interface UnaryExpression {
118
+ readonly type: 'UnaryExpression';
119
+ readonly operator: '!';
120
+ readonly argument: ConditionAST;
121
+ }
122
+
123
+ /**
124
+ * Operands of a comparison / bare truthy condition.
125
+ */
126
+ export type Comparable = Literal | RuntimeExpression | RuntimeExpressionNavigation;
127
+
128
+ /**
129
+ * Any node in a parsed "simple" criterion condition.
130
+ */
131
+ export type ConditionAST = LogicalExpression | UnaryExpression | BinaryExpression | Comparable;
132
+
133
+ /**
134
+ * ASTTranslator - Produces Abstract Syntax Tree
135
+ */
136
+ export class ASTTranslator implements Translator {
137
+ constructor();
138
+ getTree(): ConditionAST;
139
+ }
140
+
141
+ /**
142
+ * Trace - For debugging parse operations
143
+ */
144
+ export class Trace {
145
+ constructor();
146
+ displayTrace(): string;
147
+ inferExpectations(): Expectations;
148
+ }
149
+
150
+ /**
151
+ * Expectations - Array of expected tokens at parse failure point
152
+ */
153
+ export class Expectations extends Array<string> {
154
+ toString(): string;
155
+ }
156
+
157
+ /**
158
+ * Stats - For collecting parse statistics
159
+ */
160
+ export class Stats {
161
+ constructor();
162
+ displayStats(): string;
163
+ }
164
+
165
+ /**
166
+ * Parse options
167
+ */
168
+ export interface ParseOptions<T extends Translator | null = Translator | null> {
169
+ /** Grammar rule to start parsing from. Defaults to 'condition'. */
170
+ readonly startRule?: string;
171
+ readonly stats?: boolean;
172
+ readonly trace?: boolean;
173
+ readonly translator?: T;
174
+ }
175
+
176
+ /**
177
+ * Parse result metadata
178
+ */
179
+ export interface ParseResultMeta {
180
+ readonly success: boolean;
181
+ readonly state: number;
182
+ readonly stateName: string;
183
+ readonly length: number;
184
+ readonly matched: number;
185
+ readonly maxMatched: number;
186
+ readonly maxTreeDepth: number;
187
+ readonly nodeHits: number;
188
+ }
189
+
190
+ /**
191
+ * Parse result
192
+ */
193
+ export interface ParseResult<T = CSTNode | ConditionAST | string | undefined> {
194
+ readonly result: ParseResultMeta;
195
+ readonly tree: T;
196
+ readonly stats: Stats | undefined;
197
+ readonly trace: Trace | undefined;
198
+ }
199
+
200
+ /**
201
+ * Parse a "simple" criterion condition with ASTTranslator (default).
202
+ */
203
+ export function parse(
204
+ condition: string,
205
+ options?: ParseOptions<ASTTranslator>,
206
+ ): ParseResult<ConditionAST>;
207
+ export function parse(
208
+ condition: string,
209
+ options: ParseOptions<CSTTranslator>,
210
+ ): ParseResult<CSTNode>;
211
+ export function parse(
212
+ condition: string,
213
+ options: ParseOptions<XMLTranslator>,
214
+ ): ParseResult<string>;
215
+ export function parse(condition: string, options: ParseOptions<null>): ParseResult<undefined>;
216
+
217
+ /**
218
+ * Test whether a string is a valid "simple" criterion condition.
219
+ */
220
+ export function test(condition: string): boolean;
221
+
222
+ /**
223
+ * Resolver: given a runtime expression string and its parsed sub-AST,
224
+ * return its concrete value.
225
+ */
226
+ export type Resolver = (expression: string, ast: RuntimeExpressionAST) => unknown;
227
+
228
+ export interface EvaluateOptions {
229
+ readonly resolve: Resolver;
230
+ }
231
+
232
+ /**
233
+ * Evaluate a "simple" criterion condition against caller-supplied values.
234
+ */
235
+ export function evaluate(condition: string, options: EvaluateOptions): boolean;
236
+
237
+ /**
238
+ * Grammar - ABNF grammar for "simple" criterion conditions
239
+ */
240
+ export class Grammar {
241
+ constructor();
242
+ grammarObject: string;
243
+ rules: Rule[];
244
+ udts: UDT[];
245
+ toString(): string;
246
+ }
247
+
248
+ export interface Rule {
249
+ name: string;
250
+ lower: string;
251
+ index: number;
252
+ isBkr: boolean;
253
+ opcodes?: Opcode[];
254
+ }
255
+
256
+ export type Opcode =
257
+ | { type: 1; children: number[] }
258
+ | { type: 2; children: number[] }
259
+ | { type: 3; min: number; max: number }
260
+ | { type: 4; index: number }
261
+ | { type: 5; min: number; max: number }
262
+ | { type: 6 | 7; string: number[] };
263
+
264
+ export type UDT = Record<string, never>;
265
+
266
+ export interface ArazzoCriterionErrorOptions {
267
+ cause?: unknown;
268
+ [key: string]: unknown;
269
+ }
270
+
271
+ export class ArazzoCriterionError extends Error {
272
+ constructor(message?: string, options?: ArazzoCriterionErrorOptions);
273
+ name: string;
274
+ cause?: unknown;
275
+ }
276
+
277
+ export class ArazzoCriterionParseError extends ArazzoCriterionError {
278
+ condition?: string;
279
+ }
280
+
281
+ export class ArazzoCriterionEvaluateError extends ArazzoCriterionError {
282
+ condition?: string;
283
+ }