@lionweb/class-core-generator 0.6.13-beta.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/LICENSE +201 -0
- package/dist/api/entity-types.templates.d.ts +4 -0
- package/dist/api/entity-types.templates.d.ts.map +1 -0
- package/dist/api/entity-types.templates.js +171 -0
- package/dist/api/entity-types.templates.js.map +1 -0
- package/dist/api/generation-headers.d.ts +9 -0
- package/dist/api/generation-headers.d.ts.map +1 -0
- package/dist/api/generation-headers.js +40 -0
- package/dist/api/generation-headers.js.map +1 -0
- package/dist/api/generator.d.ts +11 -0
- package/dist/api/generator.d.ts.map +1 -0
- package/dist/api/generator.js +59 -0
- package/dist/api/generator.js.map +1 -0
- package/dist/api/helpers/classifiers.d.ts +6 -0
- package/dist/api/helpers/classifiers.d.ts.map +1 -0
- package/dist/api/helpers/classifiers.js +49 -0
- package/dist/api/helpers/classifiers.js.map +1 -0
- package/dist/api/helpers/dependencies.d.ts +11 -0
- package/dist/api/helpers/dependencies.d.ts.map +1 -0
- package/dist/api/helpers/dependencies.js +52 -0
- package/dist/api/helpers/dependencies.js.map +1 -0
- package/dist/api/helpers/entities.d.ts +5 -0
- package/dist/api/helpers/entities.d.ts.map +1 -0
- package/dist/api/helpers/entities.js +33 -0
- package/dist/api/helpers/entities.js.map +1 -0
- package/dist/api/helpers/features.d.ts +9 -0
- package/dist/api/helpers/features.d.ts.map +1 -0
- package/dist/api/helpers/features.js +96 -0
- package/dist/api/helpers/features.js.map +1 -0
- package/dist/api/helpers/imports-tracking.d.ts +22 -0
- package/dist/api/helpers/imports-tracking.d.ts.map +1 -0
- package/dist/api/helpers/imports-tracking.js +80 -0
- package/dist/api/helpers/imports-tracking.js.map +1 -0
- package/dist/api/helpers/index.d.ts +8 -0
- package/dist/api/helpers/index.d.ts.map +1 -0
- package/dist/api/helpers/index.js +24 -0
- package/dist/api/helpers/index.js.map +1 -0
- package/dist/api/helpers/mps-annotations.d.ts +19 -0
- package/dist/api/helpers/mps-annotations.d.ts.map +1 -0
- package/dist/api/helpers/mps-annotations.js +65 -0
- package/dist/api/helpers/mps-annotations.js.map +1 -0
- package/dist/api/helpers/types.d.ts +3 -0
- package/dist/api/helpers/types.d.ts.map +1 -0
- package/dist/api/helpers/types.js +28 -0
- package/dist/api/helpers/types.js.map +1 -0
- package/dist/api/index-ts.d.ts +4 -0
- package/dist/api/index-ts.d.ts.map +1 -0
- package/dist/api/index-ts.js +52 -0
- package/dist/api/index-ts.js.map +1 -0
- package/dist/api/index.d.ts +5 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +21 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/language-file.templates.d.ts +4 -0
- package/dist/api/language-file.templates.d.ts.map +1 -0
- package/dist/api/language-file.templates.js +65 -0
- package/dist/api/language-file.templates.js.map +1 -0
- package/dist/api/mega-factory.templates.d.ts +4 -0
- package/dist/api/mega-factory.templates.d.ts.map +1 -0
- package/dist/api/mega-factory.templates.js +61 -0
- package/dist/api/mega-factory.templates.js.map +1 -0
- package/dist/api/reflective-layer.templates.d.ts +4 -0
- package/dist/api/reflective-layer.templates.d.ts.map +1 -0
- package/dist/api/reflective-layer.templates.js +211 -0
- package/dist/api/reflective-layer.templates.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/json-as-ts.d.ts +2 -0
- package/dist/utils/json-as-ts.d.ts.map +1 -0
- package/dist/utils/json-as-ts.js +18 -0
- package/dist/utils/json-as-ts.js.map +1 -0
- package/dist/utils/string-sorting.d.ts +4 -0
- package/dist/utils/string-sorting.d.ts.map +1 -0
- package/dist/utils/string-sorting.js +23 -0
- package/dist/utils/string-sorting.js.map +1 -0
- package/dist/utils/textgen.d.ts +21 -0
- package/dist/utils/textgen.d.ts.map +1 -0
- package/dist/utils/textgen.js +67 -0
- package/dist/utils/textgen.js.map +1 -0
- package/dist/utils/toposort.d.ts +6 -0
- package/dist/utils/toposort.d.ts.map +1 -0
- package/dist/utils/toposort.js +41 -0
- package/dist/utils/toposort.js.map +1 -0
- package/package.json +34 -0
- package/src/api/entity-types.templates.ts +257 -0
- package/src/api/generation-headers.ts +47 -0
- package/src/api/generator.ts +84 -0
- package/src/api/helpers/classifiers.ts +67 -0
- package/src/api/helpers/dependencies.ts +62 -0
- package/src/api/helpers/entities.ts +43 -0
- package/src/api/helpers/features.ts +121 -0
- package/src/api/helpers/imports-tracking.ts +96 -0
- package/src/api/helpers/index.ts +24 -0
- package/src/api/helpers/mps-annotations.ts +69 -0
- package/src/api/helpers/types.ts +31 -0
- package/src/api/index-ts.ts +59 -0
- package/src/api/index.ts +21 -0
- package/src/api/language-file.templates.ts +79 -0
- package/src/api/mega-factory.templates.ts +79 -0
- package/src/api/reflective-layer.templates.ts +273 -0
- package/src/index.ts +19 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/json-as-ts.ts +20 -0
- package/src/utils/string-sorting.ts +29 -0
- package/src/utils/textgen.ts +89 -0
- package/src/utils/toposort.ts +49 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
Annotation,
|
|
20
|
+
asArray,
|
|
21
|
+
Classifier,
|
|
22
|
+
Concept,
|
|
23
|
+
Enumeration,
|
|
24
|
+
EnumerationLiteral,
|
|
25
|
+
Feature,
|
|
26
|
+
featureMetaType,
|
|
27
|
+
Interface,
|
|
28
|
+
isConcrete,
|
|
29
|
+
isEnumeration,
|
|
30
|
+
isMultiple,
|
|
31
|
+
Language,
|
|
32
|
+
LanguageEntity,
|
|
33
|
+
Link,
|
|
34
|
+
nameOf,
|
|
35
|
+
PrimitiveType,
|
|
36
|
+
Property,
|
|
37
|
+
SingleRef
|
|
38
|
+
} from "@lionweb/core"
|
|
39
|
+
import {when, withNewlineAppended} from "littoral-templates"
|
|
40
|
+
|
|
41
|
+
import {indent, switchOrIf} from "../utils/textgen.js"
|
|
42
|
+
import {entityMetaType, extendsFrom, Imports, nameOfBaseClassForLanguage} from "./helpers/index.js"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
export const reflectiveClassFor = (imports: Imports) => {
|
|
46
|
+
|
|
47
|
+
// classifier:
|
|
48
|
+
|
|
49
|
+
const reflectiveMembersForFeature = (feature: Feature) => {
|
|
50
|
+
const {classifier, name, key, id, optional} = feature
|
|
51
|
+
const metaType = featureMetaType(feature)
|
|
52
|
+
const qName = `${classifier.name}_${name}`
|
|
53
|
+
return [
|
|
54
|
+
`private readonly _${qName} = new ${imports.core(metaType)}(this._${classifier.name}, "${name}", "${key}", "${id}")${optional ? ".isOptional()" : ""}${isMultiple(feature) ? ".isMultiple()" : ""};`,
|
|
55
|
+
// | core (2nd x) |
|
|
56
|
+
`get ${qName}(): ${metaType} {`,
|
|
57
|
+
indent([
|
|
58
|
+
`this.ensureWiredUp();`,
|
|
59
|
+
`return this._${qName};`
|
|
60
|
+
]),
|
|
61
|
+
`}`
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const reflectiveMembersForClassifier = (classifier: Classifier) => {
|
|
66
|
+
const {name, key, id, features} = classifier
|
|
67
|
+
const metaType = entityMetaType(classifier)
|
|
68
|
+
return [
|
|
69
|
+
`public readonly _${name} = new ${imports.core(metaType)}(this._language, "${name}", "${key}", "${id}"${classifier instanceof Concept ? (", " + classifier.abstract) : ""});`,
|
|
70
|
+
// | core (2nd x) |
|
|
71
|
+
`get ${name}(): ${metaType} {`,
|
|
72
|
+
indent([
|
|
73
|
+
`this.ensureWiredUp();`,
|
|
74
|
+
`return this._${name};`
|
|
75
|
+
]),
|
|
76
|
+
`}`,
|
|
77
|
+
features.map(reflectiveMembersForFeature)
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
// enumeration:
|
|
83
|
+
|
|
84
|
+
const reflectiveMemberForEnumerationLiteral = ({enumeration, name, key, id}: EnumerationLiteral) => {
|
|
85
|
+
const qName = `${enumeration.name}_${name}`
|
|
86
|
+
return [
|
|
87
|
+
`private readonly _${qName} = new ${imports.core("EnumerationLiteral")}(this._${enumeration.name}, "${name}", "${key}", "${id}");`,
|
|
88
|
+
// | core (2nd x) |
|
|
89
|
+
`get ${qName}(): EnumerationLiteral {`,
|
|
90
|
+
indent([
|
|
91
|
+
`this.ensureWiredUp();`,
|
|
92
|
+
`return this._${qName};`
|
|
93
|
+
]),
|
|
94
|
+
`}`
|
|
95
|
+
]
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const reflectiveMembersForEnumeration = ({name, key, id, literals}: Enumeration) =>
|
|
99
|
+
[
|
|
100
|
+
`public readonly _${name} = new ${imports.core("Enumeration")}(this._language, "${name}", "${key}", "${id}");`,
|
|
101
|
+
// | core (2nd x) |
|
|
102
|
+
`get ${name}(): Enumeration {`,
|
|
103
|
+
indent([
|
|
104
|
+
`this.ensureWiredUp();`,
|
|
105
|
+
`return this._${name};`
|
|
106
|
+
]),
|
|
107
|
+
`}`,
|
|
108
|
+
literals.map(reflectiveMemberForEnumerationLiteral)
|
|
109
|
+
]
|
|
110
|
+
|
|
111
|
+
const reflectiveMembersForPrimitiveType = ({name, key, id}: PrimitiveType) =>
|
|
112
|
+
[
|
|
113
|
+
`public readonly _${name} = new ${imports.core("PrimitiveType")}(this._language, "${name}", "${key}", "${id}");`,
|
|
114
|
+
// | core (2nd x) |
|
|
115
|
+
`get ${name}(): PrimitiveType {`,
|
|
116
|
+
indent([
|
|
117
|
+
`this.ensureWiredUp();`,
|
|
118
|
+
`return this._${name};`
|
|
119
|
+
]),
|
|
120
|
+
`}`
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
const reflectiveMembersForEntity = (entity: LanguageEntity) => {
|
|
125
|
+
if (entity instanceof Classifier) {
|
|
126
|
+
return reflectiveMembersForClassifier(entity)
|
|
127
|
+
}
|
|
128
|
+
if (entity instanceof Enumeration) {
|
|
129
|
+
return reflectiveMembersForEnumeration(entity)
|
|
130
|
+
}
|
|
131
|
+
if (entity instanceof PrimitiveType) {
|
|
132
|
+
return reflectiveMembersForPrimitiveType(entity)
|
|
133
|
+
}
|
|
134
|
+
return `// unhandled language entity <${entityMetaType(entity)}>"${entity.name}"`
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
const refForType = (type: SingleRef<LanguageEntity>) => {
|
|
139
|
+
if (type === null) {
|
|
140
|
+
return `???`
|
|
141
|
+
}
|
|
142
|
+
if (type.language === imports.thisLanguage) {
|
|
143
|
+
return `this._${type.name}`
|
|
144
|
+
}
|
|
145
|
+
return `${imports.language(type.language)}.INSTANCE._${type.name}`
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const wireUpStatementsForProperty = (property: Property) => {
|
|
149
|
+
const {classifier, name, type, optional} = property
|
|
150
|
+
return `this._${classifier.name}_${name}.ofType(${refForType(type)})${optional ? ".isOptional()" : ""};`
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const wireUpStatementsForLink = (link: Link) => {
|
|
154
|
+
const {classifier, name, type} = link
|
|
155
|
+
return `this._${classifier.name}_${name}.ofType(${refForType(type)});`
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const wireUpStatementsForFeature = (feature: Feature) => {
|
|
159
|
+
if (feature instanceof Property) {
|
|
160
|
+
return wireUpStatementsForProperty(feature)
|
|
161
|
+
}
|
|
162
|
+
if (feature instanceof Link) {
|
|
163
|
+
return wireUpStatementsForLink(feature)
|
|
164
|
+
}
|
|
165
|
+
return `// unhandled feature <${featureMetaType(feature)}>"${feature.name}"`
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const isConcreteClassifier = (classifier: Classifier): classifier is (Annotation | Concept) =>
|
|
169
|
+
classifier instanceof Annotation || classifier instanceof Concept
|
|
170
|
+
|
|
171
|
+
const wireUpStatementsForEntity = (entity: LanguageEntity) => {
|
|
172
|
+
const {name} = entity
|
|
173
|
+
const thisLocalName = (localName: string) => `this._${name}_${localName}`
|
|
174
|
+
if (entity instanceof Classifier) {
|
|
175
|
+
const extends_ = extendsFrom(entity)
|
|
176
|
+
return [
|
|
177
|
+
extends_ instanceof Classifier
|
|
178
|
+
? `this._${name}.extends = ${refForType(extends_)};`
|
|
179
|
+
: [],
|
|
180
|
+
when(entity instanceof Interface && entity.extends.length > 0)(
|
|
181
|
+
() => `this._${name}.extending(${(entity as Interface).extends.map((interface_) => refForType(interface_)).join(", ")});`
|
|
182
|
+
),
|
|
183
|
+
when(isConcreteClassifier(entity) && entity.implements.length > 0)(
|
|
184
|
+
() => `this._${name}.implementing(${asArray((entity as (Annotation | Concept)).implements).map((interface_) => refForType(interface_)).join(", ")});`
|
|
185
|
+
),
|
|
186
|
+
when(entity.features.length > 0)(
|
|
187
|
+
`this._${name}.havingFeatures(${entity.features.map(nameOf).map(thisLocalName).join(", ")});`
|
|
188
|
+
),
|
|
189
|
+
entity.features.map(wireUpStatementsForFeature)
|
|
190
|
+
]
|
|
191
|
+
}
|
|
192
|
+
if (entity instanceof Enumeration) {
|
|
193
|
+
return `this._${name}.havingLiterals(${entity.literals.map(nameOf).map(thisLocalName).join(", ")});`
|
|
194
|
+
}
|
|
195
|
+
if (entity instanceof PrimitiveType) {
|
|
196
|
+
return [] // (nothing to do)
|
|
197
|
+
}
|
|
198
|
+
return `// unhandled language entity <${entityMetaType(entity)}>"${name}"`
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return (language: Language) => {
|
|
202
|
+
const {version, id, key, entities} = language
|
|
203
|
+
const enumerations = entities.filter(isEnumeration)
|
|
204
|
+
const concreteClassifiers = entities.filter(isConcrete)
|
|
205
|
+
const parameterPrefix = concreteClassifiers.length === 0 ? "_" : ""
|
|
206
|
+
const thisLocalName = (localName: string) => `this._${localName}`
|
|
207
|
+
return [
|
|
208
|
+
`export class ${imports.thisBaseClassName} implements ${imports.generic("ILanguageBase")} {`,
|
|
209
|
+
``,
|
|
210
|
+
indent([
|
|
211
|
+
// | core | (2nd x)
|
|
212
|
+
`private readonly _language: ${imports.core("Language")} = new Language("${imports.thisLanguageNameAsJsIdentifier}", "${version}", "${id}", "${key}");`,
|
|
213
|
+
// | core | (3rd x)
|
|
214
|
+
`get language(): Language {`,
|
|
215
|
+
indent([
|
|
216
|
+
`this.ensureWiredUp();`,
|
|
217
|
+
`return this._language;`
|
|
218
|
+
]),
|
|
219
|
+
`}`,
|
|
220
|
+
``,
|
|
221
|
+
entities.map(withNewlineAppended(reflectiveMembersForEntity)),
|
|
222
|
+
`private _wiredUp: boolean = false;`,
|
|
223
|
+
`private ensureWiredUp() {`,
|
|
224
|
+
indent([
|
|
225
|
+
`if (this._wiredUp) {`,
|
|
226
|
+
indent(`return;`),
|
|
227
|
+
`}`,
|
|
228
|
+
`this._language.havingEntities(${entities.map(nameOf).map(thisLocalName).join(", ")});`,
|
|
229
|
+
entities.map(wireUpStatementsForEntity),
|
|
230
|
+
`this._wiredUp = true;`
|
|
231
|
+
]),
|
|
232
|
+
`}`,
|
|
233
|
+
``,
|
|
234
|
+
`factory(${parameterPrefix}handleDelta?: ${imports.generic("DeltaHandler")}): ${imports.generic("NodeBaseFactory")} {`,
|
|
235
|
+
indent([
|
|
236
|
+
`return (classifier: ${imports.core("Classifier")}, ${parameterPrefix}id: ${imports.core("Id")}) => {`,
|
|
237
|
+
indent(
|
|
238
|
+
switchOrIf(
|
|
239
|
+
"classifier.key",
|
|
240
|
+
concreteClassifiers.map(nameOf).map((name) => [`this._${name}.key`, `${name}.create(id, ${parameterPrefix}handleDelta)`]),
|
|
241
|
+
[
|
|
242
|
+
`const {language} = classifier;`,
|
|
243
|
+
`throw new Error(\`can't instantiate \${classifier.name} (key=\${classifier.key}): classifier is not known in language \${language.name} (key=\${language.key}, version=\${language.version})\`);`
|
|
244
|
+
]
|
|
245
|
+
)
|
|
246
|
+
),
|
|
247
|
+
`}`
|
|
248
|
+
]),
|
|
249
|
+
`}`,
|
|
250
|
+
``,
|
|
251
|
+
`enumLiteralFrom<EnumType>(enumerationLiteral: ${imports.core("EnumerationLiteral")}): EnumType {`,
|
|
252
|
+
// (Why can this work?! EnumType is an unsatisfied generic parameter!)
|
|
253
|
+
indent([
|
|
254
|
+
`const {enumeration} = enumerationLiteral;`,
|
|
255
|
+
switchOrIf(
|
|
256
|
+
"enumeration.key",
|
|
257
|
+
enumerations.map(nameOf).map((name) => [`this._${name}.key`, "enumerationLiteral.key as EnumType"]),
|
|
258
|
+
[
|
|
259
|
+
`const {language} = enumeration;`,
|
|
260
|
+
`throw new Error(\`enumeration with key \${enumeration.key} is not known in language \${language.name} (key=\${language.key}, version=\${language.version})\`);`
|
|
261
|
+
]
|
|
262
|
+
)
|
|
263
|
+
]),
|
|
264
|
+
`}`,
|
|
265
|
+
``,
|
|
266
|
+
`public static readonly INSTANCE = new ${nameOfBaseClassForLanguage(language)}();`
|
|
267
|
+
]),
|
|
268
|
+
`}`
|
|
269
|
+
]
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
}
|
|
273
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
|
|
18
|
+
export * from "./api/index.js"
|
|
19
|
+
export * from "./utils/index.js"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
|
|
18
|
+
export {asTypeScript} from "./json-as-ts.js"
|
|
19
|
+
export {sortedStrings, sortedStringsByUppercase} from "./string-sorting.js"
|
|
20
|
+
export {indent} from "./textgen.js"
|
|
21
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
|
|
18
|
+
export const asTypeScript = (json: unknown) =>
|
|
19
|
+
JSON.stringify(json, null, 4).replaceAll(/"(.+?)": /g, `$1: `)
|
|
20
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
|
|
18
|
+
export type StringSorter = (strings: string[]) => string[]
|
|
19
|
+
|
|
20
|
+
const sortedStringsWith = (strMap: (str: string) => string): StringSorter =>
|
|
21
|
+
(strings) => {
|
|
22
|
+
strings.sort((l, r) => strMap(l).localeCompare(strMap(r)))
|
|
23
|
+
return strings
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const sortedStrings = sortedStringsWith((str) => str)
|
|
27
|
+
|
|
28
|
+
export const sortedStringsByUppercase = sortedStringsWith((str) => str.toUpperCase())
|
|
29
|
+
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
* NOTE: this code is copied verbatim from the @lionweb/utilities package, and subsequently modified/added to.
|
|
20
|
+
* This code should “flow back” to the @lionweb packages.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
import {asString, indentWith, Template} from "littoral-templates"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @return an indenter function that indents by 4 spaces.
|
|
29
|
+
*/
|
|
30
|
+
export const indent = indentWith(" ")(1)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
const withFirstCased = (charFunc: (ch: string) => string) =>
|
|
34
|
+
(str: string) => {
|
|
35
|
+
if (str.length === 0) {
|
|
36
|
+
return str
|
|
37
|
+
}
|
|
38
|
+
return charFunc(str.charAt(0))
|
|
39
|
+
+ (str.length > 1 ? str.substring(1) : "")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @return the given string but with its first character lower-cased (when possible).
|
|
44
|
+
*/
|
|
45
|
+
export const withFirstLower = withFirstCased((ch) => ch.toLowerCase())
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @return the given string but with its first character upper-cased (when possible).
|
|
49
|
+
*/
|
|
50
|
+
export const withFirstUpper = withFirstCased((ch) => ch.toUpperCase())
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @return a sanitized version of the given string that should be suitable as a valid JavaScript identifier.
|
|
55
|
+
*/
|
|
56
|
+
export const asJSIdentifier = (str: string): string =>
|
|
57
|
+
str
|
|
58
|
+
.replaceAll(".", "_")
|
|
59
|
+
.replaceAll("-", "_")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
export const wrapInIf = (condition: boolean, left: () => string, right: string) =>
|
|
63
|
+
(text: string) =>
|
|
64
|
+
condition
|
|
65
|
+
? `${left()}${text}${right}`
|
|
66
|
+
: text
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
export type MatchCase = [caseExpression: string, returnValue: string]
|
|
70
|
+
|
|
71
|
+
export const switchOrIf = (expression: string, cases: MatchCase[], defaultBlock: Template) =>
|
|
72
|
+
cases.length > 1
|
|
73
|
+
? [
|
|
74
|
+
`switch (${expression}) {`,
|
|
75
|
+
indent([
|
|
76
|
+
cases.map(([caseExpression, returnValue]) => `case ${caseExpression}: return ${returnValue};`),
|
|
77
|
+
`default: ${typeof defaultBlock === "string" ? defaultBlock : asString(["{", indent(defaultBlock), "}"])}`
|
|
78
|
+
]),
|
|
79
|
+
`}`
|
|
80
|
+
]
|
|
81
|
+
: [
|
|
82
|
+
cases.map(([caseExpression, returnValue]) => [
|
|
83
|
+
`if (${expression} === ${caseExpression}) {`,
|
|
84
|
+
indent(`return ${returnValue};`),
|
|
85
|
+
`}`
|
|
86
|
+
]),
|
|
87
|
+
defaultBlock
|
|
88
|
+
]
|
|
89
|
+
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Copyright 2025 TRUMPF Laser SE and other contributors
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-FileCopyrightText: 2025 TRUMPF Laser SE and other contributors
|
|
16
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Computes the topological order of the transitive closure of the graph with the given vertices and edges given by the edge function,
|
|
20
|
+
* or returns {@code false} if there's a cycle.
|
|
21
|
+
*/
|
|
22
|
+
export const dependencyOrderOf = <T>(vertices: T[], edgesOf: (vertex: T) => T[]): T[] | false => {
|
|
23
|
+
const ordered: T[] = []
|
|
24
|
+
|
|
25
|
+
const visit = (current: T, chain: T[]) => {
|
|
26
|
+
if (ordered.indexOf(current) > -1) {
|
|
27
|
+
return false
|
|
28
|
+
}
|
|
29
|
+
if (chain.indexOf(current) > -1) {
|
|
30
|
+
return true
|
|
31
|
+
}
|
|
32
|
+
const extendedChain = [ ...chain, current ]
|
|
33
|
+
const hasCycle = edgesOf(current).some(
|
|
34
|
+
(edge) => visit(edge, extendedChain)
|
|
35
|
+
)
|
|
36
|
+
ordered.push(current)
|
|
37
|
+
if (hasCycle) {
|
|
38
|
+
console.dir(ordered)
|
|
39
|
+
}
|
|
40
|
+
return hasCycle
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const hasCycle = vertices.some(
|
|
44
|
+
(vertex) => visit(vertex, [])
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
return hasCycle ? false : ordered
|
|
48
|
+
}
|
|
49
|
+
|