@tsplc/typegen 0.2.17 → 0.2.19
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/README.md +2 -2
- package/dist/cli.js +798 -544
- package/dist/cli.js.map +15 -9
- package/dist/codegen.js +252 -177
- package/dist/codegen.js.map +5 -5
- package/dist/hardware.js +447 -338
- package/dist/hardware.js.map +10 -5
- package/dist/index.js +848 -602
- package/dist/index.js.map +16 -9
- package/package.json +55 -62
- package/src/cli.ts +68 -51
- package/src/codegen/constants.ts +177 -86
- package/src/codegen/generate.ts +436 -388
- package/src/codegen/parser.ts +358 -287
- package/src/device-tree.ts +152 -140
- package/src/hardware/channels.ts +157 -0
- package/src/hardware/eds.ts +408 -0
- package/src/hardware/mapping-types.ts +88 -0
- package/src/hardware/modbus.ts +105 -0
- package/src/hardware/process-image.ts +102 -0
- package/src/hardware/selection.ts +136 -0
- package/src/hardware.ts +195 -844
- package/src/index.ts +25 -1
- package/src/project.ts +246 -151
package/package.json
CHANGED
|
@@ -1,64 +1,57 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
},
|
|
58
|
-
"bin": {
|
|
59
|
-
"tsplc-typegen": "./dist/cli.js"
|
|
60
|
-
},
|
|
61
|
-
"publishConfig": {
|
|
62
|
-
"access": "public"
|
|
63
|
-
}
|
|
2
|
+
"name": "@tsplc/typegen",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"description": "Generate TypeScript PLC contracts and typed EtherNet/IP hardware IO mappings from Structured Text and EDS files.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"plc",
|
|
7
|
+
"codesys",
|
|
8
|
+
"eds",
|
|
9
|
+
"ethernet-ip",
|
|
10
|
+
"typegen"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/rip-technologies/tsplc#readme",
|
|
13
|
+
"bugs": "https://github.com/rip-technologies/tsplc/issues",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/rip-technologies/tsplc.git",
|
|
17
|
+
"directory": "packages/typegen"
|
|
18
|
+
},
|
|
19
|
+
"version": "0.2.19",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./src/index.ts",
|
|
24
|
+
"bun": "./src/index.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./codegen": {
|
|
28
|
+
"types": "./src/codegen/generate.ts",
|
|
29
|
+
"bun": "./src/codegen/generate.ts",
|
|
30
|
+
"default": "./dist/codegen.js"
|
|
31
|
+
},
|
|
32
|
+
"./hardware": {
|
|
33
|
+
"types": "./src/hardware.ts",
|
|
34
|
+
"bun": "./src/hardware.ts",
|
|
35
|
+
"default": "./dist/hardware.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist",
|
|
40
|
+
"src",
|
|
41
|
+
"!**/*.test.ts"
|
|
42
|
+
],
|
|
43
|
+
"engines": {
|
|
44
|
+
"bun": ">=1.4.3"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@tsplc/config": "0.2.19",
|
|
48
|
+
"prettier": "3.9.10",
|
|
49
|
+
"@tsplc/iec": "0.2.19"
|
|
50
|
+
},
|
|
51
|
+
"bin": {
|
|
52
|
+
"tsplc-typegen": "./dist/cli.js"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
64
57
|
}
|
package/src/cli.ts
CHANGED
|
@@ -6,58 +6,75 @@ import { generateProject } from "./project.ts";
|
|
|
6
6
|
import { generateTypes } from "./codegen/generate";
|
|
7
7
|
|
|
8
8
|
async function main(): Promise<void> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
9
|
+
const { values } = parseArgs({
|
|
10
|
+
options: {
|
|
11
|
+
config: { type: "string" },
|
|
12
|
+
types: { type: "string" },
|
|
13
|
+
globals: { type: "string" },
|
|
14
|
+
out: { type: "string" },
|
|
15
|
+
check: { type: "boolean" },
|
|
16
|
+
help: { type: "boolean", short: "h" },
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
if (values.help) {
|
|
20
|
+
console.log(
|
|
21
|
+
"Usage: tsplc-typegen --types src/types [--globals src/globals] --out generated.ts [--check]\n\nGenerate TypeScript types and runtime field metadata from CODESYS ST declarations.\n--check exits unsuccessfully if the output is missing or stale, without writing files.",
|
|
22
|
+
);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (!values.types && !values.out) {
|
|
26
|
+
const result = await generateProject(values.config, {
|
|
27
|
+
check: values.check,
|
|
28
|
+
});
|
|
29
|
+
console.log(`Generated ${result.files.join(", ")}`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (!values.types || !values.out) {
|
|
33
|
+
throw new Error("--types and --out are required; run tsplc-typegen --help");
|
|
34
|
+
}
|
|
35
|
+
const out = resolve(values.out);
|
|
36
|
+
if (!/\.ts$/i.test(out) || /\.d\.ts$/i.test(out)) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
"--out must be a .ts file (the generated file contains runtime metadata)",
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
const result = await generateTypes({
|
|
42
|
+
types: resolve(values.types),
|
|
43
|
+
globals: values.globals ? resolve(values.globals) : undefined,
|
|
44
|
+
});
|
|
45
|
+
if (values.check) {
|
|
46
|
+
const existing = await readFile(out, "utf8").catch((error: unknown) => {
|
|
47
|
+
if (
|
|
48
|
+
error instanceof Error &&
|
|
49
|
+
"code" in error &&
|
|
50
|
+
error.code === "ENOENT"
|
|
51
|
+
) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
throw error;
|
|
55
|
+
});
|
|
56
|
+
if (existing !== result) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`${out} is missing or stale; rerun tsplc-typegen without --check`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
console.log(`Up to date: ${out}`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
await mkdir(dirname(out), { recursive: true });
|
|
65
|
+
const temporary = `${out}.${process.pid}.tmp`;
|
|
66
|
+
try {
|
|
67
|
+
await writeFile(temporary, result, { flag: "wx" });
|
|
68
|
+
await rename(temporary, out);
|
|
69
|
+
} finally {
|
|
70
|
+
await rm(temporary, { force: true });
|
|
71
|
+
}
|
|
72
|
+
console.log(`Generated ${out}`);
|
|
58
73
|
}
|
|
59
74
|
|
|
60
75
|
main().catch((error: unknown) => {
|
|
61
|
-
|
|
62
|
-
|
|
76
|
+
console.error(
|
|
77
|
+
`tsplc-typegen: ${error instanceof Error ? error.message : String(error)}`,
|
|
78
|
+
);
|
|
79
|
+
process.exitCode = 1;
|
|
63
80
|
});
|
package/src/codegen/constants.ts
CHANGED
|
@@ -1,90 +1,181 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
REAL: 32,
|
|
14
|
-
LINT: 64,
|
|
15
|
-
ULINT: 64,
|
|
16
|
-
LWORD: 64,
|
|
17
|
-
LREAL: 64,
|
|
1
|
+
import {
|
|
2
|
+
elementaryType,
|
|
3
|
+
elementaryTypes,
|
|
4
|
+
integerRange,
|
|
5
|
+
isIntegerValued,
|
|
6
|
+
} from "@tsplc/iec";
|
|
7
|
+
import type { GlobalList } from "./parser.ts";
|
|
8
|
+
|
|
9
|
+
// Constants and enum values are emitted as numbers, so they take integer types of 32 bits and under.
|
|
10
|
+
export const isNumberInteger = (name: string) => {
|
|
11
|
+
const type = elementaryType(name);
|
|
12
|
+
return isIntegerValued(type) && type.bits <= 32;
|
|
18
13
|
};
|
|
19
14
|
|
|
15
|
+
const integerRanges = new Map(
|
|
16
|
+
elementaryTypes
|
|
17
|
+
.filter(isIntegerValued)
|
|
18
|
+
.map((type) => [type.name, integerRange(type)] as const),
|
|
19
|
+
);
|
|
20
|
+
const integerToken = new RegExp(
|
|
21
|
+
String.raw`(?:${[...integerRanges.keys()].join("|")})\s*#\s*[+-]?(?:(?:2|8|16)#[\da-fA-F_]+|\d[\d_]*)|(?:2|8|16)#[\da-fA-F_]+|\d[\d_]*|[A-Za-z_]\w*(?:\s*\.\s*[A-Za-z_]\w*)?|[()+\-*/]`,
|
|
22
|
+
"g",
|
|
23
|
+
);
|
|
24
|
+
|
|
20
25
|
/** Evaluate the integer-expression subset used for array bounds and constants. Never executes PLC text. */
|
|
21
|
-
export function integerExpression(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
26
|
+
export function integerExpression(
|
|
27
|
+
text: string,
|
|
28
|
+
lookup: (name: string) => number,
|
|
29
|
+
): number {
|
|
30
|
+
const compact = text.replace(/\s+/g, "");
|
|
31
|
+
const tokens = (text.match(integerToken) ?? []).map((token) =>
|
|
32
|
+
token.replace(/\s+/g, ""),
|
|
33
|
+
);
|
|
34
|
+
if (tokens.join("") !== compact || !tokens.length) {
|
|
35
|
+
throw new Error(`unsupported integer expression: ${text}`);
|
|
36
|
+
}
|
|
37
|
+
const safe = (value: number) => {
|
|
38
|
+
if (!Number.isSafeInteger(value)) {
|
|
39
|
+
throw new Error(`integer outside JavaScript's safe range: ${text}`);
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
};
|
|
43
|
+
let position = 0;
|
|
44
|
+
function atom(): number {
|
|
45
|
+
const token = tokens[position++];
|
|
46
|
+
if (token === "+") {
|
|
47
|
+
return atom();
|
|
48
|
+
}
|
|
49
|
+
if (token === "-") {
|
|
50
|
+
return -atom();
|
|
51
|
+
}
|
|
52
|
+
if (token === "(") {
|
|
53
|
+
const value = sum();
|
|
54
|
+
if (tokens[position++] !== ")") {
|
|
55
|
+
throw new Error(`unclosed integer expression: ${text}`);
|
|
56
|
+
}
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
const typed =
|
|
60
|
+
token &&
|
|
61
|
+
[...integerRanges].find(([name]) =>
|
|
62
|
+
token.toUpperCase().startsWith(`${name}#`),
|
|
63
|
+
);
|
|
64
|
+
if (typed) {
|
|
65
|
+
const [name, [minimum, maximum]] = typed;
|
|
66
|
+
const value = integerExpression(token.slice(name.length + 1), lookup);
|
|
67
|
+
if (BigInt(value) < minimum || BigInt(value) > maximum) {
|
|
68
|
+
throw new Error(`typed integer outside ${name}: ${token}`);
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
if (token && /^\d/.test(token)) {
|
|
73
|
+
const [base, digits] = token.replace(/_/g, "").split("#");
|
|
74
|
+
if (
|
|
75
|
+
digits !== undefined &&
|
|
76
|
+
![...digits].every((digit) => parseInt(digit, 16) < Number(base))
|
|
77
|
+
) {
|
|
78
|
+
throw new Error(`invalid base-${base} literal: ${token}`);
|
|
79
|
+
}
|
|
80
|
+
return safe(
|
|
81
|
+
digits === undefined ? Number(base) : parseInt(digits, Number(base)),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
if (token && /^[A-Za-z_]/.test(token)) {
|
|
85
|
+
return lookup(token);
|
|
86
|
+
}
|
|
87
|
+
throw new Error(`invalid integer expression: ${text}`);
|
|
88
|
+
}
|
|
89
|
+
function product(): number {
|
|
90
|
+
let value = atom();
|
|
91
|
+
while (tokens[position] === "*" || tokens[position] === "/") {
|
|
92
|
+
const operator = tokens[position++];
|
|
93
|
+
const right = atom();
|
|
94
|
+
if (operator === "/" && right === 0) {
|
|
95
|
+
throw new Error(`division by zero: ${text}`);
|
|
96
|
+
}
|
|
97
|
+
value = safe(
|
|
98
|
+
operator === "*" ? value * right : Math.trunc(value / right),
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
function sum(): number {
|
|
104
|
+
let value = product();
|
|
105
|
+
while (tokens[position] === "+" || tokens[position] === "-") {
|
|
106
|
+
const operator = tokens[position++];
|
|
107
|
+
const right = product();
|
|
108
|
+
value = safe(operator === "+" ? value + right : value - right);
|
|
109
|
+
}
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
const value = sum();
|
|
113
|
+
if (position !== tokens.length || !Number.isSafeInteger(value)) {
|
|
114
|
+
throw new Error(`invalid or unsafe integer expression: ${text}`);
|
|
115
|
+
}
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Looks constants of CONSTANT global lists up by qualified name, or by a bare name that
|
|
121
|
+
* only one list declares; inside a list's own initializers (`scope`), its members come
|
|
122
|
+
* first. Each is evaluated once, when first used, so they may refer to each other in
|
|
123
|
+
* any order.
|
|
124
|
+
*/
|
|
125
|
+
export function constantResolver(
|
|
126
|
+
lists: readonly GlobalList[],
|
|
127
|
+
): (name: string, scope: string | null) => number {
|
|
128
|
+
const values = new Map<string, number>();
|
|
129
|
+
const resolving = new Set<string>();
|
|
130
|
+
const definitions = lists.flatMap((list) =>
|
|
131
|
+
list.members.map((member) => ({
|
|
132
|
+
list,
|
|
133
|
+
member,
|
|
134
|
+
key: `${list.name}.${member.name}`.toUpperCase(),
|
|
135
|
+
})),
|
|
136
|
+
);
|
|
137
|
+
const constant = (name: string, scope: string | null): number => {
|
|
138
|
+
const key = name.toUpperCase();
|
|
139
|
+
const scoped =
|
|
140
|
+
scope === null
|
|
141
|
+
? undefined
|
|
142
|
+
: definitions.find(
|
|
143
|
+
(item) => item.key === `${scope}.${name}`.toUpperCase(),
|
|
144
|
+
);
|
|
145
|
+
const [definition, ...others] = scoped
|
|
146
|
+
? [scoped]
|
|
147
|
+
: definitions.filter(
|
|
148
|
+
(item) =>
|
|
149
|
+
item.key === key ||
|
|
150
|
+
(!name.includes(".") && item.member.name.toUpperCase() === key),
|
|
151
|
+
);
|
|
152
|
+
if (!definition || others.length) {
|
|
153
|
+
throw new Error(`unknown or ambiguous constant ${name}`);
|
|
154
|
+
}
|
|
155
|
+
const cached = values.get(definition.key);
|
|
156
|
+
if (cached !== undefined) {
|
|
157
|
+
return cached;
|
|
158
|
+
}
|
|
159
|
+
if (resolving.has(definition.key)) {
|
|
160
|
+
throw new Error(`cyclic constant ${name}`);
|
|
161
|
+
}
|
|
162
|
+
const { member, list } = definition;
|
|
163
|
+
if (
|
|
164
|
+
member.initial === undefined ||
|
|
165
|
+
member.type.kind !== "named" ||
|
|
166
|
+
!isNumberInteger(member.type.name.toUpperCase())
|
|
167
|
+
) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`${list.source}: constant ${member.name} must have an integer type and initializer`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
resolving.add(definition.key);
|
|
173
|
+
const value = integerExpression(member.initial, (reference) =>
|
|
174
|
+
constant(reference, list.name),
|
|
175
|
+
);
|
|
176
|
+
resolving.delete(definition.key);
|
|
177
|
+
values.set(definition.key, value);
|
|
178
|
+
return value;
|
|
179
|
+
};
|
|
180
|
+
return constant;
|
|
90
181
|
}
|