@sdkgen/parser 0.0.0-dev.20231008160426 → 0.0.0-dev.20231221180829

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 (77) hide show
  1. package/.eslintrc.json +3 -0
  2. package/.prettierrc +12 -0
  3. package/dist/spec/lexer.spec.d.ts +1 -0
  4. package/dist/spec/lexer.spec.js +152 -0
  5. package/dist/spec/parser.spec.d.ts +1 -0
  6. package/dist/spec/parser.spec.js +967 -0
  7. package/dist/src/ast.js +355 -0
  8. package/dist/src/compatibility/index.js +211 -0
  9. package/dist/src/helpers.js +23 -0
  10. package/dist/src/index.js +29 -0
  11. package/dist/src/json.js +234 -0
  12. package/dist/src/lexer.js +287 -0
  13. package/dist/src/parser.js +423 -0
  14. package/dist/src/restparser.js +65 -0
  15. package/dist/src/semantic/01_check_multiple_declaration.js +22 -0
  16. package/dist/src/semantic/02_give_struct_and_enum_names.js +61 -0
  17. package/dist/src/semantic/03_match_type_definitions.js +18 -0
  18. package/dist/src/semantic/04_check_dont_return_secret.js +42 -0
  19. package/dist/src/semantic/05_expand_spreads.js +60 -0
  20. package/dist/src/semantic/06_check_empty_struct_or_enum.js +21 -0
  21. package/dist/src/semantic/07_validate_recursive_types.js +49 -0
  22. package/dist/src/semantic/08_collect_struct_and_enum_types.js +25 -0
  23. package/dist/src/semantic/09_validate_annotations.js +125 -0
  24. package/dist/src/semantic/10_check_duplicate_members_on_enum.js +16 -0
  25. package/dist/src/semantic/analyser.js +30 -0
  26. package/dist/src/semantic/transformer.js +50 -0
  27. package/dist/src/semantic/visitor.js +65 -0
  28. package/dist/src/token.js +119 -0
  29. package/dist/src/utils.js +27 -0
  30. package/dist/tsconfig.tsbuildinfo +1 -0
  31. package/package.json +8 -24
  32. package/dist/ast.js +0 -298
  33. package/dist/compatibility/index.js +0 -206
  34. package/dist/helpers.js +0 -20
  35. package/dist/index.js +0 -9
  36. package/dist/json.js +0 -225
  37. package/dist/lexer.js +0 -283
  38. package/dist/parser.js +0 -419
  39. package/dist/restparser.js +0 -59
  40. package/dist/semantic/01_check_multiple_declaration.js +0 -19
  41. package/dist/semantic/02_give_struct_and_enum_names.js +0 -58
  42. package/dist/semantic/03_match_type_definitions.js +0 -15
  43. package/dist/semantic/04_check_dont_return_secret.js +0 -39
  44. package/dist/semantic/05_expand_spreads.js +0 -57
  45. package/dist/semantic/06_check_empty_struct_or_enum.js +0 -18
  46. package/dist/semantic/07_validate_recursive_types.js +0 -46
  47. package/dist/semantic/08_collect_struct_and_enum_types.js +0 -22
  48. package/dist/semantic/09_validate_annotations.js +0 -122
  49. package/dist/semantic/10_check_duplicate_members_on_enum.js +0 -13
  50. package/dist/semantic/analyser.js +0 -27
  51. package/dist/semantic/transformer.js +0 -46
  52. package/dist/semantic/visitor.js +0 -61
  53. package/dist/token.js +0 -93
  54. package/dist/utils.js +0 -25
  55. /package/dist/{ast.d.ts → src/ast.d.ts} +0 -0
  56. /package/dist/{compatibility → src/compatibility}/index.d.ts +0 -0
  57. /package/dist/{helpers.d.ts → src/helpers.d.ts} +0 -0
  58. /package/dist/{index.d.ts → src/index.d.ts} +0 -0
  59. /package/dist/{json.d.ts → src/json.d.ts} +0 -0
  60. /package/dist/{lexer.d.ts → src/lexer.d.ts} +0 -0
  61. /package/dist/{parser.d.ts → src/parser.d.ts} +0 -0
  62. /package/dist/{restparser.d.ts → src/restparser.d.ts} +0 -0
  63. /package/dist/{semantic → src/semantic}/01_check_multiple_declaration.d.ts +0 -0
  64. /package/dist/{semantic → src/semantic}/02_give_struct_and_enum_names.d.ts +0 -0
  65. /package/dist/{semantic → src/semantic}/03_match_type_definitions.d.ts +0 -0
  66. /package/dist/{semantic → src/semantic}/04_check_dont_return_secret.d.ts +0 -0
  67. /package/dist/{semantic → src/semantic}/05_expand_spreads.d.ts +0 -0
  68. /package/dist/{semantic → src/semantic}/06_check_empty_struct_or_enum.d.ts +0 -0
  69. /package/dist/{semantic → src/semantic}/07_validate_recursive_types.d.ts +0 -0
  70. /package/dist/{semantic → src/semantic}/08_collect_struct_and_enum_types.d.ts +0 -0
  71. /package/dist/{semantic → src/semantic}/09_validate_annotations.d.ts +0 -0
  72. /package/dist/{semantic → src/semantic}/10_check_duplicate_members_on_enum.d.ts +0 -0
  73. /package/dist/{semantic → src/semantic}/analyser.d.ts +0 -0
  74. /package/dist/{semantic → src/semantic}/transformer.d.ts +0 -0
  75. /package/dist/{semantic → src/semantic}/visitor.d.ts +0 -0
  76. /package/dist/{token.d.ts → src/token.d.ts} +0 -0
  77. /package/dist/{utils.d.ts → src/utils.d.ts} +0 -0
