@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
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { basename } from "node:path";
|
|
3
|
+
import { blockCommentEnd, lineCommentEnd } from "@tsplc/iec/st";
|
|
4
|
+
import { stFiles } from "./generate.ts";
|
|
5
|
+
import {
|
|
6
|
+
type Declaration,
|
|
7
|
+
type GlobalList,
|
|
8
|
+
parseDeclarations,
|
|
9
|
+
parseGlobals,
|
|
10
|
+
} from "./parser.ts";
|
|
11
|
+
|
|
12
|
+
export type {
|
|
13
|
+
Declaration,
|
|
14
|
+
GlobalList,
|
|
15
|
+
Member,
|
|
16
|
+
SymbolAccess,
|
|
17
|
+
TypeReference,
|
|
18
|
+
} from "./parser.ts";
|
|
19
|
+
|
|
20
|
+
/** Where ST code begins at or after `offset`: past whitespace, comments and pragmas. */
|
|
21
|
+
const codeStart = (text: string, offset: number): number => {
|
|
22
|
+
const start = offset + text.slice(offset).search(/\S|$/);
|
|
23
|
+
if (text.startsWith("(*", start)) {
|
|
24
|
+
const end = blockCommentEnd(text, start);
|
|
25
|
+
return end === null ? start : codeStart(text, end);
|
|
26
|
+
}
|
|
27
|
+
if (text.startsWith("//", start)) {
|
|
28
|
+
return codeStart(text, lineCommentEnd(text, start));
|
|
29
|
+
}
|
|
30
|
+
if (text.startsWith("{", start)) {
|
|
31
|
+
const end = text.indexOf("}", start);
|
|
32
|
+
return end < 0 ? start : codeStart(text, end + 1);
|
|
33
|
+
}
|
|
34
|
+
return start;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** The files among `files` that declare TYPEs and those that declare VAR_GLOBAL lists; other POUs are neither. */
|
|
38
|
+
export const declarationFiles = async (files: readonly string[]) => {
|
|
39
|
+
const sorted: { types: string[]; globals: string[] } = {
|
|
40
|
+
types: [],
|
|
41
|
+
globals: [],
|
|
42
|
+
};
|
|
43
|
+
for (const file of files) {
|
|
44
|
+
const text = await readFile(file, "utf8");
|
|
45
|
+
const code = text.slice(codeStart(text, 0));
|
|
46
|
+
if (/^TYPE\b/i.test(code)) {
|
|
47
|
+
sorted.types.push(file);
|
|
48
|
+
} else if (/^VAR_GLOBAL\b/i.test(code)) {
|
|
49
|
+
sorted.globals.push(file);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return sorted;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The TYPE declarations and global lists of a directory of Structured Text, such as the
|
|
57
|
+
* compiler's emitted ST: what CODESYS compiles, each list named after its file.
|
|
58
|
+
*/
|
|
59
|
+
export const readDeclarations = async (
|
|
60
|
+
directory: string,
|
|
61
|
+
): Promise<{ types: Declaration[]; globals: GlobalList[] }> => {
|
|
62
|
+
const files = await declarationFiles(await stFiles(directory));
|
|
63
|
+
const read = (file: string) =>
|
|
64
|
+
readFile(file, "utf8").then((text) => ({ name: file, text }));
|
|
65
|
+
return {
|
|
66
|
+
types: (await Promise.all(files.types.map(read))).flatMap(
|
|
67
|
+
parseDeclarations,
|
|
68
|
+
),
|
|
69
|
+
globals: (await Promise.all(files.globals.map(read))).map((source) =>
|
|
70
|
+
parseGlobals(source, basename(source.name).replace(/\.st$/i, "")),
|
|
71
|
+
),
|
|
72
|
+
};
|
|
73
|
+
};
|
package/src/codegen/generate.ts
CHANGED
package/src/codegen/parser.ts
CHANGED
|
@@ -25,10 +25,15 @@ export interface Member {
|
|
|
25
25
|
description: string;
|
|
26
26
|
type: TypeReference;
|
|
27
27
|
initial?: string;
|
|
28
|
-
/**
|
|
29
|
-
|
|
28
|
+
/** The CODESYS symbol attribute that publishes this global for clients, and the access it grants them. */
|
|
29
|
+
symbol?: SymbolAccess;
|
|
30
|
+
/** Declared in a `RETAIN` or `PERSISTENT` global section. */
|
|
31
|
+
retain?: true;
|
|
30
32
|
}
|
|
31
33
|
|
|
34
|
+
/** What a published global's `{attribute 'symbol'}` lets clients do. */
|
|
35
|
+
export type SymbolAccess = "read" | "write" | "readwrite";
|
|
36
|
+
|
|
32
37
|
export type Declaration = {
|
|
33
38
|
name: string;
|
|
34
39
|
description: string;
|
|
@@ -253,18 +258,15 @@ class Parser {
|
|
|
253
258
|
return { kind: "named", name };
|
|
254
259
|
}
|
|
255
260
|
|
|
256
|
-
private members(end: string): Member[] {
|
|
261
|
+
private members(end: string, { retain }: { retain: boolean }): Member[] {
|
|
257
262
|
const members: Member[] = [];
|
|
258
263
|
while (this.peek() !== end) {
|
|
259
|
-
const
|
|
264
|
+
const symbol = this.tokens
|
|
260
265
|
.slice(this.position, this.nextIndex())
|
|
261
|
-
.
|
|
262
|
-
(token)
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
token.text,
|
|
266
|
-
),
|
|
267
|
-
);
|
|
266
|
+
.flatMap((token) =>
|
|
267
|
+
token.kind === "attribute" ? symbolAccess(token.text) : [],
|
|
268
|
+
)
|
|
269
|
+
.at(-1);
|
|
268
270
|
const description = this.documentation();
|
|
269
271
|
const names = [this.identifier()];
|
|
270
272
|
while (this.accept(",")) {
|
|
@@ -295,7 +297,8 @@ class Parser {
|
|
|
295
297
|
description,
|
|
296
298
|
type,
|
|
297
299
|
...(initial === undefined ? {} : { initial }),
|
|
298
|
-
...(
|
|
300
|
+
...(symbol ? { symbol } : {}),
|
|
301
|
+
...(retain ? { retain } : {}),
|
|
299
302
|
});
|
|
300
303
|
}
|
|
301
304
|
}
|
|
@@ -322,7 +325,7 @@ class Parser {
|
|
|
322
325
|
declarations.push({
|
|
323
326
|
...common,
|
|
324
327
|
kind: "struct",
|
|
325
|
-
members: this.members("END_STRUCT"),
|
|
328
|
+
members: this.members("END_STRUCT", { retain: false }),
|
|
326
329
|
});
|
|
327
330
|
this.accept(";");
|
|
328
331
|
} else if (this.accept("(")) {
|
|
@@ -396,12 +399,24 @@ class Parser {
|
|
|
396
399
|
}
|
|
397
400
|
written.add(qualifier);
|
|
398
401
|
}
|
|
399
|
-
const members = this.members("END_VAR"
|
|
402
|
+
const members = this.members("END_VAR", {
|
|
403
|
+
retain: written.has("RETAIN") || written.has("PERSISTENT"),
|
|
404
|
+
});
|
|
400
405
|
this.accept(";");
|
|
401
406
|
return { constant: written.has("CONSTANT"), members };
|
|
402
407
|
}
|
|
403
408
|
}
|
|
404
409
|
|
|
410
|
+
const symbolAccess = (attribute: string): SymbolAccess[] => {
|
|
411
|
+
const access =
|
|
412
|
+
/^\{\s*attribute\s+'symbol'\s*:=\s*'(read|write|readwrite)'\s*\}$/i
|
|
413
|
+
.exec(attribute)?.[1]
|
|
414
|
+
?.toLowerCase();
|
|
415
|
+
return access === "read" || access === "write" || access === "readwrite"
|
|
416
|
+
? [access]
|
|
417
|
+
: [];
|
|
418
|
+
};
|
|
419
|
+
|
|
405
420
|
export function parseDeclarations(source: Source): Declaration[] {
|
|
406
421
|
return new Parser(source).declarations();
|
|
407
422
|
}
|
package/src/project.ts
CHANGED
|
@@ -13,9 +13,9 @@ import {
|
|
|
13
13
|
emittedStDirectory,
|
|
14
14
|
projectSnapshotFile,
|
|
15
15
|
} from "@tsplc/config/project-files";
|
|
16
|
-
import { blockCommentEnd, lineCommentEnd } from "@tsplc/iec/st";
|
|
17
16
|
import { format } from "prettier";
|
|
18
17
|
import type { z } from "zod";
|
|
18
|
+
import { declarationFiles } from "./codegen/declarations.ts";
|
|
19
19
|
import { generateTypes, stFiles } from "./codegen/generate.ts";
|
|
20
20
|
import {
|
|
21
21
|
generateHardwareTypes,
|
|
@@ -110,34 +110,7 @@ async function contractSources(
|
|
|
110
110
|
if (!files.length) {
|
|
111
111
|
throw new Error(`no .st declarations found in ${sourceRoot}`);
|
|
112
112
|
}
|
|
113
|
-
|
|
114
|
-
for (const file of files) {
|
|
115
|
-
const text = await readFile(file, "utf8");
|
|
116
|
-
const code = text.slice(codeStart(text, 0));
|
|
117
|
-
if (/^TYPE\b/i.test(code)) {
|
|
118
|
-
sources.types.push(file);
|
|
119
|
-
} else if (/^VAR_GLOBAL\b/i.test(code)) {
|
|
120
|
-
sources.globals.push(file);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return sources;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** Where ST code begins at or after `offset`: past whitespace, comments and pragmas. */
|
|
127
|
-
function codeStart(text: string, offset: number): number {
|
|
128
|
-
const start = offset + text.slice(offset).search(/\S|$/);
|
|
129
|
-
if (text.startsWith("(*", start)) {
|
|
130
|
-
const end = blockCommentEnd(text, start);
|
|
131
|
-
return end === null ? start : codeStart(text, end);
|
|
132
|
-
}
|
|
133
|
-
if (text.startsWith("//", start)) {
|
|
134
|
-
return codeStart(text, lineCommentEnd(text, start));
|
|
135
|
-
}
|
|
136
|
-
if (text.startsWith("{", start)) {
|
|
137
|
-
const end = text.indexOf("}", start);
|
|
138
|
-
return end < 0 ? start : codeStart(text, end + 1);
|
|
139
|
-
}
|
|
140
|
-
return start;
|
|
113
|
+
return { ...(await declarationFiles(files)), explicit: false };
|
|
141
114
|
}
|
|
142
115
|
|
|
143
116
|
/** Every .st file under each directory, in directory order. */
|