protobufjs 8.0.3-experimental → 8.0.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 (98) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +727 -727
  3. package/dist/light/protobuf.js +7724 -7352
  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 +2606 -2546
  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 +9588 -9131
  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/debug/README.md +4 -4
  16. package/ext/debug/index.js +71 -71
  17. package/ext/descriptor/README.md +72 -72
  18. package/ext/descriptor/index.d.ts +195 -191
  19. package/ext/descriptor/index.js +1181 -1052
  20. package/ext/descriptor/test.js +54 -54
  21. package/google/LICENSE +27 -27
  22. package/google/README.md +1 -1
  23. package/google/api/annotations.json +82 -82
  24. package/google/api/annotations.proto +10 -10
  25. package/google/api/http.json +85 -85
  26. package/google/api/http.proto +30 -30
  27. package/google/protobuf/api.json +117 -117
  28. package/google/protobuf/api.proto +33 -33
  29. package/google/protobuf/descriptor.json +1381 -738
  30. package/google/protobuf/descriptor.proto +535 -286
  31. package/google/protobuf/source_context.json +19 -19
  32. package/google/protobuf/source_context.proto +7 -7
  33. package/google/protobuf/type.json +201 -201
  34. package/google/protobuf/type.proto +89 -89
  35. package/index.d.ts +2817 -2792
  36. package/index.js +4 -4
  37. package/light.d.ts +2 -2
  38. package/light.js +3 -3
  39. package/minimal.d.ts +2 -2
  40. package/minimal.js +4 -4
  41. package/package.json +96 -112
  42. package/scripts/postinstall.js +32 -32
  43. package/src/common.js +399 -399
  44. package/src/converter.js +310 -301
  45. package/src/decoder.js +135 -127
  46. package/src/encoder.js +100 -100
  47. package/src/enum.js +226 -241
  48. package/src/field.js +453 -432
  49. package/src/index-light.js +104 -104
  50. package/src/index-minimal.js +36 -36
  51. package/src/index.js +12 -12
  52. package/src/mapfield.js +126 -126
  53. package/src/message.js +139 -139
  54. package/src/method.js +160 -160
  55. package/src/namespace.js +550 -434
  56. package/src/object.js +385 -330
  57. package/src/oneof.js +222 -222
  58. package/src/parse.js +1024 -944
  59. package/src/reader.js +426 -416
  60. package/src/reader_buffer.js +51 -51
  61. package/src/root.js +404 -384
  62. package/src/roots.js +17 -17
  63. package/src/rpc/service.js +142 -142
  64. package/src/rpc.js +36 -36
  65. package/src/service.js +193 -168
  66. package/src/tokenize.js +421 -416
  67. package/src/type.js +625 -590
  68. package/src/types.js +196 -196
  69. package/src/typescript.jsdoc +15 -15
  70. package/src/util/aspromise.d.ts +13 -0
  71. package/src/util/aspromise.js +52 -0
  72. package/src/util/base64.d.ts +32 -0
  73. package/src/util/base64.js +139 -0
  74. package/src/util/codegen.d.ts +31 -0
  75. package/src/util/codegen.js +113 -0
  76. package/src/util/eventemitter.d.ts +45 -0
  77. package/src/util/eventemitter.js +84 -0
  78. package/src/util/fetch.d.ts +56 -0
  79. package/src/util/fetch.js +114 -0
  80. package/src/util/float.d.ts +83 -0
  81. package/src/util/float.js +335 -0
  82. package/src/util/inquire.d.ts +9 -0
  83. package/src/util/inquire.js +37 -0
  84. package/src/util/longbits.js +200 -200
  85. package/src/util/minimal.js +461 -438
  86. package/src/util/path.d.ts +22 -0
  87. package/src/util/path.js +72 -0
  88. package/src/util/patterns.js +8 -0
  89. package/src/util/pool.d.ts +32 -0
  90. package/src/util/pool.js +48 -0
  91. package/src/util/utf8.d.ts +24 -0
  92. package/src/util/utf8.js +104 -0
  93. package/src/util.js +215 -213
  94. package/src/verifier.js +180 -177
  95. package/src/wrappers.js +103 -102
  96. package/src/writer.js +465 -465
  97. package/src/writer_buffer.js +85 -85
  98. package/tsconfig.json +8 -8
package/src/tokenize.js CHANGED
@@ -1,416 +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
- 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
+ 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
+ }