@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 CHANGED
@@ -301,6 +301,7 @@ class Parser {
301
301
  declarations() {
302
302
  const declarations = [];
303
303
  while (this.peek()) {
304
+ const keyed = this.tokens.slice(this.position, this.nextIndex()).some((token) => token.kind === "attribute" && /^\{\s*attribute\s+'tsplc_keyed'\s*\}$/i.test(token.text));
304
305
  const description = this.documentation();
305
306
  this.expect("TYPE");
306
307
  const name = this.identifier();
@@ -329,7 +330,7 @@ class Parser {
329
330
  } else {
330
331
  const type = this.reference();
331
332
  this.expect(";");
332
- declarations.push({ ...common, kind: "alias", type });
333
+ declarations.push({ ...common, kind: "alias", type, ...keyed ? { keyed } : {} });
333
334
  }
334
335
  this.expect("END_TYPE");
335
336
  this.accept(";");
@@ -564,6 +565,12 @@ function emit(declarations, globals, publishedGlobalsOnly, emitDeclarations) {
564
565
  return complex(actual.element);
565
566
  return actual.kind === "named" && !scalars[actual.name.toUpperCase()];
566
567
  }
568
+ function keyed(type) {
569
+ if (type.kind !== "named" || scalars[type.name.toUpperCase()])
570
+ return false;
571
+ const definition = declaration(type.name);
572
+ return definition.kind === "alias" && (!!definition.keyed || keyed(definition.type));
573
+ }
567
574
  function fields(type, path, description, depth = 0) {
568
575
  const actual = resolved(type);
569
576
  if (actual.kind === "named" && complex(actual)) {
@@ -572,7 +579,7 @@ function emit(declarations, globals, publishedGlobalsOnly, emitDeclarations) {
572
579
  return children;
573
580
  return `${children}.map((member) => ({ ...member, field: ${path} + "." + member.field }))`;
574
581
  }
575
- if (actual.kind === "array" && complex(actual.element)) {
582
+ if (actual.kind === "array" && (complex(actual.element) || keyed(type))) {
576
583
  const ranges = dimensions(actual);
577
584
  const coordinates = ranges.map((range, index) => `String(i${depth}_${index} + (${range.start}))`);
578
585
  const arrayPath = `${path} + "[" + ${coordinates.join(' + "," + ')} + "]"`;
@@ -604,7 +611,7 @@ ${type.members.map((member) => `...${fields(member.type, quote(member.name), mem
604
611
  } else if (type.kind === "alias") {
605
612
  output.push(...declare(`${doc}export type ${type.name} = ${tsType(type.type)};
606
613
  `));
607
- output.push(`const _fields${indexes.get(type.name)}: PlcField[] = ${fields(type.type, '""', type.description)};
614
+ output.push(`const _fields${indexes.get(type.name)}: PlcField[] = ${fields({ kind: "named", name: type.name }, '""', type.description)};
608
615
  `);
609
616
  } else {
610
617
  if (!integerTypePattern.test(type.base))
@@ -1164,6 +1171,18 @@ function resolveIo(config, catalog) {
1164
1171
  ];
1165
1172
  }));
1166
1173
  }
1174
+ var timeLayouts = new Map([
1175
+ ["TIME", "TIME"],
1176
+ ["DATE", "DATE"],
1177
+ ["TOD", "TIME_OF_DAY"],
1178
+ ["TIME_OF_DAY", "TIME_OF_DAY"],
1179
+ ["DT", "DATE_AND_TIME"],
1180
+ ["DATE_AND_TIME", "DATE_AND_TIME"]
1181
+ ]);
1182
+ function ioScalarBits(type) {
1183
+ const name = type.trim().toUpperCase();
1184
+ return timeLayouts.has(name) ? 32 : iecTypeBits[name];
1185
+ }
1167
1186
  function ioLayout(type) {
1168
1187
  const array = /^ARRAY\s*\[\s*(-?\d+)\s*\.\.\s*(-?\d+)\s*\]\s*OF\s+(\w+)$/i.exec(type.trim());
1169
1188
  if (array) {
@@ -1173,8 +1192,8 @@ function ioLayout(type) {
1173
1192
  const name = type.trim().toUpperCase();
1174
1193
  if (name === "BOOL" || name === "REAL" || name === "LREAL")
1175
1194
  return name;
1176
- const bits = iecTypeBits[name];
1177
- return bits ? `INT${bits}` : undefined;
1195
+ const bits = ioScalarBits(name);
1196
+ return timeLayouts.get(name) ?? (bits ? `INT${bits}` : undefined);
1178
1197
  }
1179
1198
  var directWidths = {
1180
1199
  BOOL: "X",
@@ -1184,7 +1203,8 @@ var directWidths = {
1184
1203
  REAL: "D"
1185
1204
  };
1186
1205
  var directBinding = (layout) => {
1187
- const width = directWidths[layout.replace(/^\d+x/, "")];
1206
+ const scalar = layout.replace(/^\d+x/, "");
1207
+ const width = timeLayouts.has(scalar) ? "D" : directWidths[scalar];
1188
1208
  if (!width)
1189
1209
  return;
1190
1210
  return width === "X" ? '{ address: `%${"I" | "Q"}X${number}.${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7}`; type?: string }' : `{ address: \`%\${"I" | "Q"}${width}\${number}\`; type?: string }`;
@@ -1383,5 +1403,5 @@ main().catch((error) => {
1383
1403
  process.exitCode = 1;
1384
1404
  });
1385
1405
 
1386
- //# debugId=137B0C4C6BCD5A5A64756E2164756E21
1406
+ //# debugId=354773F5D2FD28F264756E2164756E21
1387
1407
  //# sourceMappingURL=cli.js.map