@ocap/rule-dsl 0.0.0-stage → 1.30.26
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/esm/compiler.d.mts +12 -0
- package/esm/compiler.mjs +277 -0
- package/esm/errors.d.mts +28 -0
- package/esm/errors.mjs +36 -0
- package/esm/expr.d.mts +29 -0
- package/esm/expr.mjs +218 -0
- package/esm/index.d.mts +9 -0
- package/esm/index.mjs +9 -0
- package/esm/parser.d.mts +12 -0
- package/esm/parser.mjs +398 -0
- package/esm/pipeline.d.mts +18 -0
- package/esm/pipeline.mjs +37 -0
- package/esm/primitives.d.mts +22 -0
- package/esm/primitives.mjs +57 -0
- package/esm/types.d.mts +139 -0
- package/esm/types.mjs +1 -0
- package/esm/validator.d.mts +12 -0
- package/esm/validator.mjs +72 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/compiler.cjs +277 -0
- package/lib/compiler.d.cts +12 -0
- package/lib/errors.cjs +38 -0
- package/lib/errors.d.cts +28 -0
- package/lib/expr.cjs +222 -0
- package/lib/expr.d.cts +29 -0
- package/lib/index.cjs +30 -0
- package/lib/index.d.cts +9 -0
- package/lib/parser.cjs +400 -0
- package/lib/parser.d.cts +12 -0
- package/lib/pipeline.cjs +37 -0
- package/lib/pipeline.d.cts +18 -0
- package/lib/primitives.cjs +69 -0
- package/lib/primitives.d.cts +22 -0
- package/lib/types.cjs +0 -0
- package/lib/types.d.cts +139 -0
- package/lib/validator.cjs +72 -0
- package/lib/validator.d.cts +12 -0
- package/package.json +72 -4
- package/README.md +0 -3
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RuleDslError } from "./errors.mjs";
|
|
2
|
+
import { CompiledRuleDag, RuleAst } from "./types.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/compiler.d.ts
|
|
5
|
+
|
|
6
|
+
interface CompileResult {
|
|
7
|
+
dag: CompiledRuleDag | null;
|
|
8
|
+
errors: RuleDslError[];
|
|
9
|
+
}
|
|
10
|
+
declare function compileRule(ast: RuleAst): CompileResult;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { CompileResult, compileRule };
|
package/esm/compiler.mjs
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { makeError } from "./errors.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/compiler.ts
|
|
4
|
+
/**
|
|
5
|
+
* Compiles a validated RuleAst into an Execution Plan (DAG), per planning/35
|
|
6
|
+
* L1067-1087:
|
|
7
|
+
*
|
|
8
|
+
* Match Nodes: Eligibility -> Time -> Rate
|
|
9
|
+
* Effect Nodes: Counter -> Allocation
|
|
10
|
+
* Constraint Nodes: Threshold
|
|
11
|
+
*
|
|
12
|
+
* "强制约束" implemented here:
|
|
13
|
+
* - DAG 必须无环 (DAG must be acyclic) -> CYCLIC_DEPENDENCY
|
|
14
|
+
* - effects 在 constraints 之前 (default ordering) -> match -> effects -> constraints edge
|
|
15
|
+
* - 不允许跨 rule 写 state (no cross-rule state writes) is a multi-rule
|
|
16
|
+
* concern; a single compiled document only ever has one rule's own
|
|
17
|
+
* writes in scope, so there is nothing to check for it here.
|
|
18
|
+
*
|
|
19
|
+
* Cross-effect ordering: primitives don't define any inter-counter ordering
|
|
20
|
+
* syntax, but Primitive 1 says `value` can reference `state.<key>`. If
|
|
21
|
+
* counter A's value expression references the key that counter B writes (or
|
|
22
|
+
* vice-versa in a cycle), that's a real data dependency among effect nodes.
|
|
23
|
+
* We build a dependency graph over `effects` from these references and
|
|
24
|
+
* topologically sort it; an unsortable graph is CYCLIC_DEPENDENCY.
|
|
25
|
+
*/
|
|
26
|
+
function matchNodeData(node) {
|
|
27
|
+
if (node.primitive === "eligibility") return {
|
|
28
|
+
set: node.set,
|
|
29
|
+
mode: node.mode
|
|
30
|
+
};
|
|
31
|
+
if (node.primitive === "time") return {
|
|
32
|
+
timeType: node.timeType,
|
|
33
|
+
value: node.value
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
window: node.window,
|
|
37
|
+
limit: node.limit
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function effectNodeData(node) {
|
|
41
|
+
if (node.primitive === "counter") return {
|
|
42
|
+
op: node.op,
|
|
43
|
+
key: node.key,
|
|
44
|
+
value: node.value
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
source: node.source,
|
|
48
|
+
strategy: node.strategy
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function buildMatchNodes(match) {
|
|
52
|
+
const nodes = [];
|
|
53
|
+
const edges = [];
|
|
54
|
+
const order = [
|
|
55
|
+
"eligibility",
|
|
56
|
+
"time",
|
|
57
|
+
"rate"
|
|
58
|
+
];
|
|
59
|
+
const counters = {
|
|
60
|
+
eligibility: 0,
|
|
61
|
+
time: 0,
|
|
62
|
+
rate: 0
|
|
63
|
+
};
|
|
64
|
+
let prevId = null;
|
|
65
|
+
for (const kind of order) match.filter((m) => m.primitive === kind).forEach((item) => {
|
|
66
|
+
const id = `match.${kind}.${counters[kind]}`;
|
|
67
|
+
counters[kind] += 1;
|
|
68
|
+
nodes.push({
|
|
69
|
+
id,
|
|
70
|
+
kind: "match",
|
|
71
|
+
type: kind,
|
|
72
|
+
data: matchNodeData(item)
|
|
73
|
+
});
|
|
74
|
+
if (prevId) edges.push({
|
|
75
|
+
from: prevId,
|
|
76
|
+
to: id
|
|
77
|
+
});
|
|
78
|
+
prevId = id;
|
|
79
|
+
});
|
|
80
|
+
return {
|
|
81
|
+
nodes,
|
|
82
|
+
edges
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Topologically sorts counters by cross-references in their `value` field; returns null on cycle. */
|
|
86
|
+
function sortCounters(counters) {
|
|
87
|
+
const keyToIndex = /* @__PURE__ */ new Map();
|
|
88
|
+
counters.forEach((c, idx) => keyToIndex.set(c.key, idx));
|
|
89
|
+
const deps = counters.map(() => []);
|
|
90
|
+
counters.forEach((c, idx) => {
|
|
91
|
+
if (c.value.kind === "state") {
|
|
92
|
+
const producerIdx = keyToIndex.get(c.value.path);
|
|
93
|
+
if (producerIdx !== void 0 && producerIdx !== idx) deps[idx].push(producerIdx);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
const state = counters.map(() => "unvisited");
|
|
97
|
+
const order = [];
|
|
98
|
+
let hasCycle = false;
|
|
99
|
+
function visit(i) {
|
|
100
|
+
if (state[i] === "done" || hasCycle) return;
|
|
101
|
+
if (state[i] === "visiting") {
|
|
102
|
+
hasCycle = true;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
state[i] = "visiting";
|
|
106
|
+
for (const dep of deps[i]) visit(dep);
|
|
107
|
+
state[i] = "done";
|
|
108
|
+
order.push(i);
|
|
109
|
+
}
|
|
110
|
+
for (let i = 0; i < counters.length; i += 1) visit(i);
|
|
111
|
+
return hasCycle ? null : order;
|
|
112
|
+
}
|
|
113
|
+
function buildEffectNodes(effects, errors) {
|
|
114
|
+
const nodes = [];
|
|
115
|
+
const edges = [];
|
|
116
|
+
const counters = effects.filter((e) => e.primitive === "counter");
|
|
117
|
+
const allocates = effects.filter((e) => e.primitive === "allocate");
|
|
118
|
+
const sortedOrder = sortCounters(counters);
|
|
119
|
+
if (sortedOrder === null) {
|
|
120
|
+
errors.push(makeError("CYCLIC_DEPENDENCY", "effects", "Cyclic dependency among counter effects (mutual state-key references)"));
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
const counterIds = [];
|
|
124
|
+
sortedOrder.forEach((origIdx, position) => {
|
|
125
|
+
const c = counters[origIdx];
|
|
126
|
+
const id = `effect.counter.${position}`;
|
|
127
|
+
counterIds.push(id);
|
|
128
|
+
nodes.push({
|
|
129
|
+
id,
|
|
130
|
+
kind: "effect",
|
|
131
|
+
type: "counter",
|
|
132
|
+
data: effectNodeData(c)
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
const keyToPosition = /* @__PURE__ */ new Map();
|
|
136
|
+
sortedOrder.forEach((origIdx, position) => keyToPosition.set(counters[origIdx].key, position));
|
|
137
|
+
sortedOrder.forEach((origIdx, position) => {
|
|
138
|
+
const c = counters[origIdx];
|
|
139
|
+
if (c.value.kind === "state") {
|
|
140
|
+
const producerPos = keyToPosition.get(c.value.path);
|
|
141
|
+
if (producerPos !== void 0 && producerPos !== position) edges.push({
|
|
142
|
+
from: counterIds[producerPos],
|
|
143
|
+
to: counterIds[position]
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
const allocateIds = [];
|
|
148
|
+
allocates.forEach((a, idx) => {
|
|
149
|
+
const id = `effect.allocate.${idx}`;
|
|
150
|
+
allocateIds.push(id);
|
|
151
|
+
nodes.push({
|
|
152
|
+
id,
|
|
153
|
+
kind: "effect",
|
|
154
|
+
type: "allocate",
|
|
155
|
+
data: effectNodeData(a)
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
if (counterIds.length > 0 && allocateIds.length > 0) edges.push({
|
|
159
|
+
from: counterIds[counterIds.length - 1],
|
|
160
|
+
to: allocateIds[0]
|
|
161
|
+
});
|
|
162
|
+
for (let i = 0; i + 1 < allocateIds.length; i += 1) edges.push({
|
|
163
|
+
from: allocateIds[i],
|
|
164
|
+
to: allocateIds[i + 1]
|
|
165
|
+
});
|
|
166
|
+
return {
|
|
167
|
+
nodes,
|
|
168
|
+
edges
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function buildConstraintNodes(ast) {
|
|
172
|
+
const nodes = [];
|
|
173
|
+
const edges = [];
|
|
174
|
+
ast.constraints.forEach((c, idx) => {
|
|
175
|
+
const id = `constraint.threshold.${idx}`;
|
|
176
|
+
nodes.push({
|
|
177
|
+
id,
|
|
178
|
+
kind: "constraint",
|
|
179
|
+
type: "threshold",
|
|
180
|
+
data: {
|
|
181
|
+
expr: c.expr,
|
|
182
|
+
onFail: c.onFail
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
if (idx > 0) edges.push({
|
|
186
|
+
from: `constraint.threshold.${idx - 1}`,
|
|
187
|
+
to: id
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
return {
|
|
191
|
+
nodes,
|
|
192
|
+
edges
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function buildBranchNodes(ast) {
|
|
196
|
+
const nodes = [];
|
|
197
|
+
const edges = [];
|
|
198
|
+
(ast.branches ?? []).forEach((b, idx) => {
|
|
199
|
+
const id = `branch.${idx}`;
|
|
200
|
+
nodes.push({
|
|
201
|
+
id,
|
|
202
|
+
kind: "branch",
|
|
203
|
+
type: "branch",
|
|
204
|
+
data: {
|
|
205
|
+
when: b.when ?? null,
|
|
206
|
+
decision: b.decision,
|
|
207
|
+
delay: b.delay ?? null
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
if (idx > 0) edges.push({
|
|
211
|
+
from: `branch.${idx - 1}`,
|
|
212
|
+
to: id
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
return {
|
|
216
|
+
nodes,
|
|
217
|
+
edges
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
function compileRule(ast) {
|
|
221
|
+
const errors = [];
|
|
222
|
+
const matchResult = buildMatchNodes(ast.match);
|
|
223
|
+
const effectResult = buildEffectNodes(ast.effects, errors);
|
|
224
|
+
if (effectResult === null) return {
|
|
225
|
+
dag: null,
|
|
226
|
+
errors
|
|
227
|
+
};
|
|
228
|
+
const constraintResult = buildConstraintNodes(ast);
|
|
229
|
+
const branchResult = buildBranchNodes(ast);
|
|
230
|
+
const nodes = [
|
|
231
|
+
...matchResult.nodes,
|
|
232
|
+
...effectResult.nodes,
|
|
233
|
+
...constraintResult.nodes,
|
|
234
|
+
...branchResult.nodes
|
|
235
|
+
];
|
|
236
|
+
const edges = [
|
|
237
|
+
...matchResult.edges,
|
|
238
|
+
...effectResult.edges,
|
|
239
|
+
...constraintResult.edges,
|
|
240
|
+
...branchResult.edges
|
|
241
|
+
];
|
|
242
|
+
const lastOf = (list) => list.length > 0 ? list[list.length - 1].id : null;
|
|
243
|
+
const firstOf = (list) => list.length > 0 ? list[0].id : null;
|
|
244
|
+
const lastMatch = lastOf(matchResult.nodes);
|
|
245
|
+
const firstEffect = firstOf(effectResult.nodes);
|
|
246
|
+
const lastEffect = lastOf(effectResult.nodes);
|
|
247
|
+
const firstConstraint = firstOf(constraintResult.nodes);
|
|
248
|
+
const lastConstraint = lastOf(constraintResult.nodes);
|
|
249
|
+
const firstBranch = firstOf(branchResult.nodes);
|
|
250
|
+
if (lastMatch && firstEffect) edges.push({
|
|
251
|
+
from: lastMatch,
|
|
252
|
+
to: firstEffect
|
|
253
|
+
});
|
|
254
|
+
if (lastEffect && firstConstraint) edges.push({
|
|
255
|
+
from: lastEffect,
|
|
256
|
+
to: firstConstraint
|
|
257
|
+
});
|
|
258
|
+
const lastBeforeBranch = lastConstraint ?? lastEffect ?? lastMatch;
|
|
259
|
+
if (lastBeforeBranch && firstBranch) edges.push({
|
|
260
|
+
from: lastBeforeBranch,
|
|
261
|
+
to: firstBranch
|
|
262
|
+
});
|
|
263
|
+
return {
|
|
264
|
+
dag: {
|
|
265
|
+
ruleId: ast.rule,
|
|
266
|
+
appliesTo: ast.appliesTo,
|
|
267
|
+
...ast.ordering ? { ordering: ast.ordering } : {},
|
|
268
|
+
...ast.metadata ? { metadata: ast.metadata } : {},
|
|
269
|
+
nodes,
|
|
270
|
+
edges
|
|
271
|
+
},
|
|
272
|
+
errors
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
//#endregion
|
|
277
|
+
export { compileRule };
|
package/esm/errors.d.mts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/errors.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Error codes for Rule DSL v1 static validation.
|
|
4
|
+
*
|
|
5
|
+
* The first 5 codes are exactly the enum defined in planning/35 L1263-1271:
|
|
6
|
+
* UNKNOWN_PRIMITIVE | INVALID_EXPR | STATE_KEY_NOT_FOUND | CYCLIC_DEPENDENCY | TYPE_MISMATCH
|
|
7
|
+
*
|
|
8
|
+
* `MISSING_REQUIRED_FIELD` and `YAML_SYNTAX_ERROR` are pragmatic additions
|
|
9
|
+
* beyond that 5-code enum: the spec's enum covers "closed world" semantic
|
|
10
|
+
* mistakes (wrong/unknown primitive, bad expression, dangling ref, cycle,
|
|
11
|
+
* type mismatch) but says nothing about plain structural gaps (a required
|
|
12
|
+
* field missing entirely) or outer YAML syntax errors. Rather than force
|
|
13
|
+
* those into one of the 5 spec codes (which would misreport the failure
|
|
14
|
+
* reason), we surface them under these two extra codes. All 5 spec codes are
|
|
15
|
+
* fully implemented and covered by tests per issue #870's acceptance
|
|
16
|
+
* criteria; these two extras only add robustness around the edges.
|
|
17
|
+
*/
|
|
18
|
+
declare const RULE_DSL_ERROR_CODES: readonly ["UNKNOWN_PRIMITIVE", "INVALID_EXPR", "STATE_KEY_NOT_FOUND", "CYCLIC_DEPENDENCY", "TYPE_MISMATCH", "MISSING_REQUIRED_FIELD", "YAML_SYNTAX_ERROR"];
|
|
19
|
+
type RuleDslErrorCode = (typeof RULE_DSL_ERROR_CODES)[number];
|
|
20
|
+
interface RuleDslError {
|
|
21
|
+
code: RuleDslErrorCode;
|
|
22
|
+
message: string;
|
|
23
|
+
/** Dotted/bracketed path into the document, e.g. "effects[0].counter.value". */
|
|
24
|
+
path: string;
|
|
25
|
+
}
|
|
26
|
+
declare function makeError(code: RuleDslErrorCode, path: string, message: string): RuleDslError;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { RULE_DSL_ERROR_CODES, RuleDslError, RuleDslErrorCode, makeError };
|
package/esm/errors.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/errors.ts
|
|
2
|
+
/**
|
|
3
|
+
* Error codes for Rule DSL v1 static validation.
|
|
4
|
+
*
|
|
5
|
+
* The first 5 codes are exactly the enum defined in planning/35 L1263-1271:
|
|
6
|
+
* UNKNOWN_PRIMITIVE | INVALID_EXPR | STATE_KEY_NOT_FOUND | CYCLIC_DEPENDENCY | TYPE_MISMATCH
|
|
7
|
+
*
|
|
8
|
+
* `MISSING_REQUIRED_FIELD` and `YAML_SYNTAX_ERROR` are pragmatic additions
|
|
9
|
+
* beyond that 5-code enum: the spec's enum covers "closed world" semantic
|
|
10
|
+
* mistakes (wrong/unknown primitive, bad expression, dangling ref, cycle,
|
|
11
|
+
* type mismatch) but says nothing about plain structural gaps (a required
|
|
12
|
+
* field missing entirely) or outer YAML syntax errors. Rather than force
|
|
13
|
+
* those into one of the 5 spec codes (which would misreport the failure
|
|
14
|
+
* reason), we surface them under these two extra codes. All 5 spec codes are
|
|
15
|
+
* fully implemented and covered by tests per issue #870's acceptance
|
|
16
|
+
* criteria; these two extras only add robustness around the edges.
|
|
17
|
+
*/
|
|
18
|
+
const RULE_DSL_ERROR_CODES = [
|
|
19
|
+
"UNKNOWN_PRIMITIVE",
|
|
20
|
+
"INVALID_EXPR",
|
|
21
|
+
"STATE_KEY_NOT_FOUND",
|
|
22
|
+
"CYCLIC_DEPENDENCY",
|
|
23
|
+
"TYPE_MISMATCH",
|
|
24
|
+
"MISSING_REQUIRED_FIELD",
|
|
25
|
+
"YAML_SYNTAX_ERROR"
|
|
26
|
+
];
|
|
27
|
+
function makeError(code, path, message) {
|
|
28
|
+
return {
|
|
29
|
+
code,
|
|
30
|
+
path,
|
|
31
|
+
message
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
export { RULE_DSL_ERROR_CODES, makeError };
|
package/esm/expr.d.mts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BoolExpr, ValueExpr } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/expr.d.ts
|
|
4
|
+
|
|
5
|
+
interface ExprParseResult<T> {
|
|
6
|
+
ok: boolean;
|
|
7
|
+
value?: T;
|
|
8
|
+
/** Present when ok === false: a short reason, e.g. 'function-call', 'malformed'. */
|
|
9
|
+
reason?: string;
|
|
10
|
+
}
|
|
11
|
+
declare function parseValueExpr(src: string): ExprParseResult<ValueExpr>;
|
|
12
|
+
declare function parseBoolExpr(src: string): ExprParseResult<BoolExpr>;
|
|
13
|
+
/**
|
|
14
|
+
* Literal-vs-literal type compatibility check for a bool expr (Primitive 2 /
|
|
15
|
+
* error code TYPE_MISMATCH). Refs (`tx.*`/state key) have no statically known
|
|
16
|
+
* type in this PoC (no external state schema is part of the DSL grammar), so
|
|
17
|
+
* we only flag mismatches we can prove from literal values themselves:
|
|
18
|
+
* - equality ops (==, !=) between literals of different kinds
|
|
19
|
+
* - ordering ops (<, <=, >, >=) applied to a boolean or string literal
|
|
20
|
+
*/
|
|
21
|
+
declare function boolExprTypeMismatch(expr: BoolExpr): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* `counter.value` (and `rate.limit.sum`) must be `number | tx.field | state.field`
|
|
24
|
+
* (planning/35 Primitive 1: "value 只能是纯表达式(number | tx.field | state.field)").
|
|
25
|
+
* A string or boolean literal here is a TYPE_MISMATCH.
|
|
26
|
+
*/
|
|
27
|
+
declare function isValidCounterValueType(v: ValueExpr): boolean;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { ExprParseResult, boolExprTypeMismatch, isValidCounterValueType, parseBoolExpr, parseValueExpr };
|
package/esm/expr.mjs
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
//#region src/expr.ts
|
|
2
|
+
function tokenize(src) {
|
|
3
|
+
const tokens = [];
|
|
4
|
+
let i = 0;
|
|
5
|
+
const n = src.length;
|
|
6
|
+
while (i < n) {
|
|
7
|
+
const ch = src[i];
|
|
8
|
+
if (/\s/.test(ch)) {
|
|
9
|
+
i += 1;
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
if (ch === "(") {
|
|
13
|
+
tokens.push({ kind: "lparen" });
|
|
14
|
+
i += 1;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (ch === ")") {
|
|
18
|
+
tokens.push({ kind: "rparen" });
|
|
19
|
+
i += 1;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
const two = src.slice(i, i + 2);
|
|
23
|
+
if (two === "<=" || two === ">=" || two === "==" || two === "!=") {
|
|
24
|
+
tokens.push({
|
|
25
|
+
kind: "op",
|
|
26
|
+
value: two
|
|
27
|
+
});
|
|
28
|
+
i += 2;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (ch === "<" || ch === ">") {
|
|
32
|
+
tokens.push({
|
|
33
|
+
kind: "op",
|
|
34
|
+
value: ch
|
|
35
|
+
});
|
|
36
|
+
i += 1;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (ch === "\"" || ch === "'") {
|
|
40
|
+
const quote = ch;
|
|
41
|
+
let j = i + 1;
|
|
42
|
+
let value = "";
|
|
43
|
+
let terminated = false;
|
|
44
|
+
while (j < n) {
|
|
45
|
+
if (src[j] === quote) {
|
|
46
|
+
terminated = true;
|
|
47
|
+
j += 1;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
value += src[j];
|
|
51
|
+
j += 1;
|
|
52
|
+
}
|
|
53
|
+
if (!terminated) {
|
|
54
|
+
tokens.push({
|
|
55
|
+
kind: "invalid",
|
|
56
|
+
value: src.slice(i)
|
|
57
|
+
});
|
|
58
|
+
return tokens;
|
|
59
|
+
}
|
|
60
|
+
tokens.push({
|
|
61
|
+
kind: "string",
|
|
62
|
+
value
|
|
63
|
+
});
|
|
64
|
+
i = j;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const numMatch = /^-?\d+(\.\d+)?/.exec(src.slice(i));
|
|
68
|
+
if (numMatch) {
|
|
69
|
+
tokens.push({
|
|
70
|
+
kind: "number",
|
|
71
|
+
value: Number(numMatch[0])
|
|
72
|
+
});
|
|
73
|
+
i += numMatch[0].length;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const identMatch = /^[A-Za-z_][A-Za-z0-9_.]*/.exec(src.slice(i));
|
|
77
|
+
if (identMatch) {
|
|
78
|
+
const text = identMatch[0];
|
|
79
|
+
if (text === "true" || text === "false") tokens.push({
|
|
80
|
+
kind: "boolean",
|
|
81
|
+
value: text === "true"
|
|
82
|
+
});
|
|
83
|
+
else tokens.push({
|
|
84
|
+
kind: "ident",
|
|
85
|
+
value: text
|
|
86
|
+
});
|
|
87
|
+
i += text.length;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
tokens.push({
|
|
91
|
+
kind: "invalid",
|
|
92
|
+
value: src.slice(i)
|
|
93
|
+
});
|
|
94
|
+
return tokens;
|
|
95
|
+
}
|
|
96
|
+
return tokens;
|
|
97
|
+
}
|
|
98
|
+
function refFromIdent(text) {
|
|
99
|
+
if (text.startsWith("tx.")) return {
|
|
100
|
+
kind: "tx",
|
|
101
|
+
path: text.slice(3)
|
|
102
|
+
};
|
|
103
|
+
if (text.startsWith("state.")) return {
|
|
104
|
+
kind: "state",
|
|
105
|
+
path: text.slice(6)
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
kind: "state",
|
|
109
|
+
path: text
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function tokenToOperand(token) {
|
|
113
|
+
switch (token.kind) {
|
|
114
|
+
case "number": return {
|
|
115
|
+
kind: "number",
|
|
116
|
+
value: token.value
|
|
117
|
+
};
|
|
118
|
+
case "string": return {
|
|
119
|
+
kind: "string",
|
|
120
|
+
value: token.value
|
|
121
|
+
};
|
|
122
|
+
case "boolean": return {
|
|
123
|
+
kind: "boolean",
|
|
124
|
+
value: token.value
|
|
125
|
+
};
|
|
126
|
+
case "ident": return refFromIdent(token.value);
|
|
127
|
+
default: return null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** Detects `ident(` sequences - the DSL forbids custom function calls entirely. */
|
|
131
|
+
function containsFunctionCall(tokens) {
|
|
132
|
+
for (let k = 0; k < tokens.length - 1; k += 1) if (tokens[k].kind === "ident" && tokens[k + 1].kind === "lparen") return true;
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
function parseValueExpr(src) {
|
|
136
|
+
const tokens = tokenize(src);
|
|
137
|
+
if (tokens.some((t) => t.kind === "invalid")) return {
|
|
138
|
+
ok: false,
|
|
139
|
+
reason: "unrecognized-token"
|
|
140
|
+
};
|
|
141
|
+
if (containsFunctionCall(tokens)) return {
|
|
142
|
+
ok: false,
|
|
143
|
+
reason: "function-call"
|
|
144
|
+
};
|
|
145
|
+
if (tokens.length !== 1) return {
|
|
146
|
+
ok: false,
|
|
147
|
+
reason: "malformed"
|
|
148
|
+
};
|
|
149
|
+
const operand = tokenToOperand(tokens[0]);
|
|
150
|
+
if (!operand) return {
|
|
151
|
+
ok: false,
|
|
152
|
+
reason: "malformed"
|
|
153
|
+
};
|
|
154
|
+
return {
|
|
155
|
+
ok: true,
|
|
156
|
+
value: operand
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function parseBoolExpr(src) {
|
|
160
|
+
const tokens = tokenize(src);
|
|
161
|
+
if (tokens.some((t) => t.kind === "invalid")) return {
|
|
162
|
+
ok: false,
|
|
163
|
+
reason: "unrecognized-token"
|
|
164
|
+
};
|
|
165
|
+
if (containsFunctionCall(tokens)) return {
|
|
166
|
+
ok: false,
|
|
167
|
+
reason: "function-call"
|
|
168
|
+
};
|
|
169
|
+
if (tokens.length !== 3 || tokens[1].kind !== "op") return {
|
|
170
|
+
ok: false,
|
|
171
|
+
reason: "malformed"
|
|
172
|
+
};
|
|
173
|
+
const left = tokenToOperand(tokens[0]);
|
|
174
|
+
const right = tokenToOperand(tokens[2]);
|
|
175
|
+
if (!left || !right) return {
|
|
176
|
+
ok: false,
|
|
177
|
+
reason: "malformed"
|
|
178
|
+
};
|
|
179
|
+
return {
|
|
180
|
+
ok: true,
|
|
181
|
+
value: {
|
|
182
|
+
op: tokens[1].value,
|
|
183
|
+
left,
|
|
184
|
+
right
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Literal-vs-literal type compatibility check for a bool expr (Primitive 2 /
|
|
190
|
+
* error code TYPE_MISMATCH). Refs (`tx.*`/state key) have no statically known
|
|
191
|
+
* type in this PoC (no external state schema is part of the DSL grammar), so
|
|
192
|
+
* we only flag mismatches we can prove from literal values themselves:
|
|
193
|
+
* - equality ops (==, !=) between literals of different kinds
|
|
194
|
+
* - ordering ops (<, <=, >, >=) applied to a boolean or string literal
|
|
195
|
+
*/
|
|
196
|
+
function boolExprTypeMismatch(expr) {
|
|
197
|
+
const isOrdering = expr.op === "<" || expr.op === "<=" || expr.op === ">" || expr.op === ">=";
|
|
198
|
+
const literalKind = (v) => v.kind === "number" || v.kind === "string" || v.kind === "boolean" ? v.kind : null;
|
|
199
|
+
const leftKind = literalKind(expr.left);
|
|
200
|
+
const rightKind = literalKind(expr.right);
|
|
201
|
+
if (isOrdering) {
|
|
202
|
+
if (leftKind === "boolean" || leftKind === "string") return true;
|
|
203
|
+
if (rightKind === "boolean" || rightKind === "string") return true;
|
|
204
|
+
}
|
|
205
|
+
if (leftKind && rightKind && leftKind !== rightKind) return true;
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* `counter.value` (and `rate.limit.sum`) must be `number | tx.field | state.field`
|
|
210
|
+
* (planning/35 Primitive 1: "value 只能是纯表达式(number | tx.field | state.field)").
|
|
211
|
+
* A string or boolean literal here is a TYPE_MISMATCH.
|
|
212
|
+
*/
|
|
213
|
+
function isValidCounterValueType(v) {
|
|
214
|
+
return v.kind === "number" || v.kind === "tx" || v.kind === "state";
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
//#endregion
|
|
218
|
+
export { boolExprTypeMismatch, isValidCounterValueType, parseBoolExpr, parseValueExpr };
|
package/esm/index.d.mts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RULE_DSL_ERROR_CODES, RuleDslError, RuleDslErrorCode, makeError } from "./errors.mjs";
|
|
2
|
+
import { AllocateEffect, AllocateStrategy, AppliesTo, BoolExpr, Branch, ComparisonOp, CompiledRuleDag, ConstraintNode, CounterEffect, DagEdge, DagNode, DagNodeKind, EffectNode, EligibilityMatch, ExprLiteral, ExprRef, MatchNode, Metadata, Ordering, RateLimit, RateMatch, RuleAst, ThresholdConstraint, TimeMatch, ValueExpr } from "./types.mjs";
|
|
3
|
+
import { CompileResult, compileRule } from "./compiler.mjs";
|
|
4
|
+
import { boolExprTypeMismatch, isValidCounterValueType, parseBoolExpr, parseValueExpr } from "./expr.mjs";
|
|
5
|
+
import { BANNED_KEYS, CONSTRAINT_PRIMITIVES, COUNTER_OPS, DECISIONS, EFFECT_PRIMITIVES, ELIGIBILITY_MODES, MATCH_PRIMITIVES, ON_FAIL_MODES, ORDERING_MODES, RULE_TOP_LEVEL_KEYS, STRATEGY_TYPES, TIME_TYPES } from "./primitives.mjs";
|
|
6
|
+
import { ParseResult, parseRuleDsl } from "./parser.mjs";
|
|
7
|
+
import { ValidateOptions, validateRule } from "./validator.mjs";
|
|
8
|
+
import { ProcessResult, processRuleDsl } from "./pipeline.mjs";
|
|
9
|
+
export { AllocateEffect, AllocateStrategy, AppliesTo, BANNED_KEYS, BoolExpr, Branch, CONSTRAINT_PRIMITIVES, COUNTER_OPS, ComparisonOp, type CompileResult, CompiledRuleDag, ConstraintNode, CounterEffect, DECISIONS, DagEdge, DagNode, DagNodeKind, EFFECT_PRIMITIVES, ELIGIBILITY_MODES, EffectNode, EligibilityMatch, ExprLiteral, ExprRef, MATCH_PRIMITIVES, MatchNode, Metadata, ON_FAIL_MODES, ORDERING_MODES, Ordering, type ParseResult, type ProcessResult, RULE_DSL_ERROR_CODES, RULE_TOP_LEVEL_KEYS, RateLimit, RateMatch, RuleAst, RuleDslError, RuleDslErrorCode, STRATEGY_TYPES, TIME_TYPES, ThresholdConstraint, TimeMatch, type ValidateOptions, ValueExpr, boolExprTypeMismatch, compileRule, isValidCounterValueType, makeError, parseBoolExpr, parseRuleDsl, parseValueExpr, processRuleDsl, validateRule };
|
package/esm/index.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RULE_DSL_ERROR_CODES, makeError } from "./errors.mjs";
|
|
2
|
+
import { compileRule } from "./compiler.mjs";
|
|
3
|
+
import { boolExprTypeMismatch, isValidCounterValueType, parseBoolExpr, parseValueExpr } from "./expr.mjs";
|
|
4
|
+
import { BANNED_KEYS, CONSTRAINT_PRIMITIVES, COUNTER_OPS, DECISIONS, EFFECT_PRIMITIVES, ELIGIBILITY_MODES, MATCH_PRIMITIVES, ON_FAIL_MODES, ORDERING_MODES, RULE_TOP_LEVEL_KEYS, STRATEGY_TYPES, TIME_TYPES } from "./primitives.mjs";
|
|
5
|
+
import { parseRuleDsl } from "./parser.mjs";
|
|
6
|
+
import { validateRule } from "./validator.mjs";
|
|
7
|
+
import { processRuleDsl } from "./pipeline.mjs";
|
|
8
|
+
|
|
9
|
+
export { BANNED_KEYS, CONSTRAINT_PRIMITIVES, COUNTER_OPS, DECISIONS, EFFECT_PRIMITIVES, ELIGIBILITY_MODES, MATCH_PRIMITIVES, ON_FAIL_MODES, ORDERING_MODES, RULE_DSL_ERROR_CODES, RULE_TOP_LEVEL_KEYS, STRATEGY_TYPES, TIME_TYPES, boolExprTypeMismatch, compileRule, isValidCounterValueType, makeError, parseBoolExpr, parseRuleDsl, parseValueExpr, processRuleDsl, validateRule };
|
package/esm/parser.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RuleDslError } from "./errors.mjs";
|
|
2
|
+
import { RuleAst } from "./types.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/parser.d.ts
|
|
5
|
+
interface ParseResult {
|
|
6
|
+
ast: RuleAst | null;
|
|
7
|
+
errors: RuleDslError[];
|
|
8
|
+
}
|
|
9
|
+
/** Parses Rule DSL v1 YAML text into an AST, collecting all shape/expr errors found. */
|
|
10
|
+
declare function parseRuleDsl(text: string): ParseResult;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { ParseResult, parseRuleDsl };
|