@aster-cloud/aster-lang-ts 0.0.5 → 0.0.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/dist/scripts/aster.js +0 -0
- package/dist/scripts/emit-core.js +0 -0
- package/dist/src/browser.d.ts +168 -0
- package/dist/src/browser.d.ts.map +1 -0
- package/dist/src/browser.js +311 -0
- package/dist/src/browser.js.map +1 -0
- package/dist/src/cli/policy-converter.js +0 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/lsp/server.js +0 -0
- package/dist/src/parser/constraint-parser.d.ts.map +1 -1
- package/dist/src/parser/constraint-parser.js +63 -9
- package/dist/src/parser/constraint-parser.js.map +1 -1
- package/dist/src/parser/input-generator.d.ts +68 -0
- package/dist/src/parser/input-generator.d.ts.map +1 -0
- package/dist/src/parser/input-generator.js +577 -0
- package/dist/src/parser/input-generator.js.map +1 -0
- package/dist/test/unit/parser/input-generator.test.d.ts +5 -0
- package/dist/test/unit/parser/input-generator.test.d.ts.map +1 -0
- package/dist/test/unit/parser/input-generator.test.js +239 -0
- package/dist/test/unit/parser/input-generator.test.js.map +1 -0
- package/dist/test/unit/parser/parser.test.js +55 -0
- package/dist/test/unit/parser/parser.test.js.map +1 -1
- package/package.json +44 -36
package/dist/scripts/aster.js
CHANGED
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @aster-cloud/aster-lang-ts/browser
|
|
3
|
+
*
|
|
4
|
+
* Lightweight browser/edge-compatible entry point for Aster CNL compiler.
|
|
5
|
+
* This module excludes Node.js dependencies and is suitable for:
|
|
6
|
+
* - Browser-based editors
|
|
7
|
+
* - Cloudflare Workers/Pages
|
|
8
|
+
* - Edge runtimes (Vercel Edge, Deno Deploy, etc.)
|
|
9
|
+
*
|
|
10
|
+
* **Compilation Pipeline**:
|
|
11
|
+
* ```
|
|
12
|
+
* CNL Source → canonicalize → lex → parse → AST → lowerModule → Core IR → typecheck
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @example Basic compilation
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { compile, compileWithDiagnostics } from '@aster-cloud/aster-lang-ts/browser';
|
|
18
|
+
*
|
|
19
|
+
* const source = `This module is app. To greet name String, produce String: Return "Hello, " + name.`;
|
|
20
|
+
* const result = compile(source);
|
|
21
|
+
*
|
|
22
|
+
* if (result.success) {
|
|
23
|
+
* console.log('Compiled Core IR:', result.core);
|
|
24
|
+
* } else {
|
|
25
|
+
* console.error('Compilation errors:', result.errors);
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export { canonicalize } from './frontend/canonicalizer.js';
|
|
30
|
+
export { lex } from './frontend/lexer.js';
|
|
31
|
+
export { parse } from './parser.js';
|
|
32
|
+
export { lowerModule } from './lower_to_core.js';
|
|
33
|
+
export { createKeywordTranslator, buildKeywordTranslationIndex, buildFullTranslationIndex, translateTokens, translateTokensWithMarkers, translateToken, needsKeywordTranslation, } from './frontend/keyword-translator.js';
|
|
34
|
+
export type { KeywordTranslationIndex, MarkerKeywordIndex, TranslationIndexResult, } from './frontend/keyword-translator.js';
|
|
35
|
+
export { Core, Effect } from './core/core_ir.js';
|
|
36
|
+
export { TokenKind, KW } from './frontend/tokens.js';
|
|
37
|
+
export { Node } from './ast/ast.js';
|
|
38
|
+
export { generateFieldValue, generateInputValues, getFieldValueHint, } from './parser/input-generator.js';
|
|
39
|
+
export type { TypeKind, FieldInfo, ParameterInfo, } from './parser/input-generator.js';
|
|
40
|
+
export type * from './types.js';
|
|
41
|
+
export { EN_US, ZH_CN, DE_DE, LexiconRegistry, initializeDefaultLexicons } from './config/lexicons/index.js';
|
|
42
|
+
export type { Lexicon } from './config/lexicons/types.js';
|
|
43
|
+
import type { Core as CoreTypes, Token, Module as AstModule } from './types.js';
|
|
44
|
+
import type { Lexicon } from './config/lexicons/types.js';
|
|
45
|
+
/**
|
|
46
|
+
* Compilation result with success/failure status
|
|
47
|
+
*/
|
|
48
|
+
export interface CompileResult {
|
|
49
|
+
/** Whether compilation succeeded */
|
|
50
|
+
success: boolean;
|
|
51
|
+
/** Compiled Core IR module (if successful) */
|
|
52
|
+
core?: CoreTypes.Module;
|
|
53
|
+
/** Parse errors (if any) */
|
|
54
|
+
parseErrors?: string[];
|
|
55
|
+
/** Lowering errors (if any) */
|
|
56
|
+
loweringErrors?: string[];
|
|
57
|
+
/** Raw tokens from lexer (only when includeIntermediates is true) */
|
|
58
|
+
tokens?: Token[];
|
|
59
|
+
/** AST from parser (only when includeIntermediates is true) */
|
|
60
|
+
ast?: AstModule;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Compilation options
|
|
64
|
+
*/
|
|
65
|
+
export interface CompileOptions {
|
|
66
|
+
/** CNL lexicon (default: EN_US). Pass a Lexicon object from the exports. */
|
|
67
|
+
lexicon?: Lexicon;
|
|
68
|
+
/** Include intermediate representations in result */
|
|
69
|
+
includeIntermediates?: boolean;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Compile CNL source code to Core IR
|
|
73
|
+
*
|
|
74
|
+
* This is the main compilation function for browser/edge use.
|
|
75
|
+
* It runs the full compilation pipeline without type checking
|
|
76
|
+
* (type checking requires module resolution which needs file system access).
|
|
77
|
+
*
|
|
78
|
+
* @param source - CNL source code
|
|
79
|
+
* @param options - Compilation options
|
|
80
|
+
* @returns Compilation result with Core IR or errors
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const result = compile(`
|
|
85
|
+
* This module is pricing.
|
|
86
|
+
* To calculate_discount amount Number, produce Number:
|
|
87
|
+
* If amount > 100 then Return amount * 0.1
|
|
88
|
+
* Otherwise Return 0.
|
|
89
|
+
* `);
|
|
90
|
+
*
|
|
91
|
+
* if (result.success) {
|
|
92
|
+
* console.log(result.core);
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function compile(source: string, options?: CompileOptions): CompileResult;
|
|
97
|
+
/**
|
|
98
|
+
* Validate CNL source code syntax without full compilation
|
|
99
|
+
*
|
|
100
|
+
* This is a lightweight validation that only runs lexer and parser,
|
|
101
|
+
* useful for real-time editor validation.
|
|
102
|
+
*
|
|
103
|
+
* @param source - CNL source code
|
|
104
|
+
* @param lexicon - CNL lexicon (optional, defaults to EN_US)
|
|
105
|
+
* @returns Array of error messages (empty if valid)
|
|
106
|
+
*/
|
|
107
|
+
export declare function validateSyntax(source: string, lexicon?: Lexicon): string[];
|
|
108
|
+
/**
|
|
109
|
+
* Get tokens from CNL source (for syntax highlighting)
|
|
110
|
+
*
|
|
111
|
+
* @param source - CNL source code
|
|
112
|
+
* @param lexicon - CNL lexicon (optional, defaults to EN_US)
|
|
113
|
+
* @returns Array of tokens
|
|
114
|
+
*/
|
|
115
|
+
export declare function tokenize(source: string, lexicon?: Lexicon): Token[];
|
|
116
|
+
import type { ParameterInfo } from './parser/input-generator.js';
|
|
117
|
+
/**
|
|
118
|
+
* Schema extraction result
|
|
119
|
+
*/
|
|
120
|
+
export interface SchemaResult {
|
|
121
|
+
/** Whether extraction succeeded */
|
|
122
|
+
success: boolean;
|
|
123
|
+
/** Module name */
|
|
124
|
+
moduleName?: string;
|
|
125
|
+
/** Function name */
|
|
126
|
+
functionName?: string;
|
|
127
|
+
/** Parameter schema */
|
|
128
|
+
parameters?: ParameterInfo[];
|
|
129
|
+
/** Error message if failed */
|
|
130
|
+
error?: string;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Schema extraction options
|
|
134
|
+
*/
|
|
135
|
+
export interface SchemaOptions {
|
|
136
|
+
/** CNL lexicon (default: EN_US) */
|
|
137
|
+
lexicon?: Lexicon;
|
|
138
|
+
/** Target function name (default: first function) */
|
|
139
|
+
functionName?: string;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Extract schema from CNL source code
|
|
143
|
+
*
|
|
144
|
+
* Parses CNL source and extracts parameter schema for the specified function,
|
|
145
|
+
* suitable for dynamic form generation.
|
|
146
|
+
*
|
|
147
|
+
* @param source - CNL source code
|
|
148
|
+
* @param options - Schema extraction options
|
|
149
|
+
* @returns Schema extraction result
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const result = extractSchema(`
|
|
154
|
+
* This module is loan.
|
|
155
|
+
* A LoanApplication has creditScore Int, amount Float, term Int.
|
|
156
|
+
* To evaluate application LoanApplication, produce Bool:
|
|
157
|
+
* If application.creditScore >= 700 then Return true
|
|
158
|
+
* Otherwise Return false.
|
|
159
|
+
* `);
|
|
160
|
+
*
|
|
161
|
+
* if (result.success) {
|
|
162
|
+
* console.log(result.parameters);
|
|
163
|
+
* // [{ name: 'application', type: 'LoanApplication', typeKind: 'struct', ... }]
|
|
164
|
+
* }
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export declare function extractSchema(source: string, options?: SchemaOptions): SchemaResult;
|
|
168
|
+
//# sourceMappingURL=browser.d.ts.map
|
|
@@ -0,0 +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,YA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oB,CAAC,EAAE,OAAO,CAAC;CAChC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,aAAa,CAyC/E;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,EAAE,CAc1E;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,KAAK,EAAE,CAGnE;AAMD,OAAO,KAAK,EAAY,aAAa,EAAa,MAAM,6BAA6B,CAAC;AAGtF;;GAEG;AACH,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;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,mCAAmC;IACnC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,qDAAqD;IACrD,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AA6DD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,YAAY,CA2FnF"}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @aster-cloud/aster-lang-ts/browser
|
|
3
|
+
*
|
|
4
|
+
* Lightweight browser/edge-compatible entry point for Aster CNL compiler.
|
|
5
|
+
* This module excludes Node.js dependencies and is suitable for:
|
|
6
|
+
* - Browser-based editors
|
|
7
|
+
* - Cloudflare Workers/Pages
|
|
8
|
+
* - Edge runtimes (Vercel Edge, Deno Deploy, etc.)
|
|
9
|
+
*
|
|
10
|
+
* **Compilation Pipeline**:
|
|
11
|
+
* ```
|
|
12
|
+
* CNL Source → canonicalize → lex → parse → AST → lowerModule → Core IR → typecheck
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @example Basic compilation
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { compile, compileWithDiagnostics } from '@aster-cloud/aster-lang-ts/browser';
|
|
18
|
+
*
|
|
19
|
+
* const source = `This module is app. To greet name String, produce String: Return "Hello, " + name.`;
|
|
20
|
+
* const result = compile(source);
|
|
21
|
+
*
|
|
22
|
+
* if (result.success) {
|
|
23
|
+
* console.log('Compiled Core IR:', result.core);
|
|
24
|
+
* } else {
|
|
25
|
+
* console.error('Compilation errors:', result.errors);
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
// Core compilation pipeline functions
|
|
30
|
+
export { canonicalize } from './frontend/canonicalizer.js';
|
|
31
|
+
export { lex } from './frontend/lexer.js';
|
|
32
|
+
export { parse } from './parser.js';
|
|
33
|
+
export { lowerModule } from './lower_to_core.js';
|
|
34
|
+
// Keyword translation (multi-language CNL support)
|
|
35
|
+
export { createKeywordTranslator, buildKeywordTranslationIndex, buildFullTranslationIndex, translateTokens, translateTokensWithMarkers, translateToken, needsKeywordTranslation, } from './frontend/keyword-translator.js';
|
|
36
|
+
// Core types and enums
|
|
37
|
+
export { Core, Effect } from './core/core_ir.js';
|
|
38
|
+
export { TokenKind, KW } from './frontend/tokens.js';
|
|
39
|
+
export { Node } from './ast/ast.js';
|
|
40
|
+
// Input value generation (for policy execution)
|
|
41
|
+
export { generateFieldValue, generateInputValues, getFieldValueHint, } from './parser/input-generator.js';
|
|
42
|
+
// Lexicons for multi-language support
|
|
43
|
+
export { EN_US, ZH_CN, DE_DE, LexiconRegistry, initializeDefaultLexicons } from './config/lexicons/index.js';
|
|
44
|
+
// ============================================================================
|
|
45
|
+
// High-level compilation API (browser-friendly)
|
|
46
|
+
// ============================================================================
|
|
47
|
+
import { canonicalize } from './frontend/canonicalizer.js';
|
|
48
|
+
import { lex } from './frontend/lexer.js';
|
|
49
|
+
import { parse } from './parser.js';
|
|
50
|
+
import { lowerModule } from './lower_to_core.js';
|
|
51
|
+
/**
|
|
52
|
+
* Compile CNL source code to Core IR
|
|
53
|
+
*
|
|
54
|
+
* This is the main compilation function for browser/edge use.
|
|
55
|
+
* It runs the full compilation pipeline without type checking
|
|
56
|
+
* (type checking requires module resolution which needs file system access).
|
|
57
|
+
*
|
|
58
|
+
* @param source - CNL source code
|
|
59
|
+
* @param options - Compilation options
|
|
60
|
+
* @returns Compilation result with Core IR or errors
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const result = compile(`
|
|
65
|
+
* This module is pricing.
|
|
66
|
+
* To calculate_discount amount Number, produce Number:
|
|
67
|
+
* If amount > 100 then Return amount * 0.1
|
|
68
|
+
* Otherwise Return 0.
|
|
69
|
+
* `);
|
|
70
|
+
*
|
|
71
|
+
* if (result.success) {
|
|
72
|
+
* console.log(result.core);
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export function compile(source, options) {
|
|
77
|
+
try {
|
|
78
|
+
// Step 1: Canonicalize source
|
|
79
|
+
const canonical = canonicalize(source);
|
|
80
|
+
// Step 2: Lexical analysis
|
|
81
|
+
const tokens = lex(canonical, options?.lexicon);
|
|
82
|
+
// Step 3: Parse to AST
|
|
83
|
+
const ast = parse(tokens);
|
|
84
|
+
// Check for parse errors
|
|
85
|
+
if ('error' in ast || !ast) {
|
|
86
|
+
const result = {
|
|
87
|
+
success: false,
|
|
88
|
+
parseErrors: ['error' in ast ? ast.error : 'Parse failed'],
|
|
89
|
+
};
|
|
90
|
+
if (options?.includeIntermediates) {
|
|
91
|
+
result.tokens = tokens;
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
// Step 4: Lower to Core IR
|
|
96
|
+
const core = lowerModule(ast);
|
|
97
|
+
const result = {
|
|
98
|
+
success: true,
|
|
99
|
+
core,
|
|
100
|
+
};
|
|
101
|
+
if (options?.includeIntermediates) {
|
|
102
|
+
result.tokens = tokens;
|
|
103
|
+
result.ast = ast;
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
return {
|
|
109
|
+
success: false,
|
|
110
|
+
loweringErrors: [error instanceof Error ? error.message : String(error)],
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Validate CNL source code syntax without full compilation
|
|
116
|
+
*
|
|
117
|
+
* This is a lightweight validation that only runs lexer and parser,
|
|
118
|
+
* useful for real-time editor validation.
|
|
119
|
+
*
|
|
120
|
+
* @param source - CNL source code
|
|
121
|
+
* @param lexicon - CNL lexicon (optional, defaults to EN_US)
|
|
122
|
+
* @returns Array of error messages (empty if valid)
|
|
123
|
+
*/
|
|
124
|
+
export function validateSyntax(source, lexicon) {
|
|
125
|
+
try {
|
|
126
|
+
const canonical = canonicalize(source);
|
|
127
|
+
const tokens = lex(canonical, lexicon);
|
|
128
|
+
const ast = parse(tokens);
|
|
129
|
+
if ('error' in ast) {
|
|
130
|
+
return [ast.error];
|
|
131
|
+
}
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
return [error instanceof Error ? error.message : String(error)];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Get tokens from CNL source (for syntax highlighting)
|
|
140
|
+
*
|
|
141
|
+
* @param source - CNL source code
|
|
142
|
+
* @param lexicon - CNL lexicon (optional, defaults to EN_US)
|
|
143
|
+
* @returns Array of tokens
|
|
144
|
+
*/
|
|
145
|
+
export function tokenize(source, lexicon) {
|
|
146
|
+
const canonical = canonicalize(source);
|
|
147
|
+
return lex(canonical, lexicon);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Convert AST Type to type kind
|
|
151
|
+
*/
|
|
152
|
+
function getTypeKind(type) {
|
|
153
|
+
if (!type || typeof type !== 'object' || !('kind' in type)) {
|
|
154
|
+
return 'unknown';
|
|
155
|
+
}
|
|
156
|
+
switch (type.kind) {
|
|
157
|
+
case 'TypeName': {
|
|
158
|
+
const name = type.name.toLowerCase();
|
|
159
|
+
if (['int', 'float', 'double', 'number', 'bool', 'boolean', 'text', 'string', 'datetime', 'date', 'time'].includes(name)) {
|
|
160
|
+
return 'primitive';
|
|
161
|
+
}
|
|
162
|
+
// Custom type names are typically structs
|
|
163
|
+
return 'struct';
|
|
164
|
+
}
|
|
165
|
+
case 'List':
|
|
166
|
+
return 'list';
|
|
167
|
+
case 'Map':
|
|
168
|
+
return 'map';
|
|
169
|
+
case 'Option':
|
|
170
|
+
case 'Maybe':
|
|
171
|
+
return 'option';
|
|
172
|
+
case 'Result':
|
|
173
|
+
return 'result';
|
|
174
|
+
case 'FuncType':
|
|
175
|
+
return 'function';
|
|
176
|
+
default:
|
|
177
|
+
return 'unknown';
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Convert AST Type to display string
|
|
182
|
+
*/
|
|
183
|
+
function typeToString(type) {
|
|
184
|
+
if (!type || typeof type !== 'object' || !('kind' in type)) {
|
|
185
|
+
return 'Unknown';
|
|
186
|
+
}
|
|
187
|
+
switch (type.kind) {
|
|
188
|
+
case 'TypeName':
|
|
189
|
+
return type.name;
|
|
190
|
+
case 'List':
|
|
191
|
+
return `List<${typeToString(type.type)}>`;
|
|
192
|
+
case 'Map':
|
|
193
|
+
return `Map<${typeToString(type.key)}, ${typeToString(type.val)}>`;
|
|
194
|
+
case 'Option':
|
|
195
|
+
return `Option<${typeToString(type.type)}>`;
|
|
196
|
+
case 'Maybe':
|
|
197
|
+
return `Maybe<${typeToString(type.type)}>`;
|
|
198
|
+
case 'Result':
|
|
199
|
+
return `Result<${typeToString(type.ok)}, ${typeToString(type.err)}>`;
|
|
200
|
+
default:
|
|
201
|
+
return 'Unknown';
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Extract schema from CNL source code
|
|
206
|
+
*
|
|
207
|
+
* Parses CNL source and extracts parameter schema for the specified function,
|
|
208
|
+
* suitable for dynamic form generation.
|
|
209
|
+
*
|
|
210
|
+
* @param source - CNL source code
|
|
211
|
+
* @param options - Schema extraction options
|
|
212
|
+
* @returns Schema extraction result
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* const result = extractSchema(`
|
|
217
|
+
* This module is loan.
|
|
218
|
+
* A LoanApplication has creditScore Int, amount Float, term Int.
|
|
219
|
+
* To evaluate application LoanApplication, produce Bool:
|
|
220
|
+
* If application.creditScore >= 700 then Return true
|
|
221
|
+
* Otherwise Return false.
|
|
222
|
+
* `);
|
|
223
|
+
*
|
|
224
|
+
* if (result.success) {
|
|
225
|
+
* console.log(result.parameters);
|
|
226
|
+
* // [{ name: 'application', type: 'LoanApplication', typeKind: 'struct', ... }]
|
|
227
|
+
* }
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
export function extractSchema(source, options) {
|
|
231
|
+
try {
|
|
232
|
+
// Parse to AST
|
|
233
|
+
const canonical = canonicalize(source);
|
|
234
|
+
const tokens = lex(canonical, options?.lexicon);
|
|
235
|
+
const ast = parse(tokens);
|
|
236
|
+
if ('error' in ast || !ast) {
|
|
237
|
+
return {
|
|
238
|
+
success: false,
|
|
239
|
+
error: 'error' in ast ? ast.error : 'Parse failed',
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
const module = ast;
|
|
243
|
+
const moduleName = module.name ?? 'unknown';
|
|
244
|
+
// Find all Data declarations (for struct field resolution)
|
|
245
|
+
const dataDecls = new Map();
|
|
246
|
+
for (const decl of module.decls) {
|
|
247
|
+
if (decl.kind === 'Data') {
|
|
248
|
+
const data = decl;
|
|
249
|
+
const fields = data.fields.map((f) => ({
|
|
250
|
+
name: f.name,
|
|
251
|
+
type: typeToString(f.type),
|
|
252
|
+
typeKind: getTypeKind(f.type),
|
|
253
|
+
}));
|
|
254
|
+
dataDecls.set(data.name, fields);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
// Find the target function
|
|
258
|
+
const funcs = module.decls.filter((d) => d.kind === 'Func');
|
|
259
|
+
if (funcs.length === 0) {
|
|
260
|
+
return {
|
|
261
|
+
success: false,
|
|
262
|
+
error: 'No functions found in module',
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
const targetFuncName = options?.functionName;
|
|
266
|
+
const func = targetFuncName
|
|
267
|
+
? funcs.find((f) => f.name === targetFuncName)
|
|
268
|
+
: funcs[0];
|
|
269
|
+
if (!func) {
|
|
270
|
+
return {
|
|
271
|
+
success: false,
|
|
272
|
+
error: targetFuncName
|
|
273
|
+
? `Function '${targetFuncName}' not found`
|
|
274
|
+
: 'No functions found in module',
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
// Extract parameters
|
|
278
|
+
const parameters = func.params.map((param, index) => {
|
|
279
|
+
const typeName = typeToString(param.type);
|
|
280
|
+
const typeKind = getTypeKind(param.type);
|
|
281
|
+
// Resolve struct fields if applicable
|
|
282
|
+
const fields = typeKind === 'struct' && dataDecls.has(typeName)
|
|
283
|
+
? dataDecls.get(typeName)
|
|
284
|
+
: undefined;
|
|
285
|
+
const result = {
|
|
286
|
+
name: param.name,
|
|
287
|
+
type: typeName,
|
|
288
|
+
typeKind,
|
|
289
|
+
optional: false, // Parameters don't have optional field in AST
|
|
290
|
+
position: index,
|
|
291
|
+
};
|
|
292
|
+
if (fields) {
|
|
293
|
+
result.fields = fields;
|
|
294
|
+
}
|
|
295
|
+
return result;
|
|
296
|
+
});
|
|
297
|
+
return {
|
|
298
|
+
success: true,
|
|
299
|
+
moduleName,
|
|
300
|
+
functionName: func.name,
|
|
301
|
+
parameters,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
return {
|
|
306
|
+
success: false,
|
|
307
|
+
error: error instanceof Error ? error.message : String(error),
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,sCAAsC;AACtC,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;AAEjD,mDAAmD;AACnD,OAAO,EACL,uBAAuB,EACvB,4BAA4B,EAC5B,yBAAyB,EACzB,eAAe,EACf,0BAA0B,EAC1B,cAAc,EACd,uBAAuB,GACxB,MAAM,kCAAkC,CAAC;AAO1C,uBAAuB;AACvB,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC,gDAAgD;AAChD,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,6BAA6B,CAAC;AAUrC,sCAAsC;AACtC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AAG7G,+EAA+E;AAC/E,gDAAgD;AAChD,+EAA+E;AAE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YACtC,MAAM,MAAM,GAAG,QAAQ,KAAK,QAAQ,IAAI,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC;gBAC7D,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC;gBACzB,CAAC,CAAC,SAAS,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,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"}
|
|
File without changes
|
package/dist/src/index.d.ts
CHANGED
|
@@ -34,5 +34,7 @@ export { TokenKind, KW } from './frontend/tokens.js';
|
|
|
34
34
|
export { Node } from './ast/ast.js';
|
|
35
35
|
export { typecheckModule, typecheckModuleWithCapabilities, loadImportedEffects, } from './typecheck.js';
|
|
36
36
|
export type { TypecheckDiagnostic, TypecheckOptions } from './typecheck.js';
|
|
37
|
+
export { generateFieldValue, generateInputValues, getFieldValueHint, } from './parser/input-generator.js';
|
|
38
|
+
export type { TypeKind, FieldInfo, ParameterInfo, } from './parser/input-generator.js';
|
|
37
39
|
export type * from './types.js';
|
|
38
40
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;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,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAGpC,OAAO,EACL,eAAe,EACf,+BAA+B,EAC/B,mBAAmB,GACpB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAG5E,mBAAmB,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;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,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAGpC,OAAO,EACL,eAAe,EACf,+BAA+B,EAC/B,mBAAmB,GACpB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAG5E,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,6BAA6B,CAAC;AACrC,YAAY,EACV,QAAQ,EACR,SAAS,EACT,aAAa,GACd,MAAM,6BAA6B,CAAC;AAGrC,mBAAmB,YAAY,CAAC"}
|
package/dist/src/index.js
CHANGED
|
@@ -36,4 +36,6 @@ export { TokenKind, KW } from './frontend/tokens.js';
|
|
|
36
36
|
export { Node } from './ast/ast.js';
|
|
37
37
|
// 类型检查
|
|
38
38
|
export { typecheckModule, typecheckModuleWithCapabilities, loadImportedEffects, } from './typecheck.js';
|
|
39
|
+
// 输入值生成器(用于策略执行时自动生成示例输入)
|
|
40
|
+
export { generateFieldValue, generateInputValues, getFieldValueHint, } from './parser/input-generator.js';
|
|
39
41
|
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAE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sBAAsB,CAAC;AACrD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAEpC,OAAO;AACP,OAAO,EACL,eAAe,EACf,+BAA+B,EAC/B,mBAAmB,GACpB,MAAM,gBAAgB,CAAC;AAGxB,0BAA0B;AAC1B,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,6BAA6B,CAAC"}
|
package/dist/src/lsp/server.js
CHANGED
|
File without changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constraint-parser.d.ts","sourceRoot":"","sources":["../../../src/parser/constraint-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,KAAK,EAAE,UAAU,EAA0D,IAAI,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACnH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD;;;;;;;;GAQG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;IAC5C,QAAQ,CAAC,SAAS,CAAC,EAAE,KAAK,CAAC;CAC5B;AAED,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,aAAa,EAClB,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,KAAK,KAAK,KAAK,GACzC,iBAAiB,
|
|
1
|
+
{"version":3,"file":"constraint-parser.d.ts","sourceRoot":"","sources":["../../../src/parser/constraint-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,KAAK,EAAE,UAAU,EAA0D,IAAI,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACnH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD;;;;;;;;GAQG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;IAC5C,QAAQ,CAAC,SAAS,CAAC,EAAE,KAAK,CAAC;CAC5B;AAED,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,aAAa,EAClB,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,KAAK,KAAK,KAAK,GACzC,iBAAiB,CA4BnB;AAqOD;;;;GAIG;AACH,wBAAgB,sBAAsB,CACpC,UAAU,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,EAClE,IAAI,EAAE,IAAI,GACT,UAAU,GAAG,IAAI,CAuBnB"}
|
|
@@ -25,10 +25,11 @@ export function parseConstraints(ctx, error) {
|
|
|
25
25
|
}
|
|
26
26
|
constraints.push(constraint.constraint);
|
|
27
27
|
lastToken = constraint.endToken;
|
|
28
|
-
//
|
|
29
|
-
// 注意:不要消费字段之间的 'and'
|
|
28
|
+
// 检查是否还有后续约束
|
|
30
29
|
if (isConstraintKeyword(ctx)) {
|
|
31
|
-
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (skipConstraintConnector(ctx)) {
|
|
32
33
|
continue;
|
|
33
34
|
}
|
|
34
35
|
break;
|
|
@@ -42,15 +43,67 @@ export function parseConstraints(ctx, error) {
|
|
|
42
43
|
* 检查当前位置是否是约束关键词
|
|
43
44
|
*/
|
|
44
45
|
function isConstraintKeyword(ctx) {
|
|
45
|
-
|
|
46
|
+
return isConstraintToken(ctx.peek());
|
|
47
|
+
}
|
|
48
|
+
function isConstraintToken(token) {
|
|
49
|
+
if (!token)
|
|
50
|
+
return false;
|
|
51
|
+
if (token.kind !== TokenKind.IDENT &&
|
|
52
|
+
token.kind !== TokenKind.TYPE_IDENT &&
|
|
53
|
+
token.kind !== TokenKind.KEYWORD) {
|
|
46
54
|
return false;
|
|
47
55
|
}
|
|
48
|
-
const value = (
|
|
56
|
+
const value = (token.value || '').toLowerCase();
|
|
49
57
|
return (value === KW.REQUIRED ||
|
|
50
58
|
value === KW.BETWEEN ||
|
|
51
59
|
value === KW.MATCHING ||
|
|
52
|
-
value === 'at'
|
|
53
|
-
|
|
60
|
+
value === 'at');
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 获取下一个非布局 token(跳过换行/缩进),便于判断连接词后的 token 类型
|
|
64
|
+
*/
|
|
65
|
+
function nextSignificantToken(ctx, startIndex) {
|
|
66
|
+
let idx = startIndex;
|
|
67
|
+
while (idx < ctx.tokens.length) {
|
|
68
|
+
const token = ctx.tokens[idx];
|
|
69
|
+
if (token.kind === TokenKind.NEWLINE ||
|
|
70
|
+
token.kind === TokenKind.INDENT ||
|
|
71
|
+
token.kind === TokenKind.DEDENT) {
|
|
72
|
+
idx++;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
return token;
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 跳过约束之间的连接符(',' 或 'and')
|
|
81
|
+
*/
|
|
82
|
+
function skipConstraintConnector(ctx) {
|
|
83
|
+
const current = ctx.peek();
|
|
84
|
+
if (current.kind === TokenKind.COMMA) {
|
|
85
|
+
const nextToken = nextSignificantToken(ctx, ctx.index + 1);
|
|
86
|
+
if (isConstraintToken(nextToken ?? undefined)) {
|
|
87
|
+
ctx.next(); // consume comma
|
|
88
|
+
ctx.consumeNewlines();
|
|
89
|
+
ctx.consumeIndent();
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
if ((current.kind === TokenKind.IDENT ||
|
|
95
|
+
current.kind === TokenKind.TYPE_IDENT ||
|
|
96
|
+
current.kind === TokenKind.KEYWORD) &&
|
|
97
|
+
(current.value || '').toLowerCase() === KW.AND) {
|
|
98
|
+
const nextToken = nextSignificantToken(ctx, ctx.index + 1);
|
|
99
|
+
if (isConstraintToken(nextToken ?? undefined)) {
|
|
100
|
+
ctx.nextWord(); // consume 'and'
|
|
101
|
+
ctx.consumeNewlines();
|
|
102
|
+
ctx.consumeIndent();
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
54
107
|
}
|
|
55
108
|
/**
|
|
56
109
|
* 尝试解析单个约束
|
|
@@ -58,10 +111,11 @@ function isConstraintKeyword(ctx) {
|
|
|
58
111
|
* @returns 解析的约束,如果当前位置不是约束则返回 null
|
|
59
112
|
*/
|
|
60
113
|
function tryParseConstraint(ctx, error) {
|
|
61
|
-
|
|
114
|
+
const startToken = ctx.peek();
|
|
115
|
+
if (!isConstraintToken(startToken)) {
|
|
62
116
|
return null;
|
|
63
117
|
}
|
|
64
|
-
const value = (
|
|
118
|
+
const value = (startToken.value || '').toLowerCase();
|
|
65
119
|
// 'required' - 必填约束
|
|
66
120
|
if (value === KW.REQUIRED) {
|
|
67
121
|
const startTok = ctx.next();
|