@aster-cloud/aster-lang-ts 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/LICENSE +201 -0
- package/README.md +2 -0
- package/dist/scripts/core-ir-json.d.ts +3 -0
- package/dist/scripts/core-ir-json.d.ts.map +1 -0
- package/dist/scripts/core-ir-json.js +72 -0
- package/dist/scripts/core-ir-json.js.map +1 -0
- package/dist/scripts/golden.js +49 -0
- package/dist/scripts/golden.js.map +1 -1
- package/dist/src/ai/providers/anthropic.d.ts +17 -0
- package/dist/src/ai/providers/anthropic.d.ts.map +1 -1
- package/dist/src/ai/providers/anthropic.js +58 -15
- package/dist/src/ai/providers/anthropic.js.map +1 -1
- package/dist/src/browser/schema.d.ts +52 -0
- package/dist/src/browser/schema.d.ts.map +1 -0
- package/dist/src/browser/schema.js +205 -0
- package/dist/src/browser/schema.js.map +1 -0
- package/dist/src/browser.d.ts +3 -53
- package/dist/src/browser.d.ts.map +1 -1
- package/dist/src/browser.js +20 -186
- package/dist/src/browser.js.map +1 -1
- package/dist/src/cli/policy-converter.js +8 -1
- package/dist/src/cli/policy-converter.js.map +1 -1
- package/dist/src/config/lexicons/fallback-lexicon.d.ts +41 -0
- package/dist/src/config/lexicons/fallback-lexicon.d.ts.map +1 -0
- package/dist/src/config/lexicons/fallback-lexicon.js +88 -0
- package/dist/src/config/lexicons/fallback-lexicon.js.map +1 -0
- package/dist/src/config/lexicons/index.d.ts +25 -3
- package/dist/src/config/lexicons/index.d.ts.map +1 -1
- package/dist/src/config/lexicons/index.js +42 -7
- package/dist/src/config/lexicons/index.js.map +1 -1
- package/dist/src/config/lexicons/registry.d.ts.map +1 -1
- package/dist/src/config/lexicons/registry.js +43 -5
- package/dist/src/config/lexicons/registry.js.map +1 -1
- package/dist/src/config/semantic.d.ts +12 -0
- package/dist/src/config/semantic.d.ts.map +1 -1
- package/dist/src/config/semantic.js +16 -0
- package/dist/src/config/semantic.js.map +1 -1
- package/dist/src/core/interpreter.js +2 -0
- package/dist/src/core/interpreter.js.map +1 -1
- package/dist/src/core/visitor.d.ts.map +1 -1
- package/dist/src/core/visitor.js +17 -0
- package/dist/src/core/visitor.js.map +1 -1
- package/dist/src/frontend/lexer.d.ts.map +1 -1
- package/dist/src/frontend/lexer.js +7 -1
- package/dist/src/frontend/lexer.js.map +1 -1
- package/dist/src/jvm/emitter.js +2 -2
- package/dist/src/jvm/emitter.js.map +1 -1
- package/dist/src/lsp/diagnostics.d.ts +10 -0
- package/dist/src/lsp/diagnostics.d.ts.map +1 -1
- package/dist/src/lsp/diagnostics.js +43 -1
- package/dist/src/lsp/diagnostics.js.map +1 -1
- package/dist/src/lsp/pii_diagnostics.d.ts.map +1 -1
- package/dist/src/lsp/pii_diagnostics.js +4 -1
- package/dist/src/lsp/pii_diagnostics.js.map +1 -1
- package/dist/src/lsp/server.js +34 -14
- package/dist/src/lsp/server.js.map +1 -1
- package/dist/src/lsp/utils.js +1 -1
- package/dist/src/lsp/utils.js.map +1 -1
- package/dist/src/parser/decl-parser.d.ts.map +1 -1
- package/dist/src/parser/decl-parser.js +12 -1
- package/dist/src/parser/decl-parser.js.map +1 -1
- package/dist/src/parser/expr-stmt-parser.d.ts.map +1 -1
- package/dist/src/parser/expr-stmt-parser.js +81 -4
- package/dist/src/parser/expr-stmt-parser.js.map +1 -1
- package/dist/src/parser/input-generator.d.ts +2 -0
- package/dist/src/parser/input-generator.d.ts.map +1 -1
- package/dist/src/parser/input-generator.js +11 -1
- package/dist/src/parser/input-generator.js.map +1 -1
- package/dist/src/parser/type-parser.js +1 -1
- package/dist/src/parser/type-parser.js.map +1 -1
- package/dist/src/typecheck/browser.d.ts.map +1 -1
- package/dist/src/typecheck/browser.js +87 -8
- package/dist/src/typecheck/browser.js.map +1 -1
- package/dist/src/typecheck/effects.d.ts.map +1 -1
- package/dist/src/typecheck/effects.js +17 -3
- package/dist/src/typecheck/effects.js.map +1 -1
- package/dist/src/types.d.ts +1 -0
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +1 -0
- package/dist/src/types.js.map +1 -1
- package/dist/test/e2e/runner/golden-runner.js +21 -10
- package/dist/test/e2e/runner/golden-runner.js.map +1 -1
- package/dist/test/integration/compliance/compliance-smoke.test.js +27 -35
- package/dist/test/integration/compliance/compliance-smoke.test.js.map +1 -1
- package/dist/test/integration/lsp/lsp-multi-rename.test.js +8 -7
- package/dist/test/integration/lsp/lsp-multi-rename.test.js.map +1 -1
- package/dist/test/policy-converter/round-trip.test.js +6 -4
- package/dist/test/policy-converter/round-trip.test.js.map +1 -1
- package/dist/test/regression/type-checker-golden.test.js +8 -4
- package/dist/test/regression/type-checker-golden.test.js.map +1 -1
- package/dist/test/type-checker/cross-module-packages.test.js +5 -3
- package/dist/test/type-checker/cross-module-packages.test.js.map +1 -1
- package/dist/test/unit/browser/typecheck-browser-unsupported.test.d.ts +2 -0
- package/dist/test/unit/browser/typecheck-browser-unsupported.test.d.ts.map +1 -0
- package/dist/test/unit/browser/typecheck-browser-unsupported.test.js +100 -0
- package/dist/test/unit/browser/typecheck-browser-unsupported.test.js.map +1 -0
- package/dist/test/unit/config/lexicons/i18n.test.js +20 -11
- package/dist/test/unit/config/lexicons/i18n.test.js.map +1 -1
- package/dist/test/unit/config/lexicons/zh-CN.test.js +21 -11
- package/dist/test/unit/config/lexicons/zh-CN.test.js.map +1 -1
- package/dist/test/unit/lexicons/fallback-lexicon.test.d.ts +16 -0
- package/dist/test/unit/lexicons/fallback-lexicon.test.d.ts.map +1 -0
- package/dist/test/unit/lexicons/fallback-lexicon.test.js +98 -0
- package/dist/test/unit/lexicons/fallback-lexicon.test.js.map +1 -0
- package/dist/test/unit/llm-providers.test.js +105 -93
- package/dist/test/unit/llm-providers.test.js.map +1 -1
- package/package.json +5 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../../../src/ai/providers/anthropic.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E,EACpC,WAAW,EACX,KAAK,CACN,CAAC;YACJ,CAAC;YACD,MAAM,IAAI,QAAQ,CAChB,mBAAmB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAC3E,WAAW,EACX,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAC3C,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO;QACL,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;CACF;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,cAAc,GAAG,kEAAkE,CAAC;AAC1F,MAAM,cAAc,GAAG,IAAI,MAAM,CAC/B,WAAW,cAAc,uBAAuB,cAAc,KAAK,EACnE,IAAI,CACL,CAAC;AAEF,SAAS,qBAAqB,CAAC,KAAa;IAC1C,OAAO,KAAK,CAAC,OAAO,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;AACrD,CAAC;AAED,SAAS,WAAW,CAAC,QAAoC;IACvD,uEAAuE;IACvE,uEAAuE;IACvE,qEAAqE;IACrE,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,MAAM,KAAK,IAAI,QAAQ,CAAC,OAAO,EAAE,CAAC;QACrC,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAC1B,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @aster-cloud/aster-lang-ts/browser/schema
|
|
3
|
+
*
|
|
4
|
+
* Schema extraction for dynamic form generation. Split out of `browser.ts`
|
|
5
|
+
* in the Round-3 codex refactor: previously the main browser entry was a
|
|
6
|
+
* 660-line monolith mixing compile / validate / schema / evaluate / typecheck
|
|
7
|
+
* gates. Public exports remain available from `@aster-cloud/aster-lang-ts/browser`
|
|
8
|
+
* — this module is implementation detail.
|
|
9
|
+
*/
|
|
10
|
+
import type { Lexicon } from '../config/lexicons/types.js';
|
|
11
|
+
import type { ParameterInfo } from '../parser/input-generator.js';
|
|
12
|
+
/** Schema extraction options */
|
|
13
|
+
export interface SchemaOptions {
|
|
14
|
+
/** CNL lexicon (default: EN_US) */
|
|
15
|
+
lexicon?: Lexicon;
|
|
16
|
+
/** Target function name (default: first function) */
|
|
17
|
+
functionName?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface SchemaResult {
|
|
20
|
+
/** Whether extraction succeeded */
|
|
21
|
+
success: boolean;
|
|
22
|
+
/** Module name */
|
|
23
|
+
moduleName?: string;
|
|
24
|
+
/** Function name */
|
|
25
|
+
functionName?: string;
|
|
26
|
+
/** Parameter schema */
|
|
27
|
+
parameters?: ParameterInfo[];
|
|
28
|
+
/** Error message if failed */
|
|
29
|
+
error?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Extract schema from CNL source code.
|
|
33
|
+
*
|
|
34
|
+
* Parses CNL source and extracts parameter schema for the specified function,
|
|
35
|
+
* suitable for dynamic form generation.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const result = extractSchema(`
|
|
40
|
+
* Module loan.
|
|
41
|
+
* Define LoanApplication has creditScore as Int, amount as Float, term as Int.
|
|
42
|
+
* Rule evaluate given application as LoanApplication, produce Bool:
|
|
43
|
+
* If application.creditScore >= 700 then Return true
|
|
44
|
+
* Otherwise Return false.
|
|
45
|
+
* `);
|
|
46
|
+
* if (result.success) {
|
|
47
|
+
* console.log(result.parameters);
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function extractSchema(source: string, options?: SchemaOptions): SchemaResult;
|
|
52
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../src/browser/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAOH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,KAAK,EAAY,aAAa,EAAa,MAAM,8BAA8B,CAAC;AAGvF,gCAAgC;AAChC,MAAM,WAAW,aAAa;IAC5B,mCAAmC;IACnC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,qDAAqD;IACrD,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,YAAY;IAC3B,mCAAmC;IACnC,OAAO,EAAE,OAAO,CAAC;IACjB,kBAAkB;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oBAAoB;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,uBAAuB;IACvB,UAAU,CAAC,EAAE,aAAa,EAAE,CAAC;IAC7B,8BAA8B;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAuDD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,YAAY,CAkInF"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @aster-cloud/aster-lang-ts/browser/schema
|
|
3
|
+
*
|
|
4
|
+
* Schema extraction for dynamic form generation. Split out of `browser.ts`
|
|
5
|
+
* in the Round-3 codex refactor: previously the main browser entry was a
|
|
6
|
+
* 660-line monolith mixing compile / validate / schema / evaluate / typecheck
|
|
7
|
+
* gates. Public exports remain available from `@aster-cloud/aster-lang-ts/browser`
|
|
8
|
+
* — this module is implementation detail.
|
|
9
|
+
*/
|
|
10
|
+
import { canonicalize } from '../frontend/canonicalizer.js';
|
|
11
|
+
import { lex } from '../frontend/lexer.js';
|
|
12
|
+
import { parse } from '../parser.js';
|
|
13
|
+
import { createKeywordTranslator, needsKeywordTranslation } from '../frontend/keyword-translator.js';
|
|
14
|
+
import { attachTypeInferenceRules } from '../config/lexicons/type-inference-rules.js';
|
|
15
|
+
/** Convert AST Type to type kind */
|
|
16
|
+
function getTypeKind(type) {
|
|
17
|
+
if (!type || typeof type !== 'object' || !('kind' in type)) {
|
|
18
|
+
return 'unknown';
|
|
19
|
+
}
|
|
20
|
+
switch (type.kind) {
|
|
21
|
+
case 'TypeName': {
|
|
22
|
+
const name = type.name.toLowerCase();
|
|
23
|
+
if (['int', 'float', 'double', 'number', 'bool', 'boolean', 'text', 'string', 'datetime', 'date', 'time'].includes(name)) {
|
|
24
|
+
return 'primitive';
|
|
25
|
+
}
|
|
26
|
+
// Custom type names are typically structs
|
|
27
|
+
return 'struct';
|
|
28
|
+
}
|
|
29
|
+
case 'List':
|
|
30
|
+
return 'list';
|
|
31
|
+
case 'Map':
|
|
32
|
+
return 'map';
|
|
33
|
+
case 'Option':
|
|
34
|
+
case 'Maybe':
|
|
35
|
+
return 'option';
|
|
36
|
+
case 'Result':
|
|
37
|
+
return 'result';
|
|
38
|
+
case 'FuncType':
|
|
39
|
+
return 'function';
|
|
40
|
+
default:
|
|
41
|
+
return 'unknown';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Convert AST Type to display string */
|
|
45
|
+
function typeToString(type) {
|
|
46
|
+
if (!type || typeof type !== 'object' || !('kind' in type)) {
|
|
47
|
+
return 'Unknown';
|
|
48
|
+
}
|
|
49
|
+
switch (type.kind) {
|
|
50
|
+
case 'TypeName':
|
|
51
|
+
return type.name;
|
|
52
|
+
case 'List':
|
|
53
|
+
return `List<${typeToString(type.type)}>`;
|
|
54
|
+
case 'Map':
|
|
55
|
+
return `Map<${typeToString(type.key)}, ${typeToString(type.val)}>`;
|
|
56
|
+
case 'Option':
|
|
57
|
+
return `Option<${typeToString(type.type)}>`;
|
|
58
|
+
case 'Maybe':
|
|
59
|
+
return `Maybe<${typeToString(type.type)}>`;
|
|
60
|
+
case 'Result':
|
|
61
|
+
return `Result<${typeToString(type.ok)}, ${typeToString(type.err)}>`;
|
|
62
|
+
default:
|
|
63
|
+
return 'Unknown';
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Extract schema from CNL source code.
|
|
68
|
+
*
|
|
69
|
+
* Parses CNL source and extracts parameter schema for the specified function,
|
|
70
|
+
* suitable for dynamic form generation.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const result = extractSchema(`
|
|
75
|
+
* Module loan.
|
|
76
|
+
* Define LoanApplication has creditScore as Int, amount as Float, term as Int.
|
|
77
|
+
* Rule evaluate given application as LoanApplication, produce Bool:
|
|
78
|
+
* If application.creditScore >= 700 then Return true
|
|
79
|
+
* Otherwise Return false.
|
|
80
|
+
* `);
|
|
81
|
+
* if (result.success) {
|
|
82
|
+
* console.log(result.parameters);
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export function extractSchema(source, options) {
|
|
87
|
+
try {
|
|
88
|
+
const lexicon = options?.lexicon;
|
|
89
|
+
// Canonicalize with lexicon for language-specific normalization
|
|
90
|
+
const canonical = canonicalize(source, lexicon);
|
|
91
|
+
let tokens = lex(canonical, lexicon);
|
|
92
|
+
// Translate non-English tokens to English for parser compatibility
|
|
93
|
+
if (lexicon && needsKeywordTranslation(lexicon)) {
|
|
94
|
+
const translator = createKeywordTranslator(lexicon);
|
|
95
|
+
tokens = translator.translateTokens(tokens);
|
|
96
|
+
}
|
|
97
|
+
// Parse to AST
|
|
98
|
+
const effectiveLexForSchema = lexicon ? attachTypeInferenceRules(lexicon) : undefined;
|
|
99
|
+
const parseResult = parse(tokens, effectiveLexForSchema);
|
|
100
|
+
if (parseResult.diagnostics.length > 0 && parseResult.ast.decls.length === 0) {
|
|
101
|
+
return {
|
|
102
|
+
success: false,
|
|
103
|
+
error: parseResult.diagnostics[0]?.message ?? 'Parse failed',
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const module = parseResult.ast;
|
|
107
|
+
const moduleName = module.name ?? 'unknown';
|
|
108
|
+
// Find all Data and Enum declarations (for struct/enum field resolution)
|
|
109
|
+
const dataDecls = new Map();
|
|
110
|
+
const enumDecls = new Map();
|
|
111
|
+
for (const decl of module.decls) {
|
|
112
|
+
if (decl.kind === 'Data') {
|
|
113
|
+
const data = decl;
|
|
114
|
+
const fields = data.fields.map((f) => {
|
|
115
|
+
const fieldTypeKind = getTypeKind(f.type);
|
|
116
|
+
const fieldTypeName = typeToString(f.type);
|
|
117
|
+
return {
|
|
118
|
+
name: f.name,
|
|
119
|
+
type: fieldTypeName,
|
|
120
|
+
typeKind: fieldTypeKind,
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
dataDecls.set(data.name, fields);
|
|
124
|
+
}
|
|
125
|
+
else if (decl.kind === 'Enum') {
|
|
126
|
+
const enumDecl = decl;
|
|
127
|
+
enumDecls.set(enumDecl.name, [...enumDecl.variants]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Find the target function
|
|
131
|
+
const funcs = module.decls.filter((d) => d.kind === 'Func');
|
|
132
|
+
if (funcs.length === 0) {
|
|
133
|
+
return {
|
|
134
|
+
success: false,
|
|
135
|
+
error: 'No functions found in module',
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const targetFuncName = options?.functionName;
|
|
139
|
+
const func = targetFuncName
|
|
140
|
+
? funcs.find((f) => f.name === targetFuncName)
|
|
141
|
+
: funcs[0];
|
|
142
|
+
if (!func) {
|
|
143
|
+
return {
|
|
144
|
+
success: false,
|
|
145
|
+
error: targetFuncName
|
|
146
|
+
? `Function '${targetFuncName}' not found`
|
|
147
|
+
: 'No functions found in module',
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
// Extract parameters
|
|
151
|
+
const parameters = func.params.map((param, index) => {
|
|
152
|
+
let typeName = typeToString(param.type);
|
|
153
|
+
let typeKind = getTypeKind(param.type);
|
|
154
|
+
let fields;
|
|
155
|
+
// Resolve struct/enum fields if applicable
|
|
156
|
+
if (typeKind === 'struct' && enumDecls.has(typeName)) {
|
|
157
|
+
// Type is actually an enum, not a struct
|
|
158
|
+
typeKind = 'enum';
|
|
159
|
+
}
|
|
160
|
+
else if (typeKind === 'struct' && dataDecls.has(typeName)) {
|
|
161
|
+
// Resolve struct fields, also resolving enum types within fields
|
|
162
|
+
fields = dataDecls.get(typeName).map((f) => {
|
|
163
|
+
if (f.typeKind === 'struct' && enumDecls.has(f.type)) {
|
|
164
|
+
return { ...f, typeKind: 'enum', enumVariants: enumDecls.get(f.type) };
|
|
165
|
+
}
|
|
166
|
+
return f;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
else if (typeKind === 'primitive' && dataDecls.has(param.name)) {
|
|
170
|
+
// 当参数类型为基本类型但参数名与 Data 定义匹配时,
|
|
171
|
+
// 推断参数类型为该 Data 类型(支持中文无类型参数语法)
|
|
172
|
+
typeName = param.name;
|
|
173
|
+
typeKind = 'struct';
|
|
174
|
+
fields = dataDecls.get(param.name);
|
|
175
|
+
}
|
|
176
|
+
const result = {
|
|
177
|
+
name: param.name,
|
|
178
|
+
type: typeName,
|
|
179
|
+
typeKind,
|
|
180
|
+
optional: false, // Parameters don't have optional field in AST
|
|
181
|
+
position: index,
|
|
182
|
+
};
|
|
183
|
+
if (typeKind === 'enum' && enumDecls.has(typeName)) {
|
|
184
|
+
result.enumVariants = enumDecls.get(typeName);
|
|
185
|
+
}
|
|
186
|
+
if (fields) {
|
|
187
|
+
result.fields = fields;
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
});
|
|
191
|
+
return {
|
|
192
|
+
success: true,
|
|
193
|
+
moduleName,
|
|
194
|
+
functionName: func.name,
|
|
195
|
+
parameters,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
return {
|
|
200
|
+
success: false,
|
|
201
|
+
error: error instanceof Error ? error.message : String(error),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/browser/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,mCAAmC,CAAC;AACrG,OAAO,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AA0BtF,oCAAoC;AACpC,SAAS,WAAW,CAAC,IAAU;IAC7B,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,CAAC,MAAM,IAAI,IAAI,CAAC,EAAE,CAAC;QAC3D,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;QAClB,KAAK,UAAU,CAAC,CAAC,CAAC;YAChB,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;YACrC,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;gBACzH,OAAO,WAAW,CAAC;YACrB,CAAC;YACD,0CAA0C;YAC1C,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,KAAK,MAAM;YACT,OAAO,MAAM,CA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AAI,CAAC;gBACtB,QAAQ,GAAG,QAAQ,CAAC;gBACpB,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACrC,CAAC;YAED,MAAM,MAAM,GAAkB;gBAC5B,IAAI,EAAE,KAAK,CAAC,IAAI;gBAChB,IAAI,EAAE,QAAQ;gBACd,QAAQ;gBACR,QAAQ,EAAE,KAAK,EAAE,8CAA8C;gBAC/D,QAAQ,EAAE,KAAK;aAChB,CAAC;YAEF,IAAI,QAAQ,KAAK,MAAM,IAAI,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;gBACnD,MAAM,CAAC,YAAY,GAAG,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAE,CAAC;YACjD,CAAC;YAED,IAAI,MAAM,EAAE,CAAC;gBACX,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;YACzB,CAAC;YAED,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,OAAO;YACL,OAAO,EAAE,IAAI;YACb,UAAU;YACV,YAAY,EAAE,IAAI,CAAC,IAAI;YACvB,UAAU;SACX,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;SAC9D,CAAC;IACJ,CAAC;AACH,CAAC"}
|
package/dist/src/browser.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ export type { EvalResult } from './core/interpreter.js';
|
|
|
40
40
|
export { generateFieldValue, generateInputValues, getFieldValueHint, } from './parser/input-generator.js';
|
|
41
41
|
export type { TypeKind, FieldInfo, ParameterInfo, } from './parser/input-generator.js';
|
|
42
42
|
export type * from './types.js';
|
|
43
|
-
export { EN_US, ZH_CN, DE_DE, LexiconRegistry, initializeDefaultLexicons } from './config/lexicons/index.js';
|
|
43
|
+
export { EN_US, ZH_CN, DE_DE, LexiconRegistry, initializeDefaultLexicons, initializeAllBundledLexicons, } from './config/lexicons/index.js';
|
|
44
44
|
export type { Lexicon } from './config/lexicons/types.js';
|
|
45
45
|
export { getLspUiTexts } from './config/lexicons/lsp-ui-texts.js';
|
|
46
46
|
export type { LspUiTexts } from './config/lexicons/lsp-ui-texts.js';
|
|
@@ -165,58 +165,8 @@ export declare function validateSyntaxWithSpan(source: string, lexicon?: Lexicon
|
|
|
165
165
|
* @returns Array of tokens
|
|
166
166
|
*/
|
|
167
167
|
export declare function tokenize(source: string, lexicon?: Lexicon): Token[];
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
* Schema extraction result
|
|
171
|
-
*/
|
|
172
|
-
export interface SchemaResult {
|
|
173
|
-
/** Whether extraction succeeded */
|
|
174
|
-
success: boolean;
|
|
175
|
-
/** Module name */
|
|
176
|
-
moduleName?: string;
|
|
177
|
-
/** Function name */
|
|
178
|
-
functionName?: string;
|
|
179
|
-
/** Parameter schema */
|
|
180
|
-
parameters?: ParameterInfo[];
|
|
181
|
-
/** Error message if failed */
|
|
182
|
-
error?: string;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Schema extraction options
|
|
186
|
-
*/
|
|
187
|
-
export interface SchemaOptions {
|
|
188
|
-
/** CNL lexicon (default: EN_US) */
|
|
189
|
-
lexicon?: Lexicon;
|
|
190
|
-
/** Target function name (default: first function) */
|
|
191
|
-
functionName?: string;
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Extract schema from CNL source code
|
|
195
|
-
*
|
|
196
|
-
* Parses CNL source and extracts parameter schema for the specified function,
|
|
197
|
-
* suitable for dynamic form generation.
|
|
198
|
-
*
|
|
199
|
-
* @param source - CNL source code
|
|
200
|
-
* @param options - Schema extraction options
|
|
201
|
-
* @returns Schema extraction result
|
|
202
|
-
*
|
|
203
|
-
* @example
|
|
204
|
-
* ```typescript
|
|
205
|
-
* const result = extractSchema(`
|
|
206
|
-
* Module loan.
|
|
207
|
-
* Define LoanApplication has creditScore as Int, amount as Float, term as Int.
|
|
208
|
-
* Rule evaluate given application as LoanApplication, produce Bool:
|
|
209
|
-
* If application.creditScore >= 700 then Return true
|
|
210
|
-
* Otherwise Return false.
|
|
211
|
-
* `);
|
|
212
|
-
*
|
|
213
|
-
* if (result.success) {
|
|
214
|
-
* console.log(result.parameters);
|
|
215
|
-
* // [{ name: 'application', type: 'LoanApplication', typeKind: 'struct', ... }]
|
|
216
|
-
* }
|
|
217
|
-
* ```
|
|
218
|
-
*/
|
|
219
|
-
export declare function extractSchema(source: string, options?: SchemaOptions): SchemaResult;
|
|
168
|
+
export { extractSchema } from './browser/schema.js';
|
|
169
|
+
export type { SchemaResult, SchemaOptions } from './browser/schema.js';
|
|
220
170
|
export { typecheckBrowser } from './typecheck/browser.js';
|
|
221
171
|
export type { BrowserTypecheckOptions } from './typecheck/browser.js';
|
|
222
172
|
export type { TypecheckDiagnostic } from './types.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../src/browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAGjD,OAAO,EACL,uBAAuB,EACvB,4BAA4B,EAC5B,yBAAyB,EACzB,eAAe,EACf,0BAA0B,EAC1B,cAAc,EACd,uBAAuB,GACxB,MAAM,kCAAkC,CAAC;AAC1C,YAAY,EACV,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,kCAAkC,CAAC;AAG1C,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE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YAAY,CAAC;AAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,cAAc,GAAG;IAAE,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,GAClD,aAAa,GAAG;IAAE,UAAU,EAAE,OAAO,YAAY,EAAE,mBAAmB,EAAE,CAAA;CAAE,CAqB5E"}
|
package/dist/src/browser.js
CHANGED
|
@@ -41,9 +41,12 @@ export { Node } from './ast/ast.js';
|
|
|
41
41
|
export { evaluate } from './core/interpreter.js';
|
|
42
42
|
// Input value generation (for policy execution)
|
|
43
43
|
export { generateFieldValue, generateInputValues, getFieldValueHint, } from './parser/input-generator.js';
|
|
44
|
-
// Lexicons for multi-language support
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
// Lexicons for multi-language support.
|
|
45
|
+
// 浏览器路径下使用 initializeAllBundledLexicons —— 因为浏览器 bundle 本来就把
|
|
46
|
+
// en/zh/de 编译进去,不存在"按需加载"的服务端约束。后续可在 next major 时
|
|
47
|
+
// 由消费者改用 initializeDefaultLexicons + 显式 register。
|
|
48
|
+
import { LexiconRegistry, initializeAllBundledLexicons } from './config/lexicons/index.js';
|
|
49
|
+
export { EN_US, ZH_CN, DE_DE, LexiconRegistry, initializeDefaultLexicons, initializeAllBundledLexicons, } from './config/lexicons/index.js';
|
|
47
50
|
// LSP UI texts (localized labels for hover, completion, etc.)
|
|
48
51
|
export { getLspUiTexts } from './config/lexicons/lsp-ui-texts.js';
|
|
49
52
|
// ============================================================================
|
|
@@ -91,7 +94,8 @@ export function compile(source, options) {
|
|
|
91
94
|
let canonical;
|
|
92
95
|
if (domain) {
|
|
93
96
|
// Resolve effective lexicon for domain translation (default to EN_US)
|
|
94
|
-
|
|
97
|
+
// M3: 浏览器入口预注册 en + zh + de(这些已在 bundle 里)
|
|
98
|
+
const effectiveLexicon = lexicon ?? (initializeAllBundledLexicons(), LexiconRegistry.getDefault());
|
|
95
99
|
canonical = canonicalize(source, { lexicon: effectiveLexicon, domain, locale: effectiveLexicon.id });
|
|
96
100
|
}
|
|
97
101
|
else {
|
|
@@ -109,8 +113,11 @@ export function compile(source, options) {
|
|
|
109
113
|
// Step 4: Parse to AST (now with English tokens)
|
|
110
114
|
const effectiveLex = lexicon ? attachTypeInferenceRules(lexicon) : undefined;
|
|
111
115
|
const parseResult = parse(tokens, effectiveLex);
|
|
112
|
-
//
|
|
113
|
-
|
|
116
|
+
// Parse 诊断分级:error 必须导致 success=false(即使部分 decl 可恢复),
|
|
117
|
+
// warning 不阻塞编译。原实现仅在 decls 全空时才报失败,导致调用方在
|
|
118
|
+
// 部分恢复场景下误以为编译成功 —— 实际产物(Core IR)来自残缺 AST。
|
|
119
|
+
const parseErrorDiagnostics = parseResult.diagnostics.filter(d => d.severity === 'error');
|
|
120
|
+
if (parseErrorDiagnostics.length > 0) {
|
|
114
121
|
const result = {
|
|
115
122
|
success: false,
|
|
116
123
|
parseErrors: parseResult.diagnostics.map(d => ({
|
|
@@ -131,6 +138,7 @@ export function compile(source, options) {
|
|
|
131
138
|
core,
|
|
132
139
|
};
|
|
133
140
|
if (parseResult.diagnostics.length > 0) {
|
|
141
|
+
// 此时只剩 warning(error 已在上面 short-circuit),保留以便调用方展示
|
|
134
142
|
result.parseErrors = parseResult.diagnostics.map(d => ({ message: d.message, span: d.span }));
|
|
135
143
|
}
|
|
136
144
|
if (options?.includeIntermediates) {
|
|
@@ -212,186 +220,12 @@ export function tokenize(source, lexicon) {
|
|
|
212
220
|
const canonical = canonicalize(source, lexicon);
|
|
213
221
|
return lex(canonical, lexicon);
|
|
214
222
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
switch (type.kind) {
|
|
223
|
-
case 'TypeName': {
|
|
224
|
-
const name = type.name.toLowerCase();
|
|
225
|
-
if (['int', 'float', 'double', 'number', 'bool', 'boolean', 'text', 'string', 'datetime', 'date', 'time'].includes(name)) {
|
|
226
|
-
return 'primitive';
|
|
227
|
-
}
|
|
228
|
-
// Custom type names are typically structs
|
|
229
|
-
return 'struct';
|
|
230
|
-
}
|
|
231
|
-
case 'List':
|
|
232
|
-
return 'list';
|
|
233
|
-
case 'Map':
|
|
234
|
-
return 'map';
|
|
235
|
-
case 'Option':
|
|
236
|
-
case 'Maybe':
|
|
237
|
-
return 'option';
|
|
238
|
-
case 'Result':
|
|
239
|
-
return 'result';
|
|
240
|
-
case 'FuncType':
|
|
241
|
-
return 'function';
|
|
242
|
-
default:
|
|
243
|
-
return 'unknown';
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Convert AST Type to display string
|
|
248
|
-
*/
|
|
249
|
-
function typeToString(type) {
|
|
250
|
-
if (!type || typeof type !== 'object' || !('kind' in type)) {
|
|
251
|
-
return 'Unknown';
|
|
252
|
-
}
|
|
253
|
-
switch (type.kind) {
|
|
254
|
-
case 'TypeName':
|
|
255
|
-
return type.name;
|
|
256
|
-
case 'List':
|
|
257
|
-
return `List<${typeToString(type.type)}>`;
|
|
258
|
-
case 'Map':
|
|
259
|
-
return `Map<${typeToString(type.key)}, ${typeToString(type.val)}>`;
|
|
260
|
-
case 'Option':
|
|
261
|
-
return `Option<${typeToString(type.type)}>`;
|
|
262
|
-
case 'Maybe':
|
|
263
|
-
return `Maybe<${typeToString(type.type)}>`;
|
|
264
|
-
case 'Result':
|
|
265
|
-
return `Result<${typeToString(type.ok)}, ${typeToString(type.err)}>`;
|
|
266
|
-
default:
|
|
267
|
-
return 'Unknown';
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Extract schema from CNL source code
|
|
272
|
-
*
|
|
273
|
-
* Parses CNL source and extracts parameter schema for the specified function,
|
|
274
|
-
* suitable for dynamic form generation.
|
|
275
|
-
*
|
|
276
|
-
* @param source - CNL source code
|
|
277
|
-
* @param options - Schema extraction options
|
|
278
|
-
* @returns Schema extraction result
|
|
279
|
-
*
|
|
280
|
-
* @example
|
|
281
|
-
* ```typescript
|
|
282
|
-
* const result = extractSchema(`
|
|
283
|
-
* Module loan.
|
|
284
|
-
* Define LoanApplication has creditScore as Int, amount as Float, term as Int.
|
|
285
|
-
* Rule evaluate given application as LoanApplication, produce Bool:
|
|
286
|
-
* If application.creditScore >= 700 then Return true
|
|
287
|
-
* Otherwise Return false.
|
|
288
|
-
* `);
|
|
289
|
-
*
|
|
290
|
-
* if (result.success) {
|
|
291
|
-
* console.log(result.parameters);
|
|
292
|
-
* // [{ name: 'application', type: 'LoanApplication', typeKind: 'struct', ... }]
|
|
293
|
-
* }
|
|
294
|
-
* ```
|
|
295
|
-
*/
|
|
296
|
-
export function extractSchema(source, options) {
|
|
297
|
-
try {
|
|
298
|
-
const lexicon = options?.lexicon;
|
|
299
|
-
// Canonicalize with lexicon for language-specific normalization
|
|
300
|
-
const canonical = canonicalize(source, lexicon);
|
|
301
|
-
let tokens = lex(canonical, lexicon);
|
|
302
|
-
// Translate non-English tokens to English for parser compatibility
|
|
303
|
-
if (lexicon && needsKeywordTranslation(lexicon)) {
|
|
304
|
-
const translator = createKeywordTranslator(lexicon);
|
|
305
|
-
tokens = translator.translateTokens(tokens);
|
|
306
|
-
}
|
|
307
|
-
// Parse to AST
|
|
308
|
-
const effectiveLexForSchema = lexicon ? attachTypeInferenceRules(lexicon) : undefined;
|
|
309
|
-
const parseResult = parse(tokens, effectiveLexForSchema);
|
|
310
|
-
if (parseResult.diagnostics.length > 0 && parseResult.ast.decls.length === 0) {
|
|
311
|
-
return {
|
|
312
|
-
success: false,
|
|
313
|
-
error: parseResult.diagnostics[0]?.message ?? 'Parse failed',
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
const module = parseResult.ast;
|
|
317
|
-
const moduleName = module.name ?? 'unknown';
|
|
318
|
-
// Find all Data declarations (for struct field resolution)
|
|
319
|
-
const dataDecls = new Map();
|
|
320
|
-
for (const decl of module.decls) {
|
|
321
|
-
if (decl.kind === 'Data') {
|
|
322
|
-
const data = decl;
|
|
323
|
-
const fields = data.fields.map((f) => ({
|
|
324
|
-
name: f.name,
|
|
325
|
-
type: typeToString(f.type),
|
|
326
|
-
typeKind: getTypeKind(f.type),
|
|
327
|
-
}));
|
|
328
|
-
dataDecls.set(data.name, fields);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
// Find the target function
|
|
332
|
-
const funcs = module.decls.filter((d) => d.kind === 'Func');
|
|
333
|
-
if (funcs.length === 0) {
|
|
334
|
-
return {
|
|
335
|
-
success: false,
|
|
336
|
-
error: 'No functions found in module',
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
const targetFuncName = options?.functionName;
|
|
340
|
-
const func = targetFuncName
|
|
341
|
-
? funcs.find((f) => f.name === targetFuncName)
|
|
342
|
-
: funcs[0];
|
|
343
|
-
if (!func) {
|
|
344
|
-
return {
|
|
345
|
-
success: false,
|
|
346
|
-
error: targetFuncName
|
|
347
|
-
? `Function '${targetFuncName}' not found`
|
|
348
|
-
: 'No functions found in module',
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
// Extract parameters
|
|
352
|
-
const parameters = func.params.map((param, index) => {
|
|
353
|
-
let typeName = typeToString(param.type);
|
|
354
|
-
let typeKind = getTypeKind(param.type);
|
|
355
|
-
let fields;
|
|
356
|
-
// Resolve struct fields if applicable
|
|
357
|
-
if (typeKind === 'struct' && dataDecls.has(typeName)) {
|
|
358
|
-
fields = dataDecls.get(typeName);
|
|
359
|
-
}
|
|
360
|
-
else if (typeKind === 'primitive' && dataDecls.has(param.name)) {
|
|
361
|
-
// 当参数类型为基本类型但参数名与 Data 定义匹配时,
|
|
362
|
-
// 推断参数类型为该 Data 类型(支持中文无类型参数语法)
|
|
363
|
-
// 例如:规则 评估贷款 给定 申请人:
|
|
364
|
-
// 此时参数名"申请人"与 Data "申请人"匹配,推断类型为结构体
|
|
365
|
-
typeName = param.name;
|
|
366
|
-
typeKind = 'struct';
|
|
367
|
-
fields = dataDecls.get(param.name);
|
|
368
|
-
}
|
|
369
|
-
const result = {
|
|
370
|
-
name: param.name,
|
|
371
|
-
type: typeName,
|
|
372
|
-
typeKind,
|
|
373
|
-
optional: false, // Parameters don't have optional field in AST
|
|
374
|
-
position: index,
|
|
375
|
-
};
|
|
376
|
-
if (fields) {
|
|
377
|
-
result.fields = fields;
|
|
378
|
-
}
|
|
379
|
-
return result;
|
|
380
|
-
});
|
|
381
|
-
return {
|
|
382
|
-
success: true,
|
|
383
|
-
moduleName,
|
|
384
|
-
functionName: func.name,
|
|
385
|
-
parameters,
|
|
386
|
-
};
|
|
387
|
-
}
|
|
388
|
-
catch (error) {
|
|
389
|
-
return {
|
|
390
|
-
success: false,
|
|
391
|
-
error: error instanceof Error ? error.message : String(error),
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
}
|
|
223
|
+
// ============================================================================
|
|
224
|
+
// Schema extraction API (for dynamic form generation)
|
|
225
|
+
// ============================================================================
|
|
226
|
+
// 实现移到 ./browser/schema.ts;这里只再导出以保持公共 API 不变。
|
|
227
|
+
// 见 Round-3 codex 重构记录。
|
|
228
|
+
export { extractSchema } from './browser/schema.js';
|
|
395
229
|
// ============================================================================
|
|
396
230
|
// Browser-compatible Type Checking API
|
|
397
231
|
// ============================================================================
|