protobufjs 7.3.2 → 7.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +727 -727
  3. package/ext/debug/README.md +4 -4
  4. package/ext/debug/index.js +71 -71
  5. package/ext/descriptor/README.md +72 -72
  6. package/ext/descriptor/index.d.ts +191 -191
  7. package/ext/descriptor/index.js +1052 -1052
  8. package/ext/descriptor/test.js +54 -54
  9. package/google/LICENSE +27 -27
  10. package/google/README.md +1 -1
  11. package/google/api/annotations.json +82 -82
  12. package/google/api/annotations.proto +10 -10
  13. package/google/api/http.json +85 -85
  14. package/google/api/http.proto +30 -30
  15. package/google/protobuf/api.json +117 -117
  16. package/google/protobuf/api.proto +33 -33
  17. package/google/protobuf/descriptor.json +738 -738
  18. package/google/protobuf/descriptor.proto +286 -286
  19. package/google/protobuf/source_context.json +19 -19
  20. package/google/protobuf/source_context.proto +7 -7
  21. package/google/protobuf/type.json +201 -201
  22. package/google/protobuf/type.proto +89 -89
  23. package/index.d.ts +2741 -2741
  24. package/index.js +4 -4
  25. package/light.d.ts +2 -2
  26. package/light.js +3 -3
  27. package/minimal.d.ts +2 -2
  28. package/minimal.js +4 -4
  29. package/package.json +111 -111
  30. package/scripts/postinstall.js +32 -32
  31. package/src/common.js +399 -399
  32. package/src/converter.js +301 -301
  33. package/src/decoder.js +129 -129
  34. package/src/encoder.js +100 -100
  35. package/src/enum.js +198 -198
  36. package/src/field.js +377 -377
  37. package/src/index-light.js +104 -104
  38. package/src/index-minimal.js +36 -36
  39. package/src/index.js +12 -12
  40. package/src/mapfield.js +126 -126
  41. package/src/message.js +138 -138
  42. package/src/method.js +160 -160
  43. package/src/namespace.js +433 -433
  44. package/src/object.js +243 -243
  45. package/src/oneof.js +203 -203
  46. package/src/parse.js +893 -889
  47. package/src/reader.js +416 -416
  48. package/src/reader_buffer.js +51 -51
  49. package/src/root.js +368 -368
  50. package/src/roots.js +18 -18
  51. package/src/rpc/service.js +142 -142
  52. package/src/rpc.js +36 -36
  53. package/src/service.js +167 -167
  54. package/src/tokenize.js +416 -416
  55. package/src/type.js +589 -589
  56. package/src/types.js +196 -196
  57. package/src/typescript.jsdoc +15 -15
  58. package/src/util/longbits.js +200 -200
  59. package/src/util/minimal.js +438 -438
  60. package/src/util.js +212 -212
  61. package/src/verifier.js +176 -176
  62. package/src/wrappers.js +102 -102
  63. package/src/writer.js +465 -465
  64. package/src/writer_buffer.js +85 -85
  65. package/tsconfig.json +7 -7
  66. package/dist/light/protobuf.js +0 -7381
  67. package/dist/light/protobuf.js.map +0 -1
  68. package/dist/light/protobuf.min.js +0 -8
  69. package/dist/light/protobuf.min.js.map +0 -1
  70. package/dist/minimal/protobuf.js +0 -2736
  71. package/dist/minimal/protobuf.js.map +0 -1
  72. package/dist/minimal/protobuf.min.js +0 -8
  73. package/dist/minimal/protobuf.min.js.map +0 -1
  74. package/dist/protobuf.js +0 -9105
  75. package/dist/protobuf.js.map +0 -1
  76. package/dist/protobuf.min.js +0 -8
  77. package/dist/protobuf.min.js.map +0 -1
package/src/tokenize.js CHANGED
@@ -1,416 +1,416 @@
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
- isLeadingComment = offset === 0;
229
- do {
230
- if (offset === length)
231
- return null;
232
- repeat = false;
233
- while (whitespaceRe.test(curr = charAt(offset))) {
234
- if (curr === "\n") {
235
- isLeadingComment = true;
236
- ++line;
237
- }
238
- if (++offset === length)
239
- return null;
240
- }
241
-
242
- if (charAt(offset) === "/") {
243
- if (++offset === length) {
244
- throw illegal("comment");
245
- }
246
- if (charAt(offset) === "/") { // Line
247
- if (!alternateCommentMode) {
248
- // check for triple-slash comment
249
- isDoc = charAt(start = offset + 1) === "/";
250
-
251
- while (charAt(++offset) !== "\n") {
252
- if (offset === length) {
253
- return null;
254
- }
255
- }
256
- ++offset;
257
- if (isDoc) {
258
- setComment(start, offset - 1, isLeadingComment);
259
- // Trailing comment cannot not be multi-line,
260
- // so leading comment state should be reset to handle potential next comments
261
- isLeadingComment = true;
262
- }
263
- ++line;
264
- repeat = true;
265
- } else {
266
- // check for double-slash comments, consolidating consecutive lines
267
- start = offset;
268
- isDoc = false;
269
- if (isDoubleSlashCommentLine(offset - 1)) {
270
- isDoc = true;
271
- do {
272
- offset = findEndOfLine(offset);
273
- if (offset === length) {
274
- break;
275
- }
276
- offset++;
277
- if (!isLeadingComment) {
278
- // Trailing comment cannot not be multi-line
279
- break;
280
- }
281
- } while (isDoubleSlashCommentLine(offset));
282
- } else {
283
- offset = Math.min(length, findEndOfLine(offset) + 1);
284
- }
285
- if (isDoc) {
286
- setComment(start, offset, isLeadingComment);
287
- isLeadingComment = true;
288
- }
289
- line++;
290
- repeat = true;
291
- }
292
- } else if ((curr = charAt(offset)) === "*") { /* Block */
293
- // check for /** (regular comment mode) or /* (alternate comment mode)
294
- start = offset + 1;
295
- isDoc = alternateCommentMode || charAt(start) === "*";
296
- do {
297
- if (curr === "\n") {
298
- ++line;
299
- }
300
- if (++offset === length) {
301
- throw illegal("comment");
302
- }
303
- prev = curr;
304
- curr = charAt(offset);
305
- } while (prev !== "*" || curr !== "/");
306
- ++offset;
307
- if (isDoc) {
308
- setComment(start, offset - 2, isLeadingComment);
309
- isLeadingComment = true;
310
- }
311
- repeat = true;
312
- } else {
313
- return "/";
314
- }
315
- }
316
- } while (repeat);
317
-
318
- // offset !== length if we got here
319
-
320
- var end = offset;
321
- delimRe.lastIndex = 0;
322
- var delim = delimRe.test(charAt(end++));
323
- if (!delim)
324
- while (end < length && !delimRe.test(charAt(end)))
325
- ++end;
326
- var token = source.substring(offset, offset = end);
327
- if (token === "\"" || token === "'")
328
- stringDelim = token;
329
- return token;
330
- }
331
-
332
- /**
333
- * Pushes a token back to the stack.
334
- * @param {string} token Token
335
- * @returns {undefined}
336
- * @inner
337
- */
338
- function push(token) {
339
- stack.push(token);
340
- }
341
-
342
- /**
343
- * Peeks for the next token.
344
- * @returns {string|null} Token or `null` on eof
345
- * @inner
346
- */
347
- function peek() {
348
- if (!stack.length) {
349
- var token = next();
350
- if (token === null)
351
- return null;
352
- push(token);
353
- }
354
- return stack[0];
355
- }
356
-
357
- /**
358
- * Skips a token.
359
- * @param {string} expected Expected token
360
- * @param {boolean} [optional=false] Whether the token is optional
361
- * @returns {boolean} `true` when skipped, `false` if not
362
- * @throws {Error} When a required token is not present
363
- * @inner
364
- */
365
- function skip(expected, optional) {
366
- var actual = peek(),
367
- equals = actual === expected;
368
- if (equals) {
369
- next();
370
- return true;
371
- }
372
- if (!optional)
373
- throw illegal("token '" + actual + "', '" + expected + "' expected");
374
- return false;
375
- }
376
-
377
- /**
378
- * Gets a comment.
379
- * @param {number} [trailingLine] Line number if looking for a trailing comment
380
- * @returns {string|null} Comment text
381
- * @inner
382
- */
383
- function cmnt(trailingLine) {
384
- var ret = null;
385
- var comment;
386
- if (trailingLine === undefined) {
387
- comment = comments[line - 1];
388
- delete comments[line - 1];
389
- if (comment && (alternateCommentMode || comment.type === "*" || comment.lineEmpty)) {
390
- ret = comment.leading ? comment.text : null;
391
- }
392
- } else {
393
- /* istanbul ignore else */
394
- if (lastCommentLine < trailingLine) {
395
- peek();
396
- }
397
- comment = comments[trailingLine];
398
- delete comments[trailingLine];
399
- if (comment && !comment.lineEmpty && (alternateCommentMode || comment.type === "/")) {
400
- ret = comment.leading ? null : comment.text;
401
- }
402
- }
403
- return ret;
404
- }
405
-
406
- return Object.defineProperty({
407
- next: next,
408
- peek: peek,
409
- push: push,
410
- skip: skip,
411
- cmnt: cmnt
412
- }, "line", {
413
- get: function() { return line; }
414
- });
415
- /* eslint-enable callback-return */
416
- }
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
+ isLeadingComment = offset === 0;
229
+ do {
230
+ if (offset === length)
231
+ return null;
232
+ repeat = false;
233
+ while (whitespaceRe.test(curr = charAt(offset))) {
234
+ if (curr === "\n") {
235
+ isLeadingComment = true;
236
+ ++line;
237
+ }
238
+ if (++offset === length)
239
+ return null;
240
+ }
241
+
242
+ if (charAt(offset) === "/") {
243
+ if (++offset === length) {
244
+ throw illegal("comment");
245
+ }
246
+ if (charAt(offset) === "/") { // Line
247
+ if (!alternateCommentMode) {
248
+ // check for triple-slash comment
249
+ isDoc = charAt(start = offset + 1) === "/";
250
+
251
+ while (charAt(++offset) !== "\n") {
252
+ if (offset === length) {
253
+ return null;
254
+ }
255
+ }
256
+ ++offset;
257
+ if (isDoc) {
258
+ setComment(start, offset - 1, isLeadingComment);
259
+ // Trailing comment cannot not be multi-line,
260
+ // so leading comment state should be reset to handle potential next comments
261
+ isLeadingComment = true;
262
+ }
263
+ ++line;
264
+ repeat = true;
265
+ } else {
266
+ // check for double-slash comments, consolidating consecutive lines
267
+ start = offset;
268
+ isDoc = false;
269
+ if (isDoubleSlashCommentLine(offset - 1)) {
270
+ isDoc = true;
271
+ do {
272
+ offset = findEndOfLine(offset);
273
+ if (offset === length) {
274
+ break;
275
+ }
276
+ offset++;
277
+ if (!isLeadingComment) {
278
+ // Trailing comment cannot not be multi-line
279
+ break;
280
+ }
281
+ } while (isDoubleSlashCommentLine(offset));
282
+ } else {
283
+ offset = Math.min(length, findEndOfLine(offset) + 1);
284
+ }
285
+ if (isDoc) {
286
+ setComment(start, offset, isLeadingComment);
287
+ isLeadingComment = true;
288
+ }
289
+ line++;
290
+ repeat = true;
291
+ }
292
+ } else if ((curr = charAt(offset)) === "*") { /* Block */
293
+ // check for /** (regular comment mode) or /* (alternate comment mode)
294
+ start = offset + 1;
295
+ isDoc = alternateCommentMode || charAt(start) === "*";
296
+ do {
297
+ if (curr === "\n") {
298
+ ++line;
299
+ }
300
+ if (++offset === length) {
301
+ throw illegal("comment");
302
+ }
303
+ prev = curr;
304
+ curr = charAt(offset);
305
+ } while (prev !== "*" || curr !== "/");
306
+ ++offset;
307
+ if (isDoc) {
308
+ setComment(start, offset - 2, isLeadingComment);
309
+ isLeadingComment = true;
310
+ }
311
+ repeat = true;
312
+ } else {
313
+ return "/";
314
+ }
315
+ }
316
+ } while (repeat);
317
+
318
+ // offset !== length if we got here
319
+
320
+ var end = offset;
321
+ delimRe.lastIndex = 0;
322
+ var delim = delimRe.test(charAt(end++));
323
+ if (!delim)
324
+ while (end < length && !delimRe.test(charAt(end)))
325
+ ++end;
326
+ var token = source.substring(offset, offset = end);
327
+ if (token === "\"" || token === "'")
328
+ stringDelim = token;
329
+ return token;
330
+ }
331
+
332
+ /**
333
+ * Pushes a token back to the stack.
334
+ * @param {string} token Token
335
+ * @returns {undefined}
336
+ * @inner
337
+ */
338
+ function push(token) {
339
+ stack.push(token);
340
+ }
341
+
342
+ /**
343
+ * Peeks for the next token.
344
+ * @returns {string|null} Token or `null` on eof
345
+ * @inner
346
+ */
347
+ function peek() {
348
+ if (!stack.length) {
349
+ var token = next();
350
+ if (token === null)
351
+ return null;
352
+ push(token);
353
+ }
354
+ return stack[0];
355
+ }
356
+
357
+ /**
358
+ * Skips a token.
359
+ * @param {string} expected Expected token
360
+ * @param {boolean} [optional=false] Whether the token is optional
361
+ * @returns {boolean} `true` when skipped, `false` if not
362
+ * @throws {Error} When a required token is not present
363
+ * @inner
364
+ */
365
+ function skip(expected, optional) {
366
+ var actual = peek(),
367
+ equals = actual === expected;
368
+ if (equals) {
369
+ next();
370
+ return true;
371
+ }
372
+ if (!optional)
373
+ throw illegal("token '" + actual + "', '" + expected + "' expected");
374
+ return false;
375
+ }
376
+
377
+ /**
378
+ * Gets a comment.
379
+ * @param {number} [trailingLine] Line number if looking for a trailing comment
380
+ * @returns {string|null} Comment text
381
+ * @inner
382
+ */
383
+ function cmnt(trailingLine) {
384
+ var ret = null;
385
+ var comment;
386
+ if (trailingLine === undefined) {
387
+ comment = comments[line - 1];
388
+ delete comments[line - 1];
389
+ if (comment && (alternateCommentMode || comment.type === "*" || comment.lineEmpty)) {
390
+ ret = comment.leading ? comment.text : null;
391
+ }
392
+ } else {
393
+ /* istanbul ignore else */
394
+ if (lastCommentLine < trailingLine) {
395
+ peek();
396
+ }
397
+ comment = comments[trailingLine];
398
+ delete comments[trailingLine];
399
+ if (comment && !comment.lineEmpty && (alternateCommentMode || comment.type === "/")) {
400
+ ret = comment.leading ? null : comment.text;
401
+ }
402
+ }
403
+ return ret;
404
+ }
405
+
406
+ return Object.defineProperty({
407
+ next: next,
408
+ peek: peek,
409
+ push: push,
410
+ skip: skip,
411
+ cmnt: cmnt
412
+ }, "line", {
413
+ get: function() { return line; }
414
+ });
415
+ /* eslint-enable callback-return */
416
+ }