@@ -0,0 +1,234 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsonToAst = exports.astToJson = void 0;
4
+ const ast_1 = require("./ast");
5
+ const analyser_1 = require("./semantic/analyser");
6
+ const utils_1 = require("./utils");
7
+ function annotationToJson(ann) {
8
+ if (ann instanceof ast_1.DescriptionAnnotation) {
9
+ return { type: "description", value: ann.text };
10
+ }
11
+ else if (ann instanceof ast_1.ThrowsAnnotation) {
12
+ return { type: "throws", value: ann.error };
13
+ }
14
+ else if (ann instanceof ast_1.RestAnnotation) {
15
+ return {
16
+ type: "rest",
17
+ value: {
18
+ bodyVariable: ann.bodyVariable,
19
+ headers: [...ann.headers.entries()].sort(([a], [b]) => a.localeCompare(b)),
20
+ method: ann.method,
21
+ path: ann.path,
22
+ pathVariables: ann.pathVariables,
23
+ queryVariables: [...ann.queryVariables].sort((a, b) => a.localeCompare(b)),
24
+ },
25
+ };
26
+ }
27
+ else if (ann instanceof ast_1.HiddenAnnotation) {
28
+ return { type: "hidden", value: null };
29
+ }
30
+ else if (ann instanceof ast_1.StatusCodeAnnotation) {
31
+ return { type: "statusCode", value: ann.statusCode };
32
+ }
33
+ throw new Error(`BUG: annotationToJson with ${ann.constructor.name}`);
34
+ }
35
+ function annotationFromJson(json) {
36
+ switch (json.type) {
37
+ case "description":
38
+ return new ast_1.DescriptionAnnotation(json.value);
39
+ case "throws":
40
+ return new ast_1.ThrowsAnnotation(json.value);
41
+ case "rest": {
42
+ const { method, path, pathVariables, queryVariables, headers, bodyVariable } = json.value;
43
+ return new ast_1.RestAnnotation(method, path, pathVariables, queryVariables, new Map(headers), bodyVariable);
44
+ }
45
+ case "hidden":
46
+ return new ast_1.HiddenAnnotation();
47
+ case "statusCode":
48
+ return new ast_1.StatusCodeAnnotation(json.value);
49
+ default:
50
+ throw new Error(`BUG: annotationFromJson with ${json.type}`);
51
+ }
52
+ }
53
+ function astToJson(ast) {
54
+ var _a, _b, _c, _d;
55
+ const annotations = {};
56
+ const typeTable = {};
57
+ function processType(type) {
58
+ if (type instanceof ast_1.ArrayType) {
59
+ return `${processType(type.base)}[]`;
60
+ }
61
+ else if (type instanceof ast_1.OptionalType) {
62
+ return `${processType(type.base)}?`;
63
+ }
64
+ return type.name;
65
+ }
66
+ for (const { name, fields } of ast.structTypes) {
67
+ if (name in typeTable) {
68
+ throw new Error(`Duplicate struct type ${name}`);
69
+ }
70
+ typeTable[name] = {};
71
+ const obj = typeTable[name];
72
+ for (const field of fields) {
73
+ obj[field.name] = processType(field.type);
74
+ for (const ann of field.annotations) {
75
+ if (ann instanceof ast_1.DescriptionAnnotation) {
76
+ const target = `type.${name}.${field.name}`;
77
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
78
+ (_a = annotations[target]) !== null && _a !== void 0 ? _a : (annotations[target] = []);
79
+ annotations[target].push(annotationToJson(ann));
80
+ }
81
+ }
82
+ }
83
+ }
84
+ for (const { name, values } of ast.enumTypes) {
85
+ if (name in typeTable) {
86
+ throw new Error(`Duplicate enum type ${name}`);
87
+ }
88
+ typeTable[name] = values.map(v => {
89
+ if (!v.struct) {
90
+ return v.value;
91
+ }
92
+ return [v.value, v.struct.name];
93
+ });
94
+ }
95
+ for (const { name, type } of ast.typeDefinitions) {
96
+ if (type instanceof ast_1.StructType || type instanceof ast_1.EnumType) {
97
+ continue;
98
+ }
99
+ typeTable[name] = processType(type);
100
+ }
101
+ const functionTable = {};
102
+ for (const op of ast.operations) {
103
+ const args = {};
104
+ for (const arg of op.args) {
105
+ args[arg.name] = processType(arg.type);
106
+ for (const ann of arg.annotations) {
107
+ if (ann instanceof ast_1.DescriptionAnnotation) {
108
+ const target = `fn.${op.name}.${arg.name}`;
109
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
110
+ (_b = annotations[target]) !== null && _b !== void 0 ? _b : (annotations[target] = []);
111
+ annotations[target].push(annotationToJson(ann));
112
+ }
113
+ }
114
+ }
115
+ functionTable[op.name] = {
116
+ args,
117
+ ret: processType(op.returnType),
118
+ };
119
+ for (const ann of op.annotations) {
120
+ const target = `fn.${op.name}`;
121
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
122
+ (_c = annotations[target]) !== null && _c !== void 0 ? _c : (annotations[target] = []);
123
+ annotations[target].push(annotationToJson(ann));
124
+ }
125
+ }
126
+ const errors = ast.errors.map(error => error.dataType instanceof ast_1.VoidPrimitiveType ? error.name : [error.name, error.dataType.name]);
127
+ for (const error of ast.errors) {
128
+ for (const ann of error.annotations) {
129
+ const target = `error.${error.name}`;
130
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
131
+ (_d = annotations[target]) !== null && _d !== void 0 ? _d : (annotations[target] = []);
132
+ annotations[target].push(annotationToJson(ann));
133
+ }
134
+ }
135
+ return {
136
+ annotations,
137
+ errors,
138
+ functionTable,
139
+ typeTable,
140
+ };
141
+ }
142
+ exports.astToJson = astToJson;
143
+ function jsonToAst(json) {
144
+ var _a, _b;
145
+ const operations = [];
146
+ const typeDefinition = [];
147
+ const solveEnumValueStructRef = [];
148
+ function processType(description, typeName) {
149
+ var _a;
150
+ if (typeof description === "string") {
151
+ const primitiveClass = utils_1.primitiveToAstClass.get(description);
152
+ if (primitiveClass) {
153
+ return new primitiveClass();
154
+ }
155
+ else if (description.endsWith("?")) {
156
+ return new ast_1.OptionalType(processType(description.slice(0, description.length - 1)));
157
+ }
158
+ else if (description.endsWith("[]")) {
159
+ return new ast_1.ArrayType(processType(description.slice(0, description.length - 2)));
160
+ }
161
+ return new ast_1.TypeReference(description);
162
+ }
163
+ else if (Array.isArray(description)) {
164
+ return new ast_1.EnumType(description.map(v => {
165
+ if (Array.isArray(v)) {
166
+ const [value, structName] = v;
167
+ const enumValue = new ast_1.EnumValue(value);
168
+ solveEnumValueStructRef.push([enumValue, structName]);
169
+ return enumValue;
170
+ }
171
+ return new ast_1.EnumValue(v);
172
+ }));
173
+ }
174
+ const fields = [];
175
+ for (const fieldName of Object.keys(description)) {
176
+ const field = new ast_1.Field(fieldName, processType(description[fieldName]));
177
+ if (typeName) {
178
+ const target = `type.${typeName}.${fieldName}`;
179
+ for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
180
+ field.annotations.push(annotationFromJson(annotationJson));
181
+ }
182
+ }
183
+ fields.push(field);
184
+ }
185
+ return new ast_1.StructType(fields);
186
+ }
187
+ for (const [typeName, description] of Object.entries(json.typeTable)) {
188
+ const type = processType(description, typeName);
189
+ typeDefinition.push(new ast_1.TypeDefinition(typeName, type));
190
+ }
191
+ for (const [functionName, func] of Object.entries(json.functionTable)) {
192
+ const args = Object.keys(func.args).map(argName => {
193
+ var _a;
194
+ const field = new ast_1.Field(argName, processType(func.args[argName]));
195
+ const target = `fn.${functionName}.${argName}`;
196
+ for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
197
+ field.annotations.push(annotationFromJson(annotationJson));
198
+ }
199
+ return field;
200
+ });
201
+ const op = new ast_1.FunctionOperation(functionName, args, processType(func.ret));
202
+ const target = `fn.${functionName}`;
203
+ for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
204
+ op.annotations.push(annotationFromJson(annotationJson));
205
+ }
206
+ operations.push(op);
207
+ }
208
+ for (const [enumValue, structName] of solveEnumValueStructRef) {
209
+ const struct = (_b = typeDefinition.find(def => def.name === structName)) === null || _b === void 0 ? void 0 : _b.type;
210
+ if (struct instanceof ast_1.StructType) {
211
+ enumValue.struct = struct;
212
+ }
213
+ }
214
+ const errors = json.errors.map(error => {
215
+ var _a;
216
+ let errorNode;
217
+ if (Array.isArray(error)) {
218
+ const [name, type] = error;
219
+ errorNode = new ast_1.ErrorNode(name, processType(type));
220
+ }
221
+ else {
222
+ errorNode = new ast_1.ErrorNode(error, new ast_1.VoidPrimitiveType());
223
+ }
224
+ const target = `error.${errorNode.name}`;
225
+ for (const annotationJson of (_a = json.annotations[target]) !== null && _a !== void 0 ? _a : []) {
226
+ errorNode.annotations.push(annotationFromJson(annotationJson));
227
+ }
228
+ return errorNode;
229
+ });
230
+ const ast = new ast_1.AstRoot(typeDefinition, operations, errors);
231
+ (0, analyser_1.analyse)(ast);
232
+ return ast;
233
+ }
234
+ exports.jsonToAst = jsonToAst;
@@ -0,0 +1,287 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Lexer = exports.LexerError = void 0;
4
+ const token_1 = require("./token");
5
+ class LexerError extends Error {
6
+ }
7
+ exports.LexerError = LexerError;
8
+ class Lexer {
9
+ constructor(source, filename = "-") {
10
+ this.source = source;
11
+ this.filename = filename;
12
+ this.startPos = 0;
13
+ this.startLine = 1;
14
+ this.startColumn = 1;
15
+ this.pos = 0;
16
+ this.line = 1;
17
+ this.column = 1;
18
+ }
19
+ currentChar() {
20
+ return this.source[this.pos] || "\0";
21
+ }
22
+ nextChar() {
23
+ this.column++;
24
+ return this.source[++this.pos] || "\0";
25
+ }
26
+ nextToken() {
27
+ this.startPos = this.pos;
28
+ this.startLine = this.line;
29
+ this.startColumn = this.column;
30
+ let token = null;
31
+ switch (this.currentChar()) {
32
+ case "\0":
33
+ return null;
34
+ case " ":
35
+ case "\r":
36
+ case "\t":
37
+ this.nextChar();
38
+ return this.nextToken();
39
+ case "\n":
40
+ this.nextChar();
41
+ this.column = 1;
42
+ this.line++;
43
+ return this.nextToken();
44
+ case "/":
45
+ switch (this.nextChar()) {
46
+ case "/":
47
+ for (;;) {
48
+ switch (this.nextChar()) {
49
+ case "\0":
50
+ return null;
51
+ case "\n":
52
+ this.nextChar();
53
+ this.column = 1;
54
+ this.line++;
55
+ return this.nextToken();
56
+ default:
57
+ break;
58
+ }
59
+ }
60
+ case "*":
61
+ // eslint-disable-next-line no-labels
62
+ outerWhile: for (;;) {
63
+ switch (this.nextChar()) {
64
+ case "\0":
65
+ // eslint-disable-next-line no-labels
66
+ break outerWhile;
67
+ case "\n":
68
+ this.column = 0;
69
+ this.line++;
70
+ break;
71
+ case "*":
72
+ while (this.nextChar() === "*") {
73
+ //
74
+ }
75
+ switch (this.currentChar()) {
76
+ case "\0":
77
+ // eslint-disable-next-line no-labels
78
+ break outerWhile;
79
+ case "\n":
80
+ this.column = 0;
81
+ this.line++;
82
+ break;
83
+ case "/":
84
+ this.nextChar();
85
+ return this.nextToken();
86
+ default:
87
+ break;
88
+ }
89
+ break;
90
+ default:
91
+ break;
92
+ }
93
+ }
94
+ break;
95
+ default:
96
+ break;
97
+ }
98
+ break;
99
+ case "{":
100
+ this.nextChar();
101
+ token = new token_1.CurlyOpenSymbolToken();
102
+ break;
103
+ case "}":
104
+ this.nextChar();
105
+ token = new token_1.CurlyCloseSymbolToken();
106
+ break;
107
+ case "(":
108
+ this.nextChar();
109
+ token = new token_1.ParensOpenSymbolToken();
110
+ break;
111
+ case ")":
112
+ this.nextChar();
113
+ token = new token_1.ParensCloseSymbolToken();
114
+ break;
115
+ case "?":
116
+ this.nextChar();
117
+ token = new token_1.OptionalSymbolToken();
118
+ break;
119
+ case ":":
120
+ this.nextChar();
121
+ token = new token_1.ColonSymbolToken();
122
+ break;
123
+ case "=":
124
+ this.nextChar();
125
+ token = new token_1.EqualSymbolToken();
126
+ break;
127
+ case "!":
128
+ this.nextChar();
129
+ token = new token_1.ExclamationMarkSymbolToken();
130
+ break;
131
+ case ",":
132
+ this.nextChar();
133
+ token = new token_1.CommaSymbolToken();
134
+ break;
135
+ case "[":
136
+ switch (this.nextChar()) {
137
+ case "]": {
138
+ this.nextChar();
139
+ token = new token_1.ArraySymbolToken();
140
+ break;
141
+ }
142
+ default:
143
+ break;
144
+ }
145
+ break;
146
+ case ".":
147
+ switch (this.nextChar()) {
148
+ case ".": {
149
+ switch (this.nextChar()) {
150
+ case ".": {
151
+ this.nextChar();
152
+ token = new token_1.SpreadSymbolToken();
153
+ break;
154
+ }
155
+ default:
156
+ break;
157
+ }
158
+ break;
159
+ }
160
+ default:
161
+ break;
162
+ }
163
+ break;
164
+ case "@": {
165
+ let body = "\\";
166
+ let pos = this.startPos + 1;
167
+ while (body.endsWith("\\")) {
168
+ body = body.slice(0, body.length - 1).trim();
169
+ while (!["\0", "\n"].includes(this.nextChar())) {
170
+ //
171
+ }
172
+ body = `${body} ${this.source.substring(pos, this.pos).trim()}`.trim();
173
+ pos = this.pos + 1;
174
+ }
175
+ token = new token_1.AnnotationToken(body.trim());
176
+ break;
177
+ }
178
+ case '"': {
179
+ const chars = [];
180
+ // eslint-disable-next-line no-labels
181
+ outerLoop: for (;;) {
182
+ switch (this.nextChar()) {
183
+ case "\0":
184
+ // eslint-disable-next-line no-labels
185
+ break outerLoop;
186
+ case "\\":
187
+ switch (this.nextChar()) {
188
+ case "\0":
189
+ // eslint-disable-next-line no-labels
190
+ break outerLoop;
191
+ case "n":
192
+ chars.push("\n");
193
+ break;
194
+ case "t":
195
+ chars.push("\t");
196
+ break;
197
+ default:
198
+ chars.push(this.currentChar());
199
+ break;
200
+ }
201
+ break;
202
+ case '"':
203
+ this.nextChar();
204
+ token = new token_1.StringLiteralToken(chars.join(""));
205
+ // eslint-disable-next-line no-labels
206
+ break outerLoop;
207
+ default:
208
+ chars.push(this.currentChar());
209
+ break;
210
+ }
211
+ }
212
+ break;
213
+ }
214
+ default: {
215
+ if (/[a-zA-Z_]/u.test(this.currentChar())) {
216
+ while (/[a-zA-Z0-9_]/u.test(this.nextChar())) {
217
+ //
218
+ }
219
+ const ident = this.source.substring(this.startPos, this.pos);
220
+ switch (ident) {
221
+ case "error":
222
+ token = new token_1.ErrorKeywordToken();
223
+ break;
224
+ case "enum":
225
+ token = new token_1.EnumKeywordToken();
226
+ break;
227
+ case "type":
228
+ token = new token_1.TypeKeywordToken();
229
+ break;
230
+ case "import":
231
+ token = new token_1.ImportKeywordToken();
232
+ break;
233
+ case "fn":
234
+ token = new token_1.FnKeywordToken();
235
+ break;
236
+ case "true":
237
+ token = new token_1.TrueKeywordToken();
238
+ break;
239
+ case "false":
240
+ token = new token_1.FalseKeywordToken();
241
+ break;
242
+ default:
243
+ token = Lexer.PRIMITIVES.has(ident) ? new token_1.PrimitiveTypeToken(ident) : new token_1.IdentifierToken(ident);
244
+ }
245
+ }
246
+ }
247
+ }
248
+ if (token !== null) {
249
+ token.location.filename = this.filename;
250
+ token.location.line = this.startLine;
251
+ token.location.column = this.startColumn;
252
+ return token;
253
+ }
254
+ if (this.currentChar() === "\0") {
255
+ throw new LexerError(`Unexpected end of file at ${this.filename}`);
256
+ }
257
+ else {
258
+ throw new LexerError(`Unexpected character ${JSON.stringify(this.currentChar())} at ${this.filename}:${this.line}:${this.column}`);
259
+ }
260
+ }
261
+ }
262
+ exports.Lexer = Lexer;
263
+ Lexer.PRIMITIVES = new Set([
264
+ "bool",
265
+ "int",
266
+ "uint",
267
+ "float",
268
+ "bigint",
269
+ "string",
270
+ "date",
271
+ "datetime",
272
+ "bytes",
273
+ "money",
274
+ "decimal",
275
+ "cpf",
276
+ "cnpj",
277
+ "email",
278
+ "html",
279
+ "url",
280
+ "uuid",
281
+ "hex",
282
+ "base64",
283
+ "xml",
284
+ "void",
285
+ "json",
286
+ ]);
287
+ Lexer.KEYWORDS = new Set([...Lexer.PRIMITIVES, "error", "enum", "type", "import", "get", "function", "fn", "true", "false"]);