@depup/apollo__protobufjs 1.2.7-depup.0

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 (113) hide show
  1. package/LICENSE +39 -0
  2. package/README.md +32 -0
  3. package/bin/pbjs +6 -0
  4. package/bin/pbts +6 -0
  5. package/changes.json +14 -0
  6. package/cli/LICENSE +33 -0
  7. package/cli/README.md +11 -0
  8. package/cli/bin/pbjs +6 -0
  9. package/cli/bin/pbts +6 -0
  10. package/cli/index.d.ts +3 -0
  11. package/cli/index.js +3 -0
  12. package/cli/lib/tsd-jsdoc/LICENSE +21 -0
  13. package/cli/lib/tsd-jsdoc/README.md +23 -0
  14. package/cli/lib/tsd-jsdoc/plugin.js +21 -0
  15. package/cli/lib/tsd-jsdoc/publish.js +693 -0
  16. package/cli/lib/tsd-jsdoc.json +18 -0
  17. package/cli/package.json +7 -0
  18. package/cli/package.standalone.json +32 -0
  19. package/cli/pbjs.d.ts +9 -0
  20. package/cli/pbjs.js +331 -0
  21. package/cli/pbts.d.ts +9 -0
  22. package/cli/pbts.js +198 -0
  23. package/cli/targets/json-module.js +38 -0
  24. package/cli/targets/json.js +8 -0
  25. package/cli/targets/proto.js +326 -0
  26. package/cli/targets/proto2.js +10 -0
  27. package/cli/targets/proto3.js +10 -0
  28. package/cli/targets/static-module.js +29 -0
  29. package/cli/targets/static.js +709 -0
  30. package/cli/util.js +183 -0
  31. package/cli/wrappers/amd.js +7 -0
  32. package/cli/wrappers/closure.js +7 -0
  33. package/cli/wrappers/commonjs.js +7 -0
  34. package/cli/wrappers/default.js +15 -0
  35. package/cli/wrappers/es6.js +5 -0
  36. package/dist/README.md +31 -0
  37. package/dist/light/README.md +31 -0
  38. package/dist/light/protobuf.js +7198 -0
  39. package/dist/light/protobuf.js.map +1 -0
  40. package/dist/light/protobuf.min.js +7 -0
  41. package/dist/light/protobuf.min.js.map +1 -0
  42. package/dist/minimal/README.md +31 -0
  43. package/dist/minimal/protobuf.js +2675 -0
  44. package/dist/minimal/protobuf.js.map +1 -0
  45. package/dist/minimal/protobuf.min.js +7 -0
  46. package/dist/minimal/protobuf.min.js.map +1 -0
  47. package/dist/protobuf.js +8775 -0
  48. package/dist/protobuf.js.map +1 -0
  49. package/dist/protobuf.min.js +7 -0
  50. package/dist/protobuf.min.js.map +1 -0
  51. package/ext/debug/README.md +4 -0
  52. package/ext/debug/index.js +71 -0
  53. package/ext/descriptor/README.md +72 -0
  54. package/ext/descriptor/index.d.ts +191 -0
  55. package/ext/descriptor/index.js +1052 -0
  56. package/ext/descriptor/test.js +54 -0
  57. package/google/LICENSE +27 -0
  58. package/google/README.md +1 -0
  59. package/google/api/annotations.json +83 -0
  60. package/google/api/annotations.proto +11 -0
  61. package/google/api/http.json +86 -0
  62. package/google/api/http.proto +31 -0
  63. package/google/protobuf/api.json +118 -0
  64. package/google/protobuf/api.proto +34 -0
  65. package/google/protobuf/descriptor.json +739 -0
  66. package/google/protobuf/descriptor.proto +286 -0
  67. package/google/protobuf/source_context.json +20 -0
  68. package/google/protobuf/source_context.proto +7 -0
  69. package/google/protobuf/type.json +202 -0
  70. package/google/protobuf/type.proto +89 -0
  71. package/index.d.ts +2628 -0
  72. package/index.js +4 -0
  73. package/light.d.ts +2 -0
  74. package/light.js +4 -0
  75. package/minimal.d.ts +2 -0
  76. package/minimal.js +4 -0
  77. package/package.json +147 -0
  78. package/scripts/postinstall.js +35 -0
  79. package/src/common.js +399 -0
  80. package/src/converter.js +304 -0
  81. package/src/decoder.js +106 -0
  82. package/src/encoder.js +119 -0
  83. package/src/enum.js +181 -0
  84. package/src/field.js +379 -0
  85. package/src/index-light.js +104 -0
  86. package/src/index-minimal.js +36 -0
  87. package/src/index.js +12 -0
  88. package/src/mapfield.js +126 -0
  89. package/src/message.js +139 -0
  90. package/src/method.js +151 -0
  91. package/src/namespace.js +433 -0
  92. package/src/object.js +200 -0
  93. package/src/oneof.js +203 -0
  94. package/src/parse.js +761 -0
  95. package/src/reader.js +405 -0
  96. package/src/reader_buffer.js +44 -0
  97. package/src/root.js +353 -0
  98. package/src/roots.js +18 -0
  99. package/src/rpc/service.js +142 -0
  100. package/src/rpc.js +36 -0
  101. package/src/service.js +167 -0
  102. package/src/tokenize.js +397 -0
  103. package/src/type.js +589 -0
  104. package/src/types.js +196 -0
  105. package/src/typescript.jsdoc +22 -0
  106. package/src/util/longbits.js +200 -0
  107. package/src/util/minimal.js +406 -0
  108. package/src/util.js +178 -0
  109. package/src/verifier.js +191 -0
  110. package/src/wrappers.js +83 -0
  111. package/src/writer.js +459 -0
  112. package/src/writer_buffer.js +81 -0
  113. package/tsconfig.json +7 -0
