@tsplc/typegen 0.2.14 → 0.2.16

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/index.js CHANGED
@@ -284,6 +284,7 @@ class Parser {
284
284
  declarations() {
285
285
  const declarations = [];
286
286
  while (this.peek()) {
287
+ const keyed = this.tokens.slice(this.position, this.nextIndex()).some((token) => token.kind === "attribute" && /^\{\s*attribute\s+'tsplc_keyed'\s*\}$/i.test(token.text));
287
288
  const description = this.documentation();
288
289
  this.expect("TYPE");
289
290
  const name = this.identifier();
@@ -312,7 +313,7 @@ class Parser {
312
313
  } else {
313
314
  const type = this.reference();
314
315
  this.expect(";");
315
- declarations.push({ ...common, kind: "alias", type });
316
+ declarations.push({ ...common, kind: "alias", type, ...keyed ? { keyed } : {} });
316
317
  }
317
318
  this.expect("END_TYPE");
318
319
  this.accept(";");
@@ -547,6 +548,12 @@ function emit(declarations, globals, publishedGlobalsOnly, emitDeclarations) {
547
548
  return complex(actual.element);
548
549
  return actual.kind === "named" && !scalars[actual.name.toUpperCase()];
549
550
  }
551
+ function keyed(type) {
552
+ if (type.kind !== "named" || scalars[type.name.toUpperCase()])
553
+ return false;
554
+ const definition = declaration(type.name);
555
+ return definition.kind === "alias" && (!!definition.keyed || keyed(definition.type));
556
+ }
550
557
  function fields(type, path, description, depth = 0) {
551
558
  const actual = resolved(type);
552
559
  if (actual.kind === "named" && complex(actual)) {
@@ -555,7 +562,7 @@ function emit(declarations, globals, publishedGlobalsOnly, emitDeclarations) {
555
562
  return children;
556
563
  return `${children}.map((member) => ({ ...member, field: ${path} + "." + member.field }))`;
557
564
  }
558
- if (actual.kind === "array" && complex(actual.element)) {
565
+ if (actual.kind === "array" && (complex(actual.element) || keyed(type))) {
559
566
  const ranges = dimensions(actual);
560
567
  const coordinates = ranges.map((range, index) => `String(i${depth}_${index} + (${range.start}))`);
561
568
  const arrayPath = `${path} + "[" + ${coordinates.join(' + "," + ')} + "]"`;
@@ -587,7 +594,7 @@ ${type.members.map((member) => `...${fields(member.type, quote(member.name), mem
587
594
  } else if (type.kind === "alias") {
588
595
  output.push(...declare(`${doc}export type ${type.name} = ${tsType(type.type)};
589
596
  `));
590
- output.push(`const _fields${indexes.get(type.name)}: PlcField[] = ${fields(type.type, '""', type.description)};
597
+ output.push(`const _fields${indexes.get(type.name)}: PlcField[] = ${fields({ kind: "named", name: type.name }, '""', type.description)};
591
598
  `);
592
599
  } else {
593
600
  if (!integerTypePattern.test(type.base))
@@ -1146,6 +1153,18 @@ function resolveIo(config, catalog) {
1146
1153
  ];
1147
1154
  }));
1148
1155
  }
1156
+ var timeLayouts = new Map([
1157
+ ["TIME", "TIME"],
1158
+ ["DATE", "DATE"],
1159
+ ["TOD", "TIME_OF_DAY"],
1160
+ ["TIME_OF_DAY", "TIME_OF_DAY"],
1161
+ ["DT", "DATE_AND_TIME"],
1162
+ ["DATE_AND_TIME", "DATE_AND_TIME"]
1163
+ ]);
1164
+ function ioScalarBits(type) {
1165
+ const name = type.trim().toUpperCase();
1166
+ return timeLayouts.has(name) ? 32 : iecTypeBits[name];
1167
+ }
1149
1168
  function ioLayout(type) {
1150
1169
  const array = /^ARRAY\s*\[\s*(-?\d+)\s*\.\.\s*(-?\d+)\s*\]\s*OF\s+(\w+)$/i.exec(type.trim());
1151
1170
  if (array) {
@@ -1155,8 +1174,8 @@ function ioLayout(type) {
1155
1174
  const name = type.trim().toUpperCase();
1156
1175
  if (name === "BOOL" || name === "REAL" || name === "LREAL")
1157
1176
  return name;
1158
- const bits = iecTypeBits[name];
1159
- return bits ? `INT${bits}` : undefined;
1177
+ const bits = ioScalarBits(name);
1178
+ return timeLayouts.get(name) ?? (bits ? `INT${bits}` : undefined);
1160
1179
  }
1161
1180
  var directWidths = {
1162
1181
  BOOL: "X",
@@ -1166,7 +1185,8 @@ var directWidths = {
1166
1185
  REAL: "D"
1167
1186
  };
1168
1187
  var directBinding = (layout) => {
1169
- const width = directWidths[layout.replace(/^\d+x/, "")];
1188
+ const scalar = layout.replace(/^\d+x/, "");
1189
+ const width = timeLayouts.has(scalar) ? "D" : directWidths[scalar];
1170
1190
  if (!width)
1171
1191
  return;
1172
1192
  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 }`;
@@ -1450,6 +1470,7 @@ export {
1450
1470
  generateProject,
1451
1471
  generateTypes,
1452
1472
  ioLayout,
1473
+ ioScalarBits,
1453
1474
  loadHardware,
1454
1475
  normalizeEds,
1455
1476
  parseEds,
@@ -1457,5 +1478,5 @@ export {
1457
1478
  resolveIo
1458
1479
  };
1459
1480
 
1460
- //# debugId=DDF3189654E73FD364756E2164756E21
1481
+ //# debugId=5656406A0F686B6864756E2164756E21
1461
1482
  //# sourceMappingURL=index.js.map