@goreal-ai/echo-pdk 0.1.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/dist/ai-judge/index.d.ts +177 -0
- package/dist/ai-judge/index.d.ts.map +1 -0
- package/dist/ai-judge/index.js +299 -0
- package/dist/ai-judge/index.js.map +1 -0
- package/dist/evaluator/evaluator.d.ts +136 -0
- package/dist/evaluator/evaluator.d.ts.map +1 -0
- package/dist/evaluator/evaluator.js +407 -0
- package/dist/evaluator/evaluator.js.map +1 -0
- package/dist/evaluator/index.d.ts +7 -0
- package/dist/evaluator/index.d.ts.map +1 -0
- package/dist/evaluator/index.js +8 -0
- package/dist/evaluator/index.js.map +1 -0
- package/dist/evaluator/operators.d.ts +105 -0
- package/dist/evaluator/operators.d.ts.map +1 -0
- package/dist/evaluator/operators.js +371 -0
- package/dist/evaluator/operators.js.map +1 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +388 -0
- package/dist/index.js.map +1 -0
- package/dist/parser/ast.d.ts +106 -0
- package/dist/parser/ast.d.ts.map +1 -0
- package/dist/parser/ast.js +260 -0
- package/dist/parser/ast.js.map +1 -0
- package/dist/parser/index.d.ts +8 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +13 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/lexer.d.ts +199 -0
- package/dist/parser/lexer.d.ts.map +1 -0
- package/dist/parser/lexer.js +491 -0
- package/dist/parser/lexer.js.map +1 -0
- package/dist/parser/parser.d.ts +49 -0
- package/dist/parser/parser.d.ts.map +1 -0
- package/dist/parser/parser.js +615 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/plugins/index.d.ts +62 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +170 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/renderer/index.d.ts +6 -0
- package/dist/renderer/index.d.ts.map +1 -0
- package/dist/renderer/index.js +5 -0
- package/dist/renderer/index.js.map +1 -0
- package/dist/renderer/renderer.d.ts +97 -0
- package/dist/renderer/renderer.d.ts.map +1 -0
- package/dist/renderer/renderer.js +243 -0
- package/dist/renderer/renderer.js.map +1 -0
- package/dist/types.d.ts +255 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +54 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview AST Utilities - Node creation and traversal
|
|
3
|
+
*
|
|
4
|
+
* This file provides utility functions for working with the Echo AST.
|
|
5
|
+
* Includes factory functions for creating nodes and visitor pattern helpers.
|
|
6
|
+
*
|
|
7
|
+
* IMPLEMENTATION GUIDE:
|
|
8
|
+
*
|
|
9
|
+
* 1. NODE FACTORIES
|
|
10
|
+
* Factory functions ensure consistent node creation with proper defaults.
|
|
11
|
+
*
|
|
12
|
+
* 2. VISITOR PATTERN
|
|
13
|
+
* Implement a visitor for AST traversal. This is the primary way to:
|
|
14
|
+
* - Collect AI judge conditions
|
|
15
|
+
* - Evaluate nodes
|
|
16
|
+
* - Transform the AST
|
|
17
|
+
*
|
|
18
|
+
* 3. AST UTILITIES
|
|
19
|
+
* - Clone nodes (deep copy)
|
|
20
|
+
* - Find nodes by type
|
|
21
|
+
* - Pretty print for debugging
|
|
22
|
+
*/
|
|
23
|
+
// =============================================================================
|
|
24
|
+
// NODE FACTORY FUNCTIONS
|
|
25
|
+
// =============================================================================
|
|
26
|
+
/**
|
|
27
|
+
* Create a TextNode.
|
|
28
|
+
*/
|
|
29
|
+
export function createTextNode(value, location) {
|
|
30
|
+
return {
|
|
31
|
+
type: 'text',
|
|
32
|
+
value,
|
|
33
|
+
location,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Create a VariableNode.
|
|
38
|
+
*/
|
|
39
|
+
export function createVariableNode(path, location, defaultValue) {
|
|
40
|
+
return {
|
|
41
|
+
type: 'variable',
|
|
42
|
+
path,
|
|
43
|
+
defaultValue,
|
|
44
|
+
location,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Create a ConditionalNode.
|
|
49
|
+
*/
|
|
50
|
+
export function createConditionalNode(condition, consequent, location, alternate) {
|
|
51
|
+
return {
|
|
52
|
+
type: 'conditional',
|
|
53
|
+
condition,
|
|
54
|
+
consequent,
|
|
55
|
+
alternate,
|
|
56
|
+
location,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Create a ConditionExpr.
|
|
61
|
+
*/
|
|
62
|
+
export function createConditionExpr(variable, operator, argument) {
|
|
63
|
+
return {
|
|
64
|
+
variable,
|
|
65
|
+
operator,
|
|
66
|
+
argument,
|
|
67
|
+
isAiJudge: operator === 'ai_judge',
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Create a SectionNode.
|
|
72
|
+
*/
|
|
73
|
+
export function createSectionNode(name, body, location) {
|
|
74
|
+
return {
|
|
75
|
+
type: 'section',
|
|
76
|
+
name,
|
|
77
|
+
body,
|
|
78
|
+
location,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Create an ImportNode.
|
|
83
|
+
*/
|
|
84
|
+
export function createImportNode(path, location) {
|
|
85
|
+
return {
|
|
86
|
+
type: 'import',
|
|
87
|
+
path,
|
|
88
|
+
location,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Create an IncludeNode.
|
|
93
|
+
*/
|
|
94
|
+
export function createIncludeNode(name, location) {
|
|
95
|
+
return {
|
|
96
|
+
type: 'include',
|
|
97
|
+
name,
|
|
98
|
+
location,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Walk an AST node with a visitor.
|
|
103
|
+
*
|
|
104
|
+
* @param node - The AST node to visit
|
|
105
|
+
* @param visitor - The visitor implementation
|
|
106
|
+
* @returns The result from the visitor (if any)
|
|
107
|
+
*/
|
|
108
|
+
export function visitNode(node, visitor) {
|
|
109
|
+
switch (node.type) {
|
|
110
|
+
case 'text':
|
|
111
|
+
return visitor.visitText?.(node);
|
|
112
|
+
case 'variable':
|
|
113
|
+
return visitor.visitVariable?.(node);
|
|
114
|
+
case 'conditional':
|
|
115
|
+
return visitor.visitConditional?.(node);
|
|
116
|
+
case 'section':
|
|
117
|
+
return visitor.visitSection?.(node);
|
|
118
|
+
case 'import':
|
|
119
|
+
return visitor.visitImport?.(node);
|
|
120
|
+
case 'include':
|
|
121
|
+
return visitor.visitInclude?.(node);
|
|
122
|
+
default: {
|
|
123
|
+
// Exhaustiveness check
|
|
124
|
+
const _exhaustive = node;
|
|
125
|
+
throw new Error(`Unknown node type: ${_exhaustive.type}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Walk an array of AST nodes with a visitor.
|
|
131
|
+
*
|
|
132
|
+
* @param nodes - The AST nodes to visit
|
|
133
|
+
* @param visitor - The visitor implementation
|
|
134
|
+
* @returns Array of results from the visitor
|
|
135
|
+
*/
|
|
136
|
+
export function visitNodes(nodes, visitor) {
|
|
137
|
+
return nodes.map((node) => visitNode(node, visitor));
|
|
138
|
+
}
|
|
139
|
+
// =============================================================================
|
|
140
|
+
// AST UTILITIES
|
|
141
|
+
// =============================================================================
|
|
142
|
+
/**
|
|
143
|
+
* Collect all AI judge conditions from an AST.
|
|
144
|
+
* This is used for parallel optimization.
|
|
145
|
+
*
|
|
146
|
+
* @param ast - The AST to search
|
|
147
|
+
* @returns Array of AI judge conditions with their locations
|
|
148
|
+
*/
|
|
149
|
+
export function collectAiJudgeConditions(ast) {
|
|
150
|
+
const results = [];
|
|
151
|
+
const visitor = {
|
|
152
|
+
visitConditional(node) {
|
|
153
|
+
// Check this node's condition
|
|
154
|
+
if (node.condition.isAiJudge) {
|
|
155
|
+
results.push({
|
|
156
|
+
condition: node.condition,
|
|
157
|
+
location: node.location,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
// Recurse into children
|
|
161
|
+
visitNodes(node.consequent, visitor);
|
|
162
|
+
if (node.alternate) {
|
|
163
|
+
if (Array.isArray(node.alternate)) {
|
|
164
|
+
visitNodes(node.alternate, visitor);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
visitNode(node.alternate, visitor);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
visitSection(node) {
|
|
172
|
+
visitNodes(node.body, visitor);
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
visitNodes(ast, visitor);
|
|
176
|
+
return results;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Deep clone an AST node.
|
|
180
|
+
*
|
|
181
|
+
* @param node - The node to clone
|
|
182
|
+
* @returns A deep copy of the node
|
|
183
|
+
*/
|
|
184
|
+
export function cloneNode(node) {
|
|
185
|
+
return JSON.parse(JSON.stringify(node));
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Pretty print an AST for debugging.
|
|
189
|
+
*
|
|
190
|
+
* @param ast - The AST to print
|
|
191
|
+
* @param indent - Current indentation level
|
|
192
|
+
* @returns Formatted string representation
|
|
193
|
+
*/
|
|
194
|
+
export function prettyPrint(ast, indent = 0) {
|
|
195
|
+
const pad = ' '.repeat(indent);
|
|
196
|
+
const lines = [];
|
|
197
|
+
for (const node of ast) {
|
|
198
|
+
switch (node.type) {
|
|
199
|
+
case 'text':
|
|
200
|
+
lines.push(`${pad}TEXT: "${node.value.slice(0, 50)}..."`);
|
|
201
|
+
break;
|
|
202
|
+
case 'variable':
|
|
203
|
+
lines.push(`${pad}VAR: {{${node.path}}}${node.defaultValue ? ` ?? "${node.defaultValue}"` : ''}`);
|
|
204
|
+
break;
|
|
205
|
+
case 'conditional':
|
|
206
|
+
lines.push(`${pad}IF: {{${node.condition.variable}}} #${node.condition.operator}(${node.condition.argument ?? ''})`);
|
|
207
|
+
lines.push(prettyPrint(node.consequent, indent + 1));
|
|
208
|
+
if (node.alternate) {
|
|
209
|
+
if (Array.isArray(node.alternate)) {
|
|
210
|
+
lines.push(`${pad}ELSE:`);
|
|
211
|
+
lines.push(prettyPrint(node.alternate, indent + 1));
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
lines.push(`${pad}ELSE IF:`);
|
|
215
|
+
lines.push(prettyPrint([node.alternate], indent + 1));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
lines.push(`${pad}END IF`);
|
|
219
|
+
break;
|
|
220
|
+
case 'section':
|
|
221
|
+
lines.push(`${pad}SECTION: ${node.name}`);
|
|
222
|
+
lines.push(prettyPrint(node.body, indent + 1));
|
|
223
|
+
lines.push(`${pad}END SECTION`);
|
|
224
|
+
break;
|
|
225
|
+
case 'import':
|
|
226
|
+
lines.push(`${pad}IMPORT: ${node.path}`);
|
|
227
|
+
break;
|
|
228
|
+
case 'include':
|
|
229
|
+
lines.push(`${pad}INCLUDE: ${node.name}`);
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return lines.join('\n');
|
|
234
|
+
}
|
|
235
|
+
// =============================================================================
|
|
236
|
+
// IMPLEMENTATION NOTES
|
|
237
|
+
// =============================================================================
|
|
238
|
+
/*
|
|
239
|
+
NEXT STEPS TO IMPLEMENT:
|
|
240
|
+
|
|
241
|
+
1. SOURCE LOCATION HELPERS
|
|
242
|
+
- mergeLocations(start, end) - Combine two locations
|
|
243
|
+
- formatLocation(loc) - Human-readable format
|
|
244
|
+
|
|
245
|
+
2. AST TRANSFORMATION
|
|
246
|
+
- Implement a transformer pattern for modifying AST
|
|
247
|
+
- Useful for optimizations and plugin transforms
|
|
248
|
+
|
|
249
|
+
3. VALIDATION HELPERS
|
|
250
|
+
- isValidVariablePath(path) - Check syntax of variable paths
|
|
251
|
+
- findUndefinedVariables(ast, context) - Find missing variables
|
|
252
|
+
|
|
253
|
+
4. TESTS
|
|
254
|
+
Create ast.test.ts with tests for:
|
|
255
|
+
- Factory functions
|
|
256
|
+
- Visitor pattern
|
|
257
|
+
- AI judge collection
|
|
258
|
+
- Pretty printing
|
|
259
|
+
*/
|
|
260
|
+
//# sourceMappingURL=ast.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.js","sourceRoot":"","sources":["../../src/parser/ast.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAcH,gFAAgF;AAChF,yBAAyB;AACzB,gFAAgF;AAEhF;;GAEG;AACH,MAAM,UAAU,cAAc,CAC5B,KAAa,EACb,QAAwB;IAExB,OAAO;QACL,IAAI,EAAE,MAAM;QACZ,KAAK;QACL,QAAQ;KACT,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAChC,IAAY,EACZ,QAAwB,EACxB,YAAqB;IAErB,OAAO;QACL,IAAI,EAAE,UAAU;QAChB,IAAI;QACJ,YAAY;QACZ,QAAQ;KACT,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB,CACnC,SAAwB,EACxB,UAAqB,EACrB,QAAwB,EACxB,SAAuC;IAEvC,OAAO;QACL,IAAI,EAAE,aAAa;QACnB,SAAS;QACT,UAAU;QACV,SAAS;QACT,QAAQ;KACT,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,mBAAmB,CACjC,QAAgB,EAChB,QAAgB,EAChB,QAAqC;IAErC,OAAO;QACL,QAAQ;QACR,QAAQ;QACR,QAAQ;QACR,SAAS,EAAE,QAAQ,KAAK,UAAU;KACnC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAC/B,IAAY,EACZ,IAAe,EACf,QAAwB;IAExB,OAAO;QACL,IAAI,EAAE,SAAS;QACf,IAAI;QACJ,IAAI;QACJ,QAAQ;KACT,CAAC;AAC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,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;oBACxD,CAAC;gBACH,CAAC;gBACD,KAAK,CAAC,IAAI,CAAC,GAAG,GAAG,QAAQ,CAAC,CAAC;gBAC3B,MAAM;YACR,KAAK,SAAS;gBACZ,KAAK,CAAC,IAAI,CAAC,GAAG,GAAG,YAAY,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;gBAC1C,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;gBAC/C,KAAK,CAAC,IAAI,CAAC,GAAG,GAAG,aAAa,CAAC,CAAC;gBAChC,MAAM;YACR,KAAK,QAAQ;gBACX,KAAK,CAAC,IAAI,CAAC,GAAG,GAAG,WAAW,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;gBACzC,MAAM;YACR,KAAK,SAAS;gBACZ,KAAK,CAAC,IAAI,CAAC,GAAG,GAAG,YAAY,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;gBAC1C,MAAM;QACV,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED,gFAAgF;AAChF,uBAAuB;AACvB,gFAAgF;AAEhF;;;;;;;;;;;;;;;;;;;;;EAqBE"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Parser module exports
|
|
3
|
+
*/
|
|
4
|
+
export { tokenize, EchoLexer } from './lexer.js';
|
|
5
|
+
export { parse, getTokenLocation } from './parser.js';
|
|
6
|
+
export { createTextNode, createVariableNode, createConditionalNode, createConditionExpr, createSectionNode, createImportNode, createIncludeNode, visitNode, visitNodes, collectAiJudgeConditions, cloneNode, prettyPrint, } from './ast.js';
|
|
7
|
+
export type { ASTVisitor } from './ast.js';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/parser/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAEL,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,iBAAiB,EACjB,gBAAgB,EAChB,iBAAiB,EAEjB,SAAS,EACT,UAAU,EAEV,wBAAwB,EACxB,SAAS,EACT,WAAW,GACZ,MAAM,UAAU,CAAC;AAElB,YAAY,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Parser module exports
|
|
3
|
+
*/
|
|
4
|
+
export { tokenize, EchoLexer } from './lexer.js';
|
|
5
|
+
export { parse, getTokenLocation } from './parser.js';
|
|
6
|
+
export {
|
|
7
|
+
// Node factories
|
|
8
|
+
createTextNode, createVariableNode, createConditionalNode, createConditionExpr, createSectionNode, createImportNode, createIncludeNode,
|
|
9
|
+
// Visitor
|
|
10
|
+
visitNode, visitNodes,
|
|
11
|
+
// Utilities
|
|
12
|
+
collectAiJudgeConditions, cloneNode, prettyPrint, } from './ast.js';
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/parser/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO;AACL,iBAAiB;AACjB,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,iBAAiB,EACjB,gBAAgB,EAChB,iBAAiB;AACjB,UAAU;AACV,SAAS,EACT,UAAU;AACV,YAAY;AACZ,wBAAwB,EACxB,SAAS,EACT,WAAW,GACZ,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Echo DSL Lexer - Multi-Mode Tokenization
|
|
3
|
+
*
|
|
4
|
+
* This file implements the lexer (tokenizer) for Echo DSL using Chevrotain.
|
|
5
|
+
* Uses multi-mode lexing to handle context-sensitive token recognition.
|
|
6
|
+
*
|
|
7
|
+
* LEXER MODES:
|
|
8
|
+
* - DEFAULT_MODE: Normal text content, looking for directives and variables
|
|
9
|
+
* - DIRECTIVE_MODE: Inside [#IF ...], [#SECTION ...], etc.
|
|
10
|
+
* - VARIABLE_MODE: Inside {{ ... }}
|
|
11
|
+
*
|
|
12
|
+
* TOKEN STREAM EXAMPLE:
|
|
13
|
+
*
|
|
14
|
+
* Input: "Hello {{name}}! [#IF {{age}} #gt(18)]Adult[END IF]"
|
|
15
|
+
*
|
|
16
|
+
* Tokens:
|
|
17
|
+
* TEXT("Hello ")
|
|
18
|
+
* VARIABLE_OPEN("{{")
|
|
19
|
+
* IDENTIFIER("name")
|
|
20
|
+
* VARIABLE_CLOSE("}}")
|
|
21
|
+
* TEXT("! ")
|
|
22
|
+
* IF_OPEN("[#IF")
|
|
23
|
+
* VARIABLE_OPEN("{{")
|
|
24
|
+
* IDENTIFIER("age")
|
|
25
|
+
* VARIABLE_CLOSE("}}")
|
|
26
|
+
* OPERATOR("#gt")
|
|
27
|
+
* LPAREN("(")
|
|
28
|
+
* NUMBER("18")
|
|
29
|
+
* RPAREN(")")
|
|
30
|
+
* CLOSE_BRACKET("]")
|
|
31
|
+
* TEXT("Adult")
|
|
32
|
+
* END_IF("[END IF]")
|
|
33
|
+
*/
|
|
34
|
+
import { Lexer, type TokenType } from 'chevrotain';
|
|
35
|
+
/**
|
|
36
|
+
* Category for variable open tokens ({{)
|
|
37
|
+
*/
|
|
38
|
+
export declare const VariableOpen: TokenType;
|
|
39
|
+
/**
|
|
40
|
+
* Category for variable close tokens (}})
|
|
41
|
+
*/
|
|
42
|
+
export declare const VariableClose: TokenType;
|
|
43
|
+
/**
|
|
44
|
+
* [END IF] - End of conditional block
|
|
45
|
+
*/
|
|
46
|
+
export declare const EndIf: TokenType;
|
|
47
|
+
/**
|
|
48
|
+
* [END SECTION] - End of section definition
|
|
49
|
+
*/
|
|
50
|
+
export declare const EndSection: TokenType;
|
|
51
|
+
/**
|
|
52
|
+
* [ELSE] - Else branch (complete token)
|
|
53
|
+
*/
|
|
54
|
+
export declare const Else: TokenType;
|
|
55
|
+
/**
|
|
56
|
+
* [#IF - Start of conditional (followed by condition)
|
|
57
|
+
*/
|
|
58
|
+
export declare const IfOpen: TokenType;
|
|
59
|
+
/**
|
|
60
|
+
* [ELSE IF - Else-if branch (followed by condition)
|
|
61
|
+
*/
|
|
62
|
+
export declare const ElseIf: TokenType;
|
|
63
|
+
/**
|
|
64
|
+
* [#SECTION - Section definition (followed by name="value")
|
|
65
|
+
*/
|
|
66
|
+
export declare const SectionOpen: TokenType;
|
|
67
|
+
/**
|
|
68
|
+
* [#IMPORT - Import directive (followed by path)
|
|
69
|
+
*/
|
|
70
|
+
export declare const Import: TokenType;
|
|
71
|
+
/**
|
|
72
|
+
* [#INCLUDE - Include directive (followed by section name)
|
|
73
|
+
*/
|
|
74
|
+
export declare const Include: TokenType;
|
|
75
|
+
/**
|
|
76
|
+
* ] - End of directive (pops back to DEFAULT_MODE)
|
|
77
|
+
*/
|
|
78
|
+
export declare const CloseBracket: TokenType;
|
|
79
|
+
/**
|
|
80
|
+
* Operator - #equals, #contains, #ai_judge, etc.
|
|
81
|
+
*/
|
|
82
|
+
export declare const Operator: TokenType;
|
|
83
|
+
/**
|
|
84
|
+
* Identifier - variable names, section names, etc.
|
|
85
|
+
* Supports nested paths: user.name, items[0]
|
|
86
|
+
*/
|
|
87
|
+
export declare const Identifier: TokenType;
|
|
88
|
+
/**
|
|
89
|
+
* String literal - "value" or 'value'
|
|
90
|
+
*/
|
|
91
|
+
export declare const StringLiteral: TokenType;
|
|
92
|
+
/**
|
|
93
|
+
* Number literal - integers and decimals
|
|
94
|
+
*/
|
|
95
|
+
export declare const NumberLiteral: TokenType;
|
|
96
|
+
/**
|
|
97
|
+
* ( - Left parenthesis for operator arguments
|
|
98
|
+
* Pushes to OPERATOR_ARG_MODE to capture free-form text
|
|
99
|
+
*/
|
|
100
|
+
export declare const LParen: TokenType;
|
|
101
|
+
/**
|
|
102
|
+
* ) - Right parenthesis for operator arguments (in DIRECTIVE_MODE)
|
|
103
|
+
*/
|
|
104
|
+
export declare const RParen: TokenType;
|
|
105
|
+
/**
|
|
106
|
+
* Operator argument text - captures everything until closing paren.
|
|
107
|
+
* This allows natural text like "My Girlfriend" without quotes.
|
|
108
|
+
*/
|
|
109
|
+
export declare const OperatorArgText: TokenType;
|
|
110
|
+
/**
|
|
111
|
+
* , - Comma separator in argument lists
|
|
112
|
+
*/
|
|
113
|
+
export declare const Comma: TokenType;
|
|
114
|
+
/**
|
|
115
|
+
* = - Equals sign for attribute assignment (name="value")
|
|
116
|
+
*/
|
|
117
|
+
export declare const Equals: TokenType;
|
|
118
|
+
/**
|
|
119
|
+
* ?? - Default value operator
|
|
120
|
+
*/
|
|
121
|
+
export declare const DefaultOp: TokenType;
|
|
122
|
+
/**
|
|
123
|
+
* Whitespace - skipped in directive and variable modes
|
|
124
|
+
*/
|
|
125
|
+
export declare const WhiteSpace: TokenType;
|
|
126
|
+
/**
|
|
127
|
+
* Text - Plain text content.
|
|
128
|
+
*
|
|
129
|
+
* Matches any character sequence that doesn't start a special Echo construct.
|
|
130
|
+
* The lexer must stop when encountering:
|
|
131
|
+
* - `{{` (variable start)
|
|
132
|
+
* - `[#` (directive start: [#IF, [#SECTION, [#IMPORT, [#INCLUDE)
|
|
133
|
+
* - `[E` (branch/end markers: [ELSE], [ELSE IF, [END IF], [END SECTION])
|
|
134
|
+
*
|
|
135
|
+
* REGEX BREAKDOWN: /(?:[^\[{]|\[(?![#E])|\{(?!\{))+/
|
|
136
|
+
*
|
|
137
|
+
* (?: Non-capturing group containing three alternatives:
|
|
138
|
+
* │
|
|
139
|
+
* ├─ [^\[{] Alt 1: Any character EXCEPT '[' or '{'
|
|
140
|
+
* │ These are safe - no special meaning
|
|
141
|
+
* │
|
|
142
|
+
* ├─ \[(?![#E]) Alt 2: A '[' NOT followed by '#' or 'E'
|
|
143
|
+
* │ │ Allows: [x, [1, [anything-else
|
|
144
|
+
* │ └─ (?![#E]) Negative lookahead excludes:
|
|
145
|
+
* │ - [# (directives like [#IF)
|
|
146
|
+
* │ - [E (branches like [ELSE], [END IF])
|
|
147
|
+
* │
|
|
148
|
+
* └─ \{(?!\{) Alt 3: A '{' NOT followed by another '{'
|
|
149
|
+
* │ Allows: single { in text
|
|
150
|
+
* └─ (?!\{) Negative lookahead excludes:
|
|
151
|
+
* - {{ (variable start)
|
|
152
|
+
* )+ One or more matches (greedy)
|
|
153
|
+
*
|
|
154
|
+
* EXAMPLES:
|
|
155
|
+
* "Hello world" → matches entirely (no special chars)
|
|
156
|
+
* "Hello {{name}}" → matches "Hello " then stops at {{
|
|
157
|
+
* "Price: $[100]" → matches entirely ([1 is not [# or [E)
|
|
158
|
+
* "Use {braces}" → matches entirely (single { is allowed)
|
|
159
|
+
* "[#IF ..." → matches nothing (starts with [#)
|
|
160
|
+
*/
|
|
161
|
+
export declare const Text: TokenType;
|
|
162
|
+
/**
|
|
163
|
+
* All unique tokens (for parser configuration).
|
|
164
|
+
* Includes category tokens and all mode-specific variants.
|
|
165
|
+
*
|
|
166
|
+
* IMPORTANT: Category tokens must come BEFORE their child tokens
|
|
167
|
+
* in this array for Chevrotain to properly recognize them.
|
|
168
|
+
*/
|
|
169
|
+
export declare const allTokens: TokenType[];
|
|
170
|
+
/**
|
|
171
|
+
* The Echo multi-mode lexer instance.
|
|
172
|
+
* Uses different token sets depending on the current lexing context.
|
|
173
|
+
*/
|
|
174
|
+
export declare const EchoLexer: Lexer;
|
|
175
|
+
/**
|
|
176
|
+
* Tokenize an Echo template.
|
|
177
|
+
*
|
|
178
|
+
* @param template - The template string to tokenize
|
|
179
|
+
* @returns Lexer result with tokens and errors
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* const result = tokenize('Hello {{name}}!');
|
|
184
|
+
* if (result.errors.length > 0) {
|
|
185
|
+
* console.error('Lexer errors:', result.errors);
|
|
186
|
+
* } else {
|
|
187
|
+
* console.log('Tokens:', result.tokens);
|
|
188
|
+
* }
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
export declare function tokenize(template: string): import("chevrotain").ILexingResult;
|
|
192
|
+
/**
|
|
193
|
+
* Format lexer errors for display.
|
|
194
|
+
*
|
|
195
|
+
* @param errors - Lexer errors from tokenize()
|
|
196
|
+
* @returns Formatted error messages
|
|
197
|
+
*/
|
|
198
|
+
export declare function formatLexerErrors(errors: ReturnType<typeof tokenize>['errors']): string[];
|
|
199
|
+
//# sourceMappingURL=lexer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lexer.d.ts","sourceRoot":"","sources":["../../src/parser/lexer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,EAEL,KAAK,EACL,KAAK,SAAS,EAEf,MAAM,YAAY,CAAC;AAYpB;;GAEG;AACH,eAAO,MAAM,YAAY,WAGvB,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,aAAa,WAGxB,CAAC;AAMH;;GAEG;AACH,eAAO,MAAM,KAAK,WAGhB,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,UAAU,WAGrB,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,IAAI,WAGf,CAAC;AAMH;;GAEG;AACH,eAAO,MA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