@tsplc/typegen 0.2.14 → 0.2.17
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/cli.js +27 -7
- package/dist/cli.js.map +5 -5
- package/dist/codegen.js +11 -4
- package/dist/codegen.js.map +4 -4
- package/dist/hardware.js +18 -4
- package/dist/hardware.js.map +3 -3
- package/dist/index.js +28 -7
- package/dist/index.js.map +5 -5
- package/package.json +2 -2
- package/src/codegen/generate.ts +8 -2
- package/src/codegen/parser.ts +13 -2
- package/src/hardware.ts +20 -6
package/src/codegen/generate.ts
CHANGED
|
@@ -300,6 +300,12 @@ function emit(
|
|
|
300
300
|
if (actual.kind === "array") return complex(actual.element);
|
|
301
301
|
return actual.kind === "named" && !scalars[actual.name.toUpperCase()];
|
|
302
302
|
}
|
|
303
|
+
/** An enum-keyed array: like a record's fields, each key is its own contract variable. */
|
|
304
|
+
function keyed(type: TypeReference): boolean {
|
|
305
|
+
if (type.kind !== "named" || scalars[type.name.toUpperCase()]) return false;
|
|
306
|
+
const definition = declaration(type.name);
|
|
307
|
+
return definition.kind === "alias" && (!!definition.keyed || keyed(definition.type));
|
|
308
|
+
}
|
|
303
309
|
function fields(type: TypeReference, path: string, description: string, depth = 0): string {
|
|
304
310
|
const actual = resolved(type);
|
|
305
311
|
if (actual.kind === "named" && complex(actual)) {
|
|
@@ -307,7 +313,7 @@ function emit(
|
|
|
307
313
|
if (path === '""') return children;
|
|
308
314
|
return `${children}.map((member) => ({ ...member, field: ${path} + "." + member.field }))`;
|
|
309
315
|
}
|
|
310
|
-
if (actual.kind === "array" && complex(actual.element)) {
|
|
316
|
+
if (actual.kind === "array" && (complex(actual.element) || keyed(type))) {
|
|
311
317
|
const ranges = dimensions(actual);
|
|
312
318
|
const coordinates = ranges.map(
|
|
313
319
|
(range, index) => `String(i${depth}_${index} + (${range.start}))`,
|
|
@@ -342,7 +348,7 @@ function emit(
|
|
|
342
348
|
} else if (type.kind === "alias") {
|
|
343
349
|
output.push(...declare(`${doc}export type ${type.name} = ${tsType(type.type)};\n`));
|
|
344
350
|
output.push(
|
|
345
|
-
`const _fields${indexes.get(type.name)}: PlcField[] = ${fields(type.
|
|
351
|
+
`const _fields${indexes.get(type.name)}: PlcField[] = ${fields({ kind: "named", name: type.name }, '""', type.description)};\n`,
|
|
346
352
|
);
|
|
347
353
|
} else {
|
|
348
354
|
if (!integerTypePattern.test(type.base))
|
package/src/codegen/parser.ts
CHANGED
|
@@ -27,7 +27,12 @@ export type Declaration = {
|
|
|
27
27
|
source: string;
|
|
28
28
|
} & (
|
|
29
29
|
| { kind: "struct"; members: Member[] }
|
|
30
|
-
| {
|
|
30
|
+
| {
|
|
31
|
+
kind: "alias";
|
|
32
|
+
type: TypeReference;
|
|
33
|
+
/** `{attribute 'tsplc_keyed'}`: an array whose indexes are enum keys, so each element is its own contract variable. */
|
|
34
|
+
keyed?: boolean;
|
|
35
|
+
}
|
|
31
36
|
| {
|
|
32
37
|
kind: "enum";
|
|
33
38
|
members: Array<{ name: string; value?: string }>;
|
|
@@ -254,6 +259,12 @@ class Parser {
|
|
|
254
259
|
declarations(): Declaration[] {
|
|
255
260
|
const declarations: Declaration[] = [];
|
|
256
261
|
while (this.peek()) {
|
|
262
|
+
const keyed = this.tokens
|
|
263
|
+
.slice(this.position, this.nextIndex())
|
|
264
|
+
.some(
|
|
265
|
+
(token) =>
|
|
266
|
+
token.kind === "attribute" && /^\{\s*attribute\s+'tsplc_keyed'\s*\}$/i.test(token.text),
|
|
267
|
+
);
|
|
257
268
|
const description = this.documentation();
|
|
258
269
|
this.expect("TYPE");
|
|
259
270
|
const name = this.identifier();
|
|
@@ -282,7 +293,7 @@ class Parser {
|
|
|
282
293
|
} else {
|
|
283
294
|
const type = this.reference();
|
|
284
295
|
this.expect(";");
|
|
285
|
-
declarations.push({ ...common, kind: "alias", type });
|
|
296
|
+
declarations.push({ ...common, kind: "alias", type, ...(keyed ? { keyed } : {}) });
|
|
286
297
|
}
|
|
287
298
|
this.expect("END_TYPE");
|
|
288
299
|
this.accept(";");
|
package/src/hardware.ts
CHANGED
|
@@ -755,10 +755,23 @@ export interface ProjectTargets {
|
|
|
755
755
|
publication: readonly string[];
|
|
756
756
|
}
|
|
757
757
|
|
|
758
|
+
const timeLayouts = new Map([
|
|
759
|
+
["TIME", "TIME"],
|
|
760
|
+
["DATE", "DATE"],
|
|
761
|
+
["TOD", "TIME_OF_DAY"],
|
|
762
|
+
["TIME_OF_DAY", "TIME_OF_DAY"],
|
|
763
|
+
["DT", "DATE_AND_TIME"],
|
|
764
|
+
["DATE_AND_TIME", "DATE_AND_TIME"],
|
|
765
|
+
]);
|
|
766
|
+
|
|
767
|
+
export function ioScalarBits(type: string): number | undefined {
|
|
768
|
+
const name = type.trim().toUpperCase();
|
|
769
|
+
return timeLayouts.has(name) ? 32 : iecTypeBits[name];
|
|
770
|
+
}
|
|
771
|
+
|
|
758
772
|
/**
|
|
759
|
-
* Binding
|
|
760
|
-
*
|
|
761
|
-
* BOOL by name, and arrays of those (not BOOL) by length, whatever their bounds.
|
|
773
|
+
* Binding classes group integers and bit strings by width, BOOL, floats and time types
|
|
774
|
+
* by domain, and non-BOOL arrays by element class and length.
|
|
762
775
|
*/
|
|
763
776
|
export function ioLayout(type: string): string | undefined {
|
|
764
777
|
const array = /^ARRAY\s*\[\s*(-?\d+)\s*\.\.\s*(-?\d+)\s*\]\s*OF\s+(\w+)$/i.exec(type.trim());
|
|
@@ -770,8 +783,8 @@ export function ioLayout(type: string): string | undefined {
|
|
|
770
783
|
}
|
|
771
784
|
const name = type.trim().toUpperCase();
|
|
772
785
|
if (name === "BOOL" || name === "REAL" || name === "LREAL") return name;
|
|
773
|
-
const bits =
|
|
774
|
-
return bits ? `INT${bits}` : undefined;
|
|
786
|
+
const bits = ioScalarBits(name);
|
|
787
|
+
return timeLayouts.get(name) ?? (bits ? `INT${bits}` : undefined);
|
|
775
788
|
}
|
|
776
789
|
|
|
777
790
|
// Direct addresses by the width of one element; 64-bit layouts have no direct form.
|
|
@@ -783,7 +796,8 @@ const directWidths: Record<string, string> = {
|
|
|
783
796
|
REAL: "D",
|
|
784
797
|
};
|
|
785
798
|
const directBinding = (layout: string): string | undefined => {
|
|
786
|
-
const
|
|
799
|
+
const scalar = layout.replace(/^\d+x/, "");
|
|
800
|
+
const width = timeLayouts.has(scalar) ? "D" : directWidths[scalar];
|
|
787
801
|
if (!width) return undefined;
|
|
788
802
|
return width === "X"
|
|
789
803
|
? '{ address: `%${"I" | "Q"}X${number}.${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7}`; type?: string }'
|