@rsconcept/domain 1.7.7 → 1.8.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/{analyzer-DqjdciC5.d.ts → analyzer-DpE0qknt.d.ts} +1 -2
- package/dist/index.d.ts +1 -1
- package/dist/library/index.d.ts +87 -1
- package/dist/library/index.js +255 -1
- package/dist/library/index.js.map +1 -0
- package/dist/library/oss-api.js +1 -1
- package/dist/library/rsform-api.d.ts +12 -2
- package/dist/library/rsform-api.js +24 -2
- package/dist/library/rsform-api.js.map +1 -1
- package/dist/library/rsform.d.ts +1 -1
- package/dist/library/structure-planner.js +1 -1
- package/dist/rslang/api.d.ts +12 -1
- package/dist/rslang/api.js +30 -2
- package/dist/rslang/api.js.map +1 -1
- package/dist/rslang/index.d.ts +1 -1
- package/dist/rslang/semantic/analyzer.d.ts +1 -1
- package/dist/rslang/semantic/analyzer.js +5 -13
- package/dist/rslang/semantic/analyzer.js.map +1 -1
- package/package.json +1 -1
- package/src/library/index.ts +7 -0
- package/src/library/rsform-api.test.ts +11 -1
- package/src/library/rsform-api.ts +40 -1
- package/src/library/template-instantiation.test.ts +308 -0
- package/src/library/template-instantiation.ts +426 -0
- package/src/rslang/api.test.ts +28 -0
- package/src/rslang/api.ts +31 -2
- package/src/rslang/semantic/analyzer.ts +6 -14
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module: template instantiation from the expression bank into a target {@link RSForm}.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { type AstNode, visitAstDFS } from '../parsing';
|
|
6
|
+
import {
|
|
7
|
+
type AliasMapping,
|
|
8
|
+
applyAliasMapping,
|
|
9
|
+
isGlobalAlias,
|
|
10
|
+
parseRSLangExpression,
|
|
11
|
+
splitTemplateDefinition,
|
|
12
|
+
substituteLocalAliases,
|
|
13
|
+
substituteTemplateArgs
|
|
14
|
+
} from '../rslang/api';
|
|
15
|
+
import { labelType } from '../rslang/labels';
|
|
16
|
+
import { TokenID } from '../rslang/parser/token';
|
|
17
|
+
import { TypeID } from '../rslang/semantic/typification';
|
|
18
|
+
|
|
19
|
+
import { type ArgumentValue, type Constituenta, type CstType, type RSForm } from './rsform';
|
|
20
|
+
import {
|
|
21
|
+
applyMappingToConstituents,
|
|
22
|
+
argumentValuesToMapping,
|
|
23
|
+
generateAlias,
|
|
24
|
+
inferTemplatedType,
|
|
25
|
+
isFunctional,
|
|
26
|
+
normalizeExpression
|
|
27
|
+
} from './rsform-api';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Payload for one {@link Constituenta} to create when instantiating an expression-bank template.
|
|
31
|
+
*
|
|
32
|
+
* Aliases are generated for the target schema. Formal definitions and typifications already
|
|
33
|
+
* reflect user argument substitution and remapping of bank aliases to those new aliases.
|
|
34
|
+
*/
|
|
35
|
+
export interface TemplateInstantiationItem {
|
|
36
|
+
/** Alias to assign in the target schema. */
|
|
37
|
+
alias: string;
|
|
38
|
+
/** Inferred type after argument substitution (function/predicate may become term/axiom). */
|
|
39
|
+
cst_type: CstType;
|
|
40
|
+
crucial: boolean;
|
|
41
|
+
convention: string;
|
|
42
|
+
definition_formal: string;
|
|
43
|
+
definition_raw: string;
|
|
44
|
+
term_raw: string;
|
|
45
|
+
term_forms: Constituenta['term_forms'];
|
|
46
|
+
typification_manual: string;
|
|
47
|
+
value_is_property: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Batch create plan: dependencies first, selected template constituent last.
|
|
52
|
+
*
|
|
53
|
+
* Skips bank functions/predicates whose alias already exists in the target schema, and
|
|
54
|
+
* constituents whose substituted formal definition already exists (normalized comparison).
|
|
55
|
+
* {@link TemplateInstantiationPlan.mainDuplicateAlias} reports when the selected template is a duplicate.
|
|
56
|
+
* Does not set `insert_after`; the caller supplies insertion on the batch DTO.
|
|
57
|
+
*/
|
|
58
|
+
export interface TemplateInstantiationPlan {
|
|
59
|
+
items: TemplateInstantiationItem[];
|
|
60
|
+
/** Set when the selected template expression already exists in the target schema. */
|
|
61
|
+
mainDuplicateAlias: string | null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Input for planning template instantiation into a conceptual schema.
|
|
66
|
+
*/
|
|
67
|
+
export interface TemplateInstantiationInput {
|
|
68
|
+
/** Schema that will receive new constituents. */
|
|
69
|
+
targetSchema: RSForm;
|
|
70
|
+
/** All constituents of the selected expression-bank RSForm (not only the prototype). */
|
|
71
|
+
templateItems: Constituenta[];
|
|
72
|
+
/** Template constituent chosen in the UI (main expression to add). */
|
|
73
|
+
prototype: Constituenta;
|
|
74
|
+
/** User bindings from template parameter aliases to target-schema global aliases. */
|
|
75
|
+
userArgs: ArgumentValue[];
|
|
76
|
+
/**
|
|
77
|
+
* Main constituent attributes from the create dialog (alias, labels, flags).
|
|
78
|
+
* `definition_formal` and `cst_type` may be overridden from `prototype` and `userArgs`.
|
|
79
|
+
*/
|
|
80
|
+
mainItem: TemplateInstantiationItem;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface FuncCallRef {
|
|
84
|
+
callee: string;
|
|
85
|
+
args: string[];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface TemplateParam {
|
|
89
|
+
alias: string;
|
|
90
|
+
typification: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Build a batch create plan for a template expression and its bank dependencies.
|
|
95
|
+
*
|
|
96
|
+
* Walks nested `F#` / `P#` calls in the substituted expression, collects missing term-functions
|
|
97
|
+
* and predicate-functions from `templateItems`, propagates argument bindings into nested calls,
|
|
98
|
+
* topologically orders dependencies, and remaps bank aliases to freshly generated target aliases.
|
|
99
|
+
*/
|
|
100
|
+
export function planTemplateInstantiation(input: TemplateInstantiationInput): TemplateInstantiationPlan {
|
|
101
|
+
return new TemplateInstantiationPlanner(input).plan();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Stateful planner for {@link planTemplateInstantiation}.
|
|
106
|
+
*
|
|
107
|
+
* Prefer the function wrapper unless you need to hold or reuse planner state between calls.
|
|
108
|
+
*/
|
|
109
|
+
export class TemplateInstantiationPlanner {
|
|
110
|
+
private readonly targetSchema: RSForm;
|
|
111
|
+
private readonly prototype: Constituenta;
|
|
112
|
+
private readonly userArgs: ArgumentValue[];
|
|
113
|
+
private readonly mainItemTemplate: TemplateInstantiationItem;
|
|
114
|
+
private readonly templateByAlias: Map<string, Constituenta>;
|
|
115
|
+
private readonly userMapping: AliasMapping;
|
|
116
|
+
private readonly depBindings = new Map<string, Map<string, string>>();
|
|
117
|
+
private readonly depRefs = new Map<string, Set<string>>();
|
|
118
|
+
|
|
119
|
+
/** @param input - See {@link TemplateInstantiationInput}. */
|
|
120
|
+
constructor(input: TemplateInstantiationInput) {
|
|
121
|
+
this.targetSchema = input.targetSchema;
|
|
122
|
+
this.prototype = input.prototype;
|
|
123
|
+
this.userArgs = input.userArgs;
|
|
124
|
+
this.mainItemTemplate = input.mainItem;
|
|
125
|
+
this.templateByAlias = new Map(
|
|
126
|
+
input.templateItems.filter(cst => isFunctional(cst.cst_type)).map(cst => [cst.alias, cst])
|
|
127
|
+
);
|
|
128
|
+
this.userMapping = argumentValuesToMapping(input.userArgs);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** @returns Dependency items in topological order, then the main template item when it is not a duplicate. */
|
|
132
|
+
plan(): TemplateInstantiationPlan {
|
|
133
|
+
const mainDefinition = substituteTemplateArgs(this.prototype.definition_formal, this.userMapping);
|
|
134
|
+
this.collectDependencies(mainDefinition, this.userMapping);
|
|
135
|
+
|
|
136
|
+
const definitionIndex = createFormalDefinitionIndex(this.targetSchema);
|
|
137
|
+
const dependencyItems = this.buildDependencyItems(definitionIndex);
|
|
138
|
+
const { item: mainItem, duplicateAlias: mainDuplicateAlias } = this.buildMainItem(
|
|
139
|
+
mainDefinition,
|
|
140
|
+
dependencyItems.takenAliases,
|
|
141
|
+
dependencyItems.aliasMapping,
|
|
142
|
+
definitionIndex
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
items: mainItem ? [...dependencyItems.items, mainItem] : dependencyItems.items,
|
|
147
|
+
mainDuplicateAlias
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private buildDependencyItems(definitionIndex: FormalDefinitionIndex): {
|
|
152
|
+
items: TemplateInstantiationItem[];
|
|
153
|
+
aliasMapping: AliasMapping;
|
|
154
|
+
takenAliases: string[];
|
|
155
|
+
} {
|
|
156
|
+
const dependencyAliases = topologicalSort([...this.depBindings.keys()], this.depRefs);
|
|
157
|
+
const aliasMapping: AliasMapping = {};
|
|
158
|
+
const takenAliases: string[] = [];
|
|
159
|
+
const reservedAliases = [this.mainItemTemplate.alias];
|
|
160
|
+
const items: TemplateInstantiationItem[] = [];
|
|
161
|
+
|
|
162
|
+
for (const templateAlias of dependencyAliases) {
|
|
163
|
+
const template = this.templateByAlias.get(templateAlias)!;
|
|
164
|
+
const bindings = this.depBindings.get(templateAlias)!;
|
|
165
|
+
const args = bindingMapToArgs(getTemplateParams(template), bindings);
|
|
166
|
+
const cst_type = inferTemplatedType(template.cst_type, args);
|
|
167
|
+
const definition = applyAliasMapping(
|
|
168
|
+
substituteTemplateArgs(template.definition_formal, argumentValuesToMapping(args)),
|
|
169
|
+
aliasMapping
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const existingAlias = definitionIndex.find(cst_type, definition);
|
|
173
|
+
if (existingAlias) {
|
|
174
|
+
aliasMapping[templateAlias] = existingAlias;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const alias = generateAlias(template.cst_type, this.targetSchema, [...reservedAliases, ...takenAliases]);
|
|
179
|
+
takenAliases.push(alias);
|
|
180
|
+
aliasMapping[templateAlias] = alias;
|
|
181
|
+
|
|
182
|
+
const item = instantiateTemplateItem(template, args, alias);
|
|
183
|
+
item.definition_formal = definition;
|
|
184
|
+
applyMappingToConstituents([item], aliasMapping, false);
|
|
185
|
+
definitionIndex.register(cst_type, item.definition_formal, alias);
|
|
186
|
+
items.push(item);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return { items, aliasMapping, takenAliases };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private buildMainItem(
|
|
193
|
+
mainDefinition: string,
|
|
194
|
+
takenAliases: string[],
|
|
195
|
+
aliasMapping: AliasMapping,
|
|
196
|
+
definitionIndex: FormalDefinitionIndex
|
|
197
|
+
): { item: TemplateInstantiationItem | null; duplicateAlias: string | null } {
|
|
198
|
+
const mappedDefinition = applyAliasMapping(mainDefinition, aliasMapping);
|
|
199
|
+
const cst_type = inferTemplatedType(this.prototype.cst_type, this.userArgs);
|
|
200
|
+
|
|
201
|
+
const duplicateAlias = definitionIndex.find(cst_type, mappedDefinition);
|
|
202
|
+
if (duplicateAlias) {
|
|
203
|
+
return { item: null, duplicateAlias };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const mainItem: TemplateInstantiationItem = {
|
|
207
|
+
...this.mainItemTemplate,
|
|
208
|
+
definition_formal: mappedDefinition,
|
|
209
|
+
cst_type
|
|
210
|
+
};
|
|
211
|
+
applyMappingToConstituents([mainItem], aliasMapping, false);
|
|
212
|
+
|
|
213
|
+
if (takenAliases.includes(mainItem.alias)) {
|
|
214
|
+
mainItem.alias = generateAlias(mainItem.cst_type, this.targetSchema, [
|
|
215
|
+
...takenAliases,
|
|
216
|
+
this.mainItemTemplate.alias
|
|
217
|
+
]);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return { item: mainItem, duplicateAlias: null };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private ensureDep(templateAlias: string, bindings: Map<string, string>, referrer?: string): void {
|
|
224
|
+
if (targetHasAlias(this.targetSchema, templateAlias) || !this.templateByAlias.has(templateAlias)) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (referrer && this.depBindings.has(referrer)) {
|
|
228
|
+
const refs = this.depRefs.get(referrer) ?? new Set<string>();
|
|
229
|
+
refs.add(templateAlias);
|
|
230
|
+
this.depRefs.set(referrer, refs);
|
|
231
|
+
}
|
|
232
|
+
const merged = mergeBindings(this.depBindings.get(templateAlias), bindings);
|
|
233
|
+
const changed =
|
|
234
|
+
!this.depBindings.has(templateAlias) || merged.size !== (this.depBindings.get(templateAlias)?.size ?? 0);
|
|
235
|
+
this.depBindings.set(templateAlias, merged);
|
|
236
|
+
if (!changed && referrer) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const template = this.templateByAlias.get(templateAlias)!;
|
|
240
|
+
const args = bindingMapToArgs(getTemplateParams(template), merged);
|
|
241
|
+
const definition = substituteTemplateArgs(template.definition_formal, argumentValuesToMapping(args));
|
|
242
|
+
this.collectDependencies(definition, argumentValuesToMapping(args), templateAlias);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private collectDependencies(expression: string, scope: AliasMapping, referrer?: string): void {
|
|
246
|
+
const ast = parseRSLangExpression(expression);
|
|
247
|
+
if (!ast) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
for (const call of extractFuncCalls(ast, expression)) {
|
|
251
|
+
const template = this.templateByAlias.get(call.callee);
|
|
252
|
+
if (!template) {
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
const scopedArgs = call.args.map(arg => substituteLocalAliases(arg, scope));
|
|
256
|
+
const bindings = bindingsFromCall(getTemplateParams(template), scopedArgs, scope);
|
|
257
|
+
this.ensureDep(call.callee, bindings, referrer);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// ======= Internals ========
|
|
263
|
+
|
|
264
|
+
/** Normalized formal definitions in the target schema and in the current plan. */
|
|
265
|
+
class FormalDefinitionIndex {
|
|
266
|
+
private readonly byKey = new Map<string, string>();
|
|
267
|
+
|
|
268
|
+
register(cst_type: CstType, definition_formal: string, alias: string): void {
|
|
269
|
+
const normalized = normalizeExpression(definition_formal, cst_type);
|
|
270
|
+
if (!normalized) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
this.byKey.set(formalDefinitionKey(cst_type, normalized), alias);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
find(cst_type: CstType, definition_formal: string): string | null {
|
|
277
|
+
const normalized = normalizeExpression(definition_formal, cst_type);
|
|
278
|
+
if (!normalized) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
return this.byKey.get(formalDefinitionKey(cst_type, normalized)) ?? null;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function createFormalDefinitionIndex(schema: RSForm): FormalDefinitionIndex {
|
|
286
|
+
const index = new FormalDefinitionIndex();
|
|
287
|
+
for (const cst of schema.items) {
|
|
288
|
+
index.register(cst.cst_type, cst.definition_formal, cst.alias);
|
|
289
|
+
}
|
|
290
|
+
return index;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function formalDefinitionKey(cst_type: CstType, normalized: string): string {
|
|
294
|
+
return `${cst_type}\0${normalized}`;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function instantiateTemplateItem(
|
|
298
|
+
template: Constituenta,
|
|
299
|
+
args: ArgumentValue[],
|
|
300
|
+
alias: string
|
|
301
|
+
): TemplateInstantiationItem {
|
|
302
|
+
return {
|
|
303
|
+
alias,
|
|
304
|
+
cst_type: inferTemplatedType(template.cst_type, args),
|
|
305
|
+
crucial: template.crucial,
|
|
306
|
+
convention: template.convention,
|
|
307
|
+
definition_formal: substituteTemplateArgs(template.definition_formal, argumentValuesToMapping(args)),
|
|
308
|
+
definition_raw: template.definition_raw,
|
|
309
|
+
term_raw: template.term_raw,
|
|
310
|
+
term_forms: [...template.term_forms],
|
|
311
|
+
typification_manual: template.typification_manual,
|
|
312
|
+
value_is_property: template.value_is_property
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function bindingsFromCall(params: TemplateParam[], callArgs: string[], scope: AliasMapping): Map<string, string> {
|
|
317
|
+
const bindings = new Map<string, string>();
|
|
318
|
+
params.forEach((param, index) => {
|
|
319
|
+
const value = resolveArgBinding(callArgs[index] ?? '', scope);
|
|
320
|
+
if (value) {
|
|
321
|
+
bindings.set(param.alias, value);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
return bindings;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function resolveArgBinding(argExpr: string, scope: AliasMapping): string | undefined {
|
|
328
|
+
const scoped = substituteLocalAliases(argExpr, scope).trim();
|
|
329
|
+
if (isGlobalAlias(scoped)) {
|
|
330
|
+
return scoped;
|
|
331
|
+
}
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function mergeBindings(existing: Map<string, string> | undefined, incoming: Map<string, string>): Map<string, string> {
|
|
336
|
+
const result = new Map(existing);
|
|
337
|
+
for (const [alias, value] of incoming) {
|
|
338
|
+
if (!result.has(alias)) {
|
|
339
|
+
result.set(alias, value);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function bindingMapToArgs(params: TemplateParam[], bindings: Map<string, string>): ArgumentValue[] {
|
|
346
|
+
return params.map(param => ({
|
|
347
|
+
alias: param.alias,
|
|
348
|
+
typification: param.typification,
|
|
349
|
+
value: bindings.get(param.alias)
|
|
350
|
+
}));
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function getTemplateParams(template: Constituenta): TemplateParam[] {
|
|
354
|
+
const effectiveType = template.effectiveType;
|
|
355
|
+
if (effectiveType && (effectiveType.typeID === TypeID.function || effectiveType.typeID === TypeID.predicate)) {
|
|
356
|
+
return effectiveType.args.map(arg => ({
|
|
357
|
+
alias: arg.alias,
|
|
358
|
+
typification: labelType(arg.type)
|
|
359
|
+
}));
|
|
360
|
+
}
|
|
361
|
+
const { head } = splitTemplateDefinition(template.definition_formal);
|
|
362
|
+
if (!head) {
|
|
363
|
+
return [];
|
|
364
|
+
}
|
|
365
|
+
return head.split(',').map(part => {
|
|
366
|
+
const trimmed = part.trim();
|
|
367
|
+
const separator = trimmed.indexOf('∈');
|
|
368
|
+
if (separator === -1) {
|
|
369
|
+
return { alias: trimmed, typification: '' };
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
alias: trimmed.slice(0, separator).trim(),
|
|
373
|
+
typification: trimmed.slice(separator + 1).trim()
|
|
374
|
+
};
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function extractFuncCalls(ast: AstNode, expression: string): FuncCallRef[] {
|
|
379
|
+
const result: FuncCallRef[] = [];
|
|
380
|
+
visitAstDFS(ast, node => {
|
|
381
|
+
if (node.typeID !== TokenID.NT_FUNC_CALL || node.children.length < 1) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const calleeNode = node.children[0];
|
|
385
|
+
if (
|
|
386
|
+
calleeNode.typeID !== TokenID.ID_FUNCTION &&
|
|
387
|
+
calleeNode.typeID !== TokenID.ID_PREDICATE &&
|
|
388
|
+
calleeNode.typeID !== TokenID.ID_GLOBAL
|
|
389
|
+
) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
const callee = expression.slice(calleeNode.from, calleeNode.to);
|
|
393
|
+
if (!/^[FP]\d+$/.test(callee)) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
result.push({
|
|
397
|
+
callee,
|
|
398
|
+
args: node.children.slice(1).map(child => expression.slice(child.from, child.to).trim())
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
return result;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function targetHasAlias(schema: RSForm, alias: string): boolean {
|
|
405
|
+
return schema.items.some(cst => cst.alias === alias);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function topologicalSort(aliases: string[], refs: Map<string, Set<string>>): string[] {
|
|
409
|
+
const remaining = new Set(aliases);
|
|
410
|
+
const sorted: string[] = [];
|
|
411
|
+
while (remaining.size > 0) {
|
|
412
|
+
const ready = [...remaining].filter(alias => {
|
|
413
|
+
const deps = refs.get(alias) ?? new Set<string>();
|
|
414
|
+
return [...deps].every(dep => !remaining.has(dep));
|
|
415
|
+
});
|
|
416
|
+
if (ready.length === 0) {
|
|
417
|
+
return [...remaining, ...sorted].reverse();
|
|
418
|
+
}
|
|
419
|
+
ready.sort();
|
|
420
|
+
for (const alias of ready) {
|
|
421
|
+
sorted.push(alias);
|
|
422
|
+
remaining.delete(alias);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return sorted;
|
|
426
|
+
}
|
package/src/rslang/api.test.ts
CHANGED
|
@@ -3,11 +3,28 @@ import { describe, expect, it } from 'vitest';
|
|
|
3
3
|
import {
|
|
4
4
|
applyTypificationMapping,
|
|
5
5
|
extractGlobals,
|
|
6
|
+
isGlobalAlias,
|
|
6
7
|
isSetTypification,
|
|
7
8
|
isSimpleExpression,
|
|
9
|
+
parseRSLangExpression,
|
|
8
10
|
splitTemplateDefinition
|
|
9
11
|
} from './api';
|
|
10
12
|
|
|
13
|
+
describe('Testing isGlobalAlias', () => {
|
|
14
|
+
it.each([
|
|
15
|
+
['X1', true],
|
|
16
|
+
['F42', true],
|
|
17
|
+
['P6', true],
|
|
18
|
+
['S1', true],
|
|
19
|
+
['X1×X2', false],
|
|
20
|
+
['F42[α]', false],
|
|
21
|
+
['α', false],
|
|
22
|
+
['', false]
|
|
23
|
+
])('isGlobalAlias("%s") -> %s', (value, expected) => {
|
|
24
|
+
expect(isGlobalAlias(value)).toBe(expected);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
11
28
|
const globalsData: [string, string][] = [
|
|
12
29
|
['', ''],
|
|
13
30
|
['X1', 'X1'],
|
|
@@ -114,3 +131,14 @@ describe('Testing isSetTypification', () => {
|
|
|
114
131
|
});
|
|
115
132
|
});
|
|
116
133
|
});
|
|
134
|
+
|
|
135
|
+
describe('parseRSLangExpression', () => {
|
|
136
|
+
it('returns null for blank input', () => {
|
|
137
|
+
expect(parseRSLangExpression('')).toBeNull();
|
|
138
|
+
expect(parseRSLangExpression(' ')).toBeNull();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('parses non-blank expressions', () => {
|
|
142
|
+
expect(parseRSLangExpression('1 = 2')?.hasError).toBe(false);
|
|
143
|
+
});
|
|
144
|
+
});
|
package/src/rslang/api.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Module: API for RSLanguage.
|
|
3
3
|
*/
|
|
4
|
-
import { type AstNode } from '../parsing';
|
|
4
|
+
import { type AstNode, buildTree } from '../parsing';
|
|
5
5
|
|
|
6
|
+
import { normalizeAST } from './parser/normalize';
|
|
7
|
+
import { parser as rslangParser } from './parser/parser';
|
|
6
8
|
import { ArgumentsExtractor, type ArgumentsType } from './semantic/arguments-extractor';
|
|
7
9
|
export { generateExpressionFromAst } from './parser/expression-generator';
|
|
8
10
|
|
|
@@ -11,11 +13,33 @@ export type AliasMapping = Record<string, string>;
|
|
|
11
13
|
|
|
12
14
|
// cspell:disable
|
|
13
15
|
const LOCALS_REGEXP = /[_a-zα-ω][a-zα-ω]*\d*/g;
|
|
14
|
-
const
|
|
16
|
+
const GLOBAL_ALIAS_SOURCE = '[XCSADFPTN]\\d+';
|
|
17
|
+
const GLOBALS_REGEXP = new RegExp(GLOBAL_ALIAS_SOURCE, 'g');
|
|
15
18
|
const COMPLEX_SYMBOLS_REGEXP = /[∀∃×ℬ;|:]/g;
|
|
16
19
|
const TYPIFICATION_SET = /^ℬ+\([ℬ\(((X|C)\d+|Z)\)×]*\)$/g;
|
|
17
20
|
// cspell:enable
|
|
18
21
|
|
|
22
|
+
/** Whether {@link value} is a single schema global alias (e.g. `X1`, `F42`). */
|
|
23
|
+
export function isGlobalAlias(value: string): boolean {
|
|
24
|
+
return new RegExp(`^${GLOBAL_ALIAS_SOURCE}$`).test(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Parse a RSLang expression into a normalized AST.
|
|
29
|
+
*
|
|
30
|
+
* Returns `null` for blank input. Parses the original `expression` string so node
|
|
31
|
+
* offsets match the source text (required for slice-based AST walks).
|
|
32
|
+
*/
|
|
33
|
+
export function parseRSLangExpression(expression: string): AstNode | null {
|
|
34
|
+
if (!expression.trim()) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const tree = rslangParser.parse(expression);
|
|
38
|
+
const ast = buildTree(tree.cursor());
|
|
39
|
+
normalizeAST(ast, expression);
|
|
40
|
+
return ast;
|
|
41
|
+
}
|
|
42
|
+
|
|
19
43
|
/** Extract arguments from AST. */
|
|
20
44
|
export function extractArguments(ast: AstNode): ArgumentsType[] {
|
|
21
45
|
return new ArgumentsExtractor().run(ast);
|
|
@@ -100,6 +124,11 @@ export function substituteTemplateArgs(expression: string, mapping: AliasMapping
|
|
|
100
124
|
}
|
|
101
125
|
}
|
|
102
126
|
|
|
127
|
+
/** Substitute local parameter aliases in an expression fragment. */
|
|
128
|
+
export function substituteLocalAliases(expression: string, mapping: AliasMapping): string {
|
|
129
|
+
return applyPattern(expression, mapping, LOCALS_REGEXP);
|
|
130
|
+
}
|
|
131
|
+
|
|
103
132
|
/** Apply alias mapping. */
|
|
104
133
|
export function applyAliasMapping(target: string, mapping: AliasMapping): string {
|
|
105
134
|
return applyPattern(target, mapping, GLOBALS_REGEXP);
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { type AstNode
|
|
1
|
+
import { type AstNode } from '../../parsing';
|
|
2
|
+
import { parseRSLangExpression } from '../api';
|
|
2
3
|
import { annotateError } from '../ast-annotations';
|
|
3
4
|
import { RSErrorCode, type RSErrorDescription } from '../error';
|
|
4
5
|
import { labelType } from '../labels';
|
|
5
|
-
import { normalizeAST } from '../parser/normalize';
|
|
6
|
-
import { parser as rslangParser } from '../parser/parser';
|
|
7
6
|
import { extractSyntaxErrors } from '../parser/syntax-errors';
|
|
8
7
|
import { TokenID } from '../parser/token';
|
|
9
8
|
|
|
@@ -74,10 +73,10 @@ export class RSLangAnalyzer {
|
|
|
74
73
|
}
|
|
75
74
|
|
|
76
75
|
public checkFast(expression: string, options?: AnalysisOptions): AnalysisFast {
|
|
77
|
-
|
|
76
|
+
const ast = parseRSLangExpression(expression);
|
|
77
|
+
if (!ast) {
|
|
78
78
|
return { success: false, type: null, valueClass: null, ast: null };
|
|
79
79
|
}
|
|
80
|
-
const ast = this.parse(expression);
|
|
81
80
|
if (ast.hasError) {
|
|
82
81
|
return { success: false, type: null, valueClass: null, ast: ast };
|
|
83
82
|
}
|
|
@@ -109,11 +108,11 @@ export class RSLangAnalyzer {
|
|
|
109
108
|
const reporter = (error: RSErrorDescription) => {
|
|
110
109
|
errors.push(error);
|
|
111
110
|
};
|
|
112
|
-
|
|
111
|
+
const ast = parseRSLangExpression(expression);
|
|
112
|
+
if (!ast) {
|
|
113
113
|
reporter({ code: RSErrorCode.cstEmptyDerived, from: 0, to: 0 });
|
|
114
114
|
return { success: false, type: null, valueClass: null, errors: errors, ast: null };
|
|
115
115
|
}
|
|
116
|
-
const ast = this.parse(expression);
|
|
117
116
|
if (ast.hasError) {
|
|
118
117
|
extractSyntaxErrors(ast, expression, reporter, options?.annotateErrors ?? false, {
|
|
119
118
|
expected: options?.expected
|
|
@@ -154,13 +153,6 @@ export class RSLangAnalyzer {
|
|
|
154
153
|
: this.valueAuditor.run(ast, reporter, options?.annotateErrors ?? false);
|
|
155
154
|
return { success: true, type, valueClass, errors, ast };
|
|
156
155
|
}
|
|
157
|
-
|
|
158
|
-
private parse(expression: string): AstNode {
|
|
159
|
-
const tree = rslangParser.parse(expression);
|
|
160
|
-
const ast = buildTree(tree.cursor());
|
|
161
|
-
normalizeAST(ast, expression);
|
|
162
|
-
return ast;
|
|
163
|
-
}
|
|
164
156
|
}
|
|
165
157
|
|
|
166
158
|
// ======= Internals ========
|