@sdkgen/parser 0.0.0-dev.20231002135133 → 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.
- package/dist/ast.js +298 -0
- package/dist/compatibility/index.js +206 -0
- package/dist/helpers.js +20 -0
- package/dist/index.js +9 -0
- package/dist/json.js +225 -0
- package/dist/lexer.js +283 -0
- package/dist/parser.js +419 -0
- package/dist/restparser.js +59 -0
- package/dist/semantic/01_check_multiple_declaration.js +19 -0
- package/dist/semantic/02_give_struct_and_enum_names.js +58 -0
- package/dist/semantic/03_match_type_definitions.js +15 -0
- package/dist/semantic/04_check_dont_return_secret.js +39 -0
- package/dist/semantic/05_expand_spreads.js +57 -0
- package/dist/semantic/06_check_empty_struct_or_enum.js +18 -0
- package/dist/semantic/07_validate_recursive_types.js +46 -0
- package/dist/semantic/08_collect_struct_and_enum_types.js +22 -0
- package/dist/semantic/09_validate_annotations.js +122 -0
- package/dist/semantic/10_check_duplicate_members_on_enum.js +13 -0
- package/dist/semantic/analyser.js +27 -0
- package/dist/semantic/transformer.js +46 -0
- package/dist/semantic/visitor.js +61 -0
- package/dist/token.js +93 -0
- package/dist/utils.js +25 -0
- package/package.json +10 -3
- package/.eslintrc.json +0 -3
- package/.prettierrc +0 -12
- package/dist/spec/lexer.spec.d.ts +0 -1
- package/dist/spec/lexer.spec.js +0 -152
- package/dist/spec/parser.spec.d.ts +0 -1
- package/dist/spec/parser.spec.js +0 -967
- package/dist/src/ast.js +0 -355
- package/dist/src/compatibility/index.js +0 -211
- package/dist/src/helpers.js +0 -23
- package/dist/src/index.js +0 -29
- package/dist/src/json.js +0 -234
- package/dist/src/lexer.js +0 -287
- package/dist/src/parser.js +0 -423
- package/dist/src/restparser.js +0 -65
- package/dist/src/semantic/01_check_multiple_declaration.js +0 -22
- package/dist/src/semantic/02_give_struct_and_enum_names.js +0 -61
- package/dist/src/semantic/03_match_type_definitions.js +0 -18
- package/dist/src/semantic/04_check_dont_return_secret.js +0 -42
- package/dist/src/semantic/05_expand_spreads.js +0 -60
- package/dist/src/semantic/06_check_empty_struct_or_enum.js +0 -21
- package/dist/src/semantic/07_validate_recursive_types.js +0 -49
- package/dist/src/semantic/08_collect_struct_and_enum_types.js +0 -25
- package/dist/src/semantic/09_validate_annotations.js +0 -125
- package/dist/src/semantic/10_check_duplicate_members_on_enum.js +0 -16
- package/dist/src/semantic/analyser.js +0 -30
- package/dist/src/semantic/transformer.js +0 -50
- package/dist/src/semantic/visitor.js +0 -65
- package/dist/src/token.js +0 -119
- package/dist/src/utils.js +0 -27
- package/dist/tsconfig.tsbuildinfo +0 -1
- /package/dist/{src/ast.d.ts → ast.d.ts} +0 -0
- /package/dist/{src/compatibility → compatibility}/index.d.ts +0 -0
- /package/dist/{src/helpers.d.ts → helpers.d.ts} +0 -0
- /package/dist/{src/index.d.ts → index.d.ts} +0 -0
- /package/dist/{src/json.d.ts → json.d.ts} +0 -0
- /package/dist/{src/lexer.d.ts → lexer.d.ts} +0 -0
- /package/dist/{src/parser.d.ts → parser.d.ts} +0 -0
- /package/dist/{src/restparser.d.ts → restparser.d.ts} +0 -0
- /package/dist/{src/semantic → semantic}/01_check_multiple_declaration.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/02_give_struct_and_enum_names.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/03_match_type_definitions.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/04_check_dont_return_secret.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/05_expand_spreads.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/06_check_empty_struct_or_enum.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/07_validate_recursive_types.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/08_collect_struct_and_enum_types.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/09_validate_annotations.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/10_check_duplicate_members_on_enum.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/analyser.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/transformer.d.ts +0 -0
- /package/dist/{src/semantic → semantic}/visitor.d.ts +0 -0
- /package/dist/{src/token.d.ts → token.d.ts} +0 -0
- /package/dist/{src/utils.d.ts → utils.d.ts} +0 -0
package/dist/src/parser.js
DELETED
|
@@ -1,423 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/* eslint-disable no-sync */
|
|
3
|
-
/* eslint-disable no-loop-func */
|
|
4
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.Parser = exports.ParserError = void 0;
|
|
6
|
-
const ast_1 = require("./ast");
|
|
7
|
-
const lexer_1 = require("./lexer");
|
|
8
|
-
const restparser_1 = require("./restparser");
|
|
9
|
-
const analyser_1 = require("./semantic/analyser");
|
|
10
|
-
const token_1 = require("./token");
|
|
11
|
-
const utils_1 = require("./utils");
|
|
12
|
-
class ParserError extends Error {
|
|
13
|
-
}
|
|
14
|
-
exports.ParserError = ParserError;
|
|
15
|
-
class Parser {
|
|
16
|
-
constructor(source) {
|
|
17
|
-
this.token = null;
|
|
18
|
-
this.annotations = [];
|
|
19
|
-
this.warnings = [];
|
|
20
|
-
this.readFileSync = () => {
|
|
21
|
-
throw "Not implemented";
|
|
22
|
-
};
|
|
23
|
-
try {
|
|
24
|
-
// eslint-disable-next-line
|
|
25
|
-
this.readFileSync = require("fs").readFileSync;
|
|
26
|
-
}
|
|
27
|
-
catch (e) {
|
|
28
|
-
// do nothing
|
|
29
|
-
}
|
|
30
|
-
const sources = Array.isArray(source) ? [...source].reverse() : [source];
|
|
31
|
-
this.lexers = sources.map(x => (x instanceof lexer_1.Lexer ? x : new lexer_1.Lexer(this.readFileSync(x).toString(), x)));
|
|
32
|
-
this.nextToken();
|
|
33
|
-
}
|
|
34
|
-
nextToken() {
|
|
35
|
-
while (this.lexers.length > 0) {
|
|
36
|
-
this.token = this.lexers[this.lexers.length - 1].nextToken();
|
|
37
|
-
if (this.token) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
this.lexers.pop();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
multiExpect(matcher) {
|
|
44
|
-
if (!this.token) {
|
|
45
|
-
throw new ParserError(`Expected ${Object.keys(matcher)
|
|
46
|
-
.map(x => x.replace("Token", ""))
|
|
47
|
-
.join(" or ")}, but found end of file`);
|
|
48
|
-
}
|
|
49
|
-
const tokenName = this.token.constructor.name;
|
|
50
|
-
if (tokenName in matcher) {
|
|
51
|
-
return matcher[tokenName](this.token);
|
|
52
|
-
}
|
|
53
|
-
else if (matcher.IdentifierToken) {
|
|
54
|
-
const tokenAsIdent = this.token.maybeAsIdentifier();
|
|
55
|
-
if (tokenAsIdent instanceof token_1.IdentifierToken) {
|
|
56
|
-
return matcher.IdentifierToken(tokenAsIdent);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
throw new ParserError(`Expected ${Object.keys(matcher)
|
|
60
|
-
.map(x => x.replace("Token", ""))
|
|
61
|
-
.join(" or ")} at ${this.token.location}, but found ${this.token}`);
|
|
62
|
-
}
|
|
63
|
-
expect(type) {
|
|
64
|
-
if (this.token === null) {
|
|
65
|
-
throw new ParserError(`Expected ${type.name.replace("Token", "")}, but found end of file`);
|
|
66
|
-
}
|
|
67
|
-
else if (this.token instanceof type) {
|
|
68
|
-
return this.token;
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
if (type === token_1.IdentifierToken) {
|
|
72
|
-
const tokenAsIdent = this.token.maybeAsIdentifier();
|
|
73
|
-
if (tokenAsIdent instanceof token_1.IdentifierToken) {
|
|
74
|
-
return tokenAsIdent;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
throw new ParserError(`Expected ${type.name.replace("Token", "")} at ${this.token.location}, but found ${this.token}`);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
parse() {
|
|
81
|
-
const operations = [];
|
|
82
|
-
const typeDefinition = [];
|
|
83
|
-
const errors = [];
|
|
84
|
-
this.warnings = [];
|
|
85
|
-
while (this.token) {
|
|
86
|
-
this.acceptAnnotations();
|
|
87
|
-
this.multiExpect({
|
|
88
|
-
ErrorKeywordToken: () => {
|
|
89
|
-
errors.push(this.parseError());
|
|
90
|
-
},
|
|
91
|
-
FnKeywordToken: () => {
|
|
92
|
-
operations.push(this.parseOperation());
|
|
93
|
-
},
|
|
94
|
-
ImportKeywordToken: () => {
|
|
95
|
-
this.checkCannotHaveAnnotationsHere();
|
|
96
|
-
this.nextToken();
|
|
97
|
-
const pathToken = this.expect(token_1.StringLiteralToken);
|
|
98
|
-
const resolvedPath = pathToken.location.filename.replace(/[^/\\]+$/u, `${pathToken.value}.sdkgen`);
|
|
99
|
-
this.lexers.push(new lexer_1.Lexer(this.readFileSync(resolvedPath).toString(), resolvedPath));
|
|
100
|
-
this.nextToken();
|
|
101
|
-
},
|
|
102
|
-
TypeKeywordToken: () => {
|
|
103
|
-
typeDefinition.push(this.parseTypeDefinition());
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
const ast = new ast_1.AstRoot(typeDefinition, operations, errors);
|
|
108
|
-
ast.warnings = this.warnings;
|
|
109
|
-
(0, analyser_1.analyse)(ast);
|
|
110
|
-
return ast;
|
|
111
|
-
}
|
|
112
|
-
acceptAnnotations() {
|
|
113
|
-
while (this.token instanceof token_1.AnnotationToken) {
|
|
114
|
-
const words = this.token.value.split(" ");
|
|
115
|
-
const body = this.token.value.slice(words[0].length).trim();
|
|
116
|
-
switch (words[0]) {
|
|
117
|
-
case "description":
|
|
118
|
-
this.annotations.push(new ast_1.DescriptionAnnotation(body).at(this.token));
|
|
119
|
-
break;
|
|
120
|
-
case "arg":
|
|
121
|
-
this.annotations.push(new ast_1.ArgDescriptionAnnotation(words[1], this.token.value.slice(words[0].length + 1 + words[1].length).trim()).at(this.token));
|
|
122
|
-
break;
|
|
123
|
-
case "throws":
|
|
124
|
-
this.annotations.push(new ast_1.ThrowsAnnotation(body).at(this.token));
|
|
125
|
-
break;
|
|
126
|
-
case "rest":
|
|
127
|
-
try {
|
|
128
|
-
this.annotations.push((0, restparser_1.parseRestAnnotation)(body).at(this.token));
|
|
129
|
-
}
|
|
130
|
-
catch (error) {
|
|
131
|
-
throw new ParserError(`${error instanceof Error ? error.message : error} at ${this.token.location}`);
|
|
132
|
-
}
|
|
133
|
-
break;
|
|
134
|
-
case "hidden":
|
|
135
|
-
if (body !== "") {
|
|
136
|
-
throw new ParserError(`@hidden annotation doesn't take any argument`);
|
|
137
|
-
}
|
|
138
|
-
this.annotations.push(new ast_1.HiddenAnnotation().at(this.token));
|
|
139
|
-
break;
|
|
140
|
-
case "statusCode": {
|
|
141
|
-
const statusCode = parseInt(body, 10);
|
|
142
|
-
if (statusCode.toString() !== body.trim()) {
|
|
143
|
-
throw new ParserError(`@statusCode annotation takes an integer as argument`);
|
|
144
|
-
}
|
|
145
|
-
this.annotations.push(new ast_1.StatusCodeAnnotation(statusCode).at(this.token));
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
default:
|
|
149
|
-
throw new ParserError(`Unknown annotation '${words[0]}' at ${this.token.location}`);
|
|
150
|
-
}
|
|
151
|
-
this.nextToken();
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
checkCannotHaveAnnotationsHere() {
|
|
155
|
-
if (this.annotations.length > 0) {
|
|
156
|
-
throw new ParserError(`Cannot have annotations at ${this.annotations[0].location}`);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
parseTypeDefinition() {
|
|
160
|
-
const typeToken = this.expect(token_1.TypeKeywordToken);
|
|
161
|
-
this.nextToken();
|
|
162
|
-
const nameToken = this.expect(token_1.IdentifierToken);
|
|
163
|
-
const name = nameToken.value;
|
|
164
|
-
if (!/[A-Z]/u.test(name[0])) {
|
|
165
|
-
throw new ParserError(`The custom type name must start with an uppercase letter, but found ${JSON.stringify(name)} at ${nameToken.location}`);
|
|
166
|
-
}
|
|
167
|
-
this.nextToken();
|
|
168
|
-
const { annotations } = this;
|
|
169
|
-
this.annotations = [];
|
|
170
|
-
const type = this.parseType();
|
|
171
|
-
const definitions = new ast_1.TypeDefinition(name, type).at(typeToken);
|
|
172
|
-
definitions.annotations = annotations;
|
|
173
|
-
return definitions;
|
|
174
|
-
}
|
|
175
|
-
parseError() {
|
|
176
|
-
const errorToken = this.expect(token_1.ErrorKeywordToken);
|
|
177
|
-
this.nextToken();
|
|
178
|
-
const nameToken = this.expect(token_1.IdentifierToken);
|
|
179
|
-
const name = nameToken.value;
|
|
180
|
-
if (!/[A-Z]/u.test(name[0])) {
|
|
181
|
-
throw new ParserError(`Error name must start with an uppercase letter, but found ${JSON.stringify(name)} at ${nameToken.location}`);
|
|
182
|
-
}
|
|
183
|
-
this.nextToken();
|
|
184
|
-
let type = new ast_1.VoidPrimitiveType();
|
|
185
|
-
if (this.token instanceof token_1.CurlyOpenSymbolToken ||
|
|
186
|
-
this.token instanceof token_1.EnumKeywordToken ||
|
|
187
|
-
this.token instanceof token_1.IdentifierToken ||
|
|
188
|
-
this.token instanceof token_1.PrimitiveTypeToken) {
|
|
189
|
-
type = this.parseType();
|
|
190
|
-
}
|
|
191
|
-
const node = new ast_1.ErrorNode(name, type).at(errorToken);
|
|
192
|
-
node.annotations = this.annotations;
|
|
193
|
-
this.annotations = [];
|
|
194
|
-
return node;
|
|
195
|
-
}
|
|
196
|
-
parseOperation() {
|
|
197
|
-
let { annotations } = this;
|
|
198
|
-
this.annotations = [];
|
|
199
|
-
this.expect(token_1.FnKeywordToken);
|
|
200
|
-
this.nextToken();
|
|
201
|
-
const name = this.expect(token_1.IdentifierToken).value;
|
|
202
|
-
this.nextToken();
|
|
203
|
-
this.expect(token_1.ParensOpenSymbolToken);
|
|
204
|
-
this.nextToken();
|
|
205
|
-
const argNames = new Set();
|
|
206
|
-
const fieldsAndSpreads = [];
|
|
207
|
-
let finished = false;
|
|
208
|
-
let parensCloseToken;
|
|
209
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
210
|
-
while (!finished) {
|
|
211
|
-
this.acceptAnnotations();
|
|
212
|
-
this.multiExpect({
|
|
213
|
-
ParensCloseSymbolToken: token => {
|
|
214
|
-
this.checkCannotHaveAnnotationsHere();
|
|
215
|
-
this.nextToken();
|
|
216
|
-
finished = true;
|
|
217
|
-
parensCloseToken = token;
|
|
218
|
-
},
|
|
219
|
-
IdentifierToken: () => {
|
|
220
|
-
const field = this.parseField();
|
|
221
|
-
if (argNames.has(field.name)) {
|
|
222
|
-
throw new ParserError(`Cannot redeclare argument '${field.name}'`);
|
|
223
|
-
}
|
|
224
|
-
argNames.add(field.name);
|
|
225
|
-
fieldsAndSpreads.push(field);
|
|
226
|
-
this.multiExpect({
|
|
227
|
-
ParensCloseSymbolToken: token => {
|
|
228
|
-
this.checkCannotHaveAnnotationsHere();
|
|
229
|
-
this.nextToken();
|
|
230
|
-
finished = true;
|
|
231
|
-
parensCloseToken = token;
|
|
232
|
-
},
|
|
233
|
-
CommaSymbolToken: () => this.nextToken(),
|
|
234
|
-
});
|
|
235
|
-
},
|
|
236
|
-
SpreadSymbolToken: () => {
|
|
237
|
-
this.checkCannotHaveAnnotationsHere();
|
|
238
|
-
this.nextToken();
|
|
239
|
-
const identToken = this.expect(token_1.IdentifierToken);
|
|
240
|
-
this.nextToken();
|
|
241
|
-
if (!/[A-Z]/u.test(identToken.value[0])) {
|
|
242
|
-
throw new ParserError(`Expected a type but found ${JSON.stringify(identToken.value)} at ${identToken.location}`);
|
|
243
|
-
}
|
|
244
|
-
fieldsAndSpreads.push(new ast_1.Spread(new ast_1.TypeReference(identToken.value).at(identToken)).at(identToken));
|
|
245
|
-
this.multiExpect({
|
|
246
|
-
ParensCloseSymbolToken: token => {
|
|
247
|
-
this.checkCannotHaveAnnotationsHere();
|
|
248
|
-
this.nextToken();
|
|
249
|
-
finished = true;
|
|
250
|
-
parensCloseToken = token;
|
|
251
|
-
},
|
|
252
|
-
CommaSymbolToken: () => this.nextToken(),
|
|
253
|
-
});
|
|
254
|
-
},
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
for (const annotation of annotations) {
|
|
258
|
-
if (annotation instanceof ast_1.ArgDescriptionAnnotation) {
|
|
259
|
-
const arg = fieldsAndSpreads.find(x => x instanceof ast_1.Field && x.name === annotation.argName);
|
|
260
|
-
if (!arg) {
|
|
261
|
-
throw new ParserError(`Argument '${annotation.argName}' not found, at ${annotation.location}`);
|
|
262
|
-
}
|
|
263
|
-
arg.annotations.push(new ast_1.DescriptionAnnotation(annotation.text).atLocation(annotation.location));
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
annotations = annotations.filter(ann => !(ann instanceof ast_1.ArgDescriptionAnnotation));
|
|
267
|
-
let returnType = new ast_1.VoidPrimitiveType().at(parensCloseToken);
|
|
268
|
-
if (this.token instanceof token_1.ColonSymbolToken) {
|
|
269
|
-
this.nextToken();
|
|
270
|
-
returnType = this.parseType();
|
|
271
|
-
}
|
|
272
|
-
const op = new ast_1.FunctionOperation(name, fieldsAndSpreads, returnType);
|
|
273
|
-
op.annotations = annotations;
|
|
274
|
-
return op;
|
|
275
|
-
}
|
|
276
|
-
parseEnum() {
|
|
277
|
-
this.checkCannotHaveAnnotationsHere();
|
|
278
|
-
const enumToken = this.expect(token_1.EnumKeywordToken);
|
|
279
|
-
this.nextToken();
|
|
280
|
-
this.expect(token_1.CurlyOpenSymbolToken);
|
|
281
|
-
this.nextToken();
|
|
282
|
-
const enumType = new ast_1.EnumType([]).at(enumToken);
|
|
283
|
-
let finished = false;
|
|
284
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
285
|
-
while (!finished) {
|
|
286
|
-
this.acceptAnnotations();
|
|
287
|
-
this.multiExpect({
|
|
288
|
-
CurlyCloseSymbolToken: () => {
|
|
289
|
-
this.checkCannotHaveAnnotationsHere();
|
|
290
|
-
this.nextToken();
|
|
291
|
-
finished = true;
|
|
292
|
-
},
|
|
293
|
-
IdentifierToken: token => {
|
|
294
|
-
const enumValue = new ast_1.EnumValue(token.value).at(token);
|
|
295
|
-
enumValue.annotations = this.annotations;
|
|
296
|
-
this.annotations = [];
|
|
297
|
-
enumType.values.push(enumValue);
|
|
298
|
-
this.nextToken();
|
|
299
|
-
if (!(this.token instanceof token_1.ParensOpenSymbolToken)) {
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
this.nextToken();
|
|
303
|
-
const fieldNames = new Set();
|
|
304
|
-
const fields = [];
|
|
305
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
306
|
-
while (this.token && this.token.maybeAsIdentifier() instanceof token_1.IdentifierToken) {
|
|
307
|
-
const field = this.parseField();
|
|
308
|
-
// "tag" is a reserved name on tagged enum fields and can't be used.
|
|
309
|
-
if (field.name === "tag") {
|
|
310
|
-
field.name = "_tag";
|
|
311
|
-
}
|
|
312
|
-
if (fieldNames.has(field.name)) {
|
|
313
|
-
throw new ParserError(`Cannot redeclare argument '${field.name}'`);
|
|
314
|
-
}
|
|
315
|
-
fieldNames.add(field.name);
|
|
316
|
-
fields.push(field);
|
|
317
|
-
if (this.token instanceof token_1.CommaSymbolToken) {
|
|
318
|
-
this.nextToken();
|
|
319
|
-
}
|
|
320
|
-
else {
|
|
321
|
-
break;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
enumValue.struct = new ast_1.StructType(fields).atLocation(enumValue.location);
|
|
325
|
-
this.expect(token_1.ParensCloseSymbolToken);
|
|
326
|
-
this.nextToken();
|
|
327
|
-
},
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
return enumType;
|
|
331
|
-
}
|
|
332
|
-
parseField() {
|
|
333
|
-
const nameToken = this.expect(token_1.IdentifierToken);
|
|
334
|
-
this.nextToken();
|
|
335
|
-
this.expect(token_1.ColonSymbolToken);
|
|
336
|
-
this.nextToken();
|
|
337
|
-
const { annotations } = this;
|
|
338
|
-
this.annotations = [];
|
|
339
|
-
const type = this.parseType();
|
|
340
|
-
const field = new ast_1.Field(nameToken.value, type).at(nameToken);
|
|
341
|
-
field.annotations = annotations;
|
|
342
|
-
while (this.token instanceof token_1.ExclamationMarkSymbolToken) {
|
|
343
|
-
this.nextToken();
|
|
344
|
-
switch (this.expect(token_1.IdentifierToken).value) {
|
|
345
|
-
case "secret":
|
|
346
|
-
field.secret = true;
|
|
347
|
-
break;
|
|
348
|
-
default:
|
|
349
|
-
throw new ParserError(`Unknown field mark !${this.token.value} at ${this.token.location}`);
|
|
350
|
-
}
|
|
351
|
-
this.nextToken();
|
|
352
|
-
}
|
|
353
|
-
return field;
|
|
354
|
-
}
|
|
355
|
-
parseStruct() {
|
|
356
|
-
const openingToken = this.expect(token_1.CurlyOpenSymbolToken);
|
|
357
|
-
this.nextToken();
|
|
358
|
-
const fieldsAndSpreads = [];
|
|
359
|
-
const fieldNames = new Set();
|
|
360
|
-
let finished = false;
|
|
361
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
362
|
-
while (!finished) {
|
|
363
|
-
this.acceptAnnotations();
|
|
364
|
-
this.multiExpect({
|
|
365
|
-
CurlyCloseSymbolToken: () => {
|
|
366
|
-
this.checkCannotHaveAnnotationsHere();
|
|
367
|
-
this.nextToken();
|
|
368
|
-
finished = true;
|
|
369
|
-
},
|
|
370
|
-
IdentifierToken: () => {
|
|
371
|
-
const field = this.parseField();
|
|
372
|
-
if (fieldNames.has(field.name)) {
|
|
373
|
-
throw new ParserError(`Cannot redeclare field '${field.name}'`);
|
|
374
|
-
}
|
|
375
|
-
fieldNames.add(field.name);
|
|
376
|
-
fieldsAndSpreads.push(field);
|
|
377
|
-
},
|
|
378
|
-
SpreadSymbolToken: () => {
|
|
379
|
-
this.checkCannotHaveAnnotationsHere();
|
|
380
|
-
this.nextToken();
|
|
381
|
-
const identToken = this.expect(token_1.IdentifierToken);
|
|
382
|
-
this.nextToken();
|
|
383
|
-
if (!/[A-Z]/u.test(identToken.value[0])) {
|
|
384
|
-
throw new ParserError(`Expected a type but found ${JSON.stringify(identToken.value)} at ${identToken.location}`);
|
|
385
|
-
}
|
|
386
|
-
fieldsAndSpreads.push(new ast_1.Spread(new ast_1.TypeReference(identToken.value).at(identToken)).at(identToken));
|
|
387
|
-
},
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
return new ast_1.StructType(fieldsAndSpreads).at(openingToken);
|
|
391
|
-
}
|
|
392
|
-
parseType() {
|
|
393
|
-
this.checkCannotHaveAnnotationsHere();
|
|
394
|
-
let result = this.multiExpect({
|
|
395
|
-
CurlyOpenSymbolToken: () => this.parseStruct(),
|
|
396
|
-
EnumKeywordToken: () => this.parseEnum(),
|
|
397
|
-
IdentifierToken: token => {
|
|
398
|
-
this.nextToken();
|
|
399
|
-
if (!/[A-Z]/u.test(token.value[0])) {
|
|
400
|
-
throw new ParserError(`Expected a type but found ${JSON.stringify(token.value)} at ${token.location}`);
|
|
401
|
-
}
|
|
402
|
-
return new ast_1.TypeReference(token.value).at(token);
|
|
403
|
-
},
|
|
404
|
-
PrimitiveTypeToken: token => {
|
|
405
|
-
this.nextToken();
|
|
406
|
-
const primitiveClass = utils_1.primitiveToAstClass.get(token.value);
|
|
407
|
-
if (primitiveClass) {
|
|
408
|
-
return new primitiveClass().at(token);
|
|
409
|
-
}
|
|
410
|
-
throw new ParserError(`BUG! Should handle primitive ${token.value}`);
|
|
411
|
-
},
|
|
412
|
-
});
|
|
413
|
-
while (this.token instanceof token_1.ArraySymbolToken || this.token instanceof token_1.OptionalSymbolToken) {
|
|
414
|
-
this.multiExpect({
|
|
415
|
-
ArraySymbolToken: token => (result = new ast_1.ArrayType(result).at(token)),
|
|
416
|
-
OptionalSymbolToken: token => (result = new ast_1.OptionalType(result).at(token)),
|
|
417
|
-
});
|
|
418
|
-
this.nextToken();
|
|
419
|
-
}
|
|
420
|
-
return result;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
exports.Parser = Parser;
|
package/dist/src/restparser.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseRestAnnotation = void 0;
|
|
4
|
-
const ast_1 = require("./ast");
|
|
5
|
-
function scanHeaders(text) {
|
|
6
|
-
var _a;
|
|
7
|
-
// Header name: https://tools.ietf.org/html/rfc2616#section-4.2
|
|
8
|
-
const headerRegex = /\[header (?<header>[^()<>@,;:\\"/[\]?={}\s\t]+): \{(?<name>\w+)\}\]/gu;
|
|
9
|
-
const headers = new Map();
|
|
10
|
-
let match;
|
|
11
|
-
while ((match = headerRegex.exec(text)) !== null) {
|
|
12
|
-
if (((_a = match.groups) === null || _a === void 0 ? void 0 : _a.header) && match.groups.name) {
|
|
13
|
-
headers.set(match.groups.header.toLowerCase(), match.groups.name);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return headers;
|
|
17
|
-
}
|
|
18
|
-
function scanBody(text) {
|
|
19
|
-
var _a;
|
|
20
|
-
const match = /\[body \{(?<name>\w+)\}\]/u.exec(text);
|
|
21
|
-
if ((_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.name) {
|
|
22
|
-
return match.groups.name;
|
|
23
|
-
}
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
function scanVariables(text) {
|
|
27
|
-
var _a;
|
|
28
|
-
const variableRegex = /\{(?<name>\w+)\}/gu;
|
|
29
|
-
const variables = [];
|
|
30
|
-
let match;
|
|
31
|
-
while ((match = variableRegex.exec(text)) !== null) {
|
|
32
|
-
if ((_a = match.groups) === null || _a === void 0 ? void 0 : _a.name) {
|
|
33
|
-
variables.push(match.groups.name);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return variables;
|
|
37
|
-
}
|
|
38
|
-
function parseRestAnnotation(text) {
|
|
39
|
-
const fragments = text.split(" ");
|
|
40
|
-
const method = fragments[0].toUpperCase();
|
|
41
|
-
if (!["GET", "POST", "PUT", "DELETE", "PATCH"].includes(method)) {
|
|
42
|
-
throw new Error(`Unsupported method '${method}'`);
|
|
43
|
-
}
|
|
44
|
-
// eslint-disable-next-line prefer-destructuring
|
|
45
|
-
let path = fragments[1];
|
|
46
|
-
if (!path.startsWith("/")) {
|
|
47
|
-
throw new Error(`Invalid path`);
|
|
48
|
-
}
|
|
49
|
-
let queryVariables = [];
|
|
50
|
-
if (path.includes("?")) {
|
|
51
|
-
const [base, ...queryArray] = path.split("?");
|
|
52
|
-
path = base;
|
|
53
|
-
const query = queryArray.join("?");
|
|
54
|
-
if (!/^\{\w+\}(?:&\{\w+\})*$/u.test(query)) {
|
|
55
|
-
throw new Error(`Invalid querystring on path`);
|
|
56
|
-
}
|
|
57
|
-
queryVariables = scanVariables(query);
|
|
58
|
-
}
|
|
59
|
-
const pathVariables = scanVariables(path);
|
|
60
|
-
const remaining = fragments.slice(2).join(" ");
|
|
61
|
-
const headers = scanHeaders(remaining);
|
|
62
|
-
const bodyVariable = scanBody(remaining);
|
|
63
|
-
return new ast_1.RestAnnotation(method, path, pathVariables, queryVariables, headers, bodyVariable);
|
|
64
|
-
}
|
|
65
|
-
exports.parseRestAnnotation = parseRestAnnotation;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CheckMultipleDeclarationVisitor = void 0;
|
|
4
|
-
const visitor_1 = require("./visitor");
|
|
5
|
-
const ast_1 = require("../ast");
|
|
6
|
-
class CheckMultipleDeclarationVisitor extends visitor_1.Visitor {
|
|
7
|
-
constructor() {
|
|
8
|
-
super(...arguments);
|
|
9
|
-
this.nameToType = new Map();
|
|
10
|
-
}
|
|
11
|
-
visit(node) {
|
|
12
|
-
if (node instanceof ast_1.TypeDefinition) {
|
|
13
|
-
const previousType = this.nameToType.get(node.name);
|
|
14
|
-
if (previousType && JSON.stringify(previousType) !== JSON.stringify(node.type)) {
|
|
15
|
-
throw new visitor_1.SemanticError(`Type '${node.name}' at ${node.location} is defined multiple times (also at ${previousType.location})`);
|
|
16
|
-
}
|
|
17
|
-
this.nameToType.set(node.name, node.type);
|
|
18
|
-
}
|
|
19
|
-
super.visit(node);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
exports.CheckMultipleDeclarationVisitor = CheckMultipleDeclarationVisitor;
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GiveStructAndEnumNamesTransformer = void 0;
|
|
4
|
-
const transformer_1 = require("./transformer");
|
|
5
|
-
const visitor_1 = require("./visitor");
|
|
6
|
-
const ast_1 = require("../ast");
|
|
7
|
-
class GiveStructAndEnumNamesTransformer extends transformer_1.Transformer {
|
|
8
|
-
constructor() {
|
|
9
|
-
super(...arguments);
|
|
10
|
-
this.path = [];
|
|
11
|
-
this.names = new Map();
|
|
12
|
-
}
|
|
13
|
-
transform(node) {
|
|
14
|
-
if (node instanceof ast_1.TypeDefinition) {
|
|
15
|
-
this.path = [node.name];
|
|
16
|
-
return super.transform(node);
|
|
17
|
-
}
|
|
18
|
-
else if (node instanceof ast_1.ErrorNode) {
|
|
19
|
-
this.path = [`${node.name}Data`];
|
|
20
|
-
return super.transform(node);
|
|
21
|
-
}
|
|
22
|
-
else if (node instanceof ast_1.FunctionOperation) {
|
|
23
|
-
this.path = [node.name];
|
|
24
|
-
return super.transform(node);
|
|
25
|
-
}
|
|
26
|
-
else if (node instanceof ast_1.Field) {
|
|
27
|
-
try {
|
|
28
|
-
this.path.push(node.name);
|
|
29
|
-
return super.transform(node);
|
|
30
|
-
}
|
|
31
|
-
finally {
|
|
32
|
-
this.path.pop();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
else if (node instanceof ast_1.EnumValue) {
|
|
36
|
-
try {
|
|
37
|
-
this.path.push(node.value);
|
|
38
|
-
return super.transform(node);
|
|
39
|
-
}
|
|
40
|
-
finally {
|
|
41
|
-
this.path.pop();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
else if (node instanceof ast_1.StructType || node instanceof ast_1.EnumType) {
|
|
45
|
-
node.name = this.path.map(s => s[0].toUpperCase() + s.slice(1)).join("");
|
|
46
|
-
const previous = this.names.get(node.name);
|
|
47
|
-
if (previous) {
|
|
48
|
-
if (previous.type.constructor !== node.constructor || JSON.stringify(previous.type) !== JSON.stringify(node)) {
|
|
49
|
-
throw new visitor_1.SemanticError(`The name of the type '${this.path.join(".")}' at ${node.location} will conflict with '${previous.path.join(".")}' at ${previous.type.location}`);
|
|
50
|
-
}
|
|
51
|
-
return previous.type;
|
|
52
|
-
}
|
|
53
|
-
this.names.set(node.name, { path: [...this.path], type: node });
|
|
54
|
-
return super.transform(node);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
return super.transform(node);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
exports.GiveStructAndEnumNamesTransformer = GiveStructAndEnumNamesTransformer;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MatchTypeDefinitionsVisitor = void 0;
|
|
4
|
-
const visitor_1 = require("./visitor");
|
|
5
|
-
const ast_1 = require("../ast");
|
|
6
|
-
class MatchTypeDefinitionsVisitor extends visitor_1.Visitor {
|
|
7
|
-
visit(node) {
|
|
8
|
-
if (node instanceof ast_1.TypeReference) {
|
|
9
|
-
const definition = this.root.typeDefinitions.find(t => t.name === node.name);
|
|
10
|
-
if (definition === undefined) {
|
|
11
|
-
throw new visitor_1.SemanticError(`Could not find type '${node.name}' at ${node.location}`);
|
|
12
|
-
}
|
|
13
|
-
node.type = definition.type;
|
|
14
|
-
}
|
|
15
|
-
super.visit(node);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.MatchTypeDefinitionsVisitor = MatchTypeDefinitionsVisitor;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CheckDontReturnSecretVisitor = void 0;
|
|
4
|
-
const visitor_1 = require("./visitor");
|
|
5
|
-
const ast_1 = require("../ast");
|
|
6
|
-
class CheckDontReturnSecretVisitor extends visitor_1.Visitor {
|
|
7
|
-
constructor() {
|
|
8
|
-
super(...arguments);
|
|
9
|
-
this.isInReturn = false;
|
|
10
|
-
this.path = [];
|
|
11
|
-
this.visited = new Set();
|
|
12
|
-
}
|
|
13
|
-
visit(node) {
|
|
14
|
-
if (this.visited.has(node)) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
this.visited.add(node);
|
|
18
|
-
if (node instanceof ast_1.FunctionOperation) {
|
|
19
|
-
this.isInReturn = true;
|
|
20
|
-
this.visited.clear();
|
|
21
|
-
this.path.push(`${node.name}(...)`);
|
|
22
|
-
this.visit(node.returnType);
|
|
23
|
-
this.path.pop();
|
|
24
|
-
this.isInReturn = false;
|
|
25
|
-
}
|
|
26
|
-
else if (node instanceof ast_1.TypeReference) {
|
|
27
|
-
this.visit(node.type);
|
|
28
|
-
}
|
|
29
|
-
else if (node instanceof ast_1.Field) {
|
|
30
|
-
this.path.push(node.name);
|
|
31
|
-
if (this.isInReturn && node.secret) {
|
|
32
|
-
throw new visitor_1.SemanticError(`Can't return a secret value at ${this.path.join(".")} at ${node.location}`);
|
|
33
|
-
}
|
|
34
|
-
super.visit(node);
|
|
35
|
-
this.path.pop();
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
super.visit(node);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
exports.CheckDontReturnSecretVisitor = CheckDontReturnSecretVisitor;
|