package/src/parse.js ADDED
@@ -0,0 +1,761 @@
1
+ "use strict";
2
+ module.exports = parse;
3
+
4
+ parse.filename = null;
5
+ parse.defaults = { keepCase: false };
6
+
7
+ var tokenize = require("./tokenize"),
8
+ Root = require("./root"),
9
+ Type = require("./type"),
10
+ Field = require("./field"),
11
+ MapField = require("./mapfield"),
12
+ OneOf = require("./oneof"),
13
+ Enum = require("./enum"),
14
+ Service = require("./service"),
15
+ Method = require("./method"),
16
+ types = require("./types"),
17
+ util = require("./util");
18
+
19
+ var base10Re = /^[1-9][0-9]*$/,
20
+ base10NegRe = /^-?[1-9][0-9]*$/,
21
+ base16Re = /^0[x][0-9a-fA-F]+$/,
22
+ base16NegRe = /^-?0[x][0-9a-fA-F]+$/,
23
+ base8Re = /^0[0-7]+$/,
24
+ base8NegRe = /^-?0[0-7]+$/,
25
+ numberRe = /^(?![eE])[0-9]*(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?$/,
26
+ nameRe = /^[a-zA-Z_][a-zA-Z_0-9]*$/,
27
+ typeRefRe = /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*$/,
28
+ fqTypeRefRe = /^(?:\.[a-zA-Z_][a-zA-Z_0-9]*)+$/;
29
+
30
+ /**
31
+ * Result object returned from {@link parse}.
32
+ * @interface IParserResult
33
+ * @property {string|undefined} package Package name, if declared
34
+ * @property {string[]|undefined} imports Imports, if any
35
+ * @property {string[]|undefined} weakImports Weak imports, if any
36
+ * @property {string|undefined} syntax Syntax, if specified (either `"proto2"` or `"proto3"`)
37
+ * @property {Root} root Populated root instance
38
+ */
39
+
40
+ /**
41
+ * Options modifying the behavior of {@link parse}.
42
+ * @interface IParseOptions
43
+ * @property {boolean} [keepCase=false] Keeps field casing instead of converting to camel case
44
+ * @property {boolean} [alternateCommentMode=false] Recognize double-slash comments in addition to doc-block comments.
45
+ */
46
+
47
+ /**
48
+ * Options modifying the behavior of JSON serialization.
49
+ * @interface IToJSONOptions
50
+ * @property {boolean} [keepComments=false] Serializes comments.
51
+ */
52
+
53
+ /**
54
+ * Parses the given .proto source and returns an object with the parsed contents.
55
+ * @param {string} source Source contents
56
+ * @param {Root} root Root to populate
57
+ * @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
58
+ * @returns {IParserResult} Parser result
59
+ * @property {string} filename=null Currently processing file name for error reporting, if known
60
+ * @property {IParseOptions} defaults Default {@link IParseOptions}
61
+ */
62
+ function parse(source, root, options) {
63
+ /* eslint-disable callback-return */
64
+ if (!(root instanceof Root)) {
65
+ options = root;
66
+ root = new Root();
67
+ }
68
+ if (!options)
69
+ options = parse.defaults;
70
+
71
+ var tn = tokenize(source, options.alternateCommentMode || false),
72
+ next = tn.next,
73
+ push = tn.push,
74
+ peek = tn.peek,
75
+ skip = tn.skip,
76
+ cmnt = tn.cmnt;
77
+
78
+ var head = true,
79
+ pkg,
80
+ imports,
81
+ weakImports,
82
+ syntax,
83
+ isProto3 = false;
84
+
85
+ var ptr = root;
86
+
87
+ var applyCase = options.keepCase ? function(name) { return name; } : util.camelCase;
88
+
89
+ /* istanbul ignore next */
90
+ function illegal(token, name, insideTryCatch) {
91
+ var filename = parse.filename;
92
+ if (!insideTryCatch)
93
+ parse.filename = null;
94
+ return Error("illegal " + (name || "token") + " '" + token + "' (" + (filename ? filename + ", " : "") + "line " + tn.line + ")");
95
+ }
96
+
97
+ function readString() {
98
+ var values = [],
99
+ token;
100
+ do {
101
+ /* istanbul ignore if */
102
+ if ((token = next()) !== "\"" && token !== "'")
103
+ throw illegal(token);
104
+
105
+ values.push(next());
106
+ skip(token);
107
+ token = peek();
108
+ } while (token === "\"" || token === "'");
109
+ return values.join("");
110
+ }
111
+
112
+ function readValue(acceptTypeRef) {
113
+ var token = next();
114
+ switch (token) {
115
+ case "'":
116
+ case "\"":
117
+ push(token);
118
+ return readString();
119
+ case "true": case "TRUE":
120
+ return true;
121
+ case "false": case "FALSE":
122
+ return false;
123
+ }
124
+ try {
125
+ return parseNumber(token, /* insideTryCatch */ true);
126
+ } catch (e) {
127
+
128
+ /* istanbul ignore else */
129
+ if (acceptTypeRef && typeRefRe.test(token))
130
+ return token;
131
+
132
+ /* istanbul ignore next */
133
+ throw illegal(token, "value");
134
+ }
135
+ }
136
+
137
+ function readRanges(target, acceptStrings) {
138
+ var token, start;
139
+ do {
140
+ if (acceptStrings && ((token = peek()) === "\"" || token === "'"))
141
+ target.push(readString());
142
+ else
143
+ target.push([ start = parseId(next()), skip("to", true) ? parseId(next()) : start ]);
144
+ } while (skip(",", true));
145
+ skip(";");
146
+ }
147
+
148
+ function parseNumber(token, insideTryCatch) {
149
+ var sign = 1;
150
+ if (token.charAt(0) === "-") {
151
+ sign = -1;
152
+ token = token.substring(1);
153
+ }
154
+ switch (token) {
155
+ case "inf": case "INF": case "Inf":
156
+ return sign * Infinity;
157
+ case "nan": case "NAN": case "Nan": case "NaN":
158
+ return NaN;
159
+ case "0":
160
+ return 0;
161
+ }
162
+ if (base10Re.test(token))
163
+ return sign * parseInt(token, 10);
164
+ if (base16Re.test(token))
165
+ return sign * parseInt(token, 16);
166
+ if (base8Re.test(token))
167
+ return sign * parseInt(token, 8);
168
+
169
+ /* istanbul ignore else */
170
+ if (numberRe.test(token))
171
+ return sign * parseFloat(token);
172
+
173
+ /* istanbul ignore next */
174
+ throw illegal(token, "number", insideTryCatch);
175
+ }
176
+
177
+ function parseId(token, acceptNegative) {
178
+ switch (token) {
179
+ case "max": case "MAX": case "Max":
180
+ return 536870911;
181
+ case "0":
182
+ return 0;
183
+ }
184
+
185
+ /* istanbul ignore if */
186
+ if (!acceptNegative && token.charAt(0) === "-")
187
+ throw illegal(token, "id");
188
+
189
+ if (base10NegRe.test(token))
190
+ return parseInt(token, 10);
191
+ if (base16NegRe.test(token))
192
+ return parseInt(token, 16);
193
+
194
+ /* istanbul ignore else */
195
+ if (base8NegRe.test(token))
196
+ return parseInt(token, 8);
197
+
198
+ /* istanbul ignore next */
199
+ throw illegal(token, "id");
200
+ }
201
+
202
+ function parsePackage() {
203
+
204
+ /* istanbul ignore if */
205
+ if (pkg !== undefined)
206
+ throw illegal("package");
207
+
208
+ pkg = next();
209
+
210
+ /* istanbul ignore if */
211
+ if (!typeRefRe.test(pkg))
212
+ throw illegal(pkg, "name");
213
+
214
+ ptr = ptr.define(pkg);
215
+ skip(";");
216
+ }
217
+
218
+ function parseImport() {
219
+ var token = peek();
220
+ var whichImports;
221
+ switch (token) {
222
+ case "weak":
223
+ whichImports = weakImports || (weakImports = []);
224
+ next();
225
+ break;
226
+ case "public":
227
+ next();
228
+ // eslint-disable-line no-fallthrough
229
+ default:
230
+ whichImports = imports || (imports = []);
231
+ break;
232
+ }
233
+ token = readString();
234
+ skip(";");
235
+ whichImports.push(token);
236
+ }
237
+
238
+ function parseSyntax() {
239
+ skip("=");
240
+ syntax = readString();
241
+ isProto3 = syntax === "proto3";
242
+
243
+ /* istanbul ignore if */
244
+ if (!isProto3 && syntax !== "proto2")
245
+ throw illegal(syntax, "syntax");
246
+
247
+ skip(";");
248
+ }
249
+
250
+ function parseCommon(parent, token) {
251
+ switch (token) {
252
+
253
+ case "option":
254
+ parseOption(parent, token);
255
+ skip(";");
256
+ return true;
257
+
258
+ case "message":
259
+ parseType(parent, token);
260
+ return true;
261
+
262
+ case "enum":
263
+ parseEnum(parent, token);
264
+ return true;
265
+
266
+ case "service":
267
+ parseService(parent, token);
268
+ return true;
269
+
270
+ case "extend":
271
+ parseExtension(parent, token);
272
+ return true;
273
+ }
274
+ return false;
275
+ }
276
+
277
+ function ifBlock(obj, fnIf, fnElse) {
278
+ var trailingLine = tn.line;
279
+ if (obj) {
280
+ if(typeof obj.comment !== "string") {
281
+ obj.comment = cmnt(); // try block-type comment
282
+ }
283
+ obj.filename = parse.filename;
284
+ }
285
+ if (skip("{", true)) {
286
+ var token;
287
+ while ((token = next()) !== "}")
288
+ fnIf(token);
289
+ skip(";", true);
290
+ } else {
291
+ if (fnElse)
292
+ fnElse();
293
+ skip(";");
294
+ if (obj && typeof obj.comment !== "string")
295
+ obj.comment = cmnt(trailingLine); // try line-type comment if no block
296
+ }
297
+ }
298
+
299
+ function parseType(parent, token) {
300
+
301
+ /* istanbul ignore if */
302
+ if (!nameRe.test(token = next()))
303
+ throw illegal(token, "type name");
304
+
305
+ var type = new Type(token);
306
+ ifBlock(type, function parseType_block(token) {
307
+ if (parseCommon(type, token))
308
+ return;
309
+
310
+ switch (token) {
311
+
312
+ case "map":
313
+ parseMapField(type, token);
314
+ break;
315
+
316
+ case "required":
317
+ case "optional":
318
+ case "repeated":
319
+ parseField(type, token);
320
+ break;
321
+
322
+ case "oneof":
323
+ parseOneOf(type, token);
324
+ break;
325
+
326
+ case "extensions":
327
+ readRanges(type.extensions || (type.extensions = []));
328
+ break;
329
+
330
+ case "reserved":
331
+ readRanges(type.reserved || (type.reserved = []), true);
332
+ break;
333
+
334
+ default:
335
+ /* istanbul ignore if */
336
+ if (!isProto3 || !typeRefRe.test(token))
337
+ throw illegal(token);
338
+
339
+ push(token);
340
+ parseField(type, "optional");
341
+ break;
342
+ }
343
+ });
344
+ parent.add(type);
345
+ }
346
+
347
+ function parseField(parent, rule, extend) {
348
+ var type = next();
349
+ if (type === "group") {
350
+ parseGroup(parent, rule);
351
+ return;
352
+ }
353
+
354
+ /* istanbul ignore if */
355
+ if (!typeRefRe.test(type))
356
+ throw illegal(type, "type");
357
+
358
+ var name = next();
359
+
360
+ /* istanbul ignore if */
361
+ if (!nameRe.test(name))
362
+ throw illegal(name, "name");
363
+
364
+ name = applyCase(name);
365
+ skip("=");
366
+
367
+ var field = new Field(name, parseId(next()), type, rule, extend);
368
+ ifBlock(field, function parseField_block(token) {
369
+
370
+ /* istanbul ignore else */
371
+ if (token === "option") {
372
+ parseOption(field, token);
373
+ skip(";");
374
+ } else
375
+ throw illegal(token);
376
+
377
+ }, function parseField_line() {
378
+ parseInlineOptions(field);
379
+ });
380
+ parent.add(field);
381
+
382
+ // JSON defaults to packed=true if not set so we have to set packed=false explicity when
383
+ // parsing proto2 descriptors without the option, where applicable. This must be done for
384
+ // all known packable types and anything that could be an enum (= is not a basic type).
385
+ if (!isProto3 && field.repeated && (types.packed[type] !== undefined || types.basic[type] === undefined))
386
+ field.setOption("packed", false, /* ifNotSet */ true);
387
+ }
388
+
389
+ function parseGroup(parent, rule) {
390
+ var name = next();
391
+
392
+ /* istanbul ignore if */
393
+ if (!nameRe.test(name))
394
+ throw illegal(name, "name");
395
+
396
+ var fieldName = util.lcFirst(name);
397
+ if (name === fieldName)
398
+ name = util.ucFirst(name);
399
+ skip("=");
400
+ var id = parseId(next());
401
+ var type = new Type(name);
402
+ type.group = true;
403
+ var field = new Field(fieldName, id, name, rule);
404
+ field.filename = parse.filename;
405
+ ifBlock(type, function parseGroup_block(token) {
406
+ switch (token) {
407
+
408
+ case "option":
409
+ parseOption(type, token);
410
+ skip(";");
411
+ break;
412
+
413
+ case "required":
414
+ case "optional":
415
+ case "repeated":
416
+ parseField(type, token);
417
+ break;
418
+
419
+ /* istanbul ignore next */
420
+ default:
421
+ throw illegal(token); // there are no groups with proto3 semantics
422
+ }
423
+ });
424
+ parent.add(type)
425
+ .add(field);
426
+ }
427
+
428
+ function parseMapField(parent) {
429
+ skip("<");
430
+ var keyType = next();
431
+
432
+ /* istanbul ignore if */
433
+ if (types.mapKey[keyType] === undefined)
434
+ throw illegal(keyType, "type");
435
+
436
+ skip(",");
437
+ var valueType = next();
438
+
439
+ /* istanbul ignore if */
440
+ if (!typeRefRe.test(valueType))
441
+ throw illegal(valueType, "type");
442
+
443
+ skip(">");
444
+ var name = next();
445
+
446
+ /* istanbul ignore if */
447
+ if (!nameRe.test(name))
448
+ throw illegal(name, "name");
449
+
450
+ skip("=");
451
+ var field = new MapField(applyCase(name), parseId(next()), keyType, valueType);
452
+ ifBlock(field, function parseMapField_block(token) {
453
+
454
+ /* istanbul ignore else */
455
+ if (token === "option") {
456
+ parseOption(field, token);
457
+ skip(";");
458
+ } else
459
+ throw illegal(token);
460
+
461
+ }, function parseMapField_line() {
462
+ parseInlineOptions(field);
463
+ });
464
+ parent.add(field);
465
+ }
466
+
467
+ function parseOneOf(parent, token) {
468
+
469
+ /* istanbul ignore if */
470
+ if (!nameRe.test(token = next()))
471
+ throw illegal(token, "name");
472
+
473
+ var oneof = new OneOf(applyCase(token));
474
+ ifBlock(oneof, function parseOneOf_block(token) {
475
+ if (token === "option") {
476
+ parseOption(oneof, token);
477
+ skip(";");
478
+ } else {
479
+ push(token);
480
+ parseField(oneof, "optional");
481
+ }
482
+ });
483
+ parent.add(oneof);
484
+ }
485
+
486
+ function parseEnum(parent, token) {
487
+
488
+ /* istanbul ignore if */
489
+ if (!nameRe.test(token = next()))
490
+ throw illegal(token, "name");
491
+
492
+ var enm = new Enum(token);
493
+ ifBlock(enm, function parseEnum_block(token) {
494
+ switch(token) {
495
+ case "option":
496
+ parseOption(enm, token);
497
+ skip(";");
498
+ break;
499
+
500
+ case "reserved":
501
+ readRanges(enm.reserved || (enm.reserved = []), true);
502
+ break;
503
+
504
+ default:
505
+ parseEnumValue(enm, token);
506
+ }
507
+ });
508
+ parent.add(enm);
509
+ }
510
+
511
+ function parseEnumValue(parent, token) {
512
+
513
+ /* istanbul ignore if */
514
+ if (!nameRe.test(token))
515
+ throw illegal(token, "name");
516
+
517
+ skip("=");
518
+ var value = parseId(next(), true),
519
+ dummy = {};
520
+ ifBlock(dummy, function parseEnumValue_block(token) {
521
+
522
+ /* istanbul ignore else */
523
+ if (token === "option") {
524
+ parseOption(dummy, token); // skip
525
+ skip(";");
526
+ } else
527
+ throw illegal(token);
528
+
529
+ }, function parseEnumValue_line() {
530
+ parseInlineOptions(dummy); // skip
531
+ });
532
+ parent.add(token, value, dummy.comment);
533
+ }
534
+
535
+ function parseOption(parent, token) {
536
+ var isCustom = skip("(", true);
537
+
538
+ /* istanbul ignore if */
539
+ if (!typeRefRe.test(token = next()))
540
+ throw illegal(token, "name");
541
+
542
+ var name = token;
543
+ if (isCustom) {
544
+ skip(")");
545
+ name = "(" + name + ")";
546
+ token = peek();
547
+ if (fqTypeRefRe.test(token)) {
548
+ name += token;
549
+ next();
550
+ }
551
+ }
552
+ skip("=");
553
+ parseOptionValue(parent, name);
554
+ }
555
+
556
+ function parseOptionValue(parent, name) {
557
+ if (skip("{", true)) { // { a: "foo" b { c: "bar" } }
558
+ do {
559
+ /* istanbul ignore if */
560
+ if (!nameRe.test(token = next()))
561
+ throw illegal(token, "name");
562
+
563
+ if (peek() === "{")
564
+ parseOptionValue(parent, name + "." + token);
565
+ else {
566
+ skip(":");
567
+ if (peek() === "{")
568
+ parseOptionValue(parent, name + "." + token);
569
+ else
570
+ setOption(parent, name + "." + token, readValue(true));
571
+ }
572
+ skip(",", true);
573
+ } while (!skip("}", true));
574
+ } else
575
+ setOption(parent, name, readValue(true));
576
+ // Does not enforce a delimiter to be universal
577
+ }
578
+
579
+ function setOption(parent, name, value) {
580
+ if (parent.setOption)
581
+ parent.setOption(name, value);
582
+ }
583
+
584
+ function parseInlineOptions(parent) {
585
+ if (skip("[", true)) {
586
+ do {
587
+ parseOption(parent, "option");
588
+ } while (skip(",", true));
589
+ skip("]");
590
+ }
591
+ return parent;
592
+ }
593
+
594
+ function parseService(parent, token) {
595
+
596
+ /* istanbul ignore if */
597
+ if (!nameRe.test(token = next()))
598
+ throw illegal(token, "service name");
599
+
600
+ var service = new Service(token);
601
+ ifBlock(service, function parseService_block(token) {
602
+ if (parseCommon(service, token))
603
+ return;
604
+
605
+ /* istanbul ignore else */
606
+ if (token === "rpc")
607
+ parseMethod(service, token);
608
+ else
609
+ throw illegal(token);
610
+ });
611
+ parent.add(service);
612
+ }
613
+
614
+ function parseMethod(parent, token) {
615
+ // Get the comment of the preceding line now (if one exists) in case the
616
+ // method is defined across multiple lines.
617
+ var commentText = cmnt();
618
+
619
+ var type = token;
620
+
621
+ /* istanbul ignore if */
622
+ if (!nameRe.test(token = next()))
623
+ throw illegal(token, "name");
624
+
625
+ var name = token,
626
+ requestType, requestStream,
627
+ responseType, responseStream;
628
+
629
+ skip("(");
630
+ if (skip("stream", true))
631
+ requestStream = true;
632
+
633
+ /* istanbul ignore if */
634
+ if (!typeRefRe.test(token = next()))
635
+ throw illegal(token);
636
+
637
+ requestType = token;
638
+ skip(")"); skip("returns"); skip("(");
639
+ if (skip("stream", true))
640
+ responseStream = true;
641
+
642
+ /* istanbul ignore if */
643
+ if (!typeRefRe.test(token = next()))
644
+ throw illegal(token);
645
+
646
+ responseType = token;
647
+ skip(")");
648
+
649
+ var method = new Method(name, type, requestType, responseType, requestStream, responseStream);
650
+ method.comment = commentText;
651
+ ifBlock(method, function parseMethod_block(token) {
652
+
653
+ /* istanbul ignore else */
654
+ if (token === "option") {
655
+ parseOption(method, token);
656
+ skip(";");
657
+ } else
658
+ throw illegal(token);
659
+
660
+ });
661
+ parent.add(method);
662
+ }
663
+
664
+ function parseExtension(parent, token) {
665
+
666
+ /* istanbul ignore if */
667
+ if (!typeRefRe.test(token = next()))
668
+ throw illegal(token, "reference");
669
+
670
+ var reference = token;
671
+ ifBlock(null, function parseExtension_block(token) {
672
+ switch (token) {
673
+
674
+ case "required":
675
+ case "repeated":
676
+ case "optional":
677
+ parseField(parent, token, reference);
678
+ break;
679
+
680
+ default:
681
+ /* istanbul ignore if */
682
+ if (!isProto3 || !typeRefRe.test(token))
683
+ throw illegal(token);
684
+ push(token);
685
+ parseField(parent, "optional", reference);
686
+ break;
687
+ }
688
+ });
689
+ }
690
+
691
+ var token;
692
+ while ((token = next()) !== null) {
693
+ switch (token) {
694
+
695
+ case "package":
696
+
697
+ /* istanbul ignore if */
698
+ if (!head)
699
+ throw illegal(token);
700
+
701
+ parsePackage();
702
+ break;
703
+
704
+ case "import":
705
+
706
+ /* istanbul ignore if */
707
+ if (!head)
708
+ throw illegal(token);
709
+
710
+ parseImport();
711
+ break;
712
+
713
+ case "syntax":
714
+
715
+ /* istanbul ignore if */
716
+ if (!head)
717
+ throw illegal(token);
718
+
719
+ parseSyntax();
720
+ break;
721
+
722
+ case "option":
723
+
724
+ parseOption(ptr, token);
725
+ skip(";");
726
+ break;
727
+
728
+ default:
729
+
730
+ /* istanbul ignore else */
731
+ if (parseCommon(ptr, token)) {
732
+ head = false;
733
+ continue;
734
+ }
735
+
736
+ /* istanbul ignore next */
737
+ throw illegal(token);
738
+ }
739
+ }
740
+
741
+ parse.filename = null;
742
+ return {
743
+ "package" : pkg,
744
+ "imports" : imports,
745
+ weakImports : weakImports,
746
+ syntax : syntax,
747
+ root : root
748
+ };
749
+ }
750
+
751
+ /**
752
+ * Parses the given .proto source and returns an object with the parsed contents.
753
+ * @name parse
754
+ * @function
755
+ * @param {string} source Source contents
756
+ * @param {IParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
757
+ * @returns {IParserResult} Parser result
758
+ * @property {string} filename=null Currently processing file name for error reporting, if known
759
+ * @property {IParseOptions} defaults Default {@link IParseOptions}
760
+ * @variation 2
761
+ */