@zhhoceanfly/protoview 0.0.0-stage → 0.1.4

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"meta.js","sourceRoot":"","sources":["../src/meta.ts"],"names":[],"mappings":";;AAYA,0CAQC;AAED,wCAiDC;AA3DD,SAAgB,eAAe,CAAC,MAAgB;IAC9C,MAAM,EAAE,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,KAAK,KAAK,IAAI,GAAG,CAAC,KAAK,KAAK,GAAG,CAAC,CAAC;IAC9E,IAAI,EAAE,GAAG,CAAC;QAAE,OAAO,SAAS,CAAC;IAC7B,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAClE,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;QAAE,OAAO,SAAS,CAAC;IAC7C,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;IAC1C,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IACjG,OAAO,MAAM,CAAC,KAAK,CAAC;AACtB,CAAC;AAED,SAAgB,cAAc,CAAC,IAAkC;IAC/D,MAAM,MAAM,GAAgB,EAAE,CAAC;IAC/B,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACpC,IAAI,OAAO,IAAI,CAAC,OAAO,KAAK,QAAQ;QAAE,MAAM,CAAC,IAAI,CAAC,EAAE,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E,IAAI,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC;QACtB,KAAK,EAAE,CAAC;QACR,OAAO,MAAM,CAAC,KAAK,CAAC,EAAE,IAAI,KAAK,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,KAAK,GAAG,IAAI,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,IAAI,KAAK,IAAI,EAAE,CAAC;YACxG,KAAK,IAAI,IAAI,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;YACvC,KAAK,IAAI,CAAC,CAAC;QACb,CAAC;QACD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;IAC1B,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,KAAK,CAAC,KAA4B;IACzC,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IAClF,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,OAAO,CAAC,KAAgB;IAC/B,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxH,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AAChD,CAAC"}
@@ -0,0 +1,638 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProtoParseError = void 0;
4
+ exports.isScalar = isScalar;
5
+ exports.parseProto = parseProto;
6
+ exports.flattenMessages = flattenMessages;
7
+ exports.flattenEnums = flattenEnums;
8
+ const meta_1 = require("./meta");
9
+ class ProtoParseError extends Error {
10
+ line;
11
+ constructor(message, line) {
12
+ super(`${message}(第 ${line} 行)`);
13
+ this.line = line;
14
+ this.name = "ProtoParseError";
15
+ }
16
+ }
17
+ exports.ProtoParseError = ProtoParseError;
18
+ const SCALARS = new Set([
19
+ "double", "float", "int32", "int64", "uint32", "uint64", "sint32", "sint64",
20
+ "fixed32", "fixed64", "sfixed32", "sfixed64", "bool", "string", "bytes",
21
+ ]);
22
+ function isScalar(typeName) {
23
+ return SCALARS.has(typeName.replace(/^\./, ""));
24
+ }
25
+ function parseProto(text, filePath, relPath = filePath) {
26
+ const tokens = tokenize(text);
27
+ const parser = new Parser(tokens, filePath, text);
28
+ return parser.parseFile(relPath);
29
+ }
30
+ function tokenize(text) {
31
+ const tokens = [];
32
+ let i = 0;
33
+ let line = 1;
34
+ const state = { lineDirty: false, tokenOnLine: false, last: null };
35
+ const lead = [];
36
+ let leadFrom = -1;
37
+ const push = (kind, value, tokLine, from, to) => {
38
+ const tok = {
39
+ kind,
40
+ value,
41
+ line: tokLine,
42
+ lead: lead.join("\n").trim(),
43
+ trail: "",
44
+ from,
45
+ to,
46
+ leadFrom,
47
+ };
48
+ lead.length = 0;
49
+ leadFrom = -1;
50
+ tokens.push(tok);
51
+ state.last = tok;
52
+ state.lineDirty = true;
53
+ state.tokenOnLine = true;
54
+ };
55
+ while (i < text.length) {
56
+ const c = text[i];
57
+ if (c === "\n") {
58
+ if (!state.lineDirty) {
59
+ lead.length = 0;
60
+ leadFrom = -1;
61
+ }
62
+ line++;
63
+ state.lineDirty = false;
64
+ state.tokenOnLine = false;
65
+ i++;
66
+ continue;
67
+ }
68
+ if (c === " " || c === "\t" || c === "\r") {
69
+ i++;
70
+ continue;
71
+ }
72
+ if (c === "/" && text[i + 1] === "/") {
73
+ const commentFrom = i;
74
+ const start = i + 2;
75
+ i += 2;
76
+ while (i < text.length && text[i] !== "\n")
77
+ i++;
78
+ const raw = text.slice(start, i).replace(/^\s+/, "").replace(/\s+$/, "");
79
+ state.lineDirty = true;
80
+ if (state.tokenOnLine && state.last) {
81
+ state.last.trail = state.last.trail ? `${state.last.trail} ${raw}` : raw;
82
+ }
83
+ else if (raw) {
84
+ if (leadFrom < 0)
85
+ leadFrom = commentFrom;
86
+ lead.push(raw);
87
+ }
88
+ continue;
89
+ }
90
+ if (c === "/" && text[i + 1] === "*") {
91
+ const commentFrom = i;
92
+ const startLine = line;
93
+ i += 2;
94
+ const start = i;
95
+ while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) {
96
+ if (text[i] === "\n")
97
+ line++;
98
+ i++;
99
+ }
100
+ const raw = text.slice(start, i)
101
+ .split("\n")
102
+ .map((part) => part.replace(/^\s*\* ?/, "").trim())
103
+ .filter((part, idx, arr) => part.length > 0 || (idx > 0 && idx < arr.length - 1))
104
+ .join("\n")
105
+ .trim();
106
+ if (text[i] === "*")
107
+ i += 2;
108
+ state.lineDirty = true;
109
+ const sameLine = startLine === (state.tokenOnLine ? state.last?.line : -1);
110
+ if (state.tokenOnLine && state.last && sameLine) {
111
+ state.last.trail = state.last.trail ? `${state.last.trail}\n${raw}` : raw;
112
+ }
113
+ else if (raw && !/^\*+$/.test(raw.replace(/\s/g, "")) && !/^\*+ .+ \*+$/.test(raw)) {
114
+ if (leadFrom < 0)
115
+ leadFrom = commentFrom;
116
+ lead.push(raw);
117
+ }
118
+ else if (raw && !/service|message|enum/i.test(raw)) {
119
+ if (leadFrom < 0)
120
+ leadFrom = commentFrom;
121
+ lead.push(raw);
122
+ }
123
+ continue;
124
+ }
125
+ if (c === "\"" || c === "'") {
126
+ const quote = c;
127
+ const tokLine = line;
128
+ const from = i;
129
+ i++;
130
+ let value = "";
131
+ while (i < text.length && text[i] !== quote) {
132
+ if (text[i] === "\\") {
133
+ value += text[i + 1] ?? "";
134
+ i += 2;
135
+ continue;
136
+ }
137
+ if (text[i] === "\n")
138
+ line++;
139
+ value += text[i];
140
+ i++;
141
+ }
142
+ i++;
143
+ push("str", value, tokLine, from, i);
144
+ continue;
145
+ }
146
+ if (c >= "0" && c <= "9") {
147
+ const tokLine = line;
148
+ const start = i;
149
+ if (text.startsWith("0x", i) || text.startsWith("0X", i)) {
150
+ i += 2;
151
+ while (i < text.length && /[0-9a-fA-F]/.test(text[i]))
152
+ i++;
153
+ }
154
+ else {
155
+ while (i < text.length && /[0-9]/.test(text[i]))
156
+ i++;
157
+ }
158
+ push("num", text.slice(start, i), tokLine, start, i);
159
+ continue;
160
+ }
161
+ if (/[A-Za-z_]/.test(c)) {
162
+ const tokLine = line;
163
+ const start = i;
164
+ i++;
165
+ while (i < text.length && /[A-Za-z0-9_]/.test(text[i]))
166
+ i++;
167
+ push("id", text.slice(start, i), tokLine, start, i);
168
+ continue;
169
+ }
170
+ push("sym", c, line, i, i + 1);
171
+ i++;
172
+ }
173
+ return tokens;
174
+ }
175
+ class Parser {
176
+ tokens;
177
+ filePath;
178
+ text;
179
+ i = 0;
180
+ constructor(tokens, filePath, text) {
181
+ this.tokens = tokens;
182
+ this.filePath = filePath;
183
+ this.text = text;
184
+ }
185
+ parseFile(relPath) {
186
+ const file = {
187
+ path: this.filePath,
188
+ relPath,
189
+ package: "",
190
+ imports: [],
191
+ services: [],
192
+ messages: [],
193
+ enums: [],
194
+ };
195
+ while (!this.done()) {
196
+ if (this.matchSym(";")) {
197
+ this.eat();
198
+ continue;
199
+ }
200
+ const kw = this.expectKind("id").value;
201
+ if (kw === "syntax" || kw === "option" || kw === "extend") {
202
+ this.readStatement();
203
+ }
204
+ else if (kw === "package") {
205
+ file.package = this.readDottedName();
206
+ this.expectSym(";");
207
+ }
208
+ else if (kw === "import") {
209
+ if (this.peekId() === "public" || this.peekId() === "weak")
210
+ this.eat();
211
+ const spec = this.expectKind("str").value;
212
+ this.expectSym(";");
213
+ file.imports.push(spec);
214
+ }
215
+ else if (kw === "message") {
216
+ file.messages.push(this.parseMessage(this.previous().lead, ""));
217
+ }
218
+ else if (kw === "enum") {
219
+ file.enums.push(this.parseEnum(this.previous().lead, ""));
220
+ }
221
+ else if (kw === "service") {
222
+ file.services.push(this.parseService(this.previous().lead));
223
+ }
224
+ else {
225
+ this.readStatement();
226
+ }
227
+ }
228
+ if (!file.package) {
229
+ file.package = relPath.replace(/\\/g, "/").split("/").pop()?.replace(/\.proto$/, "") || "default";
230
+ }
231
+ return file;
232
+ }
233
+ parseService(comment) {
234
+ const nameTok = this.expectKind("id");
235
+ const start = nameTok.line;
236
+ this.expectSym("{");
237
+ const rpcs = [];
238
+ while (!this.matchSym("}") && !this.done()) {
239
+ if (this.matchSym(";")) {
240
+ this.eat();
241
+ continue;
242
+ }
243
+ const kw = this.expectKind("id");
244
+ if (kw.value === "rpc") {
245
+ rpcs.push(this.parseRpc(kw));
246
+ }
247
+ else {
248
+ this.readStatement();
249
+ }
250
+ }
251
+ const end = this.expectSym("}").line;
252
+ if (this.matchSym(";"))
253
+ this.eat();
254
+ return { name: nameTok.value, comment: cleanDoc(comment), rpcs, loc: { line: start, endLine: end } };
255
+ }
256
+ parseRpc(kw) {
257
+ const comment = kw.lead;
258
+ const nameTok = this.expectKind("id");
259
+ this.expectSym("(");
260
+ let requestStream = false;
261
+ if (this.peekId() === "stream") {
262
+ requestStream = true;
263
+ this.eat();
264
+ }
265
+ const request = this.readDottedName();
266
+ this.expectSym(")");
267
+ const returns = this.expectKind("id");
268
+ if (returns.value !== "returns") {
269
+ throw new ProtoParseError(`期望 returns,实际是 ${returns.value}`, returns.line);
270
+ }
271
+ this.expectSym("(");
272
+ let responseStream = false;
273
+ if (this.peekId() === "stream") {
274
+ responseStream = true;
275
+ this.eat();
276
+ }
277
+ const response = this.readDottedName();
278
+ this.expectSym(")");
279
+ let httpVerb;
280
+ let httpPath;
281
+ let httpBody;
282
+ let timeout;
283
+ let auth;
284
+ let closed;
285
+ const badges = [];
286
+ let endLine = nameTok.line;
287
+ if (this.matchSym("{")) {
288
+ this.eat();
289
+ while (!this.matchSym("}") && !this.done()) {
290
+ if (this.matchSym(";")) {
291
+ this.eat();
292
+ continue;
293
+ }
294
+ const kw = this.peek();
295
+ if (kw?.kind === "id" && kw.value === "option") {
296
+ this.eat();
297
+ const stmt = this.readStatement();
298
+ const text = stmt.map((tok) => (tok.kind === "str" ? `"${tok.value}"` : tok.value)).join(" ");
299
+ const meta = (0, meta_1.parseMetaOption)(stmt);
300
+ if (meta)
301
+ badges.push(...(0, meta_1.badgesFromMeta)(meta));
302
+ const http = text.match(/\b(get|post|put|delete|patch)\s*:\s*"([^"]+)"/i);
303
+ if (http) {
304
+ httpVerb = http[1].toUpperCase();
305
+ httpPath = http[2];
306
+ }
307
+ const body = text.match(/\bbody\s*:\s*"([^"]+)"/);
308
+ if (body)
309
+ httpBody = body[1];
310
+ const timeoutHit = text.match(/\bTimeout\s*:\s*(\d+)/);
311
+ if (timeoutHit)
312
+ timeout = timeoutHit[1];
313
+ const authHit = text.match(/\bAuthOption\s*:\s*([A-Za-z0-9_]+)/);
314
+ if (authHit)
315
+ auth = authHit[1];
316
+ if (/\bClosed\s*:\s*true\b/.test(text))
317
+ closed = true;
318
+ }
319
+ else {
320
+ this.readStatement();
321
+ }
322
+ }
323
+ endLine = this.expectSym("}").line;
324
+ }
325
+ if (this.matchSym(";"))
326
+ this.eat();
327
+ const end = this.previous();
328
+ const sourceStart = kw.leadFrom >= 0 ? kw.leadFrom : kw.from;
329
+ return {
330
+ name: nameTok.value,
331
+ comment: cleanDoc(comment),
332
+ request,
333
+ response,
334
+ requestStream,
335
+ responseStream,
336
+ httpVerb,
337
+ httpPath,
338
+ httpBody,
339
+ timeout,
340
+ auth,
341
+ closed,
342
+ badges,
343
+ source: this.text.slice(sourceStart, end.to).trim(),
344
+ loc: { line: nameTok.line, endLine },
345
+ };
346
+ }
347
+ parseMessage(comment, parent) {
348
+ const nameTok = this.expectKind("id");
349
+ const fullName = parent ? `${parent}.${nameTok.value}` : nameTok.value;
350
+ this.expectSym("{");
351
+ const fields = [];
352
+ const oneofs = [];
353
+ const members = [];
354
+ const nestedMessages = [];
355
+ const nestedEnums = [];
356
+ while (!this.matchSym("}") && !this.done()) {
357
+ if (this.matchSym(";")) {
358
+ this.eat();
359
+ continue;
360
+ }
361
+ const lead = this.peek()?.lead ?? "";
362
+ const kw = this.peekId();
363
+ if (kw === "message") {
364
+ this.eat();
365
+ nestedMessages.push(this.parseMessage(lead, fullName));
366
+ }
367
+ else if (kw === "enum") {
368
+ this.eat();
369
+ nestedEnums.push(this.parseEnum(lead, fullName));
370
+ }
371
+ else if (kw === "oneof") {
372
+ this.eat();
373
+ const oneof = this.parseOneof(lead);
374
+ oneofs.push(oneof);
375
+ members.push({ kind: "oneof", oneof });
376
+ }
377
+ else if (kw === "option" || kw === "reserved" || kw === "extensions" || kw === "extend") {
378
+ this.eat();
379
+ this.readStatement();
380
+ }
381
+ else {
382
+ const field = this.parseField();
383
+ fields.push(field);
384
+ members.push({ kind: "field", field });
385
+ }
386
+ }
387
+ const end = this.expectSym("}").line;
388
+ if (this.matchSym(";"))
389
+ this.eat();
390
+ return {
391
+ name: nameTok.value,
392
+ fullName,
393
+ comment: cleanDoc(comment),
394
+ fields,
395
+ oneofs,
396
+ members,
397
+ nestedMessages,
398
+ nestedEnums,
399
+ loc: { line: nameTok.line, endLine: end },
400
+ };
401
+ }
402
+ parseOneof(comment) {
403
+ const nameTok = this.expectKind("id");
404
+ this.expectSym("{");
405
+ const fields = [];
406
+ while (!this.matchSym("}") && !this.done()) {
407
+ if (this.matchSym(";")) {
408
+ this.eat();
409
+ continue;
410
+ }
411
+ const kw = this.peekId();
412
+ if (kw === "option") {
413
+ this.eat();
414
+ this.readStatement();
415
+ }
416
+ else {
417
+ fields.push(this.parseField());
418
+ }
419
+ }
420
+ const end = this.expectSym("}").line;
421
+ return { name: nameTok.value, comment: cleanDoc(comment), fields, loc: { line: nameTok.line, endLine: end } };
422
+ }
423
+ parseEnum(comment, parent) {
424
+ const nameTok = this.expectKind("id");
425
+ const fullName = parent ? `${parent}.${nameTok.value}` : nameTok.value;
426
+ this.expectSym("{");
427
+ const values = [];
428
+ while (!this.matchSym("}") && !this.done()) {
429
+ if (this.matchSym(";")) {
430
+ this.eat();
431
+ continue;
432
+ }
433
+ const kw = this.peekId();
434
+ if (kw === "option" || kw === "reserved") {
435
+ this.eat();
436
+ this.readStatement();
437
+ }
438
+ else {
439
+ values.push(this.parseEnumValue());
440
+ }
441
+ }
442
+ const end = this.expectSym("}").line;
443
+ if (this.matchSym(";"))
444
+ this.eat();
445
+ return {
446
+ name: nameTok.value,
447
+ fullName,
448
+ comment: cleanDoc(comment),
449
+ values,
450
+ loc: { line: nameTok.line, endLine: end },
451
+ };
452
+ }
453
+ parseEnumValue() {
454
+ const nameTok = this.expectKind("id");
455
+ this.expectSym("=");
456
+ const num = this.expectKind("num");
457
+ if (this.matchSym("["))
458
+ this.skipGroup("[", "]");
459
+ const semi = this.expectSym(";");
460
+ return {
461
+ name: nameTok.value,
462
+ number: Number(num.value),
463
+ comment: joinDoc(nameTok.lead, semi.trail),
464
+ loc: { line: nameTok.line, endLine: semi.line },
465
+ };
466
+ }
467
+ parseField() {
468
+ const start = this.peek();
469
+ let repeated = false;
470
+ let optional = false;
471
+ const kw = this.peekId();
472
+ if (kw === "repeated" || kw === "optional" || kw === "required") {
473
+ this.eat();
474
+ repeated = kw === "repeated";
475
+ optional = kw === "optional";
476
+ }
477
+ let typeName = "";
478
+ let mapKey;
479
+ let mapValue;
480
+ if (this.peekId() === "map") {
481
+ this.eat();
482
+ this.expectSym("<");
483
+ mapKey = this.readDottedName();
484
+ this.expectSym(",");
485
+ mapValue = this.readDottedName();
486
+ this.expectSym(">");
487
+ typeName = `map<${mapKey}, ${mapValue}>`;
488
+ }
489
+ else {
490
+ typeName = this.readDottedName();
491
+ }
492
+ const nameTok = this.expectKind("id");
493
+ this.expectSym("=");
494
+ const num = this.expectKind("num");
495
+ if (this.matchSym("["))
496
+ this.skipGroup("[", "]");
497
+ const semi = this.expectSym(";");
498
+ return {
499
+ name: nameTok.value,
500
+ typeName,
501
+ mapKey,
502
+ mapValue,
503
+ repeated,
504
+ optional,
505
+ number: Number(num.value),
506
+ comment: joinDoc(start?.lead ?? "", semi.trail || nameTok.trail),
507
+ loc: { line: nameTok.line, endLine: semi.line },
508
+ };
509
+ }
510
+ readDottedName() {
511
+ let name = "";
512
+ if (this.matchSym(".")) {
513
+ this.eat();
514
+ name = ".";
515
+ }
516
+ name += this.expectKind("id").value;
517
+ while (this.matchSym(".") && this.tokens[this.i + 1]?.kind === "id") {
518
+ this.eat();
519
+ name += `.${this.eat().value}`;
520
+ }
521
+ return name;
522
+ }
523
+ readStatement() {
524
+ const out = [];
525
+ let depth = 0;
526
+ while (!this.done()) {
527
+ const tok = this.peek();
528
+ if (tok.kind === "sym" && (tok.value === "{" || tok.value === "(" || tok.value === "[" || tok.value === "<")) {
529
+ depth++;
530
+ out.push(this.eat());
531
+ continue;
532
+ }
533
+ if (tok.kind === "sym" && (tok.value === "}" || tok.value === ")" || tok.value === "]" || tok.value === ">")) {
534
+ if (depth === 0)
535
+ return out;
536
+ depth--;
537
+ out.push(this.eat());
538
+ continue;
539
+ }
540
+ if (tok.kind === "sym" && tok.value === ";" && depth === 0) {
541
+ out.push(this.eat());
542
+ return out;
543
+ }
544
+ out.push(this.eat());
545
+ }
546
+ return out;
547
+ }
548
+ skipGroup(open, close) {
549
+ this.expectSym(open);
550
+ let depth = 1;
551
+ while (depth > 0 && !this.done()) {
552
+ const tok = this.eat();
553
+ if (tok.kind === "sym" && tok.value === open)
554
+ depth++;
555
+ else if (tok.kind === "sym" && tok.value === close)
556
+ depth--;
557
+ }
558
+ }
559
+ peek() {
560
+ return this.tokens[this.i];
561
+ }
562
+ peekId() {
563
+ const tok = this.peek();
564
+ return tok?.kind === "id" ? tok.value : undefined;
565
+ }
566
+ matchSym(value) {
567
+ const tok = this.peek();
568
+ return tok?.kind === "sym" && tok.value === value;
569
+ }
570
+ eat() {
571
+ const tok = this.tokens[this.i];
572
+ if (!tok)
573
+ throw new ProtoParseError(`文件提前结束:${this.filePath}`, this.previousLine());
574
+ this.i++;
575
+ return tok;
576
+ }
577
+ expectKind(kind) {
578
+ const tok = this.peek();
579
+ if (!tok || tok.kind !== kind) {
580
+ throw new ProtoParseError(`期望 ${kind},实际是 ${tok ? tok.value : "文件结束"}`, tok?.line ?? this.previousLine());
581
+ }
582
+ return this.eat();
583
+ }
584
+ expectSym(value) {
585
+ const tok = this.peek();
586
+ if (!tok || tok.kind !== "sym" || tok.value !== value) {
587
+ throw new ProtoParseError(`期望「${value}」,实际是 ${tok ? tok.value : "文件结束"}`, tok?.line ?? this.previousLine());
588
+ }
589
+ return this.eat();
590
+ }
591
+ previous() {
592
+ return this.tokens[this.i - 1];
593
+ }
594
+ previousLine() {
595
+ return this.tokens[this.i - 1]?.line ?? 1;
596
+ }
597
+ done() {
598
+ return this.i >= this.tokens.length;
599
+ }
600
+ }
601
+ function cleanDoc(text) {
602
+ return text
603
+ .split("\n")
604
+ .map((line) => line.trim())
605
+ .filter((line) => line && !/^\*+$/.test(line) && !/^\*{2,}.+\*{2,}$/.test(line))
606
+ .join("\n")
607
+ .trim();
608
+ }
609
+ function joinDoc(lead, trail) {
610
+ const a = cleanDoc(lead);
611
+ const b = cleanDoc(trail);
612
+ if (a && b)
613
+ return `${a} ${b}`.trim();
614
+ return a || b;
615
+ }
616
+ function flattenMessages(messages) {
617
+ const out = [];
618
+ const walk = (list) => {
619
+ for (const message of list) {
620
+ out.push(message);
621
+ walk(message.nestedMessages);
622
+ }
623
+ };
624
+ walk(messages);
625
+ return out;
626
+ }
627
+ function flattenEnums(messages, enums) {
628
+ const out = [...enums];
629
+ const walk = (list) => {
630
+ for (const message of list) {
631
+ out.push(...message.nestedEnums);
632
+ walk(message.nestedMessages);
633
+ }
634
+ };
635
+ walk(messages);
636
+ return out;
637
+ }
638
+ //# sourceMappingURL=parser.js.map