@tsplc/typegen 0.4.4 → 0.5.1
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 +59 -43
- package/dist/cli.js.map +7 -6
- package/dist/codegen.js +14 -7
- package/dist/codegen.js.map +4 -4
- package/dist/declarations.js +417 -0
- package/dist/declarations.js.map +13 -0
- package/dist/index.js +57 -39
- package/dist/index.js.map +7 -6
- package/package.json +8 -3
- package/src/codegen/declarations.ts +73 -0
- package/src/codegen/generate.ts +1 -1
- package/src/codegen/parser.ts +29 -14
- package/src/project.ts +2 -29
package/dist/index.js
CHANGED
|
@@ -309,10 +309,10 @@ class Parser {
|
|
|
309
309
|
}
|
|
310
310
|
return { kind: "named", name };
|
|
311
311
|
}
|
|
312
|
-
members(end) {
|
|
312
|
+
members(end, { retain }) {
|
|
313
313
|
const members = [];
|
|
314
314
|
while (this.peek() !== end) {
|
|
315
|
-
const
|
|
315
|
+
const symbol = this.tokens.slice(this.position, this.nextIndex()).flatMap((token) => token.kind === "attribute" ? symbolAccess(token.text) : []).at(-1);
|
|
316
316
|
const description = this.documentation();
|
|
317
317
|
const names = [this.identifier()];
|
|
318
318
|
while (this.accept(",")) {
|
|
@@ -337,7 +337,8 @@ class Parser {
|
|
|
337
337
|
description,
|
|
338
338
|
type,
|
|
339
339
|
...initial === undefined ? {} : { initial },
|
|
340
|
-
...
|
|
340
|
+
...symbol ? { symbol } : {},
|
|
341
|
+
...retain ? { retain } : {}
|
|
341
342
|
});
|
|
342
343
|
}
|
|
343
344
|
}
|
|
@@ -357,7 +358,7 @@ class Parser {
|
|
|
357
358
|
declarations.push({
|
|
358
359
|
...common,
|
|
359
360
|
kind: "struct",
|
|
360
|
-
members: this.members("END_STRUCT")
|
|
361
|
+
members: this.members("END_STRUCT", { retain: false })
|
|
361
362
|
});
|
|
362
363
|
this.accept(";");
|
|
363
364
|
} else if (this.accept("(")) {
|
|
@@ -420,11 +421,17 @@ class Parser {
|
|
|
420
421
|
}
|
|
421
422
|
written.add(qualifier);
|
|
422
423
|
}
|
|
423
|
-
const members = this.members("END_VAR"
|
|
424
|
+
const members = this.members("END_VAR", {
|
|
425
|
+
retain: written.has("RETAIN") || written.has("PERSISTENT")
|
|
426
|
+
});
|
|
424
427
|
this.accept(";");
|
|
425
428
|
return { constant: written.has("CONSTANT"), members };
|
|
426
429
|
}
|
|
427
430
|
}
|
|
431
|
+
var symbolAccess = (attribute) => {
|
|
432
|
+
const access = /^\{\s*attribute\s+'symbol'\s*:=\s*'(read|write|readwrite)'\s*\}$/i.exec(attribute)?.[1]?.toLowerCase();
|
|
433
|
+
return access === "read" || access === "write" || access === "readwrite" ? [access] : [];
|
|
434
|
+
};
|
|
428
435
|
function parseDeclarations(source) {
|
|
429
436
|
return new Parser(source).declarations();
|
|
430
437
|
}
|
|
@@ -508,7 +515,7 @@ function published(globals, publication) {
|
|
|
508
515
|
members: list.members.filter((member) => publication ? [
|
|
509
516
|
...publication.production,
|
|
510
517
|
...publication.development
|
|
511
|
-
].includes(`${list.name}.${member.name}`) : member.
|
|
518
|
+
].includes(`${list.name}.${member.name}`) : member.symbol !== undefined)
|
|
512
519
|
}).filter((list) => list.constant || list.members.length);
|
|
513
520
|
}
|
|
514
521
|
function emit(declarations, globals, {
|
|
@@ -1563,7 +1570,7 @@ function resolveIo(config, catalog) {
|
|
|
1563
1570
|
}));
|
|
1564
1571
|
}
|
|
1565
1572
|
// packages/typegen/src/project.ts
|
|
1566
|
-
import { mkdir, readFile as
|
|
1573
|
+
import { mkdir, readFile as readFile5, rename, writeFile } from "node:fs/promises";
|
|
1567
1574
|
import { dirname, resolve as resolve2 } from "node:path";
|
|
1568
1575
|
import {
|
|
1569
1576
|
ioSymbolsSchema,
|
|
@@ -1576,10 +1583,46 @@ import {
|
|
|
1576
1583
|
emittedStDirectory,
|
|
1577
1584
|
projectSnapshotFile
|
|
1578
1585
|
} from "@tsplc/config/project-files";
|
|
1579
|
-
import { blockCommentEnd as blockCommentEnd2, lineCommentEnd as lineCommentEnd2 } from "@tsplc/iec/st";
|
|
1580
1586
|
import { format } from "prettier";
|
|
1587
|
+
|
|
1588
|
+
// packages/typegen/src/codegen/declarations.ts
|
|
1589
|
+
import { readFile as readFile4 } from "node:fs/promises";
|
|
1590
|
+
import { blockCommentEnd as blockCommentEnd2, lineCommentEnd as lineCommentEnd2 } from "@tsplc/iec/st";
|
|
1591
|
+
var codeStart = (text, offset) => {
|
|
1592
|
+
const start = offset + text.slice(offset).search(/\S|$/);
|
|
1593
|
+
if (text.startsWith("(*", start)) {
|
|
1594
|
+
const end = blockCommentEnd2(text, start);
|
|
1595
|
+
return end === null ? start : codeStart(text, end);
|
|
1596
|
+
}
|
|
1597
|
+
if (text.startsWith("//", start)) {
|
|
1598
|
+
return codeStart(text, lineCommentEnd2(text, start));
|
|
1599
|
+
}
|
|
1600
|
+
if (text.startsWith("{", start)) {
|
|
1601
|
+
const end = text.indexOf("}", start);
|
|
1602
|
+
return end < 0 ? start : codeStart(text, end + 1);
|
|
1603
|
+
}
|
|
1604
|
+
return start;
|
|
1605
|
+
};
|
|
1606
|
+
var declarationFiles = async (files) => {
|
|
1607
|
+
const sorted = {
|
|
1608
|
+
types: [],
|
|
1609
|
+
globals: []
|
|
1610
|
+
};
|
|
1611
|
+
for (const file of files) {
|
|
1612
|
+
const text = await readFile4(file, "utf8");
|
|
1613
|
+
const code = text.slice(codeStart(text, 0));
|
|
1614
|
+
if (/^TYPE\b/i.test(code)) {
|
|
1615
|
+
sorted.types.push(file);
|
|
1616
|
+
} else if (/^VAR_GLOBAL\b/i.test(code)) {
|
|
1617
|
+
sorted.globals.push(file);
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
return sorted;
|
|
1621
|
+
};
|
|
1622
|
+
|
|
1623
|
+
// packages/typegen/src/project.ts
|
|
1581
1624
|
var formatTypeScript = (source) => format(source, { parser: "typescript" });
|
|
1582
|
-
var readPublication = async (root) => publicationSchema.parse(JSON.parse(await
|
|
1625
|
+
var readPublication = async (root) => publicationSchema.parse(JSON.parse(await readFile5(resolve2(root, emittedStDirectory, emittedFiles.publication), "utf8")));
|
|
1583
1626
|
var generatedFiles = {
|
|
1584
1627
|
hardware: "hardware.ts",
|
|
1585
1628
|
contract: "plc.ts",
|
|
@@ -1587,7 +1630,7 @@ var generatedFiles = {
|
|
|
1587
1630
|
};
|
|
1588
1631
|
async function readIoSymbols(root) {
|
|
1589
1632
|
const file = resolve2(root, emittedStDirectory, emittedFiles.ioSymbols);
|
|
1590
|
-
const text = await
|
|
1633
|
+
const text = await readFile5(file, "utf8").catch((error) => {
|
|
1591
1634
|
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
1592
1635
|
return "{}";
|
|
1593
1636
|
}
|
|
@@ -1626,32 +1669,7 @@ async function contractSources(config, root, settings) {
|
|
|
1626
1669
|
if (!files.length) {
|
|
1627
1670
|
throw new Error(`no .st declarations found in ${sourceRoot}`);
|
|
1628
1671
|
}
|
|
1629
|
-
|
|
1630
|
-
for (const file of files) {
|
|
1631
|
-
const text = await readFile4(file, "utf8");
|
|
1632
|
-
const code = text.slice(codeStart(text, 0));
|
|
1633
|
-
if (/^TYPE\b/i.test(code)) {
|
|
1634
|
-
sources.types.push(file);
|
|
1635
|
-
} else if (/^VAR_GLOBAL\b/i.test(code)) {
|
|
1636
|
-
sources.globals.push(file);
|
|
1637
|
-
}
|
|
1638
|
-
}
|
|
1639
|
-
return sources;
|
|
1640
|
-
}
|
|
1641
|
-
function codeStart(text, offset) {
|
|
1642
|
-
const start = offset + text.slice(offset).search(/\S|$/);
|
|
1643
|
-
if (text.startsWith("(*", start)) {
|
|
1644
|
-
const end = blockCommentEnd2(text, start);
|
|
1645
|
-
return end === null ? start : codeStart(text, end);
|
|
1646
|
-
}
|
|
1647
|
-
if (text.startsWith("//", start)) {
|
|
1648
|
-
return codeStart(text, lineCommentEnd2(text, start));
|
|
1649
|
-
}
|
|
1650
|
-
if (text.startsWith("{", start)) {
|
|
1651
|
-
const end = text.indexOf("}", start);
|
|
1652
|
-
return end < 0 ? start : codeStart(text, end + 1);
|
|
1653
|
-
}
|
|
1654
|
-
return start;
|
|
1672
|
+
return { ...await declarationFiles(files), explicit: false };
|
|
1655
1673
|
}
|
|
1656
1674
|
async function filesIn(directories) {
|
|
1657
1675
|
const files = [];
|
|
@@ -1677,7 +1695,7 @@ async function writeOutputs(outputs) {
|
|
|
1677
1695
|
}
|
|
1678
1696
|
async function checkOutputs(outputs) {
|
|
1679
1697
|
for (const output of outputs) {
|
|
1680
|
-
const actual = await
|
|
1698
|
+
const actual = await readFile5(output.path, "utf8").catch(() => {
|
|
1681
1699
|
return;
|
|
1682
1700
|
});
|
|
1683
1701
|
const normalized = actual !== undefined && /\.ts$/i.test(output.path) ? await formatTypeScript(actual) : actual;
|
|
@@ -1726,7 +1744,7 @@ async function generateProject(file = "tsplc.config.ts", options = {}) {
|
|
|
1726
1744
|
checkHmiTargets(config.hmi.controls, (await readPublication(root)).production);
|
|
1727
1745
|
}
|
|
1728
1746
|
if (config.compiler && !options.hardwareOnly) {
|
|
1729
|
-
const machine = await
|
|
1747
|
+
const machine = await readFile5(resolve2(root, emittedStDirectory, emittedFiles.machines), "utf8").catch(() => {
|
|
1730
1748
|
return;
|
|
1731
1749
|
});
|
|
1732
1750
|
if (machine) {
|
|
@@ -1754,5 +1772,5 @@ export {
|
|
|
1754
1772
|
resolveIo
|
|
1755
1773
|
};
|
|
1756
1774
|
|
|
1757
|
-
//# debugId=
|
|
1775
|
+
//# debugId=6D922A9E33BA4A4164756E2164756E21
|
|
1758
1776
|
//# sourceMappingURL=index.js.map
|