@sdkgen/parser 0.0.0-dev.20231002144112 → 0.0.0-dev.20231002175619

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.
Files changed (100) hide show
  1. package/dist/ast.js +298 -0
  2. package/dist/compatibility/index.js +206 -0
  3. package/dist/helpers.js +20 -0
  4. package/dist/index.js +9 -0
  5. package/dist/json.js +225 -0
  6. package/dist/lexer.js +283 -0
  7. package/dist/parser.js +419 -0
  8. package/dist/restparser.js +59 -0
  9. package/dist/semantic/01_check_multiple_declaration.js +19 -0
  10. package/dist/semantic/02_give_struct_and_enum_names.js +58 -0
  11. package/dist/semantic/03_match_type_definitions.js +15 -0
  12. package/dist/semantic/04_check_dont_return_secret.js +39 -0
  13. package/dist/semantic/05_expand_spreads.js +57 -0
  14. package/dist/semantic/06_check_empty_struct_or_enum.js +18 -0
  15. package/dist/semantic/07_validate_recursive_types.js +46 -0
  16. package/dist/semantic/08_collect_struct_and_enum_types.js +22 -0
  17. package/dist/semantic/09_validate_annotations.js +122 -0
  18. package/dist/semantic/10_check_duplicate_members_on_enum.js +13 -0
  19. package/dist/semantic/analyser.js +27 -0
  20. package/dist/semantic/transformer.js +46 -0
  21. package/dist/semantic/visitor.js +61 -0
  22. package/dist/token.js +93 -0
  23. package/dist/utils.js +25 -0
  24. package/package.json +10 -3
  25. package/.eslintrc.json +0 -3
  26. package/.prettierrc +0 -12
  27. package/dist/spec/lexer.spec.d.ts +0 -1
  28. package/dist/spec/lexer.spec.js +0 -152
  29. package/dist/spec/parser.spec.d.ts +0 -1
  30. package/dist/spec/parser.spec.js +0 -967
  31. package/dist/src/ast.js +0 -355
  32. package/dist/src/compatibility/index.js +0 -211
  33. package/dist/src/helpers.js +0 -23
  34. package/dist/src/index.js +0 -29
  35. package/dist/src/json.js +0 -234
  36. package/dist/src/lexer.js +0 -287
  37. package/dist/src/parser.js +0 -423
  38. package/dist/src/restparser.js +0 -65
  39. package/dist/src/semantic/01_check_multiple_declaration.js +0 -22
  40. package/dist/src/semantic/02_give_struct_and_enum_names.js +0 -61
  41. package/dist/src/semantic/03_match_type_definitions.js +0 -18
  42. package/dist/src/semantic/04_check_dont_return_secret.js +0 -42
  43. package/dist/src/semantic/05_expand_spreads.js +0 -60
  44. package/dist/src/semantic/06_check_empty_struct_or_enum.js +0 -21
  45. package/dist/src/semantic/07_validate_recursive_types.js +0 -49
  46. package/dist/src/semantic/08_collect_struct_and_enum_types.js +0 -25
  47. package/dist/src/semantic/09_validate_annotations.js +0 -125
  48. package/dist/src/semantic/10_check_duplicate_members_on_enum.js +0 -16
  49. package/dist/src/semantic/analyser.js +0 -30
  50. package/dist/src/semantic/transformer.js +0 -50
  51. package/dist/src/semantic/visitor.js +0 -65
  52. package/dist/src/token.js +0 -119
  53. package/dist/src/utils.js +0 -27
  54. package/dist/tsconfig.tsbuildinfo +0 -1
  55. package/src/ast.ts +0 -273
  56. package/src/compatibility/index.ts +0 -273
  57. package/src/helpers.ts +0 -28
  58. package/src/index.ts +0 -8
  59. package/src/json.ts +0 -350
  60. package/src/lexer.ts +0 -343
  61. package/src/parser.ts +0 -604
  62. package/src/restparser.ts +0 -81
  63. package/src/semantic/01_check_multiple_declaration.ts +0 -21
  64. package/src/semantic/02_give_struct_and_enum_names.ts +0 -57
  65. package/src/semantic/03_match_type_definitions.ts +0 -19
  66. package/src/semantic/04_check_dont_return_secret.ts +0 -40
  67. package/src/semantic/05_expand_spreads.ts +0 -67
  68. package/src/semantic/06_check_empty_struct_or_enum.ts +0 -21
  69. package/src/semantic/07_validate_recursive_types.ts +0 -47
  70. package/src/semantic/08_collect_struct_and_enum_types.ts +0 -23
  71. package/src/semantic/09_validate_annotations.ts +0 -158
  72. package/src/semantic/10_check_duplicate_members_on_enum.ts +0 -15
  73. package/src/semantic/analyser.ts +0 -29
  74. package/src/semantic/transformer.ts +0 -41
  75. package/src/semantic/visitor.ts +0 -60
  76. package/src/token.ts +0 -92
  77. package/src/utils.ts +0 -63
  78. /package/dist/{src/ast.d.ts → ast.d.ts} +0 -0
  79. /package/dist/{src/compatibility → compatibility}/index.d.ts +0 -0
  80. /package/dist/{src/helpers.d.ts → helpers.d.ts} +0 -0
  81. /package/dist/{src/index.d.ts → index.d.ts} +0 -0
  82. /package/dist/{src/json.d.ts → json.d.ts} +0 -0
  83. /package/dist/{src/lexer.d.ts → lexer.d.ts} +0 -0
  84. /package/dist/{src/parser.d.ts → parser.d.ts} +0 -0
  85. /package/dist/{src/restparser.d.ts → restparser.d.ts} +0 -0
  86. /package/dist/{src/semantic → semantic}/01_check_multiple_declaration.d.ts +0 -0
  87. /package/dist/{src/semantic → semantic}/02_give_struct_and_enum_names.d.ts +0 -0
  88. /package/dist/{src/semantic → semantic}/03_match_type_definitions.d.ts +0 -0
  89. /package/dist/{src/semantic → semantic}/04_check_dont_return_secret.d.ts +0 -0
  90. /package/dist/{src/semantic → semantic}/05_expand_spreads.d.ts +0 -0
  91. /package/dist/{src/semantic → semantic}/06_check_empty_struct_or_enum.d.ts +0 -0
  92. /package/dist/{src/semantic → semantic}/07_validate_recursive_types.d.ts +0 -0
  93. /package/dist/{src/semantic → semantic}/08_collect_struct_and_enum_types.d.ts +0 -0
  94. /package/dist/{src/semantic → semantic}/09_validate_annotations.d.ts +0 -0
  95. /package/dist/{src/semantic → semantic}/10_check_duplicate_members_on_enum.d.ts +0 -0
  96. /package/dist/{src/semantic → semantic}/analyser.d.ts +0 -0
  97. /package/dist/{src/semantic → semantic}/transformer.d.ts +0 -0
  98. /package/dist/{src/semantic → semantic}/visitor.d.ts +0 -0
  99. /package/dist/{src/token.d.ts → token.d.ts} +0 -0
  100. /package/dist/{src/utils.d.ts → utils.d.ts} +0 -0
package/src/index.ts DELETED
@@ -1,8 +0,0 @@
1
- export * from "./ast";
2
- export * from "./compatibility";
3
- export { AstJson, astToJson, jsonToAst, TypeDescription } from "./json";
4
- export * from "./lexer";
5
- export * from "./parser";
6
- export * from "./token";
7
- export { Visitor } from "./semantic/visitor";
8
- export { hasType } from "./helpers";
package/src/json.ts DELETED
@@ -1,350 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
- import type { Annotation, Type } from "./ast";
3
- import {
4
- StatusCodeAnnotation,
5
- ArrayType,
6
- AstRoot,
7
- DescriptionAnnotation,
8
- EnumType,
9
- EnumValue,
10
- ErrorNode,
11
- Field,
12
- FunctionOperation,
13
- HiddenAnnotation,
14
- OptionalType,
15
- RestAnnotation,
16
- StructType,
17
- ThrowsAnnotation,
18
- TypeDefinition,
19
- TypeReference,
20
- VoidPrimitiveType,
21
- } from "./ast";
22
- import { analyse } from "./semantic/analyser";
23
- import type { DeepReadonly } from "./utils";
24
- import { primitiveToAstClass } from "./utils";
25
-
26
- export type TypeDescription = string | Array<string | [string, string]> | Record<string, string>;
27
-
28
- type TypeTable = Record<string, TypeDescription | undefined>;
29
-
30
- type FunctionTable = Record<
31
- string,
32
- | {
33
- args: Record<string, string>;
34
- ret: TypeDescription;
35
- }
36
- | undefined
37
- >;
38
-
39
- type AnnotationJson =
40
- | {
41
- type: "description";
42
- value: string;
43
- }
44
- | {
45
- type: "throws";
46
- value: string;
47
- }
48
- | {
49
- type: "hidden";
50
- value: null;
51
- }
52
- | {
53
- type: "statusCode";
54
- value: number;
55
- }
56
- | {
57
- type: "rest";
58
- value: {
59
- bodyVariable: string | null;
60
- headers: ReadonlyArray<[string, string]>;
61
- method: string;
62
- path: string;
63
- pathVariables: readonly string[];
64
- queryVariables: readonly string[];
65
- };
66
- };
67
-
68
- function annotationToJson(ann: Annotation): AnnotationJson {
69
- if (ann instanceof DescriptionAnnotation) {
70
- return { type: "description", value: ann.text };
71
- } else if (ann instanceof ThrowsAnnotation) {
72
- return { type: "throws", value: ann.error };
73
- } else if (ann instanceof RestAnnotation) {
74
- return {
75
- type: "rest",
76
- value: {
77
- bodyVariable: ann.bodyVariable,
78
- headers: [...ann.headers.entries()].sort(([a], [b]) => a.localeCompare(b)),
79
- method: ann.method,
80
- path: ann.path,
81
- pathVariables: ann.pathVariables,
82
- queryVariables: [...ann.queryVariables].sort((a, b) => a.localeCompare(b)),
83
- },
84
- };
85
- } else if (ann instanceof HiddenAnnotation) {
86
- return { type: "hidden", value: null };
87
- } else if (ann instanceof StatusCodeAnnotation) {
88
- return { type: "statusCode", value: ann.statusCode };
89
- }
90
-
91
- throw new Error(`BUG: annotationToJson with ${ann.constructor.name}`);
92
- }
93
-
94
- function annotationFromJson(json: AnnotationJson | DeepReadonly<AnnotationJson>): Annotation {
95
- switch (json.type) {
96
- case "description":
97
- return new DescriptionAnnotation(json.value);
98
- case "throws":
99
- return new ThrowsAnnotation(json.value);
100
- case "rest": {
101
- const { method, path, pathVariables, queryVariables, headers, bodyVariable } = json.value;
102
-
103
- return new RestAnnotation(method, path, pathVariables, queryVariables, new Map(headers), bodyVariable);
104
- }
105
-
106
- case "hidden":
107
- return new HiddenAnnotation();
108
- case "statusCode":
109
- return new StatusCodeAnnotation(json.value);
110
-
111
- default:
112
- throw new Error(`BUG: annotationFromJson with ${(json as { type: string }).type}`);
113
- }
114
- }
115
-
116
- export interface AstJson {
117
- typeTable: TypeTable;
118
- functionTable: FunctionTable;
119
- errors: Array<string | [string, string]>;
120
- annotations: Record<string, AnnotationJson[] | undefined>;
121
- }
122
-
123
- export function astToJson(ast: AstRoot): AstJson {
124
- const annotations: Record<string, AnnotationJson[]> = {};
125
- const typeTable: TypeTable = {};
126
-
127
- function processType(type: Type): string {
128
- if (type instanceof ArrayType) {
129
- return `${processType(type.base)}[]`;
130
- } else if (type instanceof OptionalType) {
131
- return `${processType(type.base)}?`;
132
- }
133
-
134
- return type.name;
135
- }
136
-
137
- for (const { name, fields } of ast.structTypes) {
138
- if (name in typeTable) {
139
- throw new Error(`Duplicate struct type ${name}`);
140
- }
141
-
142
- typeTable[name] = {};
143
- const obj = typeTable[name] as Record<string, TypeDescription>;
144
-
145
- for (const field of fields) {
146
- obj[field.name] = processType(field.type);
147
-
148
- for (const ann of field.annotations) {
149
- if (ann instanceof DescriptionAnnotation) {
150
- const target = `type.${name}.${field.name}`;
151
-
152
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
153
- annotations[target] ??= [];
154
- annotations[target].push(annotationToJson(ann));
155
- }
156
- }
157
- }
158
- }
159
-
160
- for (const { name, values } of ast.enumTypes) {
161
- if (name in typeTable) {
162
- throw new Error(`Duplicate enum type ${name}`);
163
- }
164
-
165
- typeTable[name] = values.map(v => {
166
- if (!v.struct) {
167
- return v.value;
168
- }
169
-
170
- return [v.value, v.struct.name] as [string, string];
171
- });
172
- }
173
-
174
- for (const { name, type } of ast.typeDefinitions) {
175
- if (type instanceof StructType || type instanceof EnumType) {
176
- continue;
177
- }
178
-
179
- typeTable[name] = processType(type);
180
- }
181
-
182
- const functionTable: FunctionTable = {};
183
-
184
- for (const op of ast.operations) {
185
- const args: Record<string, string> = {};
186
-
187
- for (const arg of op.args) {
188
- args[arg.name] = processType(arg.type);
189
- for (const ann of arg.annotations) {
190
- if (ann instanceof DescriptionAnnotation) {
191
- const target = `fn.${op.name}.${arg.name}`;
192
-
193
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
194
- annotations[target] ??= [];
195
- annotations[target].push(annotationToJson(ann));
196
- }
197
- }
198
- }
199
-
200
- functionTable[op.name] = {
201
- args,
202
- ret: processType(op.returnType),
203
- };
204
-
205
- for (const ann of op.annotations) {
206
- const target = `fn.${op.name}`;
207
-
208
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
209
- annotations[target] ??= [];
210
- annotations[target].push(annotationToJson(ann));
211
- }
212
- }
213
-
214
- const errors = ast.errors.map(error =>
215
- error.dataType instanceof VoidPrimitiveType ? error.name : ([error.name, error.dataType.name] as [string, string]),
216
- );
217
-
218
- for (const error of ast.errors) {
219
- for (const ann of error.annotations) {
220
- const target = `error.${error.name}`;
221
-
222
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
223
- annotations[target] ??= [];
224
- annotations[target].push(annotationToJson(ann));
225
- }
226
- }
227
-
228
- return {
229
- annotations,
230
- errors,
231
- functionTable,
232
- typeTable,
233
- };
234
- }
235
-
236
- export function jsonToAst(json: DeepReadonly<AstJson>): AstRoot {
237
- const operations: FunctionOperation[] = [];
238
- const typeDefinition: TypeDefinition[] = [];
239
- const solveEnumValueStructRef: Array<[EnumValue, string]> = [];
240
-
241
- function processType(description: DeepReadonly<TypeDescription>, typeName?: string): Type {
242
- if (typeof description === "string") {
243
- const primitiveClass = primitiveToAstClass.get(description);
244
-
245
- if (primitiveClass) {
246
- return new primitiveClass();
247
- } else if (description.endsWith("?")) {
248
- return new OptionalType(processType(description.slice(0, description.length - 1)));
249
- } else if (description.endsWith("[]")) {
250
- return new ArrayType(processType(description.slice(0, description.length - 2)));
251
- }
252
-
253
- return new TypeReference(description);
254
- } else if (Array.isArray(description)) {
255
- return new EnumType(
256
- description.map(v => {
257
- if (Array.isArray(v)) {
258
- const [value, structName] = v as [string, string];
259
- const enumValue = new EnumValue(value);
260
-
261
- solveEnumValueStructRef.push([enumValue, structName]);
262
-
263
- return enumValue;
264
- }
265
-
266
- return new EnumValue(v as string);
267
- }),
268
- );
269
- }
270
-
271
- const fields: Field[] = [];
272
-
273
- for (const fieldName of Object.keys(description)) {
274
- const field = new Field(fieldName, processType((description as Record<string, string>)[fieldName]));
275
-
276
- if (typeName) {
277
- const target = `type.${typeName}.${fieldName}`;
278
-
279
- for (const annotationJson of json.annotations[target] ?? []) {
280
- field.annotations.push(annotationFromJson(annotationJson));
281
- }
282
- }
283
-
284
- fields.push(field);
285
- }
286
-
287
- return new StructType(fields);
288
- }
289
-
290
- for (const [typeName, description] of Object.entries(json.typeTable)) {
291
- const type = processType(description!, typeName);
292
-
293
- typeDefinition.push(new TypeDefinition(typeName, type));
294
- }
295
-
296
- for (const [functionName, func] of Object.entries(json.functionTable)) {
297
- const args = Object.keys(func!.args).map(argName => {
298
- const field = new Field(argName, processType(func!.args[argName]));
299
- const target = `fn.${functionName}.${argName}`;
300
-
301
- for (const annotationJson of json.annotations[target] ?? []) {
302
- field.annotations.push(annotationFromJson(annotationJson));
303
- }
304
-
305
- return field;
306
- });
307
-
308
- const op = new FunctionOperation(functionName, args, processType(func!.ret));
309
- const target = `fn.${functionName}`;
310
-
311
- for (const annotationJson of json.annotations[target] ?? []) {
312
- op.annotations.push(annotationFromJson(annotationJson));
313
- }
314
-
315
- operations.push(op);
316
- }
317
-
318
- for (const [enumValue, structName] of solveEnumValueStructRef) {
319
- const struct = typeDefinition.find(def => def.name === structName)?.type;
320
-
321
- if (struct instanceof StructType) {
322
- enumValue.struct = struct;
323
- }
324
- }
325
-
326
- const errors = json.errors.map(error => {
327
- let errorNode;
328
-
329
- if (Array.isArray(error)) {
330
- const [name, type] = error as [string, string];
331
-
332
- errorNode = new ErrorNode(name, processType(type));
333
- } else {
334
- errorNode = new ErrorNode(error as string, new VoidPrimitiveType());
335
- }
336
-
337
- const target = `error.${errorNode.name}`;
338
-
339
- for (const annotationJson of json.annotations[target] ?? []) {
340
- errorNode.annotations.push(annotationFromJson(annotationJson));
341
- }
342
-
343
- return errorNode;
344
- });
345
-
346
- const ast = new AstRoot(typeDefinition, operations, errors);
347
-
348
- analyse(ast);
349
- return ast;
350
- }
package/src/lexer.ts DELETED
@@ -1,343 +0,0 @@
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
- }