protobufjs 8.6.1 → 8.6.2

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 (105) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +441 -441
  3. package/dist/light/protobuf.js +8455 -8455
  4. package/dist/light/protobuf.js.map +1 -1
  5. package/dist/light/protobuf.min.js +3 -3
  6. package/dist/light/protobuf.min.js.map +1 -1
  7. package/dist/minimal/protobuf.js +2999 -2999
  8. package/dist/minimal/protobuf.js.map +1 -1
  9. package/dist/minimal/protobuf.min.js +3 -3
  10. package/dist/minimal/protobuf.min.js.map +1 -1
  11. package/dist/protobuf.js +10359 -10359
  12. package/dist/protobuf.js.map +1 -1
  13. package/dist/protobuf.min.js +3 -3
  14. package/dist/protobuf.min.js.map +1 -1
  15. package/ext/README.md +70 -70
  16. package/ext/debug/README.md +4 -4
  17. package/ext/debug/index.js +71 -71
  18. package/ext/descriptor/README.md +5 -5
  19. package/ext/descriptor/index.d.ts +2 -2
  20. package/ext/descriptor/index.js +2 -2
  21. package/ext/descriptor.d.ts +87 -87
  22. package/ext/descriptor.js +1354 -1354
  23. package/ext/protojson.LICENSE +201 -201
  24. package/ext/protojson.d.ts +20 -20
  25. package/ext/protojson.js +925 -925
  26. package/ext/textformat.d.ts +19 -19
  27. package/ext/textformat.js +1256 -1256
  28. package/google/LICENSE +27 -27
  29. package/google/README.md +1 -1
  30. package/google/api/annotations.json +82 -82
  31. package/google/api/annotations.proto +10 -10
  32. package/google/api/http.json +85 -85
  33. package/google/api/http.proto +30 -30
  34. package/google/protobuf/api.json +117 -117
  35. package/google/protobuf/api.proto +33 -33
  36. package/google/protobuf/compiler/plugin.json +126 -126
  37. package/google/protobuf/compiler/plugin.proto +47 -47
  38. package/google/protobuf/descriptor.json +1381 -1381
  39. package/google/protobuf/descriptor.proto +534 -534
  40. package/google/protobuf/source_context.json +19 -19
  41. package/google/protobuf/source_context.proto +7 -7
  42. package/google/protobuf/type.json +201 -201
  43. package/google/protobuf/type.proto +89 -89
  44. package/index.d.ts +1 -1
  45. package/index.js +4 -4
  46. package/light.d.ts +2 -2
  47. package/light.js +3 -3
  48. package/minimal.d.ts +2 -2
  49. package/minimal.js +4 -4
  50. package/package.json +93 -93
  51. package/src/common.js +399 -399
  52. package/src/converter.js +344 -344
  53. package/src/decoder.js +208 -208
  54. package/src/encoder.js +111 -111
  55. package/src/enum.js +231 -231
  56. package/src/field.js +497 -497
  57. package/src/index-light.js +104 -104
  58. package/src/index-minimal.js +36 -36
  59. package/src/index.js +12 -12
  60. package/src/mapfield.js +136 -136
  61. package/src/message.js +137 -137
  62. package/src/method.js +175 -175
  63. package/src/namespace.js +565 -565
  64. package/src/object.js +382 -382
  65. package/src/oneof.js +225 -225
  66. package/src/parse.js +1068 -1068
  67. package/src/reader.js +543 -543
  68. package/src/reader_buffer.js +72 -72
  69. package/src/root.js +416 -416
  70. package/src/roots.js +18 -18
  71. package/src/rpc/service.js +148 -148
  72. package/src/rpc.js +36 -36
  73. package/src/service.js +198 -198
  74. package/src/tokenize.js +421 -421
  75. package/src/type.js +655 -655
  76. package/src/types.js +196 -196
  77. package/src/typescript.js +25 -25
  78. package/src/util/aspromise.d.ts +13 -13
  79. package/src/util/aspromise.js +52 -52
  80. package/src/util/base64.d.ts +32 -32
  81. package/src/util/base64.js +146 -146
  82. package/src/util/codegen.d.ts +31 -31
  83. package/src/util/codegen.js +113 -113
  84. package/src/util/eventemitter.d.ts +45 -45
  85. package/src/util/eventemitter.js +86 -86
  86. package/src/util/fetch.d.ts +56 -56
  87. package/src/util/fetch.js +112 -112
  88. package/src/util/float.d.ts +83 -83
  89. package/src/util/float.js +335 -335
  90. package/src/util/fs.js +11 -11
  91. package/src/util/longbits.js +200 -200
  92. package/src/util/minimal.js +515 -515
  93. package/src/util/path.d.ts +22 -22
  94. package/src/util/path.js +72 -72
  95. package/src/util/patterns.js +7 -7
  96. package/src/util/pool.d.ts +32 -32
  97. package/src/util/pool.js +48 -48
  98. package/src/util/utf8.d.ts +24 -24
  99. package/src/util/utf8.js +130 -130
  100. package/src/util.js +242 -242
  101. package/src/verifier.js +180 -180
  102. package/src/wrappers.js +106 -106
  103. package/src/writer.js +495 -495
  104. package/src/writer_buffer.js +102 -102
  105. package/tsconfig.json +6 -6
package/src/tokenize.js CHANGED
@@ -1,421 +1,421 @@
1
- "use strict";
2
- module.exports = tokenize;
3
-
4
- var delimRe = /[\s{}=;:[\],'"()<>]/g,
5
- stringDoubleRe = /(?:"([^"\\]*(?:\\.[^"\\]*)*)")/g,
6
- stringSingleRe = /(?:'([^'\\]*(?:\\.[^'\\]*)*)')/g;
7
-
8
- var setCommentRe = /^ *[*/]+ */,
9
- setCommentAltRe = /^\s*\*?\/*/,
10
- setCommentSplitRe = /\n/g,
11
- whitespaceRe = /\s/,
12
- unescapeRe = /\\(.?)/g;
13
-
14
- var unescapeMap = {
15
- "0": "\0",
16
- "r": "\r",
17
- "n": "\n",
18
- "t": "\t"
19
- };
20
-
21
- /**
22
- * Unescapes a string.
23
- * @param {string} str String to unescape
24
- * @returns {string} Unescaped string
25
- * @property {Object.<string,string>} map Special characters map
26
- * @memberof tokenize
27
- */
28
- function unescape(str) {
29
- return str.replace(unescapeRe, function($0, $1) {
30
- switch ($1) {
31
- case "\\":
32
- case "":
33
- return $1;
34
- default:
35
- return unescapeMap[$1] || "";
36
- }
37
- });
38
- }
39
-
40
- tokenize.unescape = unescape;
41
-
42
- /**
43
- * Gets the next token and advances.
44
- * @typedef TokenizerHandleNext
45
- * @type {function}
46
- * @returns {string|null} Next token or `null` on eof
47
- */
48
-
49
- /**
50
- * Peeks for the next token.
51
- * @typedef TokenizerHandlePeek
52
- * @type {function}
53
- * @returns {string|null} Next token or `null` on eof
54
- */
55
-
56
- /**
57
- * Pushes a token back to the stack.
58
- * @typedef TokenizerHandlePush
59
- * @type {function}
60
- * @param {string} token Token
61
- * @returns {undefined}
62
- */
63
-
64
- /**
65
- * Skips the next token.
66
- * @typedef TokenizerHandleSkip
67
- * @type {function}
68
- * @param {string} expected Expected token
69
- * @param {boolean} [optional=false] If optional
70
- * @returns {boolean} Whether the token matched
71
- * @throws {Error} If the token didn't match and is not optional
72
- */
73
-
74
- /**
75
- * Gets the comment on the previous line or, alternatively, the line comment on the specified line.
76
- * @typedef TokenizerHandleCmnt
77
- * @type {function}
78
- * @param {number} [line] Line number
79
- * @returns {string|null} Comment text or `null` if none
80
- */
81
-
82
- /**
83
- * Handle object returned from {@link tokenize}.
84
- * @interface ITokenizerHandle
85
- * @property {TokenizerHandleNext} next Gets the next token and advances (`null` on eof)
86
- * @property {TokenizerHandlePeek} peek Peeks for the next token (`null` on eof)
87
- * @property {TokenizerHandlePush} push Pushes a token back to the stack
88
- * @property {TokenizerHandleSkip} skip Skips a token, returns its presence and advances or, if non-optional and not present, throws
89
- * @property {TokenizerHandleCmnt} cmnt Gets the comment on the previous line or the line comment on the specified line, if any
90
- * @property {number} line Current line number
91
- */
92
-
93
- /**
94
- * Tokenizes the given .proto source and returns an object with useful utility functions.
95
- * @param {string} source Source contents
96
- * @param {boolean} alternateCommentMode Whether we should activate alternate comment parsing mode.
97
- * @returns {ITokenizerHandle} Tokenizer handle
98
- */
99
- function tokenize(source, alternateCommentMode) {
100
- /* eslint-disable callback-return */
101
- source = source.toString();
102
-
103
- var offset = 0,
104
- length = source.length,
105
- line = 1,
106
- lastCommentLine = 0,
107
- comments = {};
108
-
109
- var stack = [];
110
-
111
- var stringDelim = null;
112
-
113
- /* istanbul ignore next */
114
- /**
115
- * Creates an error for illegal syntax.
116
- * @param {string} subject Subject
117
- * @returns {Error} Error created
118
- * @inner
119
- */
120
- function illegal(subject) {
121
- return Error("illegal " + subject + " (line " + line + ")");
122
- }
123
-
124
- /**
125
- * Reads a string till its end.
126
- * @returns {string} String read
127
- * @inner
128
- */
129
- function readString() {
130
- var re = stringDelim === "'" ? stringSingleRe : stringDoubleRe;
131
- re.lastIndex = offset - 1;
132
- var match = re.exec(source);
133
- if (!match)
134
- throw illegal("string");
135
- offset = re.lastIndex;
136
- push(stringDelim);
137
- stringDelim = null;
138
- return unescape(match[1]);
139
- }
140
-
141
- /**
142
- * Gets the character at `pos` within the source.
143
- * @param {number} pos Position
144
- * @returns {string} Character
145
- * @inner
146
- */
147
- function charAt(pos) {
148
- return source.charAt(pos);
149
- }
150
-
151
- /**
152
- * Sets the current comment text.
153
- * @param {number} start Start offset
154
- * @param {number} end End offset
155
- * @param {boolean} isLeading set if a leading comment
156
- * @returns {undefined}
157
- * @inner
158
- */
159
- function setComment(start, end, isLeading) {
160
- var comment = {
161
- type: source.charAt(start++),
162
- lineEmpty: false,
163
- leading: isLeading,
164
- };
165
- var lookback;
166
- if (alternateCommentMode) {
167
- lookback = 2; // alternate comment parsing: "//" or "/*"
168
- } else {
169
- lookback = 3; // "///" or "/**"
170
- }
171
- var commentOffset = start - lookback,
172
- c;
173
- do {
174
- if (--commentOffset < 0 ||
175
- (c = source.charAt(commentOffset)) === "\n") {
176
- comment.lineEmpty = true;
177
- break;
178
- }
179
- } while (c === " " || c === "\t");
180
- var lines = source
181
- .substring(start, end)
182
- .split(setCommentSplitRe);
183
- for (var i = 0; i < lines.length; ++i)
184
- lines[i] = lines[i]
185
- .replace(alternateCommentMode ? setCommentAltRe : setCommentRe, "")
186
- .trim();
187
- comment.text = lines
188
- .join("\n")
189
- .trim();
190
-
191
- comments[line] = comment;
192
- lastCommentLine = line;
193
- }
194
-
195
- function isDoubleSlashCommentLine(startOffset) {
196
- var endOffset = findEndOfLine(startOffset);
197
-
198
- // see if remaining line matches comment pattern
199
- var lineText = source.substring(startOffset, endOffset);
200
- var isComment = /^\s*\/\//.test(lineText);
201
- return isComment;
202
- }
203
-
204
- function findEndOfLine(cursor) {
205
- // find end of cursor's line
206
- var endOffset = cursor;
207
- while (endOffset < length && charAt(endOffset) !== "\n") {
208
- endOffset++;
209
- }
210
- return endOffset;
211
- }
212
-
213
- /**
214
- * Obtains the next token.
215
- * @returns {string|null} Next token or `null` on eof
216
- * @inner
217
- */
218
- function next() {
219
- if (stack.length > 0)
220
- return stack.shift();
221
- if (stringDelim)
222
- return readString();
223
- var repeat,
224
- prev,
225
- curr,
226
- start,
227
- isDoc,
228
- nextLineIsComment,
229
- isLeadingComment = offset === 0;
230
- do {
231
- if (offset === length)
232
- return null;
233
- repeat = false;
234
- while (whitespaceRe.test(curr = charAt(offset))) {
235
- if (curr === "\n") {
236
- isLeadingComment = true;
237
- ++line;
238
- }
239
- if (++offset === length)
240
- return null;
241
- }
242
-
243
- if (charAt(offset) === "/") {
244
- if (++offset === length) {
245
- throw illegal("comment");
246
- }
247
- if (charAt(offset) === "/") { // Line
248
- if (!alternateCommentMode) {
249
- // check for triple-slash comment
250
- isDoc = charAt(start = offset + 1) === "/";
251
-
252
- while (charAt(++offset) !== "\n") {
253
- if (offset === length) {
254
- return null;
255
- }
256
- }
257
- ++offset;
258
- if (isDoc) {
259
- setComment(start, offset - 1, isLeadingComment);
260
- // Trailing comment cannot not be multi-line,
261
- // so leading comment state should be reset to handle potential next comments
262
- isLeadingComment = true;
263
- }
264
- ++line;
265
- repeat = true;
266
- } else {
267
- // check for double-slash comments, consolidating consecutive lines
268
- start = offset;
269
- isDoc = false;
270
- if (isDoubleSlashCommentLine(offset - 1)) {
271
- isDoc = true;
272
- do {
273
- offset = findEndOfLine(offset);
274
- if (offset === length) {
275
- break;
276
- }
277
- offset++;
278
- if (!isLeadingComment) {
279
- // Trailing comment cannot not be multi-line
280
- break;
281
- }
282
- nextLineIsComment = isDoubleSlashCommentLine(offset);
283
- if (nextLineIsComment) {
284
- line++;
285
- }
286
- } while (nextLineIsComment);
287
- } else {
288
- offset = Math.min(length, findEndOfLine(offset) + 1);
289
- }
290
- if (isDoc) {
291
- setComment(start, offset, isLeadingComment);
292
- isLeadingComment = true;
293
- }
294
- line++;
295
- repeat = true;
296
- }
297
- } else if ((curr = charAt(offset)) === "*") { /* Block */
298
- // check for /** (regular comment mode) or /* (alternate comment mode)
299
- start = offset + 1;
300
- isDoc = alternateCommentMode || charAt(start) === "*";
301
- do {
302
- if (curr === "\n") {
303
- ++line;
304
- }
305
- if (++offset === length) {
306
- throw illegal("comment");
307
- }
308
- prev = curr;
309
- curr = charAt(offset);
310
- } while (prev !== "*" || curr !== "/");
311
- ++offset;
312
- if (isDoc) {
313
- setComment(start, offset - 2, isLeadingComment);
314
- isLeadingComment = true;
315
- }
316
- repeat = true;
317
- } else {
318
- return "/";
319
- }
320
- }
321
- } while (repeat);
322
-
323
- // offset !== length if we got here
324
-
325
- var end = offset;
326
- delimRe.lastIndex = 0;
327
- var delim = delimRe.test(charAt(end++));
328
- if (!delim)
329
- while (end < length && !delimRe.test(charAt(end)))
330
- ++end;
331
- var token = source.substring(offset, offset = end);
332
- if (token === "\"" || token === "'")
333
- stringDelim = token;
334
- return token;
335
- }
336
-
337
- /**
338
- * Pushes a token back to the stack.
339
- * @param {string} token Token
340
- * @returns {undefined}
341
- * @inner
342
- */
343
- function push(token) {
344
- stack.push(token);
345
- }
346
-
347
- /**
348
- * Peeks for the next token.
349
- * @returns {string|null} Token or `null` on eof
350
- * @inner
351
- */
352
- function peek() {
353
- if (!stack.length) {
354
- var token = next();
355
- if (token === null)
356
- return null;
357
- push(token);
358
- }
359
- return stack[0];
360
- }
361
-
362
- /**
363
- * Skips a token.
364
- * @param {string} expected Expected token
365
- * @param {boolean} [optional=false] Whether the token is optional
366
- * @returns {boolean} `true` when skipped, `false` if not
367
- * @throws {Error} When a required token is not present
368
- * @inner
369
- */
370
- function skip(expected, optional) {
371
- var actual = peek(),
372
- equals = actual === expected;
373
- if (equals) {
374
- next();
375
- return true;
376
- }
377
- if (!optional)
378
- throw illegal("token '" + actual + "', '" + expected + "' expected");
379
- return false;
380
- }
381
-
382
- /**
383
- * Gets a comment.
384
- * @param {number} [trailingLine] Line number if looking for a trailing comment
385
- * @returns {string|null} Comment text
386
- * @inner
387
- */
388
- function cmnt(trailingLine) {
389
- var ret = null;
390
- var comment;
391
- if (trailingLine === undefined) {
392
- comment = comments[line - 1];
393
- delete comments[line - 1];
394
- if (comment && (alternateCommentMode || comment.type === "*" || comment.lineEmpty)) {
395
- ret = comment.leading ? comment.text : null;
396
- }
397
- } else {
398
- /* istanbul ignore else */
399
- if (lastCommentLine < trailingLine) {
400
- peek();
401
- }
402
- comment = comments[trailingLine];
403
- delete comments[trailingLine];
404
- if (comment && !comment.lineEmpty && (alternateCommentMode || comment.type === "/")) {
405
- ret = comment.leading ? null : comment.text;
406
- }
407
- }
408
- return ret;
409
- }
410
-
411
- return Object.defineProperty({
412
- next: next,
413
- peek: peek,
414
- push: push,
415
- skip: skip,
416
- cmnt: cmnt
417
- }, "line", {
418
- get: function() { return line; }
419
- });
420
- /* eslint-enable callback-return */
421
- }
1
+ "use strict";
2
+ module.exports = tokenize;
3
+
4
+ var delimRe = /[\s{}=;:[\],'"()<>]/g,
5
+ stringDoubleRe = /(?:"([^"\\]*(?:\\.[^"\\]*)*)")/g,
6
+ stringSingleRe = /(?:'([^'\\]*(?:\\.[^'\\]*)*)')/g;
7
+
8
+ var setCommentRe = /^ *[*/]+ */,
9
+ setCommentAltRe = /^\s*\*?\/*/,
10
+ setCommentSplitRe = /\n/g,
11
+ whitespaceRe = /\s/,
12
+ unescapeRe = /\\(.?)/g;
13
+
14
+ var unescapeMap = {
15
+ "0": "\0",
16
+ "r": "\r",
17
+ "n": "\n",
18
+ "t": "\t"
19
+ };
20
+
21
+ /**
22
+ * Unescapes a string.
23
+ * @param {string} str String to unescape
24
+ * @returns {string} Unescaped string
25
+ * @property {Object.<string,string>} map Special characters map
26
+ * @memberof tokenize
27
+ */
28
+ function unescape(str) {
29
+ return str.replace(unescapeRe, function($0, $1) {
30
+ switch ($1) {
31
+ case "\\":
32
+ case "":
33
+ return $1;
34
+ default:
35
+ return unescapeMap[$1] || "";
36
+ }
37
+ });
38
+ }
39
+
40
+ tokenize.unescape = unescape;
41
+
42
+ /**
43
+ * Gets the next token and advances.
44
+ * @typedef TokenizerHandleNext
45
+ * @type {function}
46
+ * @returns {string|null} Next token or `null` on eof
47
+ */
48
+
49
+ /**
50
+ * Peeks for the next token.
51
+ * @typedef TokenizerHandlePeek
52
+ * @type {function}
53
+ * @returns {string|null} Next token or `null` on eof
54
+ */
55
+
56
+ /**
57
+ * Pushes a token back to the stack.
58
+ * @typedef TokenizerHandlePush
59
+ * @type {function}
60
+ * @param {string} token Token
61
+ * @returns {undefined}
62
+ */
63
+
64
+ /**
65
+ * Skips the next token.
66
+ * @typedef TokenizerHandleSkip
67
+ * @type {function}
68
+ * @param {string} expected Expected token
69
+ * @param {boolean} [optional=false] If optional
70
+ * @returns {boolean} Whether the token matched
71
+ * @throws {Error} If the token didn't match and is not optional
72
+ */
73
+
74
+ /**
75
+ * Gets the comment on the previous line or, alternatively, the line comment on the specified line.
76
+ * @typedef TokenizerHandleCmnt
77
+ * @type {function}
78
+ * @param {number} [line] Line number
79
+ * @returns {string|null} Comment text or `null` if none
80
+ */
81
+
82
+ /**
83
+ * Handle object returned from {@link tokenize}.
84
+ * @interface ITokenizerHandle
85
+ * @property {TokenizerHandleNext} next Gets the next token and advances (`null` on eof)
86
+ * @property {TokenizerHandlePeek} peek Peeks for the next token (`null` on eof)
87
+ * @property {TokenizerHandlePush} push Pushes a token back to the stack
88
+ * @property {TokenizerHandleSkip} skip Skips a token, returns its presence and advances or, if non-optional and not present, throws
89
+ * @property {TokenizerHandleCmnt} cmnt Gets the comment on the previous line or the line comment on the specified line, if any
90
+ * @property {number} line Current line number
91
+ */
92
+
93
+ /**
94
+ * Tokenizes the given .proto source and returns an object with useful utility functions.
95
+ * @param {string} source Source contents
96
+ * @param {boolean} alternateCommentMode Whether we should activate alternate comment parsing mode.
97
+ * @returns {ITokenizerHandle} Tokenizer handle
98
+ */
99
+ function tokenize(source, alternateCommentMode) {
100
+ /* eslint-disable callback-return */
101
+ source = source.toString();
102
+
103
+ var offset = 0,
104
+ length = source.length,
105
+ line = 1,
106
+ lastCommentLine = 0,
107
+ comments = {};
108
+
109
+ var stack = [];
110
+
111
+ var stringDelim = null;
112
+
113
+ /* istanbul ignore next */
114
+ /**
115
+ * Creates an error for illegal syntax.
116
+ * @param {string} subject Subject
117
+ * @returns {Error} Error created
118
+ * @inner
119
+ */
120
+ function illegal(subject) {
121
+ return Error("illegal " + subject + " (line " + line + ")");
122
+ }
123
+
124
+ /**
125
+ * Reads a string till its end.
126
+ * @returns {string} String read
127
+ * @inner
128
+ */
129
+ function readString() {
130
+ var re = stringDelim === "'" ? stringSingleRe : stringDoubleRe;
131
+ re.lastIndex = offset - 1;
132
+ var match = re.exec(source);
133
+ if (!match)
134
+ throw illegal("string");
135
+ offset = re.lastIndex;
136
+ push(stringDelim);
137
+ stringDelim = null;
138
+ return unescape(match[1]);
139
+ }
140
+
141
+ /**
142
+ * Gets the character at `pos` within the source.
143
+ * @param {number} pos Position
144
+ * @returns {string} Character
145
+ * @inner
146
+ */
147
+ function charAt(pos) {
148
+ return source.charAt(pos);
149
+ }
150
+
151
+ /**
152
+ * Sets the current comment text.
153
+ * @param {number} start Start offset
154
+ * @param {number} end End offset
155
+ * @param {boolean} isLeading set if a leading comment
156
+ * @returns {undefined}
157
+ * @inner
158
+ */
159
+ function setComment(start, end, isLeading) {
160
+ var comment = {
161
+ type: source.charAt(start++),
162
+ lineEmpty: false,
163
+ leading: isLeading,
164
+ };
165
+ var lookback;
166
+ if (alternateCommentMode) {
167
+ lookback = 2; // alternate comment parsing: "//" or "/*"
168
+ } else {
169
+ lookback = 3; // "///" or "/**"
170
+ }
171
+ var commentOffset = start - lookback,
172
+ c;
173
+ do {
174
+ if (--commentOffset < 0 ||
175
+ (c = source.charAt(commentOffset)) === "\n") {
176
+ comment.lineEmpty = true;
177
+ break;
178
+ }
179
+ } while (c === " " || c === "\t");
180
+ var lines = source
181
+ .substring(start, end)
182
+ .split(setCommentSplitRe);
183
+ for (var i = 0; i < lines.length; ++i)
184
+ lines[i] = lines[i]
185
+ .replace(alternateCommentMode ? setCommentAltRe : setCommentRe, "")
186
+ .trim();
187
+ comment.text = lines
188
+ .join("\n")
189
+ .trim();
190
+
191
+ comments[line] = comment;
192
+ lastCommentLine = line;
193
+ }
194
+
195
+ function isDoubleSlashCommentLine(startOffset) {
196
+ var endOffset = findEndOfLine(startOffset);
197
+
198
+ // see if remaining line matches comment pattern
199
+ var lineText = source.substring(startOffset, endOffset);
200
+ var isComment = /^\s*\/\//.test(lineText);
201
+ return isComment;
202
+ }
203
+
204
+ function findEndOfLine(cursor) {
205
+ // find end of cursor's line
206
+ var endOffset = cursor;
207
+ while (endOffset < length && charAt(endOffset) !== "\n") {
208
+ endOffset++;
209
+ }
210
+ return endOffset;
211
+ }
212
+
213
+ /**
214
+ * Obtains the next token.
215
+ * @returns {string|null} Next token or `null` on eof
216
+ * @inner
217
+ */
218
+ function next() {
219
+ if (stack.length > 0)
220
+ return stack.shift();
221
+ if (stringDelim)
222
+ return readString();
223
+ var repeat,
224
+ prev,
225
+ curr,
226
+ start,
227
+ isDoc,
228
+ nextLineIsComment,
229
+ isLeadingComment = offset === 0;
230
+ do {
231
+ if (offset === length)
232
+ return null;
233
+ repeat = false;
234
+ while (whitespaceRe.test(curr = charAt(offset))) {
235
+ if (curr === "\n") {
236
+ isLeadingComment = true;
237
+ ++line;
238
+ }
239
+ if (++offset === length)
240
+ return null;
241
+ }
242
+
243
+ if (charAt(offset) === "/") {
244
+ if (++offset === length) {
245
+ throw illegal("comment");
246
+ }
247
+ if (charAt(offset) === "/") { // Line
248
+ if (!alternateCommentMode) {
249
+ // check for triple-slash comment
250
+ isDoc = charAt(start = offset + 1) === "/";
251
+
252
+ while (charAt(++offset) !== "\n") {
253
+ if (offset === length) {
254
+ return null;
255
+ }
256
+ }
257
+ ++offset;
258
+ if (isDoc) {
259
+ setComment(start, offset - 1, isLeadingComment);
260
+ // Trailing comment cannot not be multi-line,
261
+ // so leading comment state should be reset to handle potential next comments
262
+ isLeadingComment = true;
263
+ }
264
+ ++line;
265
+ repeat = true;
266
+ } else {
267
+ // check for double-slash comments, consolidating consecutive lines
268
+ start = offset;
269
+ isDoc = false;
270
+ if (isDoubleSlashCommentLine(offset - 1)) {
271
+ isDoc = true;
272
+ do {
273
+ offset = findEndOfLine(offset);
274
+ if (offset === length) {
275
+ break;
276
+ }
277
+ offset++;
278
+ if (!isLeadingComment) {
279
+ // Trailing comment cannot not be multi-line
280
+ break;
281
+ }
282
+ nextLineIsComment = isDoubleSlashCommentLine(offset);
283
+ if (nextLineIsComment) {
284
+ line++;
285
+ }
286
+ } while (nextLineIsComment);
287
+ } else {
288
+ offset = Math.min(length, findEndOfLine(offset) + 1);
289
+ }
290
+ if (isDoc) {
291
+ setComment(start, offset, isLeadingComment);
292
+ isLeadingComment = true;
293
+ }
294
+ line++;
295
+ repeat = true;
296
+ }
297
+ } else if ((curr = charAt(offset)) === "*") { /* Block */
298
+ // check for /** (regular comment mode) or /* (alternate comment mode)
299
+ start = offset + 1;
300
+ isDoc = alternateCommentMode || charAt(start) === "*";
301
+ do {
302
+ if (curr === "\n") {
303
+ ++line;
304
+ }
305
+ if (++offset === length) {
306
+ throw illegal("comment");
307
+ }
308
+ prev = curr;
309
+ curr = charAt(offset);
310
+ } while (prev !== "*" || curr !== "/");
311
+ ++offset;
312
+ if (isDoc) {
313
+ setComment(start, offset - 2, isLeadingComment);
314
+ isLeadingComment = true;
315
+ }
316
+ repeat = true;
317
+ } else {
318
+ return "/";
319
+ }
320
+ }
321
+ } while (repeat);
322
+
323
+ // offset !== length if we got here
324
+
325
+ var end = offset;
326
+ delimRe.lastIndex = 0;
327
+ var delim = delimRe.test(charAt(end++));
328
+ if (!delim)
329
+ while (end < length && !delimRe.test(charAt(end)))
330
+ ++end;
331
+ var token = source.substring(offset, offset = end);
332
+ if (token === "\"" || token === "'")
333
+ stringDelim = token;
334
+ return token;
335
+ }
336
+
337
+ /**
338
+ * Pushes a token back to the stack.
339
+ * @param {string} token Token
340
+ * @returns {undefined}
341
+ * @inner
342
+ */
343
+ function push(token) {
344
+ stack.push(token);
345
+ }
346
+
347
+ /**
348
+ * Peeks for the next token.
349
+ * @returns {string|null} Token or `null` on eof
350
+ * @inner
351
+ */
352
+ function peek() {
353
+ if (!stack.length) {
354
+ var token = next();
355
+ if (token === null)
356
+ return null;
357
+ push(token);
358
+ }
359
+ return stack[0];
360
+ }
361
+
362
+ /**
363
+ * Skips a token.
364
+ * @param {string} expected Expected token
365
+ * @param {boolean} [optional=false] Whether the token is optional
366
+ * @returns {boolean} `true` when skipped, `false` if not
367
+ * @throws {Error} When a required token is not present
368
+ * @inner
369
+ */
370
+ function skip(expected, optional) {
371
+ var actual = peek(),
372
+ equals = actual === expected;
373
+ if (equals) {
374
+ next();
375
+ return true;
376
+ }
377
+ if (!optional)
378
+ throw illegal("token '" + actual + "', '" + expected + "' expected");
379
+ return false;
380
+ }
381
+
382
+ /**
383
+ * Gets a comment.
384
+ * @param {number} [trailingLine] Line number if looking for a trailing comment
385
+ * @returns {string|null} Comment text
386
+ * @inner
387
+ */
388
+ function cmnt(trailingLine) {
389
+ var ret = null;
390
+ var comment;
391
+ if (trailingLine === undefined) {
392
+ comment = comments[line - 1];
393
+ delete comments[line - 1];
394
+ if (comment && (alternateCommentMode || comment.type === "*" || comment.lineEmpty)) {
395
+ ret = comment.leading ? comment.text : null;
396
+ }
397
+ } else {
398
+ /* istanbul ignore else */
399
+ if (lastCommentLine < trailingLine) {
400
+ peek();
401
+ }
402
+ comment = comments[trailingLine];
403
+ delete comments[trailingLine];
404
+ if (comment && !comment.lineEmpty && (alternateCommentMode || comment.type === "/")) {
405
+ ret = comment.leading ? null : comment.text;
406
+ }
407
+ }
408
+ return ret;
409
+ }
410
+
411
+ return Object.defineProperty({
412
+ next: next,
413
+ peek: peek,
414
+ push: push,
415
+ skip: skip,
416
+ cmnt: cmnt
417
+ }, "line", {
418
+ get: function() { return line; }
419
+ });
420
+ /* eslint-enable callback-return */
421
+ }