soma-core 2.2.5 → 2.2.7
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 +5 -6
- package/bin/soma.cjs +2 -2
- package/core/build/compiler/flat-page.ts +16 -0
- package/core/build/compiler/full-manifest-builder.ts +0 -3
- package/core/build/compiler/page-assets.ts +0 -1
- package/core/build/compiler/page-closure.ts +245 -0
- package/core/build/compiler/page-declaration-compiler.ts +2 -5
- package/core/build/compiler/soma-precompile.js +1972 -3312
- package/core/build/compiler/soma-precompile.ts +4 -7
- package/core/build/component-tree/artifact-codec.ts +4 -8
- package/core/build/lsp/soma-declaration.cjs +1 -1
- package/core/build/lsp/std-completion.generated.json +3 -3
- package/core/client/a11y/a11y-config.ts +2 -1
- package/core/client/a11y/dom-id.ts +1 -1
- package/core/client/base/SomaHttpClient.ts +11 -3
- package/core/client/base/SomaPageRootBase.ts +6 -0
- package/core/client/base/defineSomaView.ts +178 -0
- package/core/client/base/index.ts +13 -0
- package/core/client/base/view-context.ts +328 -0
- package/core/client/behavior/adapter-collection.ts +2 -2
- package/core/client/compiler/manifest-compiler.ts +5 -36
- package/core/client/component/component-definition.types.ts +2 -1
- package/core/client/component/component-manager.ts +110 -20
- package/core/client/component/structure-adapter.ts +3 -7
- package/core/client/container/IContainerAdapter.ts +4 -0
- package/core/client/container/SomaContainerAdapter.ts +52 -3
- package/core/client/data/data-object-resolver.ts +523 -184
- package/core/client/data/data-write-actions.ts +79 -15
- package/core/client/data/format-config.ts +3 -1
- package/core/client/data/region-scope-port.ts +11 -0
- package/core/client/engine/asset-resolver.ts +2 -5
- package/core/client/engine/dom-renderer.ts +7 -40
- package/core/client/engine/event-engine.ts +29 -108
- package/core/client/engine/property-watcher.ts +1 -1
- package/core/client/engine/reactive-proxy.ts +78 -17
- package/core/client/engine/runtime-builder.ts +0 -14
- package/core/client/engine/scope-resolver.ts +18 -0
- package/core/client/engine/soma-bootstrap.ts +387 -99
- package/core/client/engine/style-binder.ts +49 -106
- package/core/client/engine/timer-engine.ts +11 -1
- package/core/client/host-bridge/load-host-domain-class.ts +2 -1
- package/core/client/infra/http/SomaHttpService.ts +15 -4
- package/core/client/infra/i18n/pack-locales.ts +2 -2
- package/core/client/infra/notify/DefaultNotifyService.ts +2 -2
- package/core/client/mount/domain-rule.ts +7 -3
- package/core/client/mount/mount-page.ts +38 -5
- package/core/client/mount/types.ts +2 -0
- package/core/client/parser/confirm-spec.ts +100 -7
- package/core/client/parser/data-config-parser.ts +28 -211
- package/core/client/parser/pipeline-parser.ts +16 -60
- package/core/client/parser/style-config-parser.ts +101 -95
- package/core/client/parser/timer-parser.ts +21 -0
- package/core/client/parser/ui-config-parser.ts +64 -12
- package/core/client/registry/declaration-renderer.ts +1 -1
- package/core/client/render-ports/hide-transition.ts +9 -0
- package/core/client/render-ports/index.ts +4 -0
- package/core/client/render-ports/notify-renderer.ts +2 -1
- package/core/client/runtime/SomaRuntimeServices.ts +2 -1
- package/core/client/runtime/SomaSnapshotStore.ts +5 -0
- package/core/client/security/url-guard.ts +71 -11
- package/core/client/validation/async-check-registry.ts +2 -1
- package/core/client/validation/contract-validator.ts +2 -6
- package/core/client/validation/field-rules.ts +3 -13
- package/core/client/validation/object-rules.ts +2 -10
- package/core/index.ts +17 -39
- package/core/inspect/check-product.ts +5 -5
- package/core/inspect/runtime.ts +2 -1
- package/core/server/api/dispatch-service-api.ts +35 -36
- package/core/server/api/load-host-service.ts +8 -4
- package/core/server/api/paged-list.ts +259 -0
- package/core/server/compile-port.ts +1 -1
- package/core/server/host/app-config-composer.ts +2 -1
- package/core/server/host/empty-host-bootstrap.mjs +3 -0
- package/core/server/host/host-assembly-entry.ts +1 -0
- package/core/server/host/host-assembly.ts +50 -3
- package/core/server/host/host-caller.ts +2 -1
- package/core/server/host/host-dir.ts +9 -0
- package/core/server/host/host-response-headers.ts +2 -1
- package/core/server/host/invoke-host-request-hook.ts +28 -4
- package/core/server/host/published-page-assets.ts +2 -1
- package/core/server/host/resolve-host-request-hook.ts +25 -7
- package/core/server/host/server-page-assets.ts +4 -4
- package/core/server/host/source-aliases.cjs +345 -0
- package/core/server/host/source-aliases.ts +32 -0
- package/core/server/host/source-packages.cjs +1 -0
- package/core/server/host/virtual-pages.ts +3 -2
- package/core/server/infra/cache/ServerFileCache.ts +83 -10
- package/core/server/node.ts +10 -0
- package/core/server/registry/api-registry-store.ts +3 -1
- package/core/server/registry/declaration-precompiler.ts +0 -2
- package/core/server/registry/page-registry-repository.ts +2 -1
- package/core/server/registry/page-registry-store.ts +2 -1
- package/core/server/registry/registry-error.ts +6 -1
- package/core/server/registry/registry-file-lock.ts +296 -23
- package/core/server/registry/request-body-limit.ts +3 -2
- package/core/server/registry/validation/declaration-safety-scan.ts +151 -27
- package/core/server/runtime-space/access-gate.ts +21 -7
- package/core/server/runtime-space/bindings.ts +3 -6
- package/core/server/runtime-space/dynamic-pages.ts +181 -49
- package/core/server/runtime-space/errors.ts +3 -0
- package/core/server/runtime-space/inject.ts +78 -16
- package/core/server/runtime-space/page-grants.ts +7 -1
- package/core/server/runtime-space/resolve.ts +66 -19
- package/core/server/runtime-space/store.ts +32 -3
- package/core/server/runtime-space/submit-steps.ts +2 -1
- package/core/server/runtime-space/types.ts +3 -1
- package/core/shared/component-tree-errors.ts +2 -1
- package/core/shared/contract-a11y.ts +1 -1
- package/core/shared/contract-sections.ts +2 -2
- package/core/shared/core-diagnostic-codes.ts +14 -0
- package/core/shared/data-declaration-checks.ts +29 -3
- package/core/shared/declaration-keys.ts +1 -1
- package/core/shared/engines/engine-error.ts +2 -1
- package/core/shared/generation-error.ts +2 -1
- package/core/shared/page-envelope.ts +1 -1
- package/core/shared/pure-binding.ts +88 -0
- package/core/shared/registry-generated/diagnostic-registry-index.ts +21 -19
- package/core/shared/registry-generated/diagnostic-registry.ts +170 -177
- package/core/shared/registry-generated/limits.ts +65 -13
- package/core/shared/registry-generated/std-grammar-completion.ts +0 -4
- package/core/shared/registry-generated/std-grammar-views.ts +40 -10
- package/core/shared/registry-generated/std-grammar.ts +43 -21
- package/core/shared/soma-error.ts +36 -0
- package/core/shared/types/compile-port.types.ts +1 -1
- package/core/shared/types/data-config.types.ts +20 -18
- package/core/shared/types/engine.types.ts +0 -3
- package/core/shared/types/event.types.ts +10 -11
- package/core/shared/types/generation-error.types.ts +5 -1
- package/core/shared/types/host-assembly.types.ts +7 -1
- package/core/shared/types/manifest.types.ts +0 -3
- package/core/shared/types/region-scope.types.ts +5 -1
- package/core/shared/types/scope-manifest.types.ts +0 -2
- package/core/shared/types/style-config.types.ts +21 -21
- package/core/shared/types/ui-config.types.ts +4 -5
- package/core/shared/types/watcher.types.ts +1 -1
- package/dist/browser/runtime-bundle.manifest.json +11 -13
- package/dist/browser/soma-runtime.js +2733 -2712
- package/dist/core/index.cjs +5665 -5534
- package/dist/host-native/aot-builder.cjs +330 -1
- package/dist/host-native/native-server.cjs +5434 -4726
- package/dist/lsp/component-tree.cjs +5868 -7040
- package/dist/types/build/compiler/full-manifest-builder.d.ts +0 -2
- package/dist/types/build/compiler/page-closure.d.ts +21 -0
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -4
- package/dist/types/build/compiler/soma-precompile.d.ts +1 -3
- package/dist/types/build/component-tree/artifact-codec.d.ts +0 -2
- package/dist/types/client/a11y/a11y-config.d.ts +2 -1
- package/dist/types/client/base/SomaHttpClient.d.ts +1 -1
- package/dist/types/client/base/SomaPageRootBase.d.ts +5 -0
- package/dist/types/client/base/defineSomaView.d.ts +46 -0
- package/dist/types/client/base/index.d.ts +2 -0
- package/dist/types/client/base/view-context.d.ts +136 -0
- package/dist/types/client/behavior/adapter-collection.d.ts +1 -1
- package/dist/types/client/compiler/manifest-compiler.d.ts +0 -3
- package/dist/types/client/component/component-definition.types.d.ts +2 -1
- package/dist/types/client/component/component-manager.d.ts +29 -0
- package/dist/types/client/container/IContainerAdapter.d.ts +3 -0
- package/dist/types/client/container/SomaContainerAdapter.d.ts +5 -0
- package/dist/types/client/data/data-object-resolver.d.ts +93 -31
- package/dist/types/client/data/format-config.d.ts +2 -1
- package/dist/types/client/engine/dom-renderer.d.ts +1 -2
- package/dist/types/client/engine/event-engine.d.ts +0 -7
- package/dist/types/client/engine/reactive-proxy.d.ts +11 -4
- package/dist/types/client/engine/runtime-builder.d.ts +0 -4
- package/dist/types/client/engine/soma-bootstrap.d.ts +44 -6
- package/dist/types/client/engine/style-binder.d.ts +8 -4
- package/dist/types/client/host-bridge/load-host-domain-class.d.ts +2 -1
- package/dist/types/client/mount/domain-rule.d.ts +2 -0
- package/dist/types/client/mount/mount-page.d.ts +2 -1
- package/dist/types/client/mount/types.d.ts +2 -0
- package/dist/types/client/parser/confirm-spec.d.ts +27 -3
- package/dist/types/client/parser/data-config-parser.d.ts +6 -5
- package/dist/types/client/parser/style-config-parser.d.ts +7 -3
- package/dist/types/client/parser/timer-parser.d.ts +5 -0
- package/dist/types/client/parser/ui-config-parser.d.ts +6 -2
- package/dist/types/client/render-ports/hide-transition.d.ts +9 -0
- package/dist/types/client/render-ports/index.d.ts +4 -0
- package/dist/types/client/render-ports/notify-renderer.d.ts +2 -1
- package/dist/types/client/runtime/SomaSnapshotStore.d.ts +2 -0
- package/dist/types/client/security/url-guard.d.ts +14 -0
- package/dist/types/client/validation/async-check-registry.d.ts +2 -1
- package/dist/types/client/validation/object-rules.d.ts +0 -5
- package/dist/types/index.d.ts +6 -11
- package/dist/types/inspect/runtime.d.ts +2 -1
- package/dist/types/server/api/dispatch-service-api.d.ts +5 -0
- package/dist/types/server/api/load-host-service.d.ts +1 -1
- package/dist/types/server/api/paged-list.d.ts +101 -0
- package/dist/types/server/compile-port.d.ts +1 -1
- package/dist/types/server/host/host-assembly-entry.d.ts +1 -1
- package/dist/types/server/host/host-assembly.d.ts +2 -0
- package/dist/types/server/host/host-dir.d.ts +2 -0
- package/dist/types/server/host/invoke-host-request-hook.d.ts +10 -3
- package/dist/types/server/host/published-page-assets.d.ts +2 -1
- package/dist/types/server/host/resolve-host-request-hook.d.ts +18 -5
- package/dist/types/server/host/server-page-assets.d.ts +2 -1
- package/dist/types/server/host/source-aliases.d.cts +53 -0
- package/dist/types/server/host/source-aliases.d.ts +30 -0
- package/dist/types/server/host/source-packages.d.cts +2 -0
- package/dist/types/server/infra/cache/ServerFileCache.d.ts +8 -0
- package/dist/types/server/node.d.ts +1 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +0 -1
- package/dist/types/server/registry/registry-error.d.ts +5 -1
- package/dist/types/server/registry/registry-file-lock.d.ts +30 -0
- package/dist/types/server/registry/request-body-limit.d.ts +3 -2
- package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +3 -0
- package/dist/types/server/runtime-space/access-gate.d.ts +7 -0
- package/dist/types/server/runtime-space/bindings.d.ts +3 -2
- package/dist/types/server/runtime-space/dynamic-pages.d.ts +6 -1
- package/dist/types/server/runtime-space/errors.d.ts +1 -1
- package/dist/types/server/runtime-space/resolve.d.ts +2 -0
- package/dist/types/server/runtime-space/store.d.ts +8 -1
- package/dist/types/server/runtime-space/submit-steps.d.ts +2 -1
- package/dist/types/server/runtime-space/types.d.ts +3 -1
- package/dist/types/shared/component-tree-errors.d.ts +2 -1
- package/dist/types/shared/core-diagnostic-codes.d.ts +1 -1
- package/dist/types/shared/declaration-keys.d.ts +1 -1
- package/dist/types/shared/engines/engine-error.d.ts +2 -1
- package/dist/types/shared/generation-error.d.ts +2 -1
- package/dist/types/shared/pure-binding.d.ts +48 -0
- package/dist/types/shared/registry-generated/diagnostic-registry.d.ts +149 -158
- package/dist/types/shared/registry-generated/limits.d.ts +54 -10
- package/dist/types/shared/registry-generated/std-grammar-completion.d.ts +0 -30
- package/dist/types/shared/registry-generated/std-grammar-views.d.ts +97 -23
- package/dist/types/shared/registry-generated/std-grammar.d.ts +110 -242
- package/dist/types/shared/soma-error.d.ts +15 -0
- package/dist/types/shared/types/data-config.types.d.ts +17 -17
- package/dist/types/shared/types/engine.types.d.ts +0 -3
- package/dist/types/shared/types/event.types.d.ts +9 -7
- package/dist/types/shared/types/generation-error.types.d.ts +1 -1
- package/dist/types/shared/types/host-assembly.types.d.ts +2 -0
- package/dist/types/shared/types/manifest.types.d.ts +0 -3
- package/dist/types/shared/types/region-scope.types.d.ts +4 -1
- package/dist/types/shared/types/scope-manifest.types.d.ts +0 -2
- package/dist/types/shared/types/style-config.types.d.ts +17 -19
- package/dist/types/shared/types/ui-config.types.d.ts +4 -5
- package/host-native/bin/host-native.cjs +22 -1
- package/host-native/client/browser-entry.ts +21 -172
- package/host-native/src/aot-builder.ts +75 -0
- package/host-native/src/host-prepare.ts +39 -5
- package/host-native/src/manifest-http.ts +2 -3
- package/host-native/src/native-server.ts +812 -174
- package/host-native/src/page-http.ts +3 -3
- package/host-native/src/public-packages.cjs +0 -1
- package/host-native/src/registry-handler.ts +12 -5
- package/host-native/src/static-file.ts +203 -53
- package/host-native/styles/globals.css +30 -28
- package/package.json +14 -2
- package/core/client/engine/step-runner.ts +0 -212
- package/core/client/engine/trigger-engine.ts +0 -55
- package/core/client/format/zoned.ts +0 -10
- package/core/client/parser/step-parser.ts +0 -359
- package/core/client/parser/trigger-parser.ts +0 -90
- package/core/client/parser/value-lang.ts +0 -340
- package/core/client/time/instant.ts +0 -2
- package/core/shared/calc/coerce.ts +0 -2
- package/core/shared/calc/errors.ts +0 -40
- package/core/shared/calc/evaluator.ts +0 -113
- package/core/shared/calc/index.ts +0 -11
- package/core/shared/calc/lexer.ts +0 -189
- package/core/shared/calc/parser.ts +0 -193
- package/core/shared/calc/typer.ts +0 -171
- package/core/shared/date-parts.ts +0 -2
- package/core/shared/show-when.ts +0 -77
- package/core/shared/types/step.types.ts +0 -91
- package/core/shared/types/trigger.types.ts +0 -14
- package/dist/types/client/engine/step-runner.d.ts +0 -42
- package/dist/types/client/engine/trigger-engine.d.ts +0 -16
- package/dist/types/client/format/zoned.d.ts +0 -1
- package/dist/types/client/parser/step-parser.d.ts +0 -23
- package/dist/types/client/parser/trigger-parser.d.ts +0 -13
- package/dist/types/client/parser/value-lang.d.ts +0 -63
- package/dist/types/client/time/instant.d.ts +0 -1
- package/dist/types/shared/calc/coerce.d.ts +0 -1
- package/dist/types/shared/calc/errors.d.ts +0 -14
- package/dist/types/shared/calc/evaluator.d.ts +0 -2
- package/dist/types/shared/calc/index.d.ts +0 -6
- package/dist/types/shared/calc/lexer.d.ts +0 -19
- package/dist/types/shared/calc/parser.d.ts +0 -43
- package/dist/types/shared/calc/typer.d.ts +0 -11
- package/dist/types/shared/date-parts.d.ts +0 -1
- package/dist/types/shared/show-when.d.ts +0 -21
- package/dist/types/shared/types/step.types.d.ts +0 -76
- package/dist/types/shared/types/trigger.types.d.ts +0 -12
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
// core/client/parser/trigger-parser.ts — 解析 _event.yaml 的 triggers 块
|
|
2
|
-
|
|
3
|
-
import type { PropertyTrigger } from '../../shared/types/trigger.types';
|
|
4
|
-
import { parseTimers } from './timer-parser';
|
|
5
|
-
import type { ParsedTimer } from '../../shared/types/timer.types';
|
|
6
|
-
|
|
7
|
-
export class SomaTriggerParseError extends Error {
|
|
8
|
-
constructor(message: string) {
|
|
9
|
-
super(`[SOMA TriggerParser] ${message}`);
|
|
10
|
-
this.name = 'SomaTriggerParseError';
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function parseTriggerItem(raw: unknown, index: number): PropertyTrigger {
|
|
15
|
-
if (!raw || typeof raw !== 'object') {
|
|
16
|
-
throw new SomaTriggerParseError(`triggers[${index}] 必须是对象`);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const item = raw as Record<string, unknown>;
|
|
20
|
-
|
|
21
|
-
if (typeof item.condition !== 'string' || !item.condition.trim()) {
|
|
22
|
-
throw new SomaTriggerParseError(`triggers[${index}].condition 必须是非空字符串`);
|
|
23
|
-
}
|
|
24
|
-
if (typeof item.target !== 'string' || !item.target.trim()) {
|
|
25
|
-
throw new SomaTriggerParseError(`triggers[${index}].target 必须是非空字符串`);
|
|
26
|
-
}
|
|
27
|
-
if (typeof item.event !== 'string' || !item.event.trim()) {
|
|
28
|
-
throw new SomaTriggerParseError(`triggers[${index}].event 必须是非空字符串`);
|
|
29
|
-
}
|
|
30
|
-
if (!('value' in item)) {
|
|
31
|
-
throw new SomaTriggerParseError(`triggers[${index}].value 为必填字段`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const value = item.value as PropertyTrigger['value'];
|
|
35
|
-
if (
|
|
36
|
-
value !== null &&
|
|
37
|
-
typeof value !== 'string' &&
|
|
38
|
-
typeof value !== 'number' &&
|
|
39
|
-
typeof value !== 'boolean'
|
|
40
|
-
) {
|
|
41
|
-
throw new SomaTriggerParseError(
|
|
42
|
-
`triggers[${index}].value 必须是 string | number | boolean | null`,
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const trigger: PropertyTrigger = {
|
|
47
|
-
condition: item.condition.trim(),
|
|
48
|
-
value,
|
|
49
|
-
target: item.target.trim(),
|
|
50
|
-
event: item.event.trim(),
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
if (item.detail !== undefined) {
|
|
54
|
-
if (typeof item.detail !== 'object' || item.detail === null || Array.isArray(item.detail)) {
|
|
55
|
-
throw new SomaTriggerParseError(`triggers[${index}].detail 必须是对象`);
|
|
56
|
-
}
|
|
57
|
-
trigger.detail = item.detail as Record<string, unknown>;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return trigger;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** 解析 triggers 数组(YAML 中 triggers: [...]) */
|
|
64
|
-
export function parseTriggers(raw: unknown): PropertyTrigger[] {
|
|
65
|
-
if (raw === undefined || raw === null) {
|
|
66
|
-
return [];
|
|
67
|
-
}
|
|
68
|
-
if (!Array.isArray(raw)) {
|
|
69
|
-
throw new SomaTriggerParseError('triggers 必须是数组');
|
|
70
|
-
}
|
|
71
|
-
return raw.map((item, index) => parseTriggerItem(item, index));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** 从 event 配置根对象分离 triggers / timers 与事件声明 */
|
|
75
|
-
export function splitEventConfigRaw(raw: unknown): {
|
|
76
|
-
eventsRaw: Record<string, unknown>;
|
|
77
|
-
triggers: PropertyTrigger[];
|
|
78
|
-
timers: ParsedTimer[];
|
|
79
|
-
} {
|
|
80
|
-
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
81
|
-
return { eventsRaw: {}, triggers: [], timers: [] };
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const { triggers: triggersRaw, timers: timersRaw, ...eventsRaw } = raw as Record<string, unknown>;
|
|
85
|
-
return {
|
|
86
|
-
eventsRaw,
|
|
87
|
-
triggers: parseTriggers(triggersRaw),
|
|
88
|
-
timers: parseTimers(timersRaw),
|
|
89
|
-
};
|
|
90
|
-
}
|
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
// 值语言(块声明 · REQ-005 F4):全体系唯一的一条值规则。
|
|
2
|
-
//
|
|
3
|
-
// 值 := 段 ( "|" 管道调用 )*
|
|
4
|
-
// 段 := 路径 | 数字 | 算术表达式(路径 · 数字 · + - * / · 括号 · 一元负号)
|
|
5
|
-
//
|
|
6
|
-
// 为什么算术长成数学的样子而不是 `mul()` 管道:`o.price * o.qty` 谁都看得懂
|
|
7
|
-
// (Excel 用户零学习),而且标量管道 add/sub/mul/div 四个概念因此直接消失——
|
|
8
|
-
// 功能更强的同时概念更少。代价是引入一个**永久封闭**的微文法。
|
|
9
|
-
//
|
|
10
|
-
// 封闭在哪:词元只有六种(路径 / 数字 / + - * / / 括号)。
|
|
11
|
-
// **永不放行**比较、布尔、三元、字符串拼接——拼接有 tpl,判断归 Domain,
|
|
12
|
-
// 分支归 match 与组件多态。这不是"暂时不做",是地基:
|
|
13
|
-
// 词汇封闭同时是 AI 可稳定生成与可完全预编译两条命脉的共同前提
|
|
14
|
-
// (见 REQ-005 需求说明第零条论证)。解析器遇到 `> < ? :` 直接报错并指路。
|
|
15
|
-
//
|
|
16
|
-
// 产物是**可 JSON 序列化的指令节点**,不是闭包:清单要进预编译产物与注册中心
|
|
17
|
-
// (既有约束),闭包在装配期才结。运行时因此零解析——第零条公理的兑现点。
|
|
18
|
-
|
|
19
|
-
import type { SomaPipeCall } from '../../shared/types/scope-manifest.types';
|
|
20
|
-
|
|
21
|
-
export class SomaValueLangError extends Error {
|
|
22
|
-
constructor(message: string, readonly raw: string) {
|
|
23
|
-
super(`[soma-core][引擎] ${message}(值: "${raw}")`);
|
|
24
|
-
this.name = 'SomaValueLangError';
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** 值段的指令节点:JSON 可序列化,进清单 */
|
|
29
|
-
export type ValueNode =
|
|
30
|
-
| { kind: 'path'; path: string }
|
|
31
|
-
| { kind: 'lit'; value: string | number | boolean }
|
|
32
|
-
| { kind: 'neg'; operand: ValueNode }
|
|
33
|
-
| { kind: 'arith'; op: '+' | '-' | '*' | '/'; left: ValueNode; right: ValueNode };
|
|
34
|
-
|
|
35
|
-
export interface PipedValue {
|
|
36
|
-
seg: ValueNode;
|
|
37
|
-
pipes: SomaPipeCall[];
|
|
38
|
-
/** 值内引用的全部绑定路径(段内 + 管道实参),供 watchPaths 注册 */
|
|
39
|
-
paths: string[];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 值位置。算术与计算管道是**展示位专有**——算出来的值不驱动行为,
|
|
44
|
-
* 业务规则(定价、优惠、判断)留在 Domain 才可测试。
|
|
45
|
-
*/
|
|
46
|
-
export type ValuePosition = 'display' | 'condition' | 'key';
|
|
47
|
-
|
|
48
|
-
/** 判定一个变量首段是否在当前作用域(与既有 hasVar 同款契约) */
|
|
49
|
-
export type HasVarFn = (segment: string) => boolean;
|
|
50
|
-
|
|
51
|
-
/** 段内是否出现算术运算符(路径词法不含运算符,故零歧义) */
|
|
52
|
-
const ARITH_HINT = /[+\-*/()]/;
|
|
53
|
-
/** 明确拒绝的词元:比较 / 布尔 / 三元 / 字符串拼接意图 */
|
|
54
|
-
const FORBIDDEN = /(>=|<=|==|!=|&&|\|\||[<>?])/;
|
|
55
|
-
/** 合法路径词法:首段已在作用域内时,其余部分必须严格符合此形状 */
|
|
56
|
-
const PATH_SHAPE = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)*$/;
|
|
57
|
-
|
|
58
|
-
// ── 词法 ────────────────────────────────────────────────────────────────────
|
|
59
|
-
|
|
60
|
-
type Token =
|
|
61
|
-
| { t: 'num'; v: number }
|
|
62
|
-
| { t: 'id'; v: string }
|
|
63
|
-
| { t: 'op'; v: '+' | '-' | '*' | '/' }
|
|
64
|
-
| { t: '(' }
|
|
65
|
-
| { t: ')' };
|
|
66
|
-
|
|
67
|
-
function tokenize(raw: string, source: string): Token[] {
|
|
68
|
-
const tokens: Token[] = [];
|
|
69
|
-
let i = 0;
|
|
70
|
-
while (i < raw.length) {
|
|
71
|
-
const ch = raw[i];
|
|
72
|
-
if (ch === ' ' || ch === '\t') { i += 1; continue; }
|
|
73
|
-
if (ch === '(' || ch === ')') { tokens.push({ t: ch } as Token); i += 1; continue; }
|
|
74
|
-
if (ch === '+' || ch === '-' || ch === '*' || ch === '/') {
|
|
75
|
-
tokens.push({ t: 'op', v: ch }); i += 1; continue;
|
|
76
|
-
}
|
|
77
|
-
if (/[0-9]/.test(ch)) {
|
|
78
|
-
let j = i;
|
|
79
|
-
while (j < raw.length && /[0-9.]/.test(raw[j])) j += 1;
|
|
80
|
-
const text = raw.slice(i, j);
|
|
81
|
-
const value = Number(text);
|
|
82
|
-
if (!Number.isFinite(value)) {
|
|
83
|
-
throw new SomaValueLangError(`数字字面量非法: "${text}"`, source);
|
|
84
|
-
}
|
|
85
|
-
tokens.push({ t: 'num', v: value }); i = j; continue;
|
|
86
|
-
}
|
|
87
|
-
if (/[A-Za-z_]/.test(ch)) {
|
|
88
|
-
let j = i;
|
|
89
|
-
while (j < raw.length && /[A-Za-z0-9_.]/.test(raw[j])) j += 1;
|
|
90
|
-
tokens.push({ t: 'id', v: raw.slice(i, j) }); i = j; continue;
|
|
91
|
-
}
|
|
92
|
-
throw new SomaValueLangError(
|
|
93
|
-
`无法识别的字符 "${ch}"——算术只支持路径、数字、+ - * / 与括号`,
|
|
94
|
-
source,
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
return tokens;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// ── 递归下降(* / 高于 + -,括号最大,一元负号) ──────────────────────────
|
|
101
|
-
|
|
102
|
-
class Parser {
|
|
103
|
-
private pos = 0;
|
|
104
|
-
|
|
105
|
-
constructor(
|
|
106
|
-
private readonly tokens: Token[],
|
|
107
|
-
private readonly source: string,
|
|
108
|
-
private readonly hasVar: HasVarFn,
|
|
109
|
-
private readonly paths: string[],
|
|
110
|
-
) {}
|
|
111
|
-
|
|
112
|
-
parse(): ValueNode {
|
|
113
|
-
const node = this.expression();
|
|
114
|
-
if (this.pos < this.tokens.length) {
|
|
115
|
-
throw new SomaValueLangError('算术表达式有多余词元(括号是否配对?)', this.source);
|
|
116
|
-
}
|
|
117
|
-
return node;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** 表达式 := 项 { ("+"|"-") 项 } */
|
|
121
|
-
private expression(): ValueNode {
|
|
122
|
-
let left = this.term();
|
|
123
|
-
for (;;) {
|
|
124
|
-
const token = this.tokens[this.pos];
|
|
125
|
-
if (token?.t === 'op' && (token.v === '+' || token.v === '-')) {
|
|
126
|
-
this.pos += 1;
|
|
127
|
-
left = { kind: 'arith', op: token.v, left, right: this.term() };
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
return left;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** 项 := 因子 { ("*"|"/") 因子 } */
|
|
135
|
-
private term(): ValueNode {
|
|
136
|
-
let left = this.factor();
|
|
137
|
-
for (;;) {
|
|
138
|
-
const token = this.tokens[this.pos];
|
|
139
|
-
if (token?.t === 'op' && (token.v === '*' || token.v === '/')) {
|
|
140
|
-
this.pos += 1;
|
|
141
|
-
left = { kind: 'arith', op: token.v, left, right: this.factor() };
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
return left;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/** 因子 := 路径 | 数字 | "(" 表达式 ")" | "-" 因子 */
|
|
149
|
-
private factor(): ValueNode {
|
|
150
|
-
const token = this.tokens[this.pos];
|
|
151
|
-
if (!token) {
|
|
152
|
-
throw new SomaValueLangError('算术表达式不完整(缺少运算数)', this.source);
|
|
153
|
-
}
|
|
154
|
-
if (token.t === 'op' && token.v === '-') {
|
|
155
|
-
this.pos += 1;
|
|
156
|
-
return { kind: 'neg', operand: this.factor() };
|
|
157
|
-
}
|
|
158
|
-
if (token.t === 'num') {
|
|
159
|
-
this.pos += 1;
|
|
160
|
-
return { kind: 'lit', value: token.v };
|
|
161
|
-
}
|
|
162
|
-
if (token.t === 'id') {
|
|
163
|
-
this.pos += 1;
|
|
164
|
-
return this.pathNode(token.v);
|
|
165
|
-
}
|
|
166
|
-
if (token.t === '(') {
|
|
167
|
-
this.pos += 1;
|
|
168
|
-
const inner = this.expression();
|
|
169
|
-
if (this.tokens[this.pos]?.t !== ')') {
|
|
170
|
-
throw new SomaValueLangError('括号未闭合', this.source);
|
|
171
|
-
}
|
|
172
|
-
this.pos += 1;
|
|
173
|
-
return inner;
|
|
174
|
-
}
|
|
175
|
-
throw new SomaValueLangError('算术表达式中出现意外词元', this.source);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
private pathNode(text: string): ValueNode {
|
|
179
|
-
if (text === 'true' || text === 'false') {
|
|
180
|
-
return { kind: 'lit', value: text === 'true' };
|
|
181
|
-
}
|
|
182
|
-
const head = text.split('.')[0];
|
|
183
|
-
if (!this.hasVar(head)) {
|
|
184
|
-
throw new SomaValueLangError(
|
|
185
|
-
`路径 "${text}" 的首段 "${head}" 不在当前作用域——算术只接受绑定路径与数字`,
|
|
186
|
-
this.source,
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
this.paths.push(text);
|
|
190
|
-
return { kind: 'path', path: text };
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// ── 对外入口 ────────────────────────────────────────────────────────────────
|
|
195
|
-
|
|
196
|
-
/** 段内是否需要走算术解析(出现运算符或括号即是;路径词法不含它们) */
|
|
197
|
-
export function isArithSegment(segment: string): boolean {
|
|
198
|
-
return ARITH_HINT.test(segment);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* 解析一个值段(不含管道)。
|
|
203
|
-
*
|
|
204
|
-
* 纯路径与纯字面量走快路径;含运算符时进算术文法。
|
|
205
|
-
*/
|
|
206
|
-
export function parseValueSegment(
|
|
207
|
-
segment: string,
|
|
208
|
-
hasVar: HasVarFn,
|
|
209
|
-
position: ValuePosition = 'display',
|
|
210
|
-
source = segment,
|
|
211
|
-
): { node: ValueNode; paths: string[] } {
|
|
212
|
-
const raw = segment.trim();
|
|
213
|
-
if (!raw) {
|
|
214
|
-
throw new SomaValueLangError('值段为空', source);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const forbidden = raw.match(FORBIDDEN);
|
|
218
|
-
if (forbidden) {
|
|
219
|
-
throw new SomaValueLangError(
|
|
220
|
-
`不支持的运算符 "${forbidden[0]}"——比较与布尔运算不进 DSL,判断写进 Domain`
|
|
221
|
-
+ '(显隐用 when + ! 前缀,多分支用 match 管道或组件多态)',
|
|
222
|
-
source,
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
if (!isArithSegment(raw)) {
|
|
227
|
-
// 纯路径 / 纯字面量
|
|
228
|
-
if (/^-?\d+(\.\d+)?$/.test(raw)) {
|
|
229
|
-
return { node: { kind: 'lit', value: Number(raw) }, paths: [] };
|
|
230
|
-
}
|
|
231
|
-
if (raw === 'true' || raw === 'false') {
|
|
232
|
-
return { node: { kind: 'lit', value: raw === 'true' }, paths: [] };
|
|
233
|
-
}
|
|
234
|
-
const head = raw.split(/[.\s]/)[0];
|
|
235
|
-
if (hasVar(head)) {
|
|
236
|
-
// 首段在作用域内 ⇒ 这是绑定路径的意图,其余部分必须严格合法。
|
|
237
|
-
// 不校验的话 `item.price # 2` 会被当成一个名字很怪的路径静默通过,
|
|
238
|
-
// 运行时读不到值只渲染空白——典型的"不报错但坏掉"。
|
|
239
|
-
if (!PATH_SHAPE.test(raw)) {
|
|
240
|
-
throw new SomaValueLangError(
|
|
241
|
-
`路径 "${raw}" 含非法字符——路径只允许字母、数字、下划线与点`
|
|
242
|
-
+ '(若本意是字面量文案,请换一个不以作用域变量开头的写法或加引号)',
|
|
243
|
-
source,
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
return { node: { kind: 'path', path: raw }, paths: [raw] };
|
|
247
|
-
}
|
|
248
|
-
return { node: { kind: 'lit', value: raw }, paths: [] };
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
if (position !== 'display') {
|
|
252
|
-
throw new SomaValueLangError(
|
|
253
|
-
`算术只允许出现在展示值位,当前位置是 ${position}`
|
|
254
|
-
+ '——算出来的值不驱动行为,业务判断留在 Domain',
|
|
255
|
-
source,
|
|
256
|
-
);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const paths: string[] = [];
|
|
260
|
-
const node = new Parser(tokenize(raw, source), source, hasVar, paths).parse();
|
|
261
|
-
return { node, paths };
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/** 值段内是否只是一条裸路径(供上层决定是否走既有轻路径) */
|
|
265
|
-
export function isPlainPath(node: ValueNode): node is { kind: 'path'; path: string } {
|
|
266
|
-
return node.kind === 'path';
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// ── 求值(装配期结闭包时使用;纯函数,无副作用) ────────────────────────────
|
|
270
|
-
|
|
271
|
-
export interface EvalOptions {
|
|
272
|
-
/** dev 下算术遇非数值/除零直接抛错(含表达式原文);prod 返回 null 交由上层占位 */
|
|
273
|
-
dev?: boolean;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* 对指令节点求值。
|
|
278
|
-
*
|
|
279
|
-
* 除零与非数值:dev 抛错(点名原文,避免"页面上一个 NaN 查半天"),
|
|
280
|
-
* prod 返回 null——上层渲染占位符,与 role_by 未命中的既有语义一致。
|
|
281
|
-
*/
|
|
282
|
-
export function evaluateValueNode(
|
|
283
|
-
node: ValueNode,
|
|
284
|
-
read: (path: string) => unknown,
|
|
285
|
-
options: EvalOptions = {},
|
|
286
|
-
): unknown {
|
|
287
|
-
switch (node.kind) {
|
|
288
|
-
case 'lit':
|
|
289
|
-
return node.value;
|
|
290
|
-
case 'path':
|
|
291
|
-
return read(node.path);
|
|
292
|
-
case 'neg': {
|
|
293
|
-
const operand = toNumber(evaluateValueNode(node.operand, read, options), node, options);
|
|
294
|
-
return operand === null ? null : -operand;
|
|
295
|
-
}
|
|
296
|
-
case 'arith': {
|
|
297
|
-
const left = toNumber(evaluateValueNode(node.left, read, options), node, options);
|
|
298
|
-
const right = toNumber(evaluateValueNode(node.right, read, options), node, options);
|
|
299
|
-
if (left === null || right === null) return null;
|
|
300
|
-
if (node.op === '/' && right === 0) {
|
|
301
|
-
if (options.dev) {
|
|
302
|
-
throw new SomaValueLangError('除数为 0', describeNode(node));
|
|
303
|
-
}
|
|
304
|
-
return null;
|
|
305
|
-
}
|
|
306
|
-
switch (node.op) {
|
|
307
|
-
case '+': return left + right;
|
|
308
|
-
case '-': return left - right;
|
|
309
|
-
case '*': return left * right;
|
|
310
|
-
case '/': return left / right;
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
return null;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
function toNumber(value: unknown, node: ValueNode, options: EvalOptions): number | null {
|
|
318
|
-
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
319
|
-
if (typeof value === 'string' && value.trim() !== '') {
|
|
320
|
-
const parsed = Number(value);
|
|
321
|
-
if (Number.isFinite(parsed)) return parsed;
|
|
322
|
-
}
|
|
323
|
-
if (options.dev) {
|
|
324
|
-
throw new SomaValueLangError(
|
|
325
|
-
`算术运算数不是数值: ${JSON.stringify(value)}`,
|
|
326
|
-
describeNode(node),
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
return null;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/** 把指令节点还原成近似原文,仅用于报错可读性 */
|
|
333
|
-
export function describeNode(node: ValueNode): string {
|
|
334
|
-
switch (node.kind) {
|
|
335
|
-
case 'lit': return String(node.value);
|
|
336
|
-
case 'path': return node.path;
|
|
337
|
-
case 'neg': return `-${describeNode(node.operand)}`;
|
|
338
|
-
case 'arith': return `${describeNode(node.left)} ${node.op} ${describeNode(node.right)}`;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// calc 表达式层的错误类型(派生列 `fields.<x>.calc`)。
|
|
2
|
-
//
|
|
3
|
-
// 两类错误分开:语法错是「这串字符不是一个表达式」,类型错是「它是个表达式,但算的东西说不通」。
|
|
4
|
-
// 前者作者改的是写法,后者作者改的是字段选型,提示不该混成一句「表达式无效」。
|
|
5
|
-
// 两者都带 `pos`(源串里的字符区间),出错时能一眼看到列号。
|
|
6
|
-
|
|
7
|
-
/** 源串里的字符区间 [起, 止),用于错误定位 */
|
|
8
|
-
export type CalcSpan = readonly [number, number];
|
|
9
|
-
|
|
10
|
-
export class SomaCalcSyntaxError extends Error {
|
|
11
|
-
readonly pos: CalcSpan;
|
|
12
|
-
/** 出错处附近的原文片段(供报错时回显) */
|
|
13
|
-
readonly near: string;
|
|
14
|
-
|
|
15
|
-
constructor(message: string, source: string, pos: CalcSpan) {
|
|
16
|
-
super(`[soma-core][calc] ${formatWhere(source, pos)}:${message}`);
|
|
17
|
-
this.name = 'SomaCalcSyntaxError';
|
|
18
|
-
this.pos = pos;
|
|
19
|
-
this.near = source.slice(pos[0], pos[1]);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class SomaCalcTypeError extends Error {
|
|
24
|
-
readonly pos: CalcSpan;
|
|
25
|
-
readonly expected: string;
|
|
26
|
-
readonly actual: string;
|
|
27
|
-
|
|
28
|
-
constructor(message: string, source: string, pos: CalcSpan, expected: string, actual: string) {
|
|
29
|
-
super(`[soma-core][calc] ${formatWhere(source, pos)}:${message}`);
|
|
30
|
-
this.name = 'SomaCalcTypeError';
|
|
31
|
-
this.pos = pos;
|
|
32
|
-
this.expected = expected;
|
|
33
|
-
this.actual = actual;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** `calc "[amount] - [dsicount]" 第 12 列` —— 源串短,整串回显比行号有用 */
|
|
38
|
-
function formatWhere(source: string, pos: CalcSpan): string {
|
|
39
|
-
return `calc ${JSON.stringify(source)} 第 ${pos[0] + 1} 列`;
|
|
40
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
// calc 求值(派生列)——纯递归,无 eval、无 Function 构造、无副作用。
|
|
2
|
-
//
|
|
3
|
-
// 求值发生在类型已经推过之后,所以这里只处理运行期才知道的事:
|
|
4
|
-
// 值缺失、除零、以及「声明写的是 money,接口回来是字符串 "1280.00"」这类现实。
|
|
5
|
-
//
|
|
6
|
-
// 空值语义:任一操作数缺失 → 结果为 null,不抛。派生列的空值不是错误:数据还没到、
|
|
7
|
-
// 字段本来就允许空,都会让它算不出来;抛异常会让一整行渲染失败,返回 null 只让那一格为空。
|
|
8
|
-
|
|
9
|
-
import { ordinalOf, textOf } from './coerce';
|
|
10
|
-
import type { CalcAst } from './parser';
|
|
11
|
-
|
|
12
|
-
export function evaluateCalc(ast: CalcAst, values: Record<string, unknown>): unknown {
|
|
13
|
-
return evaluate(ast, values);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function evaluate(node: CalcAst, values: Record<string, unknown>): unknown {
|
|
17
|
-
switch (node.k) {
|
|
18
|
-
case 'num': return node.v;
|
|
19
|
-
case 'str': return node.v;
|
|
20
|
-
case 'bool': return node.v;
|
|
21
|
-
case 'date': return node.v;
|
|
22
|
-
case 'ref': return values[node.name] ?? null;
|
|
23
|
-
|
|
24
|
-
case 'un': {
|
|
25
|
-
const n = num(evaluate(node.arg, values));
|
|
26
|
-
return n === null ? null : -n;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
case 'bin': {
|
|
30
|
-
const left = evaluate(node.left, values);
|
|
31
|
-
const right = evaluate(node.right, values);
|
|
32
|
-
switch (node.op) {
|
|
33
|
-
case '+': case '-': case '*': case '/': {
|
|
34
|
-
const a = num(left);
|
|
35
|
-
const b = num(right);
|
|
36
|
-
if (a === null || b === null) return null;
|
|
37
|
-
if (node.op === '+') return a + b;
|
|
38
|
-
if (node.op === '-') return a - b;
|
|
39
|
-
if (node.op === '*') return a * b;
|
|
40
|
-
// 除零返回 null 而不是 Infinity:Infinity 会一路流进 CONCAT 变成 "Infinity"
|
|
41
|
-
return b === 0 ? null : a / b;
|
|
42
|
-
}
|
|
43
|
-
case '>': case '>=': case '<': case '<=': {
|
|
44
|
-
const a = num(left);
|
|
45
|
-
const b = num(right);
|
|
46
|
-
if (a === null || b === null) return null;
|
|
47
|
-
if (node.op === '>') return a > b;
|
|
48
|
-
if (node.op === '>=') return a >= b;
|
|
49
|
-
if (node.op === '<') return a < b;
|
|
50
|
-
return a <= b;
|
|
51
|
-
}
|
|
52
|
-
case '==': case '!=': {
|
|
53
|
-
// 等值比较走字符串(与谓词图同一口径):声明来自 YAML,enum 的 key 可能是数字也可能是字符串
|
|
54
|
-
const same = textOf(left) === textOf(right);
|
|
55
|
-
return node.op === '==' ? same : !same;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
case 'call': {
|
|
61
|
-
switch (node.fn) {
|
|
62
|
-
case 'IF': {
|
|
63
|
-
const cond = evaluate(node.args[0], values);
|
|
64
|
-
// 条件算不出来时不猜分支——整条派生为空,比默认走 else 诚实
|
|
65
|
-
if (cond === null) return null;
|
|
66
|
-
return evaluate(node.args[cond === true ? 1 : 2], values);
|
|
67
|
-
}
|
|
68
|
-
case 'MIN': case 'MAX': {
|
|
69
|
-
const nums = node.args.map((arg) => num(evaluate(arg, values)));
|
|
70
|
-
if (nums.some((n) => n === null)) return null;
|
|
71
|
-
const list = nums as number[];
|
|
72
|
-
return node.fn === 'MIN' ? Math.min(...list) : Math.max(...list);
|
|
73
|
-
}
|
|
74
|
-
case 'ROUND': {
|
|
75
|
-
const value = num(evaluate(node.args[0], values));
|
|
76
|
-
if (value === null) return null;
|
|
77
|
-
const digits = node.args[1] ? Number((node.args[1] as { v: number }).v) : 0;
|
|
78
|
-
return roundDecimal(value, digits);
|
|
79
|
-
}
|
|
80
|
-
case 'ABS': {
|
|
81
|
-
const value = num(evaluate(node.args[0], values));
|
|
82
|
-
return value === null ? null : Math.abs(value);
|
|
83
|
-
}
|
|
84
|
-
case 'CONCAT': {
|
|
85
|
-
// 拼接里的 null 按空串:一段拼接不该因为其中一节没值就整体消失
|
|
86
|
-
return node.args.map((arg) => textOf(evaluate(arg, values))).join('');
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function roundDecimal(value: number, digits: number): number {
|
|
94
|
-
if (digits === 0) return Math.round(value);
|
|
95
|
-
const [mantissa, exponent = '0'] = String(value).split('e');
|
|
96
|
-
const [integerPart, fraction = ''] = mantissa.split('.');
|
|
97
|
-
const decimalPlaces = fraction.length - Number(exponent);
|
|
98
|
-
if (decimalPlaces <= digits) return value;
|
|
99
|
-
const coefficient = (integerPart + fraction).replace('-', '');
|
|
100
|
-
const integerLength = coefficient.length - decimalPlaces + digits;
|
|
101
|
-
if (integerLength < 0) return value < 0 ? -0 : 0;
|
|
102
|
-
const discarded = coefficient.slice(integerLength);
|
|
103
|
-
const increment = value < 0
|
|
104
|
-
? discarded[0] > '5' || discarded[0] === '5' && /[1-9]/.test(discarded.slice(1))
|
|
105
|
-
: discarded[0] >= '5';
|
|
106
|
-
const rounded = BigInt(coefficient.slice(0, integerLength) || '0') + BigInt(increment ? 1 : 0);
|
|
107
|
-
return Number(`${value < 0 ? '-' : ''}${rounded}e-${digits}`);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** 取数:复用与谓词图同一套数值/日期归一,保证「同一个值在两处判定一致」 */
|
|
111
|
-
function num(value: unknown): number | null {
|
|
112
|
-
return ordinalOf(value);
|
|
113
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// calc 表达式层——数据对象字段的派生列 `fields.<x>.calc`。
|
|
2
|
-
//
|
|
3
|
-
// 一句话边界:这不是表达式语言,是一张受限的算式表——没有变量绑定、没有循环、
|
|
4
|
-
// 没有用户函数,函数名是六个的封闭集。
|
|
5
|
-
|
|
6
|
-
export { SomaCalcSyntaxError, SomaCalcTypeError, type CalcSpan } from './errors';
|
|
7
|
-
export { tokenize, parseDateLiteral, CALC_FUNCTIONS, type CalcToken, type CalcTokenKind, type CalcFunctionName } from './lexer';
|
|
8
|
-
export { parseCalc, collectCalcRefs, type CalcAst, type CalcBinaryOp } from './parser';
|
|
9
|
-
export { inferCalcType, assertCalcResultType, type CalcType, type CalcTypeContext } from './typer';
|
|
10
|
-
export { evaluateCalc } from './evaluator';
|
|
11
|
-
export { ordinalOf, textOf } from './coerce';
|