@rsconcept/domain 1.0.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.
Files changed (224) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +55 -0
  3. package/dist/cctext/index.d.ts +1 -0
  4. package/dist/cctext/index.js +42 -0
  5. package/dist/cctext/index.js.map +1 -0
  6. package/dist/cctext/language-api.d.ts +43 -0
  7. package/dist/cctext/language-api.js +252 -0
  8. package/dist/cctext/language-api.js.map +1 -0
  9. package/dist/cctext/language.d.ts +58 -0
  10. package/dist/cctext/language.js +44 -0
  11. package/dist/cctext/language.js.map +1 -0
  12. package/dist/graph/graph.d.ts +62 -0
  13. package/dist/graph/graph.js +385 -0
  14. package/dist/graph/graph.js.map +1 -0
  15. package/dist/graph/index.d.ts +1 -0
  16. package/dist/graph/index.js +384 -0
  17. package/dist/graph/index.js.map +1 -0
  18. package/dist/index.d.ts +28 -0
  19. package/dist/index.js +5851 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/library/folder-tree.d.ts +32 -0
  22. package/dist/library/folder-tree.js +136 -0
  23. package/dist/library/folder-tree.js.map +1 -0
  24. package/dist/library/index.d.ts +17 -0
  25. package/dist/library/index.js +2800 -0
  26. package/dist/library/index.js.map +1 -0
  27. package/dist/library/library-api.d.ts +6 -0
  28. package/dist/library/library-api.js +13 -0
  29. package/dist/library/library-api.js.map +1 -0
  30. package/dist/library/library.d.ts +56 -0
  31. package/dist/library/library.js +23 -0
  32. package/dist/library/library.js.map +1 -0
  33. package/dist/library/oss-api.d.ts +47 -0
  34. package/dist/library/oss-api.js +1105 -0
  35. package/dist/library/oss-api.js.map +1 -0
  36. package/dist/library/oss-layout-api.d.ts +36 -0
  37. package/dist/library/oss-layout-api.js +330 -0
  38. package/dist/library/oss-layout-api.js.map +1 -0
  39. package/dist/library/oss-layout.d.ts +25 -0
  40. package/dist/library/oss-layout.js +1 -0
  41. package/dist/library/oss-layout.js.map +1 -0
  42. package/dist/library/oss.d.ts +136 -0
  43. package/dist/library/oss.js +30 -0
  44. package/dist/library/oss.js.map +1 -0
  45. package/dist/library/rsengine.d.ts +116 -0
  46. package/dist/library/rsengine.js +2604 -0
  47. package/dist/library/rsengine.js.map +1 -0
  48. package/dist/library/rsform-api.d.ts +74 -0
  49. package/dist/library/rsform-api.js +879 -0
  50. package/dist/library/rsform-api.js.map +1 -0
  51. package/dist/library/rsform.d.ts +206 -0
  52. package/dist/library/rsform.js +32 -0
  53. package/dist/library/rsform.js.map +1 -0
  54. package/dist/library/rsmodel-api.d.ts +43 -0
  55. package/dist/library/rsmodel-api.js +836 -0
  56. package/dist/library/rsmodel-api.js.map +1 -0
  57. package/dist/library/rsmodel.d.ts +52 -0
  58. package/dist/library/rsmodel.js +25 -0
  59. package/dist/library/rsmodel.js.map +1 -0
  60. package/dist/library/structure-planner.d.ts +33 -0
  61. package/dist/library/structure-planner.js +481 -0
  62. package/dist/library/structure-planner.js.map +1 -0
  63. package/dist/parsing/ast.d.ts +49 -0
  64. package/dist/parsing/ast.js +93 -0
  65. package/dist/parsing/ast.js.map +1 -0
  66. package/dist/parsing/index.d.ts +3 -0
  67. package/dist/parsing/index.js +141 -0
  68. package/dist/parsing/index.js.map +1 -0
  69. package/dist/parsing/lezer-tree.d.ts +13 -0
  70. package/dist/parsing/lezer-tree.js +50 -0
  71. package/dist/parsing/lezer-tree.js.map +1 -0
  72. package/dist/rslang/api.d.ts +53 -0
  73. package/dist/rslang/api.js +846 -0
  74. package/dist/rslang/api.js.map +1 -0
  75. package/dist/rslang/ast-annotations.d.ts +18 -0
  76. package/dist/rslang/ast-annotations.js +56 -0
  77. package/dist/rslang/ast-annotations.js.map +1 -0
  78. package/dist/rslang/error.d.ts +85 -0
  79. package/dist/rslang/error.js +159 -0
  80. package/dist/rslang/error.js.map +1 -0
  81. package/dist/rslang/eval/calculator.d.ts +43 -0
  82. package/dist/rslang/eval/calculator.js +1639 -0
  83. package/dist/rslang/eval/calculator.js.map +1 -0
  84. package/dist/rslang/eval/evaluation-cache.d.ts +36 -0
  85. package/dist/rslang/eval/evaluation-cache.js +310 -0
  86. package/dist/rslang/eval/evaluation-cache.js.map +1 -0
  87. package/dist/rslang/eval/evaluator.d.ts +70 -0
  88. package/dist/rslang/eval/evaluator.js +1514 -0
  89. package/dist/rslang/eval/evaluator.js.map +1 -0
  90. package/dist/rslang/eval/value-api.d.ts +48 -0
  91. package/dist/rslang/eval/value-api.js +490 -0
  92. package/dist/rslang/eval/value-api.js.map +1 -0
  93. package/dist/rslang/eval/value.d.ts +36 -0
  94. package/dist/rslang/eval/value.js +118 -0
  95. package/dist/rslang/eval/value.js.map +1 -0
  96. package/dist/rslang/index.d.ts +17 -0
  97. package/dist/rslang/index.js +4314 -0
  98. package/dist/rslang/index.js.map +1 -0
  99. package/dist/rslang/labels.d.ts +16 -0
  100. package/dist/rslang/labels.js +315 -0
  101. package/dist/rslang/labels.js.map +1 -0
  102. package/dist/rslang/parser/expression-generator.d.ts +10 -0
  103. package/dist/rslang/parser/expression-generator.js +451 -0
  104. package/dist/rslang/parser/expression-generator.js.map +1 -0
  105. package/dist/rslang/parser/normalize.d.ts +11 -0
  106. package/dist/rslang/parser/normalize.js +507 -0
  107. package/dist/rslang/parser/normalize.js.map +1 -0
  108. package/dist/rslang/parser/parser.d.ts +5 -0
  109. package/dist/rslang/parser/parser.js +24 -0
  110. package/dist/rslang/parser/parser.js.map +1 -0
  111. package/dist/rslang/parser/parser.terms.d.ts +42 -0
  112. package/dist/rslang/parser/parser.terms.js +84 -0
  113. package/dist/rslang/parser/parser.terms.js.map +1 -0
  114. package/dist/rslang/parser/syntax-errors.d.ts +11 -0
  115. package/dist/rslang/parser/syntax-errors.js +403 -0
  116. package/dist/rslang/parser/syntax-errors.js.map +1 -0
  117. package/dist/rslang/parser/token.d.ts +79 -0
  118. package/dist/rslang/parser/token.js +95 -0
  119. package/dist/rslang/parser/token.js.map +1 -0
  120. package/dist/rslang/semantic/analyzer.d.ts +39 -0
  121. package/dist/rslang/semantic/analyzer.js +2604 -0
  122. package/dist/rslang/semantic/analyzer.js.map +1 -0
  123. package/dist/rslang/semantic/arguments-extractor.d.ts +42 -0
  124. package/dist/rslang/semantic/arguments-extractor.js +366 -0
  125. package/dist/rslang/semantic/arguments-extractor.js.map +1 -0
  126. package/dist/rslang/semantic/type-auditor.d.ts +73 -0
  127. package/dist/rslang/semantic/type-auditor.js +1570 -0
  128. package/dist/rslang/semantic/type-auditor.js.map +1 -0
  129. package/dist/rslang/semantic/typification-api.d.ts +27 -0
  130. package/dist/rslang/semantic/typification-api.js +320 -0
  131. package/dist/rslang/semantic/typification-api.js.map +1 -0
  132. package/dist/rslang/semantic/typification-parser.d.ts +12 -0
  133. package/dist/rslang/semantic/typification-parser.js +226 -0
  134. package/dist/rslang/semantic/typification-parser.js.map +1 -0
  135. package/dist/rslang/semantic/typification.d.ts +119 -0
  136. package/dist/rslang/semantic/typification.js +74 -0
  137. package/dist/rslang/semantic/typification.js.map +1 -0
  138. package/dist/rslang/semantic/value-auditor.d.ts +43 -0
  139. package/dist/rslang/semantic/value-auditor.js +523 -0
  140. package/dist/rslang/semantic/value-auditor.js.map +1 -0
  141. package/dist/rslang/semantic/value-class.d.ts +10 -0
  142. package/dist/rslang/semantic/value-class.js +9 -0
  143. package/dist/rslang/semantic/value-class.js.map +1 -0
  144. package/dist/rslang/typification-graph.d.ts +33 -0
  145. package/dist/rslang/typification-graph.js +311 -0
  146. package/dist/rslang/typification-graph.js.map +1 -0
  147. package/dist/shared/branded.d.ts +7 -0
  148. package/dist/shared/branded.js +1 -0
  149. package/dist/shared/branded.js.map +1 -0
  150. package/dist/shared/hash.d.ts +6 -0
  151. package/dist/shared/hash.js +18 -0
  152. package/dist/shared/hash.js.map +1 -0
  153. package/dist/shared/index.d.ts +2 -0
  154. package/dist/shared/index.js +18 -0
  155. package/dist/shared/index.js.map +1 -0
  156. package/package.json +184 -0
  157. package/src/cctext/index.ts +9 -0
  158. package/src/cctext/language-api.test.ts +149 -0
  159. package/src/cctext/language-api.ts +285 -0
  160. package/src/cctext/language.ts +80 -0
  161. package/src/graph/graph.test.ts +392 -0
  162. package/src/graph/graph.ts +433 -0
  163. package/src/graph/index.ts +1 -0
  164. package/src/index.ts +96 -0
  165. package/src/library/folder-tree.test.ts +47 -0
  166. package/src/library/folder-tree.ts +156 -0
  167. package/src/library/index.ts +46 -0
  168. package/src/library/library-api.test.ts +32 -0
  169. package/src/library/library-api.ts +11 -0
  170. package/src/library/library.ts +61 -0
  171. package/src/library/oss-api.ts +449 -0
  172. package/src/library/oss-layout-api.ts +377 -0
  173. package/src/library/oss-layout.ts +27 -0
  174. package/src/library/oss.ts +150 -0
  175. package/src/library/rsengine.ts +593 -0
  176. package/src/library/rsform-api.ts +533 -0
  177. package/src/library/rsform.ts +228 -0
  178. package/src/library/rsmodel-api.ts +340 -0
  179. package/src/library/rsmodel.ts +50 -0
  180. package/src/library/structure-planner.ts +143 -0
  181. package/src/parsing/ast.ts +136 -0
  182. package/src/parsing/index.ts +15 -0
  183. package/src/parsing/lezer-tree.ts +69 -0
  184. package/src/rslang/api.test.ts +116 -0
  185. package/src/rslang/api.ts +183 -0
  186. package/src/rslang/ast-annotations.ts +70 -0
  187. package/src/rslang/error.ts +129 -0
  188. package/src/rslang/eval/calculator.test.ts +124 -0
  189. package/src/rslang/eval/calculator.ts +121 -0
  190. package/src/rslang/eval/evaluation-cache.ts +257 -0
  191. package/src/rslang/eval/evaluator.test.ts +352 -0
  192. package/src/rslang/eval/evaluator.ts +935 -0
  193. package/src/rslang/eval/value-api.test.ts +105 -0
  194. package/src/rslang/eval/value-api.ts +444 -0
  195. package/src/rslang/eval/value.ts +102 -0
  196. package/src/rslang/index.ts +23 -0
  197. package/src/rslang/labels.ts +191 -0
  198. package/src/rslang/parser/expression-generator.test.ts +100 -0
  199. package/src/rslang/parser/expression-generator.ts +466 -0
  200. package/src/rslang/parser/normalize.test.ts +99 -0
  201. package/src/rslang/parser/normalize.ts +462 -0
  202. package/src/rslang/parser/parser.terms.ts +42 -0
  203. package/src/rslang/parser/parser.test.ts +153 -0
  204. package/src/rslang/parser/parser.ts +20 -0
  205. package/src/rslang/parser/rslang.grammar +251 -0
  206. package/src/rslang/parser/syntax-errors.ts +209 -0
  207. package/src/rslang/parser/token.ts +106 -0
  208. package/src/rslang/semantic/analyzer.test.ts +59 -0
  209. package/src/rslang/semantic/analyzer.ts +179 -0
  210. package/src/rslang/semantic/arguments-extractor.ts +327 -0
  211. package/src/rslang/semantic/type-auditor.test.ts +326 -0
  212. package/src/rslang/semantic/type-auditor.ts +1049 -0
  213. package/src/rslang/semantic/typification-api.test.ts +46 -0
  214. package/src/rslang/semantic/typification-api.ts +321 -0
  215. package/src/rslang/semantic/typification-parser.test.ts +50 -0
  216. package/src/rslang/semantic/typification-parser.ts +220 -0
  217. package/src/rslang/semantic/typification.ts +180 -0
  218. package/src/rslang/semantic/value-auditor.test.ts +206 -0
  219. package/src/rslang/semantic/value-auditor.ts +332 -0
  220. package/src/rslang/semantic/value-class.ts +11 -0
  221. package/src/rslang/typification-graph.ts +155 -0
  222. package/src/shared/branded.ts +6 -0
  223. package/src/shared/hash.ts +17 -0
  224. package/src/shared/index.ts +2 -0
@@ -0,0 +1,1105 @@
1
+ // src/graph/graph.ts
2
+ var GraphNode = class _GraphNode {
3
+ /** Unique identifier of the node. */
4
+ id;
5
+ /** List of outgoing nodes. */
6
+ outputs;
7
+ /** List of incoming nodes. */
8
+ inputs;
9
+ constructor(id) {
10
+ this.id = id;
11
+ this.outputs = [];
12
+ this.inputs = [];
13
+ }
14
+ clone() {
15
+ const result = new _GraphNode(this.id);
16
+ result.outputs = [...this.outputs];
17
+ result.inputs = [...this.inputs];
18
+ return result;
19
+ }
20
+ addOutput(node) {
21
+ this.outputs.push(node);
22
+ }
23
+ addInput(node) {
24
+ this.inputs.push(node);
25
+ }
26
+ removeInput(target) {
27
+ const index = this.inputs.findIndex((node) => node === target);
28
+ return index > -1 ? this.inputs.splice(index, 1)[0] : null;
29
+ }
30
+ removeOutput(target) {
31
+ const index = this.outputs.findIndex((node) => node === target);
32
+ return index > -1 ? this.outputs.splice(index, 1)[0] : null;
33
+ }
34
+ };
35
+ var Graph = class _Graph {
36
+ /** Map of nodes. */
37
+ nodes = /* @__PURE__ */ new Map();
38
+ constructor(arr) {
39
+ if (!arr) {
40
+ return;
41
+ }
42
+ arr.forEach((edge) => {
43
+ if (edge.length === 1) {
44
+ this.addNode(edge[0]);
45
+ } else {
46
+ this.addEdge(edge[0], edge[1]);
47
+ }
48
+ });
49
+ }
50
+ clone() {
51
+ const result = new _Graph();
52
+ this.nodes.forEach((node) => result.nodes.set(node.id, node.clone()));
53
+ return result;
54
+ }
55
+ at(target) {
56
+ return this.nodes.get(target);
57
+ }
58
+ addNode(target) {
59
+ let node = this.nodes.get(target);
60
+ if (!node) {
61
+ node = new GraphNode(target);
62
+ this.nodes.set(target, node);
63
+ }
64
+ return node;
65
+ }
66
+ hasNode(target) {
67
+ return !!this.nodes.get(target);
68
+ }
69
+ removeNode(target) {
70
+ this.nodes.forEach((node) => {
71
+ node.removeInput(target);
72
+ node.removeOutput(target);
73
+ });
74
+ this.nodes.delete(target);
75
+ }
76
+ foldNode(target) {
77
+ const nodeToRemove = this.nodes.get(target);
78
+ if (!nodeToRemove) {
79
+ return;
80
+ }
81
+ nodeToRemove.inputs.forEach((input) => {
82
+ nodeToRemove.outputs.forEach((output) => {
83
+ this.addEdge(input, output);
84
+ });
85
+ });
86
+ this.removeNode(target);
87
+ }
88
+ removeIsolated() {
89
+ const result = [];
90
+ this.nodes.forEach((node) => {
91
+ if (node.outputs.length === 0 && node.inputs.length === 0) {
92
+ result.push(node);
93
+ this.nodes.delete(node.id);
94
+ }
95
+ });
96
+ return result;
97
+ }
98
+ addEdge(source, destination) {
99
+ if (this.hasEdge(source, destination)) {
100
+ return;
101
+ }
102
+ const sourceNode = this.addNode(source);
103
+ const destinationNode = this.addNode(destination);
104
+ sourceNode.addOutput(destinationNode.id);
105
+ destinationNode.addInput(sourceNode.id);
106
+ }
107
+ removeEdge(source, destination) {
108
+ const sourceNode = this.nodes.get(source);
109
+ const destinationNode = this.nodes.get(destination);
110
+ if (sourceNode && destinationNode) {
111
+ sourceNode.removeOutput(destination);
112
+ destinationNode.removeInput(source);
113
+ }
114
+ }
115
+ hasEdge(source, destination) {
116
+ const sourceNode = this.nodes.get(source);
117
+ if (!sourceNode) {
118
+ return false;
119
+ }
120
+ return !!sourceNode.outputs.find((id) => id === destination);
121
+ }
122
+ isReachable(source, destination) {
123
+ return this.expandAllOutputs([source]).includes(destination);
124
+ }
125
+ rootNodes() {
126
+ return [...this.nodes.keys()].filter((id) => !this.nodes.get(id)?.inputs.length);
127
+ }
128
+ expandOutputs(origin) {
129
+ const result = [];
130
+ origin.forEach((id) => {
131
+ const node = this.nodes.get(id);
132
+ if (node) {
133
+ node.outputs.forEach((child) => {
134
+ if (!origin.includes(child) && !result.includes(child)) {
135
+ result.push(child);
136
+ }
137
+ });
138
+ }
139
+ });
140
+ return result;
141
+ }
142
+ expandInputs(origin) {
143
+ const result = [];
144
+ origin.forEach((id) => {
145
+ const node = this.nodes.get(id);
146
+ if (node) {
147
+ node.inputs.forEach((child) => {
148
+ if (!origin.includes(child) && !result.includes(child)) {
149
+ result.push(child);
150
+ }
151
+ });
152
+ }
153
+ });
154
+ return result;
155
+ }
156
+ expandAllOutputs(origin) {
157
+ const result = this.expandOutputs(origin);
158
+ if (result.length === 0) {
159
+ return [];
160
+ }
161
+ const marked = /* @__PURE__ */ new Map();
162
+ origin.forEach((id) => marked.set(id, true));
163
+ let position = 0;
164
+ while (position < result.length) {
165
+ const node = this.nodes.get(result[position]);
166
+ if (node && !marked.get(node.id)) {
167
+ marked.set(node.id, true);
168
+ node.outputs.forEach((child) => {
169
+ if (!marked.get(child) && !result.find((id) => id === child)) {
170
+ result.push(child);
171
+ }
172
+ });
173
+ }
174
+ position += 1;
175
+ }
176
+ return result;
177
+ }
178
+ expandAllInputs(origin) {
179
+ const result = this.expandInputs(origin);
180
+ if (result.length === 0) {
181
+ return [];
182
+ }
183
+ const marked = /* @__PURE__ */ new Map();
184
+ origin.forEach((id) => marked.set(id, true));
185
+ let position = 0;
186
+ while (position < result.length) {
187
+ const node = this.nodes.get(result[position]);
188
+ if (node && !marked.get(node.id)) {
189
+ marked.set(node.id, true);
190
+ node.inputs.forEach((child) => {
191
+ if (!marked.get(child) && !result.find((id) => id === child)) {
192
+ result.push(child);
193
+ }
194
+ });
195
+ }
196
+ position += 1;
197
+ }
198
+ return result;
199
+ }
200
+ maximizePart(origin) {
201
+ const outputs = this.expandAllOutputs(origin);
202
+ const result = [...origin];
203
+ this.topologicalOrder().filter((id) => outputs.includes(id)).forEach((id) => {
204
+ const node = this.nodes.get(id);
205
+ if (node?.inputs.every((parent) => result.includes(parent))) {
206
+ result.push(id);
207
+ }
208
+ });
209
+ return result;
210
+ }
211
+ topologicalOrder() {
212
+ const result = [];
213
+ const marked = /* @__PURE__ */ new Set();
214
+ const nodeStack = [];
215
+ this.nodes.forEach((node) => {
216
+ if (marked.has(node.id)) {
217
+ return;
218
+ }
219
+ nodeStack.push(node.id);
220
+ while (nodeStack.length > 0) {
221
+ const item = nodeStack[nodeStack.length - 1];
222
+ if (marked.has(item)) {
223
+ if (!result.find((id) => id === item)) {
224
+ result.push(item);
225
+ }
226
+ nodeStack.pop();
227
+ } else {
228
+ marked.add(item);
229
+ const itemNode = this.nodes.get(item);
230
+ if (itemNode && itemNode.outputs.length > 0) {
231
+ itemNode.outputs.forEach((child) => {
232
+ if (!marked.has(child)) {
233
+ nodeStack.push(child);
234
+ }
235
+ });
236
+ }
237
+ }
238
+ }
239
+ });
240
+ return result.reverse();
241
+ }
242
+ /**
243
+ * Stably reorders the given node ids so that, for this DAG, no node appears before a transitive
244
+ * successor (edges: source → dependent; {@link GraphNode.outputs} lists dependents).
245
+ */
246
+ sortStable(target) {
247
+ if (target.length <= 1) {
248
+ return [...target];
249
+ }
250
+ const reachable = this.buildTransitiveClosureForSort();
251
+ const testSet = /* @__PURE__ */ new Set();
252
+ const result = [];
253
+ for (const nodeId of [...target].reverse()) {
254
+ const nodeReachable = reachable.get(nodeId) ?? /* @__PURE__ */ new Set();
255
+ const needMove = testSet.has(nodeId);
256
+ for (const childId of nodeReachable) {
257
+ testSet.add(childId);
258
+ }
259
+ if (!needMove) {
260
+ result.push(nodeId);
261
+ continue;
262
+ }
263
+ let inserted = false;
264
+ for (let index = 0; index < result.length; index++) {
265
+ const parent = result[index];
266
+ const parentReachable = reachable.get(parent) ?? /* @__PURE__ */ new Set();
267
+ if (nodeReachable.has(parent)) {
268
+ if (parentReachable.has(nodeId)) {
269
+ result.push(nodeId);
270
+ } else {
271
+ result.splice(index, 0, nodeId);
272
+ }
273
+ inserted = true;
274
+ break;
275
+ }
276
+ }
277
+ if (!inserted) {
278
+ result.push(nodeId);
279
+ }
280
+ }
281
+ result.reverse();
282
+ return result;
283
+ }
284
+ buildTransitiveClosureForSort() {
285
+ const closure = /* @__PURE__ */ new Map();
286
+ for (const node of this.nodes.values()) {
287
+ closure.set(node.id, new Set(node.outputs));
288
+ }
289
+ const order = this.topologicalOrder();
290
+ for (const nodeId of [...order].reverse()) {
291
+ const node = this.at(nodeId);
292
+ if (!node) {
293
+ continue;
294
+ }
295
+ const nodeClosure = closure.get(nodeId) ?? /* @__PURE__ */ new Set();
296
+ for (const parentId of node.inputs) {
297
+ const parentClosure = closure.get(parentId) ?? /* @__PURE__ */ new Set();
298
+ for (const childId of nodeClosure) {
299
+ parentClosure.add(childId);
300
+ }
301
+ closure.set(parentId, parentClosure);
302
+ }
303
+ }
304
+ return closure;
305
+ }
306
+ transitiveReduction() {
307
+ const order = this.topologicalOrder();
308
+ const marked = /* @__PURE__ */ new Map();
309
+ order.forEach((nodeID) => {
310
+ if (marked.get(nodeID)) {
311
+ return;
312
+ }
313
+ const stack = [];
314
+ stack.push({ id: nodeID, parents: [] });
315
+ while (stack.length > 0) {
316
+ const item = stack.splice(0, 1)[0];
317
+ const node = this.nodes.get(item.id);
318
+ if (node) {
319
+ node.outputs.forEach((child) => {
320
+ item.parents.forEach((parent) => this.removeEdge(parent, child));
321
+ stack.push({ id: child, parents: [item.id, ...item.parents] });
322
+ });
323
+ }
324
+ marked.set(item.id, true);
325
+ }
326
+ });
327
+ }
328
+ /**
329
+ * Finds a cycle in the graph.
330
+ *
331
+ * @returns {NodeID[] | null} The cycle if found, otherwise `null`.
332
+ * Uses non-recursive DFS.
333
+ */
334
+ findCycle() {
335
+ const visited = /* @__PURE__ */ new Set();
336
+ const nodeStack = /* @__PURE__ */ new Set();
337
+ const parents = /* @__PURE__ */ new Map();
338
+ for (const nodeId of this.nodes.keys()) {
339
+ if (visited.has(nodeId)) {
340
+ continue;
341
+ }
342
+ const callStack = [];
343
+ callStack.push({ nodeId, parentId: null });
344
+ while (callStack.length > 0) {
345
+ const { nodeId: nodeId2, parentId } = callStack[callStack.length - 1];
346
+ if (visited.has(nodeId2)) {
347
+ nodeStack.delete(nodeId2);
348
+ callStack.pop();
349
+ continue;
350
+ }
351
+ visited.add(nodeId2);
352
+ nodeStack.add(nodeId2);
353
+ if (parentId !== null) {
354
+ parents.set(nodeId2, parentId);
355
+ }
356
+ const currentNode = this.nodes.get(nodeId2);
357
+ for (const child of currentNode.outputs) {
358
+ if (!visited.has(child)) {
359
+ callStack.push({ nodeId: child, parentId: nodeId2 });
360
+ continue;
361
+ }
362
+ if (!nodeStack.has(child)) {
363
+ continue;
364
+ }
365
+ const cycle = [];
366
+ let current = nodeId2;
367
+ cycle.push(child);
368
+ while (current !== child) {
369
+ cycle.push(current);
370
+ current = parents.get(current);
371
+ }
372
+ cycle.push(child);
373
+ cycle.reverse();
374
+ return cycle;
375
+ }
376
+ }
377
+ }
378
+ return null;
379
+ }
380
+ };
381
+
382
+ // src/parsing/ast.ts
383
+ var TOKEN_ERROR = 0;
384
+
385
+ // src/rslang/parser/token.ts
386
+ var TokenID = {
387
+ // Global, local IDs and literals
388
+ ERROR: TOKEN_ERROR,
389
+ ID_LOCAL: 258,
390
+ ID_GLOBAL: 259,
391
+ ID_FUNCTION: 260,
392
+ ID_PREDICATE: 261,
393
+ ID_RADICAL: 262,
394
+ LIT_INTEGER: 263,
395
+ LIT_WHOLE_NUMBERS: 264,
396
+ LIT_EMPTYSET: 265,
397
+ // Arithmetic
398
+ PLUS: 266,
399
+ MINUS: 267,
400
+ MULTIPLY: 268,
401
+ // Integer predicate symbols
402
+ GREATER: 269,
403
+ LESSER: 270,
404
+ GREATER_OR_EQ: 271,
405
+ LESSER_OR_EQ: 272,
406
+ // Equality comparison
407
+ EQUAL: 273,
408
+ NOTEQUAL: 274,
409
+ // Logic predicate symbols
410
+ QUANTOR_UNIVERSAL: 275,
411
+ QUANTOR_EXISTS: 276,
412
+ LOGIC_NOT: 277,
413
+ LOGIC_EQUIVALENT: 278,
414
+ LOGIC_IMPLICATION: 279,
415
+ LOGIC_OR: 280,
416
+ LOGIC_AND: 281,
417
+ // Set theory predicate symbols
418
+ SET_IN: 282,
419
+ SET_NOT_IN: 283,
420
+ SUBSET: 284,
421
+ SUBSET_OR_EQ: 285,
422
+ NOT_SUBSET: 286,
423
+ // Set theory operators
424
+ DECART: 287,
425
+ SET_UNION: 288,
426
+ SET_INTERSECTION: 289,
427
+ SET_MINUS: 290,
428
+ SET_SYMMETRIC_MINUS: 291,
429
+ BOOLEAN: 292,
430
+ // Structure operations
431
+ BIGPR: 293,
432
+ SMALLPR: 294,
433
+ FILTER: 295,
434
+ CARD: 296,
435
+ BOOL: 297,
436
+ DEBOOL: 298,
437
+ REDUCE: 299,
438
+ // Term constructions prefixes
439
+ DECLARATIVE: 300,
440
+ RECURSIVE: 301,
441
+ IMPERATIVE: 302,
442
+ ITERATE: 303,
443
+ ASSIGN: 304,
444
+ // Punctuation
445
+ PUNCTUATION_DEFINE: 305,
446
+ PUNCTUATION_STRUCT: 306,
447
+ PUNCTUATION_PL: 307,
448
+ PUNCTUATION_PR: 308,
449
+ PUNCTUATION_CL: 309,
450
+ PUNCTUATION_CR: 310,
451
+ PUNCTUATION_SL: 311,
452
+ PUNCTUATION_SR: 312,
453
+ PUNCTUATION_BAR: 313,
454
+ PUNCTUATION_COMMA: 314,
455
+ PUNCTUATION_SEMICOLON: 315,
456
+ // ======= Non-terminal tokens =========
457
+ NT_ENUM_DECL: 316,
458
+ NT_TUPLE: 317,
459
+ NT_ENUMERATION: 318,
460
+ NT_TUPLE_DECL: 319,
461
+ NT_ARG_DECL: 320,
462
+ NT_FUNC_DEFINITION: 321,
463
+ NT_ARGUMENTS: 322,
464
+ NT_FUNC_CALL: 323,
465
+ NT_DECLARATIVE_EXPR: 324,
466
+ NT_IMPERATIVE_EXPR: 325,
467
+ NT_RECURSIVE_FULL: 326,
468
+ NT_RECURSIVE_SHORT: 327,
469
+ // ======= Helper tokens ========
470
+ INTERRUPT: 328,
471
+ END: 329
472
+ };
473
+
474
+ // src/rslang/api.ts
475
+ var GLOBALS_REGEXP = /[XCSADFPTN]\d+/g;
476
+ var TYPIFICATION_SET = /^ℬ+\([ℬ\(((X|C)\d+|Z)\)×]*\)$/g;
477
+ function isSetTypification(text) {
478
+ return !!text.match(TYPIFICATION_SET);
479
+ }
480
+ function applyAliasMapping(target, mapping) {
481
+ return applyPattern(target, mapping, GLOBALS_REGEXP);
482
+ }
483
+ function applyTypificationMapping(target, mapping) {
484
+ const modified = applyAliasMapping(target, mapping);
485
+ if (modified === target) {
486
+ return target;
487
+ }
488
+ const deleteBrackets = [];
489
+ const positions = [];
490
+ const booleans = [];
491
+ let boolCount = 0;
492
+ let stackSize = 0;
493
+ for (let i = 0; i < modified.length; i++) {
494
+ const char = modified[i];
495
+ if (char === "\u212C") {
496
+ boolCount++;
497
+ continue;
498
+ }
499
+ if (char === "(") {
500
+ stackSize++;
501
+ positions.push(i);
502
+ booleans.push(boolCount);
503
+ }
504
+ boolCount = 0;
505
+ if (char === ")") {
506
+ if (i < modified.length - 1 && modified[i + 1] === ")" && stackSize > 1 && positions[stackSize - 2] + booleans[stackSize - 1] + 1 === positions[stackSize - 1]) {
507
+ deleteBrackets.push(i);
508
+ deleteBrackets.push(positions[stackSize - 2]);
509
+ }
510
+ if (i === modified.length - 1 && stackSize === 1 && positions[0] === 0) {
511
+ deleteBrackets.push(i);
512
+ deleteBrackets.push(positions[0]);
513
+ }
514
+ stackSize--;
515
+ positions.pop();
516
+ booleans.pop();
517
+ }
518
+ }
519
+ let result = "";
520
+ for (let i = 0; i < modified.length; i++) {
521
+ if (!deleteBrackets.includes(i)) {
522
+ result += modified[i];
523
+ }
524
+ }
525
+ return result;
526
+ }
527
+ function applyPattern(text, mapping, pattern) {
528
+ if (text === "" || pattern === null) {
529
+ return text;
530
+ }
531
+ let posInput = 0;
532
+ let output = "";
533
+ const patternMatches = text.matchAll(pattern);
534
+ for (const segment of patternMatches) {
535
+ const entity = segment[0];
536
+ const start = segment.index ?? 0;
537
+ if (entity in mapping) {
538
+ output += text.substring(posInput, start);
539
+ output += mapping[entity];
540
+ posInput = start + segment[0].length;
541
+ }
542
+ }
543
+ output += text.substring(posInput);
544
+ return output;
545
+ }
546
+
547
+ // src/rslang/semantic/typification.ts
548
+ var TypeID = {
549
+ anyTypification: 1,
550
+ integer: 2,
551
+ basic: 3,
552
+ tuple: 4,
553
+ collection: 5,
554
+ logic: 6,
555
+ function: 7,
556
+ predicate: 8
557
+ };
558
+ var TypeClass = {
559
+ logic: 1,
560
+ typification: 2,
561
+ function: 3,
562
+ predicate: 4
563
+ };
564
+ var LogicT = { typeID: TypeID.logic };
565
+ var IntegerT = {
566
+ typeID: TypeID.integer,
567
+ isOrdered: true,
568
+ isArithmetic: true,
569
+ isIntegerCompatible: true
570
+ };
571
+ var AnyTypificationT = { typeID: TypeID.anyTypification };
572
+ var EmptySetT = bool(AnyTypificationT);
573
+ function bool(base) {
574
+ return { typeID: TypeID.collection, base };
575
+ }
576
+
577
+ // src/rslang/labels.ts
578
+ var INTEGER_TYPE_NAME = "Z";
579
+ var ANY_TYPE_NAME = "R0";
580
+ var LOGIC_TYPE_NAME = "Logic";
581
+ var labelTokenRecord = {
582
+ [TokenID.DECART]: "\xD7",
583
+ [TokenID.PUNCTUATION_PL]: "( )",
584
+ [TokenID.PUNCTUATION_SL]: "[ ]",
585
+ [TokenID.QUANTOR_UNIVERSAL]: "\u2200",
586
+ [TokenID.QUANTOR_EXISTS]: "\u2203",
587
+ [TokenID.LOGIC_NOT]: "\xAC",
588
+ [TokenID.LOGIC_AND]: "&",
589
+ [TokenID.LOGIC_OR]: "\u2228",
590
+ [TokenID.LOGIC_IMPLICATION]: "\u21D2",
591
+ [TokenID.LOGIC_EQUIVALENT]: "\u21D4",
592
+ [TokenID.LIT_EMPTYSET]: "\u2205",
593
+ [TokenID.LIT_WHOLE_NUMBERS]: "Z",
594
+ [TokenID.MULTIPLY]: "*",
595
+ [TokenID.EQUAL]: "=",
596
+ [TokenID.NOTEQUAL]: "\u2260",
597
+ [TokenID.GREATER_OR_EQ]: "\u2265",
598
+ [TokenID.LESSER_OR_EQ]: "\u2264",
599
+ [TokenID.SET_IN]: "\u2208",
600
+ [TokenID.SET_NOT_IN]: "\u2209",
601
+ [TokenID.SUBSET_OR_EQ]: "\u2286",
602
+ [TokenID.SUBSET]: "\u2282",
603
+ [TokenID.NOT_SUBSET]: "\u2284",
604
+ [TokenID.SET_INTERSECTION]: "\u2229",
605
+ [TokenID.SET_UNION]: "\u222A",
606
+ [TokenID.SET_MINUS]: "\\",
607
+ [TokenID.SET_SYMMETRIC_MINUS]: "\u2206",
608
+ [TokenID.BOOLEAN]: "\u212C()",
609
+ [TokenID.NT_DECLARATIVE_EXPR]: "D{}",
610
+ [TokenID.NT_IMPERATIVE_EXPR]: "I{}",
611
+ [TokenID.NT_RECURSIVE_FULL]: "R{}",
612
+ [TokenID.BIGPR]: "Pr1()",
613
+ [TokenID.SMALLPR]: "pr1()",
614
+ [TokenID.FILTER]: "Fi1[]()",
615
+ [TokenID.REDUCE]: "red()",
616
+ [TokenID.CARD]: "card()",
617
+ [TokenID.BOOL]: "bool()",
618
+ [TokenID.DEBOOL]: "debool()",
619
+ [TokenID.ASSIGN]: ":=",
620
+ [TokenID.ITERATE]: ":\u2208"
621
+ };
622
+ function labelType(type) {
623
+ if (!type) {
624
+ return "N/A";
625
+ }
626
+ switch (type.typeID) {
627
+ case TypeID.anyTypification:
628
+ return ANY_TYPE_NAME;
629
+ case TypeID.integer:
630
+ return INTEGER_TYPE_NAME;
631
+ case TypeID.basic:
632
+ return type.baseID;
633
+ case TypeID.tuple:
634
+ return type.factors.map((factor) => factor.typeID === TypeID.tuple ? `(${labelType(factor)})` : labelType(factor)).join("\xD7");
635
+ case TypeID.collection:
636
+ return type.base.typeID === TypeID.collection ? `\u212C${labelType(type.base)}` : `\u212C(${labelType(type.base)})`;
637
+ case TypeID.logic:
638
+ return LOGIC_TYPE_NAME;
639
+ case TypeID.predicate:
640
+ case TypeID.function:
641
+ const argsText = type.args.map((arg) => labelType(arg.type)).join(", ");
642
+ return `[${argsText}] \u2192 ${labelType(type.result)}`;
643
+ }
644
+ }
645
+
646
+ // src/rslang/semantic/typification-api.ts
647
+ var TypeIDToClass = {
648
+ [TypeID.anyTypification]: TypeClass.typification,
649
+ [TypeID.integer]: TypeClass.typification,
650
+ [TypeID.basic]: TypeClass.typification,
651
+ [TypeID.tuple]: TypeClass.typification,
652
+ [TypeID.collection]: TypeClass.typification,
653
+ [TypeID.logic]: TypeClass.logic,
654
+ [TypeID.function]: TypeClass.function,
655
+ [TypeID.predicate]: TypeClass.predicate
656
+ };
657
+ function extractBases(target) {
658
+ return new Set(extractBasesImpl(target));
659
+ }
660
+ function extractBasesImpl(target) {
661
+ switch (target.typeID) {
662
+ case TypeID.basic:
663
+ return [target.baseID];
664
+ case TypeID.collection:
665
+ return extractBasesImpl(target.base);
666
+ case TypeID.tuple:
667
+ return target.factors.reduce((result2, factor) => result2.concat(extractBasesImpl(factor)), []);
668
+ case TypeID.function:
669
+ const result = extractBasesImpl(target.result);
670
+ return target.args.reduce((result2, arg) => result2.concat(extractBasesImpl(arg.type)), result);
671
+ case TypeID.predicate:
672
+ return target.args.reduce((result2, arg) => result2.concat(extractBasesImpl(arg.type)), []);
673
+ default:
674
+ return [];
675
+ }
676
+ }
677
+
678
+ // src/library/oss.ts
679
+ var NodeType = {
680
+ OPERATION: 1,
681
+ BLOCK: 2
682
+ };
683
+ var SubstitutionErrorType = {
684
+ invalidIDs: 0,
685
+ incorrectCst: 1,
686
+ invalidClasses: 2,
687
+ invalidBasic: 3,
688
+ invalidConstant: 4,
689
+ typificationCycle: 5,
690
+ baseSubstitutionNotSet: 6,
691
+ unequalTypification: 7,
692
+ unequalExpressions: 8,
693
+ unequalArgsCount: 9,
694
+ unequalArgs: 10,
695
+ invalidNominal: 11
696
+ };
697
+
698
+ // src/library/rsform.ts
699
+ var CstType = {
700
+ NOMINAL: "nominal",
701
+ BASE: "basic",
702
+ STRUCTURED: "structure",
703
+ TERM: "term",
704
+ AXIOM: "axiom",
705
+ FUNCTION: "function",
706
+ PREDICATE: "predicate",
707
+ CONSTANT: "constant",
708
+ STATEMENT: "statement"
709
+ };
710
+ var CstClass = {
711
+ NOMINAL: "nominal",
712
+ BASIC: "basic",
713
+ DERIVED: "derived",
714
+ STATEMENT: "statement",
715
+ TEMPLATE: "template"
716
+ };
717
+
718
+ // src/library/oss-api.ts
719
+ var STARTING_SUB_INDEX = 900;
720
+ function constructNodeID(type, itemID) {
721
+ return type === NodeType.OPERATION ? "o" + String(itemID) : "b" + String(itemID);
722
+ }
723
+ function sortItemsForOSS(oss, items) {
724
+ const result = items.filter((item) => item.location === oss.location);
725
+ for (const item of items) {
726
+ if (item.visible && item.owner === oss.owner && !result.includes(item)) {
727
+ result.push(item);
728
+ }
729
+ }
730
+ for (const item of items) {
731
+ if (item.visible && !result.includes(item)) {
732
+ result.push(item);
733
+ }
734
+ }
735
+ for (const item of items) {
736
+ if (!result.includes(item)) {
737
+ result.push(item);
738
+ }
739
+ }
740
+ return result;
741
+ }
742
+ var SubstitutionValidator = class {
743
+ errors = [];
744
+ suggestions = [];
745
+ schemas;
746
+ substitutions;
747
+ constituents = /* @__PURE__ */ new Set();
748
+ originals = /* @__PURE__ */ new Set();
749
+ mapping = /* @__PURE__ */ new Map();
750
+ cstByID = /* @__PURE__ */ new Map();
751
+ schemaByID = /* @__PURE__ */ new Map();
752
+ schemaByCst = /* @__PURE__ */ new Map();
753
+ constructor(schemas, substitutions) {
754
+ this.schemas = schemas;
755
+ this.substitutions = substitutions;
756
+ if (schemas.length === 0 || substitutions.length === 0) {
757
+ return;
758
+ }
759
+ for (const schema of schemas) {
760
+ this.schemaByID.set(schema.id, schema);
761
+ this.mapping.set(schema.id, {});
762
+ for (const item of schema.items) {
763
+ this.cstByID.set(item.id, item);
764
+ this.schemaByCst.set(item.id, schema);
765
+ }
766
+ }
767
+ let index = STARTING_SUB_INDEX;
768
+ for (const item of substitutions) {
769
+ this.constituents.add(item.original);
770
+ this.constituents.add(item.substitution);
771
+ this.originals.add(item.original);
772
+ const original = this.cstByID.get(item.original);
773
+ const substitution = this.cstByID.get(item.substitution);
774
+ if (!original || !substitution) {
775
+ return;
776
+ }
777
+ index++;
778
+ const newAlias = `${substitution.alias[0]}${index}`;
779
+ this.mapping.get(original.schema)[original.alias] = newAlias;
780
+ this.mapping.get(substitution.schema)[substitution.alias] = newAlias;
781
+ }
782
+ }
783
+ validate() {
784
+ this.errors = [];
785
+ this.suggestions = [];
786
+ this.calculateSuggestions();
787
+ if (this.substitutions.length === 0) {
788
+ return true;
789
+ }
790
+ if (!this.checkTypes()) {
791
+ return false;
792
+ }
793
+ if (!this.checkCycles()) {
794
+ return false;
795
+ }
796
+ if (!this.checkSubstitutions()) {
797
+ return false;
798
+ }
799
+ return true;
800
+ }
801
+ calculateSuggestions() {
802
+ const candidates = /* @__PURE__ */ new Map();
803
+ const minors = /* @__PURE__ */ new Set();
804
+ const schemaByCst = /* @__PURE__ */ new Map();
805
+ for (const schema of this.schemas) {
806
+ for (const cst of schema.items) {
807
+ if (this.originals.has(cst.id)) {
808
+ continue;
809
+ }
810
+ if (cst.cst_class === CstClass.BASIC || cst.definition_formal.length === 0) {
811
+ continue;
812
+ }
813
+ const inputs = schema.graph.at(cst.id).inputs;
814
+ if (inputs.some((id) => !this.constituents.has(id))) {
815
+ continue;
816
+ }
817
+ if (inputs.some((id) => this.originals.has(id))) {
818
+ minors.add(cst.id);
819
+ }
820
+ candidates.set(cst.id, applyAliasMapping(cst.definition_formal, this.mapping.get(schema.id)).replace(" ", ""));
821
+ schemaByCst.set(cst.id, schema);
822
+ }
823
+ }
824
+ for (const [key1, value1] of candidates) {
825
+ for (const [key2, value2] of candidates) {
826
+ if (key1 >= key2) {
827
+ continue;
828
+ }
829
+ if (schemaByCst.get(key1) === schemaByCst.get(key2)) {
830
+ continue;
831
+ }
832
+ if (value1 != value2) {
833
+ continue;
834
+ }
835
+ if (minors.has(key2)) {
836
+ this.suggestions.push({
837
+ original: key2,
838
+ substitution: key1
839
+ });
840
+ } else {
841
+ this.suggestions.push({
842
+ original: key1,
843
+ substitution: key2
844
+ });
845
+ }
846
+ }
847
+ }
848
+ }
849
+ checkTypes() {
850
+ for (const item of this.substitutions) {
851
+ const original = this.cstByID.get(item.original);
852
+ const substitution = this.cstByID.get(item.substitution);
853
+ if (!original || !substitution) {
854
+ return this.reportError(SubstitutionErrorType.invalidIDs, []);
855
+ }
856
+ if (substitution.analysis && original.analysis && (!original.analysis.success || !substitution.analysis.success)) {
857
+ return this.reportError(SubstitutionErrorType.incorrectCst, [substitution.alias, original.alias]);
858
+ }
859
+ switch (substitution.cst_type) {
860
+ case CstType.NOMINAL: {
861
+ if (original.cst_type !== CstType.NOMINAL) {
862
+ return this.reportError(SubstitutionErrorType.invalidNominal, [substitution.alias, original.alias]);
863
+ }
864
+ break;
865
+ }
866
+ case CstType.BASE: {
867
+ if (original.cst_type !== CstType.BASE && original.cst_type !== CstType.CONSTANT && original.cst_type !== CstType.NOMINAL) {
868
+ return this.reportError(SubstitutionErrorType.invalidBasic, [substitution.alias, original.alias]);
869
+ }
870
+ break;
871
+ }
872
+ case CstType.CONSTANT: {
873
+ if (original.cst_type !== CstType.CONSTANT) {
874
+ return this.reportError(SubstitutionErrorType.invalidConstant, [substitution.alias, original.alias]);
875
+ }
876
+ break;
877
+ }
878
+ case CstType.AXIOM:
879
+ case CstType.STATEMENT: {
880
+ if (original.cst_type !== CstType.AXIOM && original.cst_type !== CstType.STATEMENT) {
881
+ return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
882
+ }
883
+ break;
884
+ }
885
+ case CstType.FUNCTION: {
886
+ if (original.cst_type !== CstType.FUNCTION) {
887
+ return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
888
+ }
889
+ break;
890
+ }
891
+ case CstType.PREDICATE: {
892
+ if (original.cst_type !== CstType.PREDICATE) {
893
+ return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
894
+ }
895
+ break;
896
+ }
897
+ case CstType.TERM:
898
+ case CstType.STRUCTURED: {
899
+ if (original.cst_type !== CstType.TERM && original.cst_type !== CstType.STRUCTURED && original.cst_type !== CstType.BASE) {
900
+ return this.reportError(SubstitutionErrorType.invalidClasses, [substitution.alias, original.alias]);
901
+ }
902
+ break;
903
+ }
904
+ }
905
+ }
906
+ return true;
907
+ }
908
+ checkCycles() {
909
+ const graph = new Graph();
910
+ for (const schema of this.schemas) {
911
+ for (const cst of schema.items) {
912
+ if (cst.cst_type === CstType.BASE || cst.cst_type === CstType.CONSTANT) {
913
+ graph.addNode(cst.id);
914
+ }
915
+ }
916
+ }
917
+ for (const item of this.substitutions) {
918
+ const original = this.cstByID.get(item.original);
919
+ const substitution = this.cstByID.get(item.substitution);
920
+ for (const cst of [original, substitution]) {
921
+ if (cst.cst_type === CstType.BASE || cst.cst_type === CstType.CONSTANT) {
922
+ continue;
923
+ }
924
+ graph.addNode(cst.id);
925
+ if (cst.effectiveType === null) {
926
+ continue;
927
+ }
928
+ const parents = extractBases(cst.effectiveType);
929
+ if (parents.size === 0) {
930
+ continue;
931
+ }
932
+ const schema = this.schemaByID.get(cst.schema);
933
+ for (const alias of parents) {
934
+ const parent = schema.cstByAlias.get(alias);
935
+ if (parent) {
936
+ graph.addEdge(parent.id, cst.id);
937
+ }
938
+ }
939
+ }
940
+ graph.addEdge(substitution.id, original.id);
941
+ }
942
+ const cycle = graph.findCycle();
943
+ if (cycle !== null) {
944
+ const cycleMsg = cycle.map((id) => {
945
+ const cst = this.cstByID.get(id);
946
+ const schema = this.schemaByID.get(cst.schema);
947
+ return `[${schema.alias}]-${cst.alias}`;
948
+ }).join(", ");
949
+ return this.reportError(SubstitutionErrorType.typificationCycle, [cycleMsg]);
950
+ }
951
+ return true;
952
+ }
953
+ checkSubstitutions() {
954
+ const baseMappings = this.prepareBaseMappings();
955
+ const typeMappings = this.calculateSubstituteMappings(baseMappings);
956
+ if (typeMappings === null) {
957
+ return false;
958
+ }
959
+ for (const item of this.substitutions) {
960
+ const original = this.cstByID.get(item.original);
961
+ if (original.cst_type === CstType.BASE || original.cst_type === CstType.CONSTANT || original.cst_type === CstType.NOMINAL) {
962
+ continue;
963
+ }
964
+ const substitution = this.cstByID.get(item.substitution);
965
+ if (original.cst_type === substitution.cst_type && original.cst_class !== CstClass.BASIC) {
966
+ if (!this.checkEqual(original, substitution)) {
967
+ this.reportError(SubstitutionErrorType.unequalExpressions, [substitution.alias, original.alias]);
968
+ }
969
+ }
970
+ if (original.analysis.success !== substitution.analysis.success) {
971
+ return this.reportError(SubstitutionErrorType.unequalTypification, [substitution.alias, original.alias]);
972
+ }
973
+ if (!original.analysis.success || !substitution.analysis.success) {
974
+ continue;
975
+ }
976
+ const originalType = applyTypificationMapping(
977
+ applyAliasMapping(labelType(original.effectiveType), baseMappings.get(original.schema)),
978
+ typeMappings
979
+ );
980
+ const substitutionType = applyTypificationMapping(
981
+ applyAliasMapping(labelType(substitution.effectiveType), baseMappings.get(substitution.schema)),
982
+ typeMappings
983
+ );
984
+ if (originalType !== substitutionType) {
985
+ return this.reportError(SubstitutionErrorType.unequalTypification, [substitution.alias, original.alias]);
986
+ }
987
+ }
988
+ return true;
989
+ }
990
+ prepareBaseMappings() {
991
+ const result = /* @__PURE__ */ new Map();
992
+ let baseCount = 0;
993
+ let constCount = 0;
994
+ for (const schema of this.schemas) {
995
+ const mapping = {};
996
+ for (const cst of schema.items) {
997
+ if (cst.cst_type === CstType.BASE) {
998
+ baseCount++;
999
+ mapping[cst.alias] = `X${baseCount}`;
1000
+ } else if (cst.cst_type === CstType.CONSTANT) {
1001
+ constCount++;
1002
+ mapping[cst.alias] = `C${constCount}`;
1003
+ }
1004
+ result.set(schema.id, mapping);
1005
+ }
1006
+ }
1007
+ return result;
1008
+ }
1009
+ calculateSubstituteMappings(baseMappings) {
1010
+ const result = {};
1011
+ const processed = /* @__PURE__ */ new Set();
1012
+ for (const item of this.substitutions) {
1013
+ const original = this.cstByID.get(item.original);
1014
+ if (original.cst_type !== CstType.BASE && original.cst_type !== CstType.CONSTANT) {
1015
+ continue;
1016
+ }
1017
+ const originalAlias = baseMappings.get(original.schema)[original.alias];
1018
+ const substitution = this.cstByID.get(item.substitution);
1019
+ let substitutionText = "";
1020
+ if (substitution.cst_type === original.cst_type || !substitution.effectiveType) {
1021
+ substitutionText = baseMappings.get(substitution.schema)[substitution.alias];
1022
+ } else {
1023
+ const typeText = labelType(substitution.effectiveType);
1024
+ substitutionText = applyAliasMapping(typeText, baseMappings.get(substitution.schema));
1025
+ substitutionText = applyTypificationMapping(substitutionText, result);
1026
+ if (!isSetTypification(substitutionText)) {
1027
+ this.reportError(SubstitutionErrorType.baseSubstitutionNotSet, [substitution.alias, typeText]);
1028
+ return null;
1029
+ }
1030
+ if (substitutionText.includes("\xD7") || substitutionText.startsWith("\u212C\u212C")) {
1031
+ substitutionText = substitutionText.slice(1);
1032
+ } else {
1033
+ substitutionText = substitutionText.slice(2, -1);
1034
+ }
1035
+ }
1036
+ for (const prevAlias of processed) {
1037
+ result[prevAlias] = applyTypificationMapping(result[prevAlias], { [originalAlias]: substitutionText });
1038
+ }
1039
+ result[originalAlias] = substitutionText;
1040
+ processed.add(originalAlias);
1041
+ }
1042
+ return result;
1043
+ }
1044
+ checkEqual(left, right) {
1045
+ const schema1 = this.schemaByID.get(left.schema);
1046
+ const inputs1 = schema1.graph.at(left.id).inputs;
1047
+ if (inputs1.some((id) => !this.constituents.has(id))) {
1048
+ return false;
1049
+ }
1050
+ const schema2 = this.schemaByID.get(right.schema);
1051
+ const inputs2 = schema2.graph.at(right.id).inputs;
1052
+ if (inputs2.some((id) => !this.constituents.has(id))) {
1053
+ return false;
1054
+ }
1055
+ const expression1 = applyAliasMapping(left.definition_formal, this.mapping.get(schema1.id));
1056
+ const expression2 = applyAliasMapping(right.definition_formal, this.mapping.get(schema2.id));
1057
+ return expression1.replace(" ", "") === expression2.replace(" ", "");
1058
+ }
1059
+ reportError(errorType, params) {
1060
+ this.errors.push({
1061
+ errorType,
1062
+ params
1063
+ });
1064
+ return false;
1065
+ }
1066
+ };
1067
+ function getRelocateCandidates(source, destination, schema, oss) {
1068
+ const destinationSchema = oss.operationByID.get(destination)?.result;
1069
+ if (!destinationSchema) {
1070
+ return [];
1071
+ }
1072
+ const node = oss.graph.at(source);
1073
+ if (!node) {
1074
+ return [];
1075
+ }
1076
+ if (node.outputs.includes(destination)) {
1077
+ return schema.items;
1078
+ }
1079
+ const addedCst = schema.items.filter((item) => !item.is_inherited).map((cst) => cst.id);
1080
+ const unreachableBases = [];
1081
+ for (const cst of schema.items.filter((item) => item.is_inherited)) {
1082
+ if (cst.parent_schema == destinationSchema) {
1083
+ continue;
1084
+ }
1085
+ const parent = schema.inheritance.find((item) => item.child === cst.id && item.child_source === cst.schema)?.parent;
1086
+ if (parent) {
1087
+ const original = oss.substitutions.find((sub) => sub.substitution === parent)?.original;
1088
+ if (original) {
1089
+ continue;
1090
+ }
1091
+ }
1092
+ unreachableBases.push(cst.id);
1093
+ }
1094
+ const unreachable = schema.graph.expandAllOutputs(unreachableBases);
1095
+ return schema.items.filter(
1096
+ (cst) => cst.parent_schema === destinationSchema || addedCst.includes(cst.id) && !unreachable.includes(cst.id)
1097
+ );
1098
+ }
1099
+ export {
1100
+ SubstitutionValidator,
1101
+ constructNodeID,
1102
+ getRelocateCandidates,
1103
+ sortItemsForOSS
1104
+ };
1105
+ //# sourceMappingURL=oss-api.js.map