soma-core 1.4.0 → 1.4.1
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/README.md +1 -1
- package/core/build/blocks/block-compile.ts +31 -2
- package/core/build/blocks/block-container.ts +2 -11
- package/core/build/compiler/page-declaration-compiler.ts +30 -2
- package/core/build/compiler/soma-precompile.js +2041 -19
- package/core/build/component/component-graph.ts +208 -0
- package/core/build/eject/component-eject.ts +473 -0
- package/core/build/eject/component-upgrade.ts +359 -0
- package/core/build/layout/constraint-solver.ts +235 -0
- package/core/build/layout/css-emitter.ts +116 -0
- package/core/build/layout/page-layout.ts +85 -0
- package/core/build/pack/pack-init.ts +265 -0
- package/core/client/base/SomaPageRootBase.ts +61 -0
- package/core/client/component/ai-catalog.ts +79 -4
- package/core/client/component/component-defaults.ts +100 -0
- package/core/client/component/component-definition.types.ts +39 -0
- package/core/client/component/component-manager.ts +79 -4
- package/core/client/component/component-pack.ts +126 -2
- package/core/client/component/keyboard-matrix.ts +144 -0
- package/core/client/component/pack-styles.ts +59 -1
- package/core/client/component/style-features.ts +131 -0
- package/core/client/component/theme-component-tokens.ts +83 -0
- package/core/client/data/data-object-resolver.ts +407 -4
- package/core/client/data/reaction-engine.ts +176 -0
- package/core/client/data/window-engine.ts +477 -0
- package/core/client/engine/event-engine.ts +98 -0
- package/core/client/engine/soma-bootstrap.ts +265 -5
- package/core/client/engine/step-runner.ts +134 -0
- package/core/client/parser/blocks/page-parser.ts +15 -1
- package/core/client/parser/blocks/use-parser.ts +5 -12
- package/core/client/parser/data-config-parser.ts +316 -8
- package/core/client/parser/duration.ts +16 -0
- package/core/client/parser/layout-parser.ts +157 -0
- package/core/client/parser/page-state-parser.ts +166 -0
- package/core/client/parser/pipeline-parser.ts +76 -1
- package/core/client/parser/reaction-parser.ts +459 -0
- package/core/client/parser/reaction-sugar.ts +47 -0
- package/core/client/parser/step-parser.ts +359 -0
- package/core/client/parser/ui-config-parser.ts +59 -0
- package/core/client/parser/window-parser.ts +294 -0
- package/core/client/theme/theme-engine.ts +102 -2
- package/core/index.ts +163 -0
- package/core/shared/block-errors.ts +11 -0
- package/core/shared/calc/coerce.ts +29 -0
- package/core/shared/calc/errors.ts +43 -0
- package/core/shared/calc/evaluator.ts +101 -0
- package/core/shared/calc/index.ts +12 -0
- package/core/shared/calc/lexer.ts +194 -0
- package/core/shared/calc/parser.ts +204 -0
- package/core/shared/calc/typer.ts +184 -0
- package/core/shared/path-algebra.ts +169 -0
- package/core/shared/predicates/index.ts +527 -0
- package/core/shared/types/component-contract.types.ts +128 -2
- package/core/shared/types/data-config.types.ts +120 -1
- package/core/shared/types/domain.types.ts +13 -0
- package/core/shared/types/event.types.ts +22 -2
- package/core/shared/types/layout-constraint.types.ts +73 -0
- package/core/shared/types/page-state.types.ts +60 -0
- package/core/shared/types/reaction.types.ts +144 -0
- package/core/shared/types/step.types.ts +91 -0
- package/core/shared/types/ui-config.types.ts +18 -0
- package/core/shared/types/window.types.ts +177 -0
- package/dist/cli/soma.js +13 -1
- package/dist/core/index.cjs +4684 -349
- package/dist/types/build/blocks/block-container.d.ts +1 -6
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -0
- package/dist/types/build/component/component-graph.d.ts +85 -0
- package/dist/types/build/eject/component-eject.d.ts +121 -0
- package/dist/types/build/eject/component-upgrade.d.ts +43 -0
- package/dist/types/build/layout/constraint-solver.d.ts +36 -0
- package/dist/types/build/layout/css-emitter.d.ts +18 -0
- package/dist/types/build/layout/page-layout.d.ts +24 -0
- package/dist/types/build/pack/pack-init.d.ts +8 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +28 -0
- package/dist/types/client/component/ai-catalog.d.ts +24 -1
- package/dist/types/client/component/component-defaults.d.ts +15 -0
- package/dist/types/client/component/component-definition.types.d.ts +38 -0
- package/dist/types/client/component/component-manager.d.ts +36 -0
- package/dist/types/client/component/component-pack.d.ts +32 -2
- package/dist/types/client/component/keyboard-matrix.d.ts +24 -0
- package/dist/types/client/component/pack-styles.d.ts +21 -0
- package/dist/types/client/component/style-features.d.ts +26 -0
- package/dist/types/client/component/theme-component-tokens.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +106 -1
- package/dist/types/client/data/reaction-engine.d.ts +21 -0
- package/dist/types/client/data/window-engine.d.ts +27 -0
- package/dist/types/client/engine/event-engine.d.ts +15 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +70 -0
- package/dist/types/client/engine/step-runner.d.ts +24 -0
- package/dist/types/client/parser/blocks/use-parser.d.ts +2 -2
- package/dist/types/client/parser/duration.d.ts +1 -0
- package/dist/types/client/parser/layout-parser.d.ts +9 -0
- package/dist/types/client/parser/page-state-parser.d.ts +32 -0
- package/dist/types/client/parser/reaction-parser.d.ts +16 -0
- package/dist/types/client/parser/reaction-sugar.d.ts +13 -0
- package/dist/types/client/parser/step-parser.d.ts +23 -0
- package/dist/types/client/parser/ui-config-parser.d.ts +5 -0
- package/dist/types/client/parser/window-parser.d.ts +25 -0
- package/dist/types/client/theme/theme-engine.d.ts +26 -0
- package/dist/types/index.d.ts +32 -2
- package/dist/types/shared/block-errors.d.ts +5 -0
- package/dist/types/shared/calc/coerce.d.ts +4 -0
- package/dist/types/shared/calc/errors.d.ts +14 -0
- package/dist/types/shared/calc/evaluator.d.ts +2 -0
- package/dist/types/shared/calc/index.d.ts +6 -0
- package/dist/types/shared/calc/lexer.d.ts +20 -0
- package/dist/types/shared/calc/parser.d.ts +49 -0
- package/dist/types/shared/calc/typer.d.ts +16 -0
- package/dist/types/shared/path-algebra.d.ts +56 -0
- package/dist/types/shared/predicates/index.d.ts +79 -0
- package/dist/types/shared/types/component-contract.types.d.ts +112 -0
- package/dist/types/shared/types/data-config.types.d.ts +115 -1
- package/dist/types/shared/types/domain.types.d.ts +12 -0
- package/dist/types/shared/types/event.types.d.ts +8 -2
- package/dist/types/shared/types/layout-constraint.types.d.ts +52 -0
- package/dist/types/shared/types/page-state.types.d.ts +49 -0
- package/dist/types/shared/types/reaction.types.d.ts +115 -0
- package/dist/types/shared/types/step.types.d.ts +76 -0
- package/dist/types/shared/types/ui-config.types.d.ts +18 -0
- package/dist/types/shared/types/window.types.d.ts +171 -0
- package/package.json +2 -1
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
// `steps:` 段解析(REQ-010 S4)——把一串交互编译成一张可终止、可画的图。
|
|
2
|
+
//
|
|
3
|
+
// 六步(《架构设计》§4.2):管道 → 条件 → 输出槽 → 控制流 → 可终止性 → 出图。
|
|
4
|
+
//
|
|
5
|
+
// 为什么可终止性要在**声明期**判:一条不终止的编排在运行期的表现是页面卡死,
|
|
6
|
+
// 而卡死的现场什么都留不下。所以这里干脆不给出不终止的写法——
|
|
7
|
+
// 没有 `while`,`goto` 只许向后,`for_each` 的 `over` 必须有界且嵌套不超过两层。
|
|
8
|
+
// 这三条加起来,步骤图是一个有限的 DAG,走完它的步数有上界。
|
|
9
|
+
|
|
10
|
+
import { collectPredicateFields, validatePredicate } from '../../shared/predicates';
|
|
11
|
+
import type { ParsedEvent } from '../../shared/types/event.types';
|
|
12
|
+
import type {
|
|
13
|
+
SomaCompiledStep,
|
|
14
|
+
SomaStepGraph,
|
|
15
|
+
SomaStepOnError,
|
|
16
|
+
SomaStepSlot,
|
|
17
|
+
} from '../../shared/types/step.types';
|
|
18
|
+
|
|
19
|
+
export interface StepParseInput {
|
|
20
|
+
raw: unknown;
|
|
21
|
+
/** 事件载荷里的字段名:`when` 的作用域起点 */
|
|
22
|
+
payloadFields: readonly string[];
|
|
23
|
+
/** bootstrap 注册的具名动作;给了就校验 `do: ACTION | <name>` 的名字存在 */
|
|
24
|
+
actionNames?: readonly string[];
|
|
25
|
+
path: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface StepParseDeps {
|
|
29
|
+
fail(message: string, path: string): never;
|
|
30
|
+
/**
|
|
31
|
+
* 单条管道的解析器(`pipeline-parser` 的 `parsePipelineBody`)。
|
|
32
|
+
*
|
|
33
|
+
* 由调用方注入而不是直接 import,是为了不成环:`pipeline-parser` 的
|
|
34
|
+
* `parseEventConfig` 要认 `{ steps: [...] }` 形态,于是它得调本模块;
|
|
35
|
+
* 本模块再反向 import 它就构成循环,而循环 import 在 ESM 下"多数时候能跑"
|
|
36
|
+
* ——它坏起来是模块初始化顺序变了之后的一个 undefined,跟本次改动毫无关联。
|
|
37
|
+
*/
|
|
38
|
+
parsePipeline(expression: string): Omit<ParsedEvent, 'somaId' | 'domEvent'>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** `for_each` 最多两层。三层嵌套的遍历在声明层几乎肯定是数据结构没设计对 */
|
|
42
|
+
const MAX_FOR_EACH_DEPTH = 2;
|
|
43
|
+
|
|
44
|
+
const STEP_KEYS = new Set(['when', 'do', 'output', 'on_error', 'switch', 'parallel', 'for_each']);
|
|
45
|
+
const SLOT_NAME_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
46
|
+
|
|
47
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
48
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 返回数组的动作:`for_each.over` 只认这种槽 */
|
|
52
|
+
function producesArray(pipelineType: string): boolean {
|
|
53
|
+
// 只有 Host 函数(UI_REFRESH / NAV_ACTION / ACTION)能返回数组;
|
|
54
|
+
// 导航与 UI 交互返回的是「做完了」,不是数据。
|
|
55
|
+
return pipelineType === 'UI_REFRESH' || pipelineType === 'NAV_ACTION' || pipelineType === 'ACTION';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function parseSteps(input: StepParseInput, deps: StepParseDeps): SomaStepGraph {
|
|
59
|
+
const { raw, payloadFields, path } = input;
|
|
60
|
+
// 显式标注:解构出来的方法不带控制流断言,收窄会整片失效
|
|
61
|
+
const fail: (message: string, at: string) => never = deps.fail;
|
|
62
|
+
|
|
63
|
+
if (!Array.isArray(raw) || raw.length === 0) {
|
|
64
|
+
fail('steps 必须是非空数组', path);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const slots: Record<string, SomaStepSlot> = {};
|
|
68
|
+
const edges: Array<[number, number]> = [];
|
|
69
|
+
let counter = 0;
|
|
70
|
+
|
|
71
|
+
/** `when` 的作用域:事件载荷字段 ∪ 已声明的输出槽(槽以 `名.子键` 的形态被引用) */
|
|
72
|
+
const scopeKeys = (): string[] => [...payloadFields, ...Object.keys(slots)];
|
|
73
|
+
|
|
74
|
+
const checkWhen = (when: unknown, at: string): void => {
|
|
75
|
+
if (when === undefined || when === null) return;
|
|
76
|
+
|
|
77
|
+
// 根引用先查,再走通用谓词校验。次序要紧:通用校验对"字段不在作用域里"只会说
|
|
78
|
+
// 「引用了不存在的字段」,而在编排里这句话答不了作者真正的问题——
|
|
79
|
+
// 到底是载荷里没有这个字段,还是槽声明在后面。先查根,才能把这两种情况分开说。
|
|
80
|
+
//
|
|
81
|
+
// 引用只许向后:槽必须**先于**引用它的步骤声明。少了这条,一个引用后面步骤输出的
|
|
82
|
+
// 条件会永远读到 undefined 并静默判假——那条分支于是永远不走,而且看不出来。
|
|
83
|
+
for (const field of collectPredicateFields(when as Parameters<typeof collectPredicateFields>[0])) {
|
|
84
|
+
const root = field.split('.')[0];
|
|
85
|
+
if (!payloadFields.includes(root) && slots[root] === undefined) {
|
|
86
|
+
fail(
|
|
87
|
+
`${at} 引用了 "${root}",但它既不是事件载荷字段`
|
|
88
|
+
+ `(${payloadFields.join(' / ') || '无'}),也不是先于本步声明的输出槽`
|
|
89
|
+
+ `(${Object.keys(slots).join(' / ') || '无'})`,
|
|
90
|
+
at,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const issues = validatePredicate(when, scopeKeys(), at, {});
|
|
96
|
+
if (issues.length > 0) fail(issues.join(';'), at);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const declareSlot = (name: unknown, at: string, index: number, pipelineType: string): string => {
|
|
100
|
+
const slot = String(name ?? '');
|
|
101
|
+
if (!SLOT_NAME_PATTERN.test(slot)) {
|
|
102
|
+
fail(`${at}.output "${slot}" 不是合法槽名(小写字母开头、下划线分隔)`, `${at}.output`);
|
|
103
|
+
}
|
|
104
|
+
if (slots[slot] !== undefined) {
|
|
105
|
+
fail(
|
|
106
|
+
`${at}.output 槽名 "${slot}" 与第 ${slots[slot].declaredAt + 1} 步重名`
|
|
107
|
+
+ '——同名槽的后写会覆盖前写,而引用它的条件读到哪一个取决于步骤次序',
|
|
108
|
+
`${at}.output`,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
slots[slot] = { declaredAt: index, producedBy: pipelineType, bounded: producesArray(pipelineType) };
|
|
112
|
+
return slot;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const parsePipe = (expression: unknown, at: string): Omit<ParsedEvent, 'somaId' | 'domEvent'> => {
|
|
116
|
+
if (typeof expression !== 'string' || expression.trim() === '') {
|
|
117
|
+
fail(`${at}.do 必须是管道表达式字符串`, `${at}.do`);
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
return deps.parsePipeline(expression);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
// 管道自己的报错already说清了问题("未知事件类型 X"),原样转出去,
|
|
123
|
+
// 只补上是哪一步——包一层自己的话,作者会看到两句话说同一件事。
|
|
124
|
+
fail(error instanceof Error ? error.message : String(error), `${at}.do`);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const parseOnError = (value: unknown, at: string, total: number, index: number): SomaStepOnError => {
|
|
129
|
+
if (value === undefined) return 'stop';
|
|
130
|
+
const raw2 = String(value);
|
|
131
|
+
if (raw2 === 'stop' || raw2 === 'continue') return raw2;
|
|
132
|
+
const match = /^goto:(\d+)$/.exec(raw2);
|
|
133
|
+
if (!match) {
|
|
134
|
+
fail(`${at}.on_error 只能是 stop / continue / goto:<步骤序号>(收到 "${raw2}")`, `${at}.on_error`);
|
|
135
|
+
}
|
|
136
|
+
const target = Number(match[1]);
|
|
137
|
+
// 只许向后跳。向前跳就能构造循环,而一条不终止的编排在运行期的表现是页面卡死,
|
|
138
|
+
// 现场什么都留不下。这是 AO-1 的核心一条。
|
|
139
|
+
if (target <= index + 1) {
|
|
140
|
+
fail(
|
|
141
|
+
`${at}.on_error 的 goto:${target} 指向第 ${target} 步,不在本步(第 ${index + 1} 步)之后`
|
|
142
|
+
+ '——只许向后跳,否则编排可以不终止',
|
|
143
|
+
`${at}.on_error`,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
if (target > total) {
|
|
147
|
+
fail(`${at}.on_error 的 goto:${target} 越过了末步(共 ${total} 步)`, `${at}.on_error`);
|
|
148
|
+
}
|
|
149
|
+
return `goto:${target}`;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const walk = (
|
|
153
|
+
list: unknown[],
|
|
154
|
+
at: string,
|
|
155
|
+
depth: number,
|
|
156
|
+
inParallel: boolean,
|
|
157
|
+
): SomaCompiledStep[] => {
|
|
158
|
+
const out: SomaCompiledStep[] = [];
|
|
159
|
+
/** parallel 分支内声明的槽:分支之间不许互引(AO-2) */
|
|
160
|
+
const parallelLocalSlots: string[] = [];
|
|
161
|
+
|
|
162
|
+
list.forEach((entry, position) => {
|
|
163
|
+
const stepAt = `${at}[${position}]`;
|
|
164
|
+
if (!isPlainObject(entry)) fail(`${stepAt} 必须是映射`, stepAt);
|
|
165
|
+
for (const key of Object.keys(entry)) {
|
|
166
|
+
if (!STEP_KEYS.has(key)) {
|
|
167
|
+
fail(`${stepAt} 有未知键 "${key}"(支持: ${[...STEP_KEYS].join(' / ')})`, `${stepAt}.${key}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const index = counter;
|
|
171
|
+
counter += 1;
|
|
172
|
+
if (out.length > 0) edges.push([out[out.length - 1].index, index]);
|
|
173
|
+
|
|
174
|
+
// ── parallel ────────────────────────────────────────────────
|
|
175
|
+
if (entry.parallel !== undefined) {
|
|
176
|
+
if (!Array.isArray(entry.parallel) || entry.parallel.length < 2) {
|
|
177
|
+
fail(`${stepAt}.parallel 至少要两个分支(一个分支的并行就是顺序)`, `${stepAt}.parallel`);
|
|
178
|
+
}
|
|
179
|
+
const before = new Set(Object.keys(slots));
|
|
180
|
+
const children = walk(entry.parallel as unknown[], `${stepAt}.parallel`, depth, true);
|
|
181
|
+
const declared = Object.keys(slots).filter((name) => !before.has(name));
|
|
182
|
+
parallelLocalSlots.push(...declared);
|
|
183
|
+
out.push({ index, kind: 'parallel', onError: 'stop', children });
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ── for_each ────────────────────────────────────────────────
|
|
188
|
+
if (entry.for_each !== undefined) {
|
|
189
|
+
if (depth + 1 > MAX_FOR_EACH_DEPTH) {
|
|
190
|
+
fail(
|
|
191
|
+
`${stepAt}.for_each 嵌套超过 ${MAX_FOR_EACH_DEPTH} 层`
|
|
192
|
+
+ '——三层遍历在声明层几乎肯定是数据结构没设计对',
|
|
193
|
+
`${stepAt}.for_each`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
const box = entry.for_each;
|
|
197
|
+
if (!isPlainObject(box)) fail(`${stepAt}.for_each 必须是 { over, as, steps }`, `${stepAt}.for_each`);
|
|
198
|
+
const over = String(box.over ?? '');
|
|
199
|
+
const as = String(box.as ?? '');
|
|
200
|
+
// `over` 必须有界:遍历一个来路不明的东西,步数就没有上界了
|
|
201
|
+
const isPayload = payloadFields.includes(over);
|
|
202
|
+
const slot = slots[over];
|
|
203
|
+
if (!isPayload && slot === undefined) {
|
|
204
|
+
fail(
|
|
205
|
+
`${stepAt}.for_each.over "${over}" 既不是事件载荷字段,也不是先于本步声明的输出槽`,
|
|
206
|
+
`${stepAt}.for_each.over`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
if (slot !== undefined && !slot.bounded) {
|
|
210
|
+
fail(
|
|
211
|
+
`${stepAt}.for_each.over "${over}" 来自 ${slot.producedBy},它不返回数组`
|
|
212
|
+
+ '——遍历一个不是数组的东西,步数就没有上界了',
|
|
213
|
+
`${stepAt}.for_each.over`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
if (!SLOT_NAME_PATTERN.test(as)) {
|
|
217
|
+
fail(`${stepAt}.for_each.as "${as}" 不是合法项名`, `${stepAt}.for_each.as`);
|
|
218
|
+
}
|
|
219
|
+
if (!Array.isArray(box.steps) || box.steps.length === 0) {
|
|
220
|
+
fail(`${stepAt}.for_each.steps 必须是非空数组`, `${stepAt}.for_each.steps`);
|
|
221
|
+
}
|
|
222
|
+
// 项名在体内可见:登记成一个有界槽,体内的 when 才能引用它
|
|
223
|
+
slots[as] = { declaredAt: index, producedBy: 'for_each', bounded: false };
|
|
224
|
+
const children = walk(box.steps as unknown[], `${stepAt}.for_each.steps`, depth + 1, inParallel);
|
|
225
|
+
delete slots[as];
|
|
226
|
+
out.push({ index, kind: 'for_each', onError: 'stop', over, as, children });
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ── switch ──────────────────────────────────────────────────
|
|
231
|
+
if (entry.switch !== undefined) {
|
|
232
|
+
if (!Array.isArray(entry.switch) || entry.switch.length === 0) {
|
|
233
|
+
fail(`${stepAt}.switch 必须是非空分支数组`, `${stepAt}.switch`);
|
|
234
|
+
}
|
|
235
|
+
checkWhen(entry.when, `${stepAt}.when`);
|
|
236
|
+
const list2 = entry.switch as unknown[];
|
|
237
|
+
const branches: NonNullable<SomaCompiledStep['branches']> = [];
|
|
238
|
+
|
|
239
|
+
// 两条规则的判定次序要紧:先数个数、再看位置。反过来的话,两个 otherwise
|
|
240
|
+
// 的写法会先撞上「必须在末尾」,于是「只能有一个」那条永远走不到——
|
|
241
|
+
// 一条永远走不到的校验分支和没写是一样的。
|
|
242
|
+
const otherwiseAt = list2
|
|
243
|
+
.map((branch, position) => (isPlainObject(branch) && branch.otherwise === true ? position : -1))
|
|
244
|
+
.filter((position) => position !== -1);
|
|
245
|
+
if (otherwiseAt.length > 1) {
|
|
246
|
+
fail(
|
|
247
|
+
`${stepAt}.switch 有 ${otherwiseAt.length} 个 otherwise 分支(第 ${otherwiseAt.map((n) => n + 1).join(' / ')} 个)`
|
|
248
|
+
+ '——兜底只能有一个',
|
|
249
|
+
`${stepAt}.switch`,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
if (otherwiseAt.length === 1 && otherwiseAt[0] !== list2.length - 1) {
|
|
253
|
+
fail(
|
|
254
|
+
`${stepAt}.switch[${otherwiseAt[0]}] 的 otherwise 必须是最后一个分支`
|
|
255
|
+
+ '(它是兜底,后面的分支永远走不到)',
|
|
256
|
+
`${stepAt}.switch[${otherwiseAt[0]}]`,
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
list2.forEach((branchRaw, branchIndex) => {
|
|
261
|
+
const branchAt = `${stepAt}.switch[${branchIndex}]`;
|
|
262
|
+
if (!isPlainObject(branchRaw)) fail(`${branchAt} 必须是映射`, branchAt);
|
|
263
|
+
const otherwise = branchRaw.otherwise === true;
|
|
264
|
+
if (!otherwise && branchRaw.when === undefined) {
|
|
265
|
+
fail(`${branchAt} 既没有 when 也不是 otherwise——它会无条件命中并吃掉后面的分支`, branchAt);
|
|
266
|
+
}
|
|
267
|
+
if (!otherwise) checkWhen(branchRaw.when, `${branchAt}.when`);
|
|
268
|
+
const pipeline = parsePipe(branchRaw.do, branchAt);
|
|
269
|
+
const output = branchRaw.output === undefined
|
|
270
|
+
? undefined
|
|
271
|
+
: declareSlot(branchRaw.output, branchAt, index, pipeline.type);
|
|
272
|
+
branches.push({
|
|
273
|
+
...(otherwise ? {} : { when: branchRaw.when as never }),
|
|
274
|
+
otherwise,
|
|
275
|
+
pipeline,
|
|
276
|
+
...(output === undefined ? {} : { output }),
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
out.push({ index, kind: 'switch', when: entry.when as never, onError: 'stop', branches });
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ── do ──────────────────────────────────────────────────────
|
|
284
|
+
checkWhen(entry.when, `${stepAt}.when`);
|
|
285
|
+
const pipeline = parsePipe(entry.do, stepAt);
|
|
286
|
+
const onError = parseOnError(entry.on_error, stepAt, list.length, position);
|
|
287
|
+
if (onError.startsWith('goto:')) {
|
|
288
|
+
edges.push([index, index + (Number(onError.slice(5)) - position - 1)]);
|
|
289
|
+
}
|
|
290
|
+
const output = entry.output === undefined
|
|
291
|
+
? undefined
|
|
292
|
+
: declareSlot(entry.output, stepAt, index, pipeline.type);
|
|
293
|
+
if (inParallel && output !== undefined) parallelLocalSlots.push(output);
|
|
294
|
+
out.push({
|
|
295
|
+
index,
|
|
296
|
+
kind: 'do',
|
|
297
|
+
...(entry.when === undefined ? {} : { when: entry.when as never }),
|
|
298
|
+
pipeline,
|
|
299
|
+
onError,
|
|
300
|
+
...(output === undefined ? {} : { output }),
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// 并行分支之间不许互引槽(AO-2):互引就意味着有顺序,那它就不是并行。
|
|
305
|
+
// 检法是「本层某个分支的 when 引用了本层另一个分支声明的槽」——
|
|
306
|
+
// 这在顺序执行下能跑通,一旦真的并行起来就变成竞态,而竞态只在压力下暴露。
|
|
307
|
+
if (inParallel && parallelLocalSlots.length > 0) {
|
|
308
|
+
out.forEach((step, position) => {
|
|
309
|
+
const refs = step.when === undefined ? [] : collectPredicateFields(step.when);
|
|
310
|
+
for (const ref of refs) {
|
|
311
|
+
const root = ref.split('.')[0];
|
|
312
|
+
if (!parallelLocalSlots.includes(root)) continue;
|
|
313
|
+
const owner = out.findIndex((other) => other.output === root);
|
|
314
|
+
if (owner !== -1 && owner !== position) {
|
|
315
|
+
fail(
|
|
316
|
+
`${at}[${position}] 的 when 引用了同一个 parallel 里另一个分支的输出槽 "${root}"`
|
|
317
|
+
+ '——并行分支之间不许互引:互引就意味着有顺序,那它就不该写成 parallel',
|
|
318
|
+
`${at}[${position}].when`,
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return out;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const steps = walk(raw as unknown[], path, 0, false);
|
|
329
|
+
|
|
330
|
+
// 具名动作全部可解析(AO-3 的声明期一半;bootstrap 期还会再查一次注册表)
|
|
331
|
+
if (input.actionNames !== undefined) {
|
|
332
|
+
const known = new Set(input.actionNames);
|
|
333
|
+
const visit = (list: SomaCompiledStep[]): void => {
|
|
334
|
+
for (const step of list) {
|
|
335
|
+
const pipes = [
|
|
336
|
+
...(step.pipeline ? [step.pipeline] : []),
|
|
337
|
+
...(step.branches ?? []).map((branch) => branch.pipeline),
|
|
338
|
+
];
|
|
339
|
+
for (const pipe of pipes) {
|
|
340
|
+
if (pipe.type !== 'ACTION') continue;
|
|
341
|
+
const name = String(pipe.funcPath ?? '');
|
|
342
|
+
if (!known.has(name)) {
|
|
343
|
+
fail(
|
|
344
|
+
`do: ACTION | ${name} —— 未注册的具名动作`
|
|
345
|
+
+ `(已注册:${[...known].join(' / ') || '无'})。`
|
|
346
|
+
+ '具名动作在 bootstrap 的 options.actions 里登记,这是 custom(script) 的替代:'
|
|
347
|
+
+ '实现在 Host 代码里、有名字、可审计。',
|
|
348
|
+
path,
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (step.children) visit(step.children);
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
visit(steps);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return { steps, slots, edges };
|
|
359
|
+
}
|
|
@@ -13,6 +13,8 @@ import type {
|
|
|
13
13
|
import type { OnChangeRule } from '../../shared/types/watcher.types';
|
|
14
14
|
import type { SomaAdaptiveVocabulary } from '../../shared/types/adaptive.types';
|
|
15
15
|
import { parseAdaptiveConfig } from './adaptive-parser';
|
|
16
|
+
import { parsePageState } from './page-state-parser';
|
|
17
|
+
import { parseLayoutConstraint } from './layout-parser';
|
|
16
18
|
|
|
17
19
|
// ─── 错误类 ───────────────────────────────────────────────────────────────────
|
|
18
20
|
|
|
@@ -49,6 +51,11 @@ export interface SomaUiConfigParseOptions {
|
|
|
49
51
|
* 所以推迟的是报错时机,不是报错本身。
|
|
50
52
|
*/
|
|
51
53
|
adaptiveVocabularyRequired?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* 已声明的数据对象名(`state.window` 切片只能指向它们)。
|
|
56
|
+
* 不给时跳过这一层校验——见 `state` 分支里的说明。
|
|
57
|
+
*/
|
|
58
|
+
dataObjectNames?: readonly string[];
|
|
52
59
|
}
|
|
53
60
|
|
|
54
61
|
// ─── 内部辅助函数 ─────────────────────────────────────────────────────────────
|
|
@@ -429,9 +436,61 @@ export function parseUiConfig(raw: any, options?: SomaUiConfigParseOptions): UiC
|
|
|
429
436
|
});
|
|
430
437
|
}
|
|
431
438
|
|
|
439
|
+
// state 同为可选保留键(REQ-010 F-37),同一条纪律:缺席时连字段都不写。
|
|
440
|
+
//
|
|
441
|
+
// 数据对象名在这里还不可知(`_data.yaml` 是另一份资产,解析次序上不保证先于它),
|
|
442
|
+
// 所以窗口名的校验交给调用方在两份资产都在手时再做一次——这里只校验形状与版本。
|
|
443
|
+
// 宁可分两处查,也不要在这里拿一个空名单去判,那会把所有合法声明都判成越界。
|
|
444
|
+
if ('state' in raw && raw.state !== undefined) {
|
|
445
|
+
config.state = parsePageState(
|
|
446
|
+
{ raw: raw.state, objectNames: options?.dataObjectNames ?? [], path: 'state' },
|
|
447
|
+
{
|
|
448
|
+
fail(message: string): never {
|
|
449
|
+
throw new SomaUiConfigError(message);
|
|
450
|
+
},
|
|
451
|
+
},
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// layout(REQ-011):锚点 id → 约束。这里只校验**声明**(词汇闭集、单位、优先级档位),
|
|
456
|
+
// 不求解——求解器住在 core/build/,运行时永远不该算布局(CL-4)。
|
|
457
|
+
// 锚点是否真的存在于 HTML 由构建期编译器对着锚点索引判;这里拿不到 HTML。
|
|
458
|
+
if ('layout' in raw && raw.layout !== undefined) {
|
|
459
|
+
config.layout = parseLayoutSection(raw.layout);
|
|
460
|
+
}
|
|
461
|
+
// layout_css:构建期求解产物。它是字符串就照收;运行时只负责把它注入 <style>。
|
|
462
|
+
if ('layout_css' in raw && raw.layout_css !== undefined) {
|
|
463
|
+
if (typeof raw.layout_css !== 'string') {
|
|
464
|
+
throw new SomaUiConfigError('layout_css 必须是字符串(构建期求解产物,不手写)', 'layout_css');
|
|
465
|
+
}
|
|
466
|
+
config.layoutCss = raw.layout_css;
|
|
467
|
+
}
|
|
468
|
+
|
|
432
469
|
return config;
|
|
433
470
|
}
|
|
434
471
|
|
|
472
|
+
/** `layout:` 段:`{ <锚点id>: <约束> }`;锚点名必须是合法 somaId */
|
|
473
|
+
function parseLayoutSection(raw: unknown): NonNullable<UiConfig['layout']> {
|
|
474
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
475
|
+
throw new SomaUiConfigError('layout 必须是映射:{ <锚点id>: { gap, wrap, <子项>: { sizing, … } } }', 'layout');
|
|
476
|
+
}
|
|
477
|
+
const out: NonNullable<UiConfig['layout']> = {};
|
|
478
|
+
for (const [anchor, spec] of Object.entries(raw as Record<string, unknown>)) {
|
|
479
|
+
if (!/^[A-Za-z_][A-Za-z0-9_-]*$/.test(anchor)) {
|
|
480
|
+
throw new SomaUiConfigError(`layout 的键必须是锚点 id(data-soma-id),收到 "${anchor}"`, `layout.${anchor}`);
|
|
481
|
+
}
|
|
482
|
+
out[anchor] = parseLayoutConstraint(
|
|
483
|
+
{ raw: spec, path: `layout.${anchor}` },
|
|
484
|
+
{
|
|
485
|
+
fail(message: string, at: string): never {
|
|
486
|
+
throw new SomaUiConfigError(message, at);
|
|
487
|
+
},
|
|
488
|
+
},
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
return out;
|
|
492
|
+
}
|
|
493
|
+
|
|
435
494
|
/**
|
|
436
495
|
* 解析 show 键中的 somaId@attr 语法
|
|
437
496
|
*
|