@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,72 @@
|
|
|
1
|
+
import { makeError } from "./errors.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/validator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Whole-AST static validation that needs more than one node in isolation.
|
|
6
|
+
*
|
|
7
|
+
* planning/35 Primitive 1 says a counter's `key` "必须在 module 的 state
|
|
8
|
+
* schema 中声明" (must be declared in the module's state schema). That state
|
|
9
|
+
* schema is an external, module-level concept that this isolated PoC package
|
|
10
|
+
* has no access to (and per issue #870 scope, must not depend on any
|
|
11
|
+
* existing pipeline/state code). We therefore make it an explicit, optional
|
|
12
|
+
* input to the validator: callers that know the state schema can pass
|
|
13
|
+
* `knownStateKeys` to get STATE_KEY_NOT_FOUND enforcement; callers that don't
|
|
14
|
+
* (e.g. editing a rule with no bound module yet) simply skip that check.
|
|
15
|
+
*/
|
|
16
|
+
function collectStateRefPaths(value, sink) {
|
|
17
|
+
if (!value) return;
|
|
18
|
+
if (value.kind === "state") sink.push(value.path);
|
|
19
|
+
}
|
|
20
|
+
function collectFromMatch(match, sink) {
|
|
21
|
+
if (match.primitive === "rate" && match.limit.sum) collectStateRefPaths(match.limit.sum, sink);
|
|
22
|
+
}
|
|
23
|
+
function collectFromEffect(effect, sink) {
|
|
24
|
+
if (effect.primitive === "counter") {
|
|
25
|
+
collectStateRefPaths(effect.value, sink);
|
|
26
|
+
return { key: effect.key };
|
|
27
|
+
}
|
|
28
|
+
return { key: effect.source };
|
|
29
|
+
}
|
|
30
|
+
function validateRule(ast, options = {}) {
|
|
31
|
+
const errors = [];
|
|
32
|
+
const { knownStateKeys } = options;
|
|
33
|
+
if (!knownStateKeys) return errors;
|
|
34
|
+
const known = new Set(knownStateKeys);
|
|
35
|
+
ast.match.forEach((m, idx) => {
|
|
36
|
+
const refs = [];
|
|
37
|
+
collectFromMatch(m, refs);
|
|
38
|
+
refs.forEach((key) => {
|
|
39
|
+
if (!known.has(key)) errors.push(makeError("STATE_KEY_NOT_FOUND", `match[${idx}]`, `Unknown state key "${key}"`));
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
ast.effects.forEach((e, idx) => {
|
|
43
|
+
const refs = [];
|
|
44
|
+
const { key } = collectFromEffect(e, refs);
|
|
45
|
+
if (key && !known.has(key)) errors.push(makeError("STATE_KEY_NOT_FOUND", `effects[${idx}]`, `Unknown state key "${key}"`));
|
|
46
|
+
refs.forEach((refKey) => {
|
|
47
|
+
if (!known.has(refKey)) errors.push(makeError("STATE_KEY_NOT_FOUND", `effects[${idx}]`, `Unknown state key "${refKey}"`));
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
ast.constraints.forEach((c, idx) => {
|
|
51
|
+
const refs = [];
|
|
52
|
+
collectStateRefPaths(c.expr.left, refs);
|
|
53
|
+
collectStateRefPaths(c.expr.right, refs);
|
|
54
|
+
refs.forEach((key) => {
|
|
55
|
+
if (!known.has(key)) errors.push(makeError("STATE_KEY_NOT_FOUND", `constraints[${idx}]`, `Unknown state key "${key}"`));
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
(ast.branches ?? []).forEach((branch, bIdx) => {
|
|
59
|
+
(branch.when ?? []).forEach((c, idx) => {
|
|
60
|
+
const refs = [];
|
|
61
|
+
collectStateRefPaths(c.expr.left, refs);
|
|
62
|
+
collectStateRefPaths(c.expr.right, refs);
|
|
63
|
+
refs.forEach((key) => {
|
|
64
|
+
if (!known.has(key)) errors.push(makeError("STATE_KEY_NOT_FOUND", `branches[${bIdx}].when[${idx}]`, `Unknown state key "${key}"`));
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
return errors;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
export { validateRule };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
package/lib/compiler.cjs
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
const require_errors = require('./errors.cjs');
|
|
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(require_errors.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
|
+
exports.compileRule = compileRule;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RuleDslError } from "./errors.cjs";
|
|
2
|
+
import { CompiledRuleDag, RuleAst } from "./types.cjs";
|
|
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/lib/errors.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/errors.ts
|
|
3
|
+
/**
|
|
4
|
+
* Error codes for Rule DSL v1 static validation.
|
|
5
|
+
*
|
|
6
|
+
* The first 5 codes are exactly the enum defined in planning/35 L1263-1271:
|
|
7
|
+
* UNKNOWN_PRIMITIVE | INVALID_EXPR | STATE_KEY_NOT_FOUND | CYCLIC_DEPENDENCY | TYPE_MISMATCH
|
|
8
|
+
*
|
|
9
|
+
* `MISSING_REQUIRED_FIELD` and `YAML_SYNTAX_ERROR` are pragmatic additions
|
|
10
|
+
* beyond that 5-code enum: the spec's enum covers "closed world" semantic
|
|
11
|
+
* mistakes (wrong/unknown primitive, bad expression, dangling ref, cycle,
|
|
12
|
+
* type mismatch) but says nothing about plain structural gaps (a required
|
|
13
|
+
* field missing entirely) or outer YAML syntax errors. Rather than force
|
|
14
|
+
* those into one of the 5 spec codes (which would misreport the failure
|
|
15
|
+
* reason), we surface them under these two extra codes. All 5 spec codes are
|
|
16
|
+
* fully implemented and covered by tests per issue #870's acceptance
|
|
17
|
+
* criteria; these two extras only add robustness around the edges.
|
|
18
|
+
*/
|
|
19
|
+
const RULE_DSL_ERROR_CODES = [
|
|
20
|
+
"UNKNOWN_PRIMITIVE",
|
|
21
|
+
"INVALID_EXPR",
|
|
22
|
+
"STATE_KEY_NOT_FOUND",
|
|
23
|
+
"CYCLIC_DEPENDENCY",
|
|
24
|
+
"TYPE_MISMATCH",
|
|
25
|
+
"MISSING_REQUIRED_FIELD",
|
|
26
|
+
"YAML_SYNTAX_ERROR"
|
|
27
|
+
];
|
|
28
|
+
function makeError(code, path, message) {
|
|
29
|
+
return {
|
|
30
|
+
code,
|
|
31
|
+
path,
|
|
32
|
+
message
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
exports.RULE_DSL_ERROR_CODES = RULE_DSL_ERROR_CODES;
|
|
38
|
+
exports.makeError = makeError;
|
package/lib/errors.d.cts
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/lib/expr.cjs
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/expr.ts
|
|
3
|
+
function tokenize(src) {
|
|
4
|
+
const tokens = [];
|
|
5
|
+
let i = 0;
|
|
6
|
+
const n = src.length;
|
|
7
|
+
while (i < n) {
|
|
8
|
+
const ch = src[i];
|
|
9
|
+
if (/\s/.test(ch)) {
|
|
10
|
+
i += 1;
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
if (ch === "(") {
|
|
14
|
+
tokens.push({ kind: "lparen" });
|
|
15
|
+
i += 1;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (ch === ")") {
|
|
19
|
+
tokens.push({ kind: "rparen" });
|
|
20
|
+
i += 1;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const two = src.slice(i, i + 2);
|
|
24
|
+
if (two === "<=" || two === ">=" || two === "==" || two === "!=") {
|
|
25
|
+
tokens.push({
|
|
26
|
+
kind: "op",
|
|
27
|
+
value: two
|
|
28
|
+
});
|
|
29
|
+
i += 2;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (ch === "<" || ch === ">") {
|
|
33
|
+
tokens.push({
|
|
34
|
+
kind: "op",
|
|
35
|
+
value: ch
|
|
36
|
+
});
|
|
37
|
+
i += 1;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (ch === "\"" || ch === "'") {
|
|
41
|
+
const quote = ch;
|
|
42
|
+
let j = i + 1;
|
|
43
|
+
let value = "";
|
|
44
|
+
let terminated = false;
|
|
45
|
+
while (j < n) {
|
|
46
|
+
if (src[j] === quote) {
|
|
47
|
+
terminated = true;
|
|
48
|
+
j += 1;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
value += src[j];
|
|
52
|
+
j += 1;
|
|
53
|
+
}
|
|
54
|
+
if (!terminated) {
|
|
55
|
+
tokens.push({
|
|
56
|
+
kind: "invalid",
|
|
57
|
+
value: src.slice(i)
|
|
58
|
+
});
|
|
59
|
+
return tokens;
|
|
60
|
+
}
|
|
61
|
+
tokens.push({
|
|
62
|
+
kind: "string",
|
|
63
|
+
value
|
|
64
|
+
});
|
|
65
|
+
i = j;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const numMatch = /^-?\d+(\.\d+)?/.exec(src.slice(i));
|
|
69
|
+
if (numMatch) {
|
|
70
|
+
tokens.push({
|
|
71
|
+
kind: "number",
|
|
72
|
+
value: Number(numMatch[0])
|
|
73
|
+
});
|
|
74
|
+
i += numMatch[0].length;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const identMatch = /^[A-Za-z_][A-Za-z0-9_.]*/.exec(src.slice(i));
|
|
78
|
+
if (identMatch) {
|
|
79
|
+
const text = identMatch[0];
|
|
80
|
+
if (text === "true" || text === "false") tokens.push({
|
|
81
|
+
kind: "boolean",
|
|
82
|
+
value: text === "true"
|
|
83
|
+
});
|
|
84
|
+
else tokens.push({
|
|
85
|
+
kind: "ident",
|
|
86
|
+
value: text
|
|
87
|
+
});
|
|
88
|
+
i += text.length;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
tokens.push({
|
|
92
|
+
kind: "invalid",
|
|
93
|
+
value: src.slice(i)
|
|
94
|
+
});
|
|
95
|
+
return tokens;
|
|
96
|
+
}
|
|
97
|
+
return tokens;
|
|
98
|
+
}
|
|
99
|
+
function refFromIdent(text) {
|
|
100
|
+
if (text.startsWith("tx.")) return {
|
|
101
|
+
kind: "tx",
|
|
102
|
+
path: text.slice(3)
|
|
103
|
+
};
|
|
104
|
+
if (text.startsWith("state.")) return {
|
|
105
|
+
kind: "state",
|
|
106
|
+
path: text.slice(6)
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
kind: "state",
|
|
110
|
+
path: text
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function tokenToOperand(token) {
|
|
114
|
+
switch (token.kind) {
|
|
115
|
+
case "number": return {
|
|
116
|
+
kind: "number",
|
|
117
|
+
value: token.value
|
|
118
|
+
};
|
|
119
|
+
case "string": return {
|
|
120
|
+
kind: "string",
|
|
121
|
+
value: token.value
|
|
122
|
+
};
|
|
123
|
+
case "boolean": return {
|
|
124
|
+
kind: "boolean",
|
|
125
|
+
value: token.value
|
|
126
|
+
};
|
|
127
|
+
case "ident": return refFromIdent(token.value);
|
|
128
|
+
default: return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** Detects `ident(` sequences - the DSL forbids custom function calls entirely. */
|
|
132
|
+
function containsFunctionCall(tokens) {
|
|
133
|
+
for (let k = 0; k < tokens.length - 1; k += 1) if (tokens[k].kind === "ident" && tokens[k + 1].kind === "lparen") return true;
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
function parseValueExpr(src) {
|
|
137
|
+
const tokens = tokenize(src);
|
|
138
|
+
if (tokens.some((t) => t.kind === "invalid")) return {
|
|
139
|
+
ok: false,
|
|
140
|
+
reason: "unrecognized-token"
|
|
141
|
+
};
|
|
142
|
+
if (containsFunctionCall(tokens)) return {
|
|
143
|
+
ok: false,
|
|
144
|
+
reason: "function-call"
|
|
145
|
+
};
|
|
146
|
+
if (tokens.length !== 1) return {
|
|
147
|
+
ok: false,
|
|
148
|
+
reason: "malformed"
|
|
149
|
+
};
|
|
150
|
+
const operand = tokenToOperand(tokens[0]);
|
|
151
|
+
if (!operand) return {
|
|
152
|
+
ok: false,
|
|
153
|
+
reason: "malformed"
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
ok: true,
|
|
157
|
+
value: operand
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function parseBoolExpr(src) {
|
|
161
|
+
const tokens = tokenize(src);
|
|
162
|
+
if (tokens.some((t) => t.kind === "invalid")) return {
|
|
163
|
+
ok: false,
|
|
164
|
+
reason: "unrecognized-token"
|
|
165
|
+
};
|
|
166
|
+
if (containsFunctionCall(tokens)) return {
|
|
167
|
+
ok: false,
|
|
168
|
+
reason: "function-call"
|
|
169
|
+
};
|
|
170
|
+
if (tokens.length !== 3 || tokens[1].kind !== "op") return {
|
|
171
|
+
ok: false,
|
|
172
|
+
reason: "malformed"
|
|
173
|
+
};
|
|
174
|
+
const left = tokenToOperand(tokens[0]);
|
|
175
|
+
const right = tokenToOperand(tokens[2]);
|
|
176
|
+
if (!left || !right) return {
|
|
177
|
+
ok: false,
|
|
178
|
+
reason: "malformed"
|
|
179
|
+
};
|
|
180
|
+
return {
|
|
181
|
+
ok: true,
|
|
182
|
+
value: {
|
|
183
|
+
op: tokens[1].value,
|
|
184
|
+
left,
|
|
185
|
+
right
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Literal-vs-literal type compatibility check for a bool expr (Primitive 2 /
|
|
191
|
+
* error code TYPE_MISMATCH). Refs (`tx.*`/state key) have no statically known
|
|
192
|
+
* type in this PoC (no external state schema is part of the DSL grammar), so
|
|
193
|
+
* we only flag mismatches we can prove from literal values themselves:
|
|
194
|
+
* - equality ops (==, !=) between literals of different kinds
|
|
195
|
+
* - ordering ops (<, <=, >, >=) applied to a boolean or string literal
|
|
196
|
+
*/
|
|
197
|
+
function boolExprTypeMismatch(expr) {
|
|
198
|
+
const isOrdering = expr.op === "<" || expr.op === "<=" || expr.op === ">" || expr.op === ">=";
|
|
199
|
+
const literalKind = (v) => v.kind === "number" || v.kind === "string" || v.kind === "boolean" ? v.kind : null;
|
|
200
|
+
const leftKind = literalKind(expr.left);
|
|
201
|
+
const rightKind = literalKind(expr.right);
|
|
202
|
+
if (isOrdering) {
|
|
203
|
+
if (leftKind === "boolean" || leftKind === "string") return true;
|
|
204
|
+
if (rightKind === "boolean" || rightKind === "string") return true;
|
|
205
|
+
}
|
|
206
|
+
if (leftKind && rightKind && leftKind !== rightKind) return true;
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* `counter.value` (and `rate.limit.sum`) must be `number | tx.field | state.field`
|
|
211
|
+
* (planning/35 Primitive 1: "value 只能是纯表达式(number | tx.field | state.field)").
|
|
212
|
+
* A string or boolean literal here is a TYPE_MISMATCH.
|
|
213
|
+
*/
|
|
214
|
+
function isValidCounterValueType(v) {
|
|
215
|
+
return v.kind === "number" || v.kind === "tx" || v.kind === "state";
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
//#endregion
|
|
219
|
+
exports.boolExprTypeMismatch = boolExprTypeMismatch;
|
|
220
|
+
exports.isValidCounterValueType = isValidCounterValueType;
|
|
221
|
+
exports.parseBoolExpr = parseBoolExpr;
|
|
222
|
+
exports.parseValueExpr = parseValueExpr;
|