@tsplc/build 0.2.16 → 0.2.18
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 +628 -352
- package/dist/cli.js.map +30 -28
- package/dist/index.js +548 -387
- package/dist/index.js.map +27 -25
- package/dist/libraries.js +68 -27
- package/dist/libraries.js.map +3 -3
- package/docker/Dockerfile +2 -0
- package/docker/Dockerfile.dockerignore +4 -1
- package/docker/Dockerfile.source +3 -0
- package/docker/config.ts +10 -5
- package/docker/runner.ts +140 -127
- package/package.json +54 -53
- package/python/build.py +183 -124
- package/python/codesys_online.py +44 -45
- package/python/hardware.py +9 -9
- package/python/test_build.py +3 -3
- package/python/test_codesys_online.py +18 -12
- package/python/test_installer_packages.py +1 -0
- package/python/worker.py +11 -7
- package/src/artifacts.ts +82 -60
- package/src/build.ts +71 -62
- package/src/cli.ts +109 -100
- package/src/controller-online.ts +400 -425
- package/src/controller-protocol.ts +140 -0
- package/src/controller-session.ts +466 -397
- package/src/device-tree.ts +19 -15
- package/src/diagnostics.ts +59 -49
- package/src/docker-build.ts +217 -156
- package/src/docker-context.ts +49 -27
- package/src/docker-run.ts +137 -111
- package/src/engineering-settings.ts +42 -0
- package/src/host/codesys-command.ts +35 -34
- package/src/host/codesys-host.ts +157 -129
- package/src/host/codesys-worker.ts +318 -301
- package/src/host/windows-path.ts +63 -59
- package/src/import-request.ts +24 -0
- package/src/index.ts +37 -31
- package/src/io.ts +152 -116
- package/src/libraries.ts +214 -146
- package/src/package-path.ts +3 -1
- package/src/run.ts +132 -108
- package/src/runtime-status.ts +25 -19
- package/src/sources.ts +264 -225
- package/src/st/declarations.ts +181 -193
- package/src/st/lexer.ts +102 -118
- package/src/st/parse.ts +193 -171
- package/src/st/reader.ts +47 -42
- package/src/__tests__/fixtures/xml/fb-simple.xml +0 -40
- package/src/__tests__/fixtures/xml/fb-state-machine.xml +0 -86
- package/src/__tests__/fixtures/xml/prg-cyclic.xml +0 -44
- package/src/config.ts +0 -12
package/src/st/declarations.ts
CHANGED
|
@@ -1,229 +1,217 @@
|
|
|
1
|
+
import {
|
|
2
|
+
conditionalDirective,
|
|
3
|
+
modifiers,
|
|
4
|
+
type ObjectKeyword,
|
|
5
|
+
variableSections,
|
|
6
|
+
} from "@tsplc/iec/st";
|
|
1
7
|
import type { Token } from "./lexer";
|
|
2
8
|
import type { TokenReader } from "./reader";
|
|
3
9
|
|
|
4
10
|
export const objectKinds = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} as const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"PRIVATE",
|
|
17
|
-
"PROTECTED",
|
|
18
|
-
"INTERNAL",
|
|
19
|
-
"FINAL",
|
|
20
|
-
"ABSTRACT",
|
|
21
|
-
"OVERRIDE",
|
|
22
|
-
]);
|
|
23
|
-
export const globalSections = new Set(["VAR_GLOBAL", "VAR_CONFIG"]);
|
|
24
|
-
export const variableSections = new Set([
|
|
25
|
-
...globalSections,
|
|
26
|
-
"VAR",
|
|
27
|
-
"VAR_INPUT",
|
|
28
|
-
"VAR_OUTPUT",
|
|
29
|
-
"VAR_IN_OUT",
|
|
30
|
-
"VAR_TEMP",
|
|
31
|
-
"VAR_STAT",
|
|
32
|
-
"VAR_INST",
|
|
33
|
-
"VAR_EXTERNAL",
|
|
34
|
-
"VAR_ACCESS",
|
|
35
|
-
"VAR_GENERIC",
|
|
36
|
-
]);
|
|
37
|
-
const closingObjects = new Set(Object.keys(objectKinds).map((kind) => `END_${kind}`));
|
|
11
|
+
PROGRAM: "program",
|
|
12
|
+
FUNCTION_BLOCK: "function_block",
|
|
13
|
+
FUNCTIONBLOCK: "function_block",
|
|
14
|
+
FUNCTION: "function",
|
|
15
|
+
METHOD: "method",
|
|
16
|
+
INTERFACE: "interface",
|
|
17
|
+
PROPERTY: "property",
|
|
18
|
+
} as const satisfies Record<ObjectKeyword | "FUNCTIONBLOCK", string>;
|
|
19
|
+
const closingObjects = new Set(
|
|
20
|
+
Object.keys(objectKinds).map((kind) => `END_${kind}`),
|
|
21
|
+
);
|
|
38
22
|
export const declarationKeywords = new Set([
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
23
|
+
...Object.keys(objectKinds),
|
|
24
|
+
...closingObjects,
|
|
25
|
+
...variableSections,
|
|
26
|
+
"END_VAR",
|
|
27
|
+
"TYPE",
|
|
28
|
+
"END_TYPE",
|
|
29
|
+
"STRUCT",
|
|
30
|
+
"END_STRUCT",
|
|
31
|
+
"UNION",
|
|
32
|
+
"END_UNION",
|
|
33
|
+
"ACTION",
|
|
34
|
+
"END_ACTION",
|
|
35
|
+
"CONFIGURATION",
|
|
36
|
+
"END_CONFIGURATION",
|
|
53
37
|
]);
|
|
54
38
|
|
|
55
|
-
const conditionalDirective = (token?: Token) => {
|
|
56
|
-
if (token?.kind === "pragma") {
|
|
57
|
-
return /^\{\s*(IF|ELSIF|ELSE|END_IF)\b/i.exec(token.value)?.[1]?.toUpperCase();
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
39
|
const isDeclarationPragma = (token?: Token) =>
|
|
61
|
-
|
|
40
|
+
token?.kind === "pragma" && !conditionalDirective(token.value);
|
|
62
41
|
|
|
63
42
|
export const readDeclarationPragmas = (reader: TokenReader) => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
43
|
+
while (isDeclarationPragma(reader.peek())) {
|
|
44
|
+
reader.take();
|
|
45
|
+
}
|
|
67
46
|
};
|
|
68
47
|
|
|
69
48
|
export const readDeclarationSemicolon = (reader: TokenReader) => {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
49
|
+
const token = reader.peek();
|
|
50
|
+
// A semicolon on the next line may be the body's first empty statement.
|
|
51
|
+
// Only attach it to a declaration when it terminates that same line.
|
|
52
|
+
if (
|
|
53
|
+
token?.value === ";" &&
|
|
54
|
+
!/[\r\n]/.test(reader.source.slice(reader.end, token.start))
|
|
55
|
+
) {
|
|
56
|
+
reader.take();
|
|
57
|
+
}
|
|
76
58
|
};
|
|
77
59
|
|
|
78
60
|
export const readIdentifier = (reader: TokenReader) => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
61
|
+
const token = reader.take();
|
|
62
|
+
if (token.kind !== "word" || declarationKeywords.has(token.value)) {
|
|
63
|
+
reader.fail("Expected an IEC identifier", token);
|
|
64
|
+
}
|
|
65
|
+
return reader.source.slice(token.start, token.end);
|
|
84
66
|
};
|
|
85
67
|
|
|
86
68
|
const isOverrideModifier = (reader: TokenReader) => {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
69
|
+
// OVERRIDE is also an established IEC name (e.g. OSCAT's function).
|
|
70
|
+
// A modifier needs a following name on the same line, not a body statement.
|
|
71
|
+
const name = reader.peek(1);
|
|
72
|
+
return (
|
|
73
|
+
name?.kind === "word" &&
|
|
74
|
+
!declarationKeywords.has(name.value) &&
|
|
75
|
+
!["(", "[", ":=", ".", "^"].includes(reader.peek(2)?.value ?? "") &&
|
|
76
|
+
!/[\r\n]/.test(reader.source.slice(reader.peek()?.end, name.start))
|
|
77
|
+
);
|
|
96
78
|
};
|
|
97
79
|
|
|
98
80
|
export const readModifiers = (reader: TokenReader) => {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
81
|
+
while (modifiers.has(reader.peek()?.value ?? "")) {
|
|
82
|
+
if (reader.peek()?.value === "OVERRIDE" && !isOverrideModifier(reader)) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
reader.take();
|
|
86
|
+
}
|
|
105
87
|
};
|
|
106
88
|
|
|
107
89
|
const typeDelimiters = { "(": ")", "[": "]", "<": ">" } as const;
|
|
108
90
|
|
|
109
|
-
const readDelimitedType = (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
91
|
+
const readDelimitedType = (
|
|
92
|
+
reader: TokenReader,
|
|
93
|
+
open: keyof typeof typeDelimiters,
|
|
94
|
+
) => {
|
|
95
|
+
reader.expect(open);
|
|
96
|
+
const closings = [typeDelimiters[open]];
|
|
97
|
+
while (closings.length) {
|
|
98
|
+
const token = reader.take();
|
|
99
|
+
if (token.kind === "string" || token.kind === "pragma") {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (token.value === "(" || token.value === "[") {
|
|
103
|
+
closings.push(typeDelimiters[token.value]);
|
|
104
|
+
} else if (token.value === ")" || token.value === "]") {
|
|
105
|
+
if (closings.pop() !== token.value) {
|
|
106
|
+
reader.fail(`Mismatched delimiter ${token.value}`, token);
|
|
107
|
+
}
|
|
108
|
+
} else if (open === "<" && closings.at(-1) === ">") {
|
|
109
|
+
// Inside parentheses, < and > are comparisons, not generic delimiters:
|
|
110
|
+
// FB_Base<(Size := BOOL_TO_INT(1 < 2))>.
|
|
111
|
+
if (token.value === "<") {
|
|
112
|
+
closings.push(">");
|
|
113
|
+
}
|
|
114
|
+
if (token.value === ">") {
|
|
115
|
+
closings.pop();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (declarationKeywords.has(token.value) || token.value === ";") {
|
|
119
|
+
reader.fail(`Unclosed ${open}`, token);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
137
122
|
};
|
|
138
123
|
|
|
139
124
|
export const readType = (reader: TokenReader): string => {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
125
|
+
const start = reader.peek()?.start ?? reader.source.length;
|
|
126
|
+
// Prefix types can nest arbitrarily; iteration avoids a JavaScript stack limit.
|
|
127
|
+
while (true) {
|
|
128
|
+
if (
|
|
129
|
+
["POINTER", "REFERENCE", "REF_TO"].includes(reader.peek()?.value ?? "")
|
|
130
|
+
) {
|
|
131
|
+
if (reader.take().value !== "REF_TO") {
|
|
132
|
+
reader.expect("TO");
|
|
133
|
+
}
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (reader.consume("ARRAY")) {
|
|
137
|
+
readDelimitedType(reader, "[");
|
|
138
|
+
reader.expect("OF");
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
readIdentifier(reader);
|
|
144
|
+
while (reader.consume(".")) {
|
|
145
|
+
readIdentifier(reader);
|
|
146
|
+
}
|
|
147
|
+
for (const open of ["(", "[", "<"] as const) {
|
|
148
|
+
if (reader.peek()?.value === open) {
|
|
149
|
+
readDelimitedType(reader, open);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return reader.source.slice(start, reader.end);
|
|
166
153
|
};
|
|
167
154
|
|
|
168
155
|
export const readVariableSection = (reader: TokenReader) => {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
156
|
+
reader.take();
|
|
157
|
+
const conditionals: { hasElse: boolean }[] = [];
|
|
158
|
+
while (reader.peek()?.value !== "END_VAR") {
|
|
159
|
+
const token = reader.take();
|
|
160
|
+
if (variableSections.has(token.value) || closingObjects.has(token.value)) {
|
|
161
|
+
reader.fail("Missing END_VAR", token);
|
|
162
|
+
}
|
|
163
|
+
const directive =
|
|
164
|
+
token.kind === "pragma" && conditionalDirective(token.value);
|
|
165
|
+
if (!directive) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (directive === "IF") {
|
|
169
|
+
conditionals.push({ hasElse: false });
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const current = conditionals.at(-1);
|
|
173
|
+
if (!current) {
|
|
174
|
+
return reader.fail("Unmatched conditional pragma", token);
|
|
175
|
+
}
|
|
176
|
+
if (directive === "END_IF") {
|
|
177
|
+
conditionals.pop();
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (current.hasElse) {
|
|
181
|
+
reader.fail("Conditional branch after ELSE", token);
|
|
182
|
+
}
|
|
183
|
+
current.hasElse = directive === "ELSE";
|
|
184
|
+
}
|
|
185
|
+
if (conditionals.length) {
|
|
186
|
+
reader.fail("Conditional pragmas must remain inside one variable section");
|
|
187
|
+
}
|
|
188
|
+
reader.take();
|
|
189
|
+
readDeclarationSemicolon(reader);
|
|
202
190
|
};
|
|
203
191
|
|
|
204
192
|
export const readDeclarationSections = (reader: TokenReader) => {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
193
|
+
while (reader.peek()) {
|
|
194
|
+
// Pragmas belong to the declaration only when another declaration follows.
|
|
195
|
+
// Look ahead without consuming them so a pragma-only body stays intact.
|
|
196
|
+
let ahead = 0;
|
|
197
|
+
while (isDeclarationPragma(reader.peek(ahead))) {
|
|
198
|
+
ahead++;
|
|
199
|
+
}
|
|
200
|
+
const next = reader.peek(ahead)?.value ?? "";
|
|
201
|
+
if (variableSections.has(next)) {
|
|
202
|
+
readDeclarationPragmas(reader);
|
|
203
|
+
readVariableSection(reader);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (next !== "EXTENDS" && next !== "IMPLEMENTS") {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
// CODESYS generic FBs can place these clauses after VAR_GENERIC.
|
|
210
|
+
readDeclarationPragmas(reader);
|
|
211
|
+
reader.take();
|
|
212
|
+
readType(reader);
|
|
213
|
+
while (reader.consume(",")) {
|
|
214
|
+
readType(reader);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
229
217
|
};
|
package/src/st/lexer.ts
CHANGED
|
@@ -1,125 +1,109 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { blockCommentEnd, lineCommentEnd, stringEnd } from "@tsplc/iec/st";
|
|
2
|
+
|
|
3
|
+
export interface Token {
|
|
4
|
+
kind: "word" | "string" | "pragma" | "symbol";
|
|
5
|
+
value: string;
|
|
6
|
+
start: number;
|
|
7
|
+
end: number;
|
|
8
|
+
}
|
|
7
9
|
|
|
8
10
|
export class StSyntaxError extends Error {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
constructor(
|
|
12
|
+
source: string,
|
|
13
|
+
offset: number,
|
|
14
|
+
filename: string,
|
|
15
|
+
message: string,
|
|
16
|
+
) {
|
|
17
|
+
const lines = source.slice(0, offset).split(/\r\n|\r|\n/);
|
|
18
|
+
super(
|
|
19
|
+
`${filename}:${lines.length}:${(lines.at(-1)?.length ?? 0) + 1}: ${message}`,
|
|
20
|
+
);
|
|
21
|
+
this.name = "StSyntaxError";
|
|
22
|
+
}
|
|
14
23
|
}
|
|
15
24
|
|
|
16
25
|
/** Keep original offsets: splitting must preserve comments, pragmas and source spelling. */
|
|
17
26
|
export const tokenize = (source: string, filename = "<source>"): Token[] => {
|
|
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
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
offset++;
|
|
101
|
-
while (/[A-Za-z_0-9]/.test(source[offset] ?? "")) {
|
|
102
|
-
offset++;
|
|
103
|
-
}
|
|
104
|
-
tokens.push({
|
|
105
|
-
kind: "word",
|
|
106
|
-
value: source.slice(start, offset).toUpperCase(),
|
|
107
|
-
start,
|
|
108
|
-
end: offset,
|
|
109
|
-
});
|
|
110
|
-
continue;
|
|
111
|
-
}
|
|
112
|
-
offset++;
|
|
113
|
-
const pair = source.slice(start, offset + 1);
|
|
114
|
-
if ([":=", "=>", "..", "<=", ">=", "<>", "**"].includes(pair)) {
|
|
115
|
-
offset++;
|
|
116
|
-
}
|
|
117
|
-
tokens.push({
|
|
118
|
-
kind: "symbol",
|
|
119
|
-
value: source.slice(start, offset),
|
|
120
|
-
start,
|
|
121
|
-
end: offset,
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
return tokens;
|
|
27
|
+
const tokens: Token[] = [];
|
|
28
|
+
let offset = 0;
|
|
29
|
+
const fail = (start: number, message: string): never => {
|
|
30
|
+
throw new StSyntaxError(source, start, filename, message);
|
|
31
|
+
};
|
|
32
|
+
const quoted = () => {
|
|
33
|
+
offset = stringEnd(source, offset) ?? fail(offset, "Unterminated string");
|
|
34
|
+
};
|
|
35
|
+
while (offset < source.length) {
|
|
36
|
+
const start = offset;
|
|
37
|
+
const character = source[offset] ?? "";
|
|
38
|
+
if (/\s|\uFEFF/.test(character)) {
|
|
39
|
+
offset++;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (source.startsWith("//", offset)) {
|
|
43
|
+
offset = lineCommentEnd(source, start);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (source.startsWith("(*", offset)) {
|
|
47
|
+
offset =
|
|
48
|
+
blockCommentEnd(source, start) ??
|
|
49
|
+
fail(start, "Unterminated block comment");
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (character === "'" || character === '"') {
|
|
53
|
+
quoted();
|
|
54
|
+
tokens.push({
|
|
55
|
+
kind: "string",
|
|
56
|
+
value: source.slice(start, offset),
|
|
57
|
+
start,
|
|
58
|
+
end: offset,
|
|
59
|
+
});
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (character === "{") {
|
|
63
|
+
offset++;
|
|
64
|
+
while (offset < source.length && source[offset] !== "}") {
|
|
65
|
+
if (source[offset] === "'" || source[offset] === '"') {
|
|
66
|
+
quoted();
|
|
67
|
+
} else {
|
|
68
|
+
offset++;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (offset === source.length) {
|
|
72
|
+
fail(start, "Unterminated pragma");
|
|
73
|
+
}
|
|
74
|
+
offset++;
|
|
75
|
+
tokens.push({
|
|
76
|
+
kind: "pragma",
|
|
77
|
+
value: source.slice(start, offset),
|
|
78
|
+
start,
|
|
79
|
+
end: offset,
|
|
80
|
+
});
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (/[A-Za-z_]/.test(character)) {
|
|
84
|
+
offset++;
|
|
85
|
+
while (/[A-Za-z_0-9]/.test(source[offset] ?? "")) {
|
|
86
|
+
offset++;
|
|
87
|
+
}
|
|
88
|
+
tokens.push({
|
|
89
|
+
kind: "word",
|
|
90
|
+
value: source.slice(start, offset).toUpperCase(),
|
|
91
|
+
start,
|
|
92
|
+
end: offset,
|
|
93
|
+
});
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
offset++;
|
|
97
|
+
const pair = source.slice(start, offset + 1);
|
|
98
|
+
if ([":=", "=>", "..", "<=", ">=", "<>", "**"].includes(pair)) {
|
|
99
|
+
offset++;
|
|
100
|
+
}
|
|
101
|
+
tokens.push({
|
|
102
|
+
kind: "symbol",
|
|
103
|
+
value: source.slice(start, offset),
|
|
104
|
+
start,
|
|
105
|
+
end: offset,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return tokens;
|
|
125
109
|
};
|