@sdkgen/parser 0.0.0-dev.20231002135133 → 0.0.0-dev.20231002144112
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/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/ast.ts +273 -0
- package/src/compatibility/index.ts +273 -0
- package/src/helpers.ts +28 -0
- package/src/index.ts +8 -0
- package/src/json.ts +350 -0
- package/src/lexer.ts +343 -0
- package/src/parser.ts +604 -0
- package/src/restparser.ts +81 -0
- package/src/semantic/01_check_multiple_declaration.ts +21 -0
- package/src/semantic/02_give_struct_and_enum_names.ts +57 -0
- package/src/semantic/03_match_type_definitions.ts +19 -0
- package/src/semantic/04_check_dont_return_secret.ts +40 -0
- package/src/semantic/05_expand_spreads.ts +67 -0
- package/src/semantic/06_check_empty_struct_or_enum.ts +21 -0
- package/src/semantic/07_validate_recursive_types.ts +47 -0
- package/src/semantic/08_collect_struct_and_enum_types.ts +23 -0
- package/src/semantic/09_validate_annotations.ts +158 -0
- package/src/semantic/10_check_duplicate_members_on_enum.ts +15 -0
- package/src/semantic/analyser.ts +29 -0
- package/src/semantic/transformer.ts +41 -0
- package/src/semantic/visitor.ts +60 -0
- package/src/token.ts +92 -0
- package/src/utils.ts +63 -0
package/src/lexer.ts
ADDED
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import type { Token } from "./token";
|
|
2
|
+
import {
|
|
3
|
+
AnnotationToken,
|
|
4
|
+
ArraySymbolToken,
|
|
5
|
+
ColonSymbolToken,
|
|
6
|
+
CommaSymbolToken,
|
|
7
|
+
CurlyCloseSymbolToken,
|
|
8
|
+
CurlyOpenSymbolToken,
|
|
9
|
+
EnumKeywordToken,
|
|
10
|
+
EqualSymbolToken,
|
|
11
|
+
ErrorKeywordToken,
|
|
12
|
+
ExclamationMarkSymbolToken,
|
|
13
|
+
FalseKeywordToken,
|
|
14
|
+
FnKeywordToken,
|
|
15
|
+
IdentifierToken,
|
|
16
|
+
ImportKeywordToken,
|
|
17
|
+
OptionalSymbolToken,
|
|
18
|
+
ParensCloseSymbolToken,
|
|
19
|
+
ParensOpenSymbolToken,
|
|
20
|
+
PrimitiveTypeToken,
|
|
21
|
+
SpreadSymbolToken,
|
|
22
|
+
StringLiteralToken,
|
|
23
|
+
TrueKeywordToken,
|
|
24
|
+
TypeKeywordToken,
|
|
25
|
+
} from "./token";
|
|
26
|
+
|
|
27
|
+
export class LexerError extends Error {}
|
|
28
|
+
|
|
29
|
+
export class Lexer {
|
|
30
|
+
public static readonly PRIMITIVES = new Set([
|
|
31
|
+
"bool",
|
|
32
|
+
"int",
|
|
33
|
+
"uint",
|
|
34
|
+
"float",
|
|
35
|
+
"bigint",
|
|
36
|
+
"string",
|
|
37
|
+
"date",
|
|
38
|
+
"datetime",
|
|
39
|
+
"bytes",
|
|
40
|
+
"money",
|
|
41
|
+
"decimal",
|
|
42
|
+
"cpf",
|
|
43
|
+
"cnpj",
|
|
44
|
+
"email",
|
|
45
|
+
"html",
|
|
46
|
+
"url",
|
|
47
|
+
"uuid",
|
|
48
|
+
"hex",
|
|
49
|
+
"base64",
|
|
50
|
+
"xml",
|
|
51
|
+
"void",
|
|
52
|
+
"json",
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
public static readonly KEYWORDS = new Set([...Lexer.PRIMITIVES, "error", "enum", "type", "import", "get", "function", "fn", "true", "false"]);
|
|
56
|
+
|
|
57
|
+
private startPos = 0;
|
|
58
|
+
|
|
59
|
+
private startLine = 1;
|
|
60
|
+
|
|
61
|
+
private startColumn = 1;
|
|
62
|
+
|
|
63
|
+
private pos = 0;
|
|
64
|
+
|
|
65
|
+
private line = 1;
|
|
66
|
+
|
|
67
|
+
private column = 1;
|
|
68
|
+
|
|
69
|
+
constructor(
|
|
70
|
+
private readonly source: string,
|
|
71
|
+
public readonly filename = "-",
|
|
72
|
+
) {}
|
|
73
|
+
|
|
74
|
+
private currentChar() {
|
|
75
|
+
return this.source[this.pos] || "\0";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private nextChar() {
|
|
79
|
+
this.column++;
|
|
80
|
+
return this.source[++this.pos] || "\0";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
nextToken(): Token | null {
|
|
84
|
+
this.startPos = this.pos;
|
|
85
|
+
this.startLine = this.line;
|
|
86
|
+
this.startColumn = this.column;
|
|
87
|
+
|
|
88
|
+
let token: Token | null = null;
|
|
89
|
+
|
|
90
|
+
switch (this.currentChar()) {
|
|
91
|
+
case "\0":
|
|
92
|
+
return null;
|
|
93
|
+
case " ":
|
|
94
|
+
case "\r":
|
|
95
|
+
case "\t":
|
|
96
|
+
this.nextChar();
|
|
97
|
+
return this.nextToken();
|
|
98
|
+
case "\n":
|
|
99
|
+
this.nextChar();
|
|
100
|
+
this.column = 1;
|
|
101
|
+
this.line++;
|
|
102
|
+
return this.nextToken();
|
|
103
|
+
case "/":
|
|
104
|
+
switch (this.nextChar()) {
|
|
105
|
+
case "/":
|
|
106
|
+
for (;;) {
|
|
107
|
+
switch (this.nextChar()) {
|
|
108
|
+
case "\0":
|
|
109
|
+
return null;
|
|
110
|
+
case "\n":
|
|
111
|
+
this.nextChar();
|
|
112
|
+
this.column = 1;
|
|
113
|
+
this.line++;
|
|
114
|
+
return this.nextToken();
|
|
115
|
+
default:
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
case "*":
|
|
121
|
+
// eslint-disable-next-line no-labels
|
|
122
|
+
outerWhile: for (;;) {
|
|
123
|
+
switch (this.nextChar()) {
|
|
124
|
+
case "\0":
|
|
125
|
+
// eslint-disable-next-line no-labels
|
|
126
|
+
break outerWhile;
|
|
127
|
+
case "\n":
|
|
128
|
+
this.column = 0;
|
|
129
|
+
this.line++;
|
|
130
|
+
break;
|
|
131
|
+
case "*":
|
|
132
|
+
while (this.nextChar() === "*") {
|
|
133
|
+
//
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
switch (this.currentChar()) {
|
|
137
|
+
case "\0":
|
|
138
|
+
// eslint-disable-next-line no-labels
|
|
139
|
+
break outerWhile;
|
|
140
|
+
case "\n":
|
|
141
|
+
this.column = 0;
|
|
142
|
+
this.line++;
|
|
143
|
+
break;
|
|
144
|
+
case "/":
|
|
145
|
+
this.nextChar();
|
|
146
|
+
return this.nextToken();
|
|
147
|
+
default:
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
break;
|
|
152
|
+
|
|
153
|
+
default:
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
break;
|
|
159
|
+
default:
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
break;
|
|
164
|
+
case "{":
|
|
165
|
+
this.nextChar();
|
|
166
|
+
token = new CurlyOpenSymbolToken();
|
|
167
|
+
break;
|
|
168
|
+
case "}":
|
|
169
|
+
this.nextChar();
|
|
170
|
+
token = new CurlyCloseSymbolToken();
|
|
171
|
+
break;
|
|
172
|
+
case "(":
|
|
173
|
+
this.nextChar();
|
|
174
|
+
token = new ParensOpenSymbolToken();
|
|
175
|
+
break;
|
|
176
|
+
case ")":
|
|
177
|
+
this.nextChar();
|
|
178
|
+
token = new ParensCloseSymbolToken();
|
|
179
|
+
break;
|
|
180
|
+
case "?":
|
|
181
|
+
this.nextChar();
|
|
182
|
+
token = new OptionalSymbolToken();
|
|
183
|
+
break;
|
|
184
|
+
case ":":
|
|
185
|
+
this.nextChar();
|
|
186
|
+
token = new ColonSymbolToken();
|
|
187
|
+
break;
|
|
188
|
+
case "=":
|
|
189
|
+
this.nextChar();
|
|
190
|
+
token = new EqualSymbolToken();
|
|
191
|
+
break;
|
|
192
|
+
case "!":
|
|
193
|
+
this.nextChar();
|
|
194
|
+
token = new ExclamationMarkSymbolToken();
|
|
195
|
+
break;
|
|
196
|
+
case ",":
|
|
197
|
+
this.nextChar();
|
|
198
|
+
token = new CommaSymbolToken();
|
|
199
|
+
break;
|
|
200
|
+
case "[":
|
|
201
|
+
switch (this.nextChar()) {
|
|
202
|
+
case "]": {
|
|
203
|
+
this.nextChar();
|
|
204
|
+
token = new ArraySymbolToken();
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
default:
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
break;
|
|
213
|
+
case ".":
|
|
214
|
+
switch (this.nextChar()) {
|
|
215
|
+
case ".": {
|
|
216
|
+
switch (this.nextChar()) {
|
|
217
|
+
case ".": {
|
|
218
|
+
this.nextChar();
|
|
219
|
+
token = new SpreadSymbolToken();
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
default:
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
default:
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
break;
|
|
235
|
+
case "@": {
|
|
236
|
+
let body = "\\";
|
|
237
|
+
let pos = this.startPos + 1;
|
|
238
|
+
|
|
239
|
+
while (body.endsWith("\\")) {
|
|
240
|
+
body = body.slice(0, body.length - 1).trim();
|
|
241
|
+
while (!["\0", "\n"].includes(this.nextChar())) {
|
|
242
|
+
//
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
body = `${body} ${this.source.substring(pos, this.pos).trim()}`.trim();
|
|
246
|
+
pos = this.pos + 1;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
token = new AnnotationToken(body.trim());
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
case '"': {
|
|
254
|
+
const chars = [];
|
|
255
|
+
|
|
256
|
+
// eslint-disable-next-line no-labels
|
|
257
|
+
outerLoop: for (;;) {
|
|
258
|
+
switch (this.nextChar()) {
|
|
259
|
+
case "\0":
|
|
260
|
+
// eslint-disable-next-line no-labels
|
|
261
|
+
break outerLoop;
|
|
262
|
+
case "\\":
|
|
263
|
+
switch (this.nextChar()) {
|
|
264
|
+
case "\0":
|
|
265
|
+
// eslint-disable-next-line no-labels
|
|
266
|
+
break outerLoop;
|
|
267
|
+
case "n":
|
|
268
|
+
chars.push("\n");
|
|
269
|
+
break;
|
|
270
|
+
case "t":
|
|
271
|
+
chars.push("\t");
|
|
272
|
+
break;
|
|
273
|
+
default:
|
|
274
|
+
chars.push(this.currentChar());
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
break;
|
|
279
|
+
case '"':
|
|
280
|
+
this.nextChar();
|
|
281
|
+
token = new StringLiteralToken(chars.join(""));
|
|
282
|
+
// eslint-disable-next-line no-labels
|
|
283
|
+
break outerLoop;
|
|
284
|
+
default:
|
|
285
|
+
chars.push(this.currentChar());
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
default: {
|
|
294
|
+
if (/[a-zA-Z_]/u.test(this.currentChar())) {
|
|
295
|
+
while (/[a-zA-Z0-9_]/u.test(this.nextChar())) {
|
|
296
|
+
//
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const ident = this.source.substring(this.startPos, this.pos);
|
|
300
|
+
|
|
301
|
+
switch (ident) {
|
|
302
|
+
case "error":
|
|
303
|
+
token = new ErrorKeywordToken();
|
|
304
|
+
break;
|
|
305
|
+
case "enum":
|
|
306
|
+
token = new EnumKeywordToken();
|
|
307
|
+
break;
|
|
308
|
+
case "type":
|
|
309
|
+
token = new TypeKeywordToken();
|
|
310
|
+
break;
|
|
311
|
+
case "import":
|
|
312
|
+
token = new ImportKeywordToken();
|
|
313
|
+
break;
|
|
314
|
+
case "fn":
|
|
315
|
+
token = new FnKeywordToken();
|
|
316
|
+
break;
|
|
317
|
+
case "true":
|
|
318
|
+
token = new TrueKeywordToken();
|
|
319
|
+
break;
|
|
320
|
+
case "false":
|
|
321
|
+
token = new FalseKeywordToken();
|
|
322
|
+
break;
|
|
323
|
+
default:
|
|
324
|
+
token = Lexer.PRIMITIVES.has(ident) ? new PrimitiveTypeToken(ident) : new IdentifierToken(ident);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (token !== null) {
|
|
331
|
+
token.location.filename = this.filename;
|
|
332
|
+
token.location.line = this.startLine;
|
|
333
|
+
token.location.column = this.startColumn;
|
|
334
|
+
return token;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (this.currentChar() === "\0") {
|
|
338
|
+
throw new LexerError(`Unexpected end of file at ${this.filename}`);
|
|
339
|
+
} else {
|
|
340
|
+
throw new LexerError(`Unexpected character ${JSON.stringify(this.currentChar())} at ${this.filename}:${this.line}:${this.column}`);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|