protobufjs 6.8.5 → 6.8.9

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 (103) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/LICENSE +39 -39
  3. package/README.md +879 -879
  4. package/cli/LICENSE +33 -33
  5. package/cli/README.md +11 -11
  6. package/cli/index.d.ts +3 -3
  7. package/cli/index.js +3 -3
  8. package/cli/lib/tsd-jsdoc/LICENSE +20 -20
  9. package/cli/lib/tsd-jsdoc/README.md +23 -23
  10. package/cli/lib/tsd-jsdoc/plugin.js +21 -21
  11. package/cli/lib/tsd-jsdoc/publish.js +693 -693
  12. package/cli/lib/tsd-jsdoc.json +18 -18
  13. package/cli/package.standalone.json +31 -31
  14. package/cli/pbjs.d.ts +9 -9
  15. package/cli/pbjs.js +329 -319
  16. package/cli/pbts.d.ts +9 -9
  17. package/cli/pbts.js +197 -168
  18. package/cli/targets/json-module.js +38 -38
  19. package/cli/targets/json.js +8 -8
  20. package/cli/targets/proto.js +326 -326
  21. package/cli/targets/proto2.js +10 -10
  22. package/cli/targets/proto3.js +10 -10
  23. package/cli/targets/static-module.js +29 -29
  24. package/cli/targets/static.js +702 -705
  25. package/cli/util.js +183 -183
  26. package/cli/wrappers/amd.js +7 -7
  27. package/cli/wrappers/closure.js +7 -7
  28. package/cli/wrappers/commonjs.js +7 -7
  29. package/cli/wrappers/default.js +15 -15
  30. package/cli/wrappers/es6.js +5 -5
  31. package/dist/README.md +31 -31
  32. package/dist/light/README.md +31 -31
  33. package/dist/light/protobuf.js +6031 -6011
  34. package/dist/light/protobuf.js.map +1 -1
  35. package/dist/light/protobuf.min.js +3 -3
  36. package/dist/light/protobuf.min.js.map +1 -1
  37. package/dist/minimal/README.md +31 -31
  38. package/dist/minimal/protobuf.js +1877 -1868
  39. package/dist/minimal/protobuf.js.map +1 -1
  40. package/dist/minimal/protobuf.min.js +3 -3
  41. package/dist/minimal/protobuf.min.js.map +1 -1
  42. package/dist/protobuf.js +7594 -7573
  43. package/dist/protobuf.js.map +1 -1
  44. package/dist/protobuf.min.js +3 -3
  45. package/dist/protobuf.min.js.map +1 -1
  46. package/ext/debug/README.md +4 -4
  47. package/ext/debug/index.js +71 -71
  48. package/ext/descriptor/README.md +72 -72
  49. package/ext/descriptor/index.d.ts +0 -1
  50. package/ext/descriptor/index.js +1052 -1049
  51. package/ext/descriptor/test.js +54 -54
  52. package/google/LICENSE +27 -27
  53. package/google/README.md +1 -1
  54. package/google/api/annotations.proto +10 -10
  55. package/google/protobuf/descriptor.proto +286 -286
  56. package/google/protobuf/source_context.proto +7 -7
  57. package/index.d.ts +3 -1
  58. package/index.js +4 -4
  59. package/light.d.ts +2 -2
  60. package/light.js +3 -3
  61. package/minimal.d.ts +2 -2
  62. package/minimal.js +4 -4
  63. package/package-lock.json +5810 -3344
  64. package/package.json +24 -30
  65. package/scripts/changelog.js +150 -150
  66. package/scripts/postinstall.js +35 -35
  67. package/src/common.js +399 -399
  68. package/src/converter.js +293 -287
  69. package/src/decoder.js +106 -106
  70. package/src/encoder.js +99 -99
  71. package/src/enum.js +181 -181
  72. package/src/field.js +371 -370
  73. package/src/index-light.js +104 -104
  74. package/src/index-minimal.js +36 -36
  75. package/src/index.js +12 -12
  76. package/src/mapfield.js +126 -126
  77. package/src/message.js +138 -138
  78. package/src/method.js +151 -151
  79. package/src/namespace.js +433 -431
  80. package/src/object.js +200 -199
  81. package/src/oneof.js +203 -203
  82. package/src/parse.js +758 -757
  83. package/src/reader.js +405 -407
  84. package/src/reader_buffer.js +44 -44
  85. package/src/root.js +351 -350
  86. package/src/roots.js +18 -18
  87. package/src/rpc/service.js +142 -142
  88. package/src/rpc.js +36 -36
  89. package/src/service.js +167 -167
  90. package/src/tokenize.js +397 -397
  91. package/src/type.js +589 -589
  92. package/src/types.js +196 -196
  93. package/src/typescript.jsdoc +15 -15
  94. package/src/util/longbits.js +200 -200
  95. package/src/util/minimal.js +414 -405
  96. package/src/util.js +178 -178
  97. package/src/verifier.js +176 -176
  98. package/src/wrappers.js +83 -83
  99. package/src/writer.js +459 -459
  100. package/src/writer_buffer.js +81 -81
  101. package/tsconfig.json +6 -6
  102. package/google/protobuf/field_mask.json +0 -21
  103. package/google/protobuf/field_mask.proto +0 -7
package/src/tokenize.js CHANGED
@@ -1,397 +1,397 @@
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
- commentType = null,
107
- commentText = null,
108
- commentLine = 0,
109
- commentLineEmpty = false;
110
-
111
- var stack = [];
112
-
113
- var stringDelim = null;
114
-
115
- /* istanbul ignore next */
116
- /**
117
- * Creates an error for illegal syntax.
118
- * @param {string} subject Subject
119
- * @returns {Error} Error created
120
- * @inner
121
- */
122
- function illegal(subject) {
123
- return Error("illegal " + subject + " (line " + line + ")");
124
- }
125
-
126
- /**
127
- * Reads a string till its end.
128
- * @returns {string} String read
129
- * @inner
130
- */
131
- function readString() {
132
- var re = stringDelim === "'" ? stringSingleRe : stringDoubleRe;
133
- re.lastIndex = offset - 1;
134
- var match = re.exec(source);
135
- if (!match)
136
- throw illegal("string");
137
- offset = re.lastIndex;
138
- push(stringDelim);
139
- stringDelim = null;
140
- return unescape(match[1]);
141
- }
142
-
143
- /**
144
- * Gets the character at `pos` within the source.
145
- * @param {number} pos Position
146
- * @returns {string} Character
147
- * @inner
148
- */
149
- function charAt(pos) {
150
- return source.charAt(pos);
151
- }
152
-
153
- /**
154
- * Sets the current comment text.
155
- * @param {number} start Start offset
156
- * @param {number} end End offset
157
- * @returns {undefined}
158
- * @inner
159
- */
160
- function setComment(start, end) {
161
- commentType = source.charAt(start++);
162
- commentLine = line;
163
- commentLineEmpty = false;
164
- var lookback;
165
- if (alternateCommentMode) {
166
- lookback = 2; // alternate comment parsing: "//" or "/*"
167
- } else {
168
- lookback = 3; // "///" or "/**"
169
- }
170
- var commentOffset = start - lookback,
171
- c;
172
- do {
173
- if (--commentOffset < 0 ||
174
- (c = source.charAt(commentOffset)) === "\n") {
175
- commentLineEmpty = true;
176
- break;
177
- }
178
- } while (c === " " || c === "\t");
179
- var lines = source
180
- .substring(start, end)
181
- .split(setCommentSplitRe);
182
- for (var i = 0; i < lines.length; ++i)
183
- lines[i] = lines[i]
184
- .replace(alternateCommentMode ? setCommentAltRe : setCommentRe, "")
185
- .trim();
186
- commentText = lines
187
- .join("\n")
188
- .trim();
189
- }
190
-
191
- function isDoubleSlashCommentLine(startOffset) {
192
- var endOffset = findEndOfLine(startOffset);
193
-
194
- // see if remaining line matches comment pattern
195
- var lineText = source.substring(startOffset, endOffset);
196
- // look for 1 or 2 slashes since startOffset would already point past
197
- // the first slash that started the comment.
198
- var isComment = /^\s*\/{1,2}/.test(lineText);
199
- return isComment;
200
- }
201
-
202
- function findEndOfLine(cursor) {
203
- // find end of cursor's line
204
- var endOffset = cursor;
205
- while (endOffset < length && charAt(endOffset) !== "\n") {
206
- endOffset++;
207
- }
208
- return endOffset;
209
- }
210
-
211
- /**
212
- * Obtains the next token.
213
- * @returns {string|null} Next token or `null` on eof
214
- * @inner
215
- */
216
- function next() {
217
- if (stack.length > 0)
218
- return stack.shift();
219
- if (stringDelim)
220
- return readString();
221
- var repeat,
222
- prev,
223
- curr,
224
- start,
225
- isDoc;
226
- do {
227
- if (offset === length)
228
- return null;
229
- repeat = false;
230
- while (whitespaceRe.test(curr = charAt(offset))) {
231
- if (curr === "\n")
232
- ++line;
233
- if (++offset === length)
234
- return null;
235
- }
236
-
237
- if (charAt(offset) === "/") {
238
- if (++offset === length) {
239
- throw illegal("comment");
240
- }
241
- if (charAt(offset) === "/") { // Line
242
- if (!alternateCommentMode) {
243
- // check for triple-slash comment
244
- isDoc = charAt(start = offset + 1) === "/";
245
-
246
- while (charAt(++offset) !== "\n") {
247
- if (offset === length) {
248
- return null;
249
- }
250
- }
251
- ++offset;
252
- if (isDoc) {
253
- setComment(start, offset - 1);
254
- }
255
- ++line;
256
- repeat = true;
257
- } else {
258
- // check for double-slash comments, consolidating consecutive lines
259
- start = offset;
260
- isDoc = false;
261
- if (isDoubleSlashCommentLine(offset)) {
262
- isDoc = true;
263
- do {
264
- offset = findEndOfLine(offset);
265
- if (offset === length) {
266
- break;
267
- }
268
- offset++;
269
- } while (isDoubleSlashCommentLine(offset));
270
- } else {
271
- offset = Math.min(length, findEndOfLine(offset) + 1);
272
- }
273
- if (isDoc) {
274
- setComment(start, offset);
275
- }
276
- line++;
277
- repeat = true;
278
- }
279
- } else if ((curr = charAt(offset)) === "*") { /* Block */
280
- // check for /** (regular comment mode) or /* (alternate comment mode)
281
- start = offset + 1;
282
- isDoc = alternateCommentMode || charAt(start) === "*";
283
- do {
284
- if (curr === "\n") {
285
- ++line;
286
- }
287
- if (++offset === length) {
288
- throw illegal("comment");
289
- }
290
- prev = curr;
291
- curr = charAt(offset);
292
- } while (prev !== "*" || curr !== "/");
293
- ++offset;
294
- if (isDoc) {
295
- setComment(start, offset - 2);
296
- }
297
- repeat = true;
298
- } else {
299
- return "/";
300
- }
301
- }
302
- } while (repeat);
303
-
304
- // offset !== length if we got here
305
-
306
- var end = offset;
307
- delimRe.lastIndex = 0;
308
- var delim = delimRe.test(charAt(end++));
309
- if (!delim)
310
- while (end < length && !delimRe.test(charAt(end)))
311
- ++end;
312
- var token = source.substring(offset, offset = end);
313
- if (token === "\"" || token === "'")
314
- stringDelim = token;
315
- return token;
316
- }
317
-
318
- /**
319
- * Pushes a token back to the stack.
320
- * @param {string} token Token
321
- * @returns {undefined}
322
- * @inner
323
- */
324
- function push(token) {
325
- stack.push(token);
326
- }
327
-
328
- /**
329
- * Peeks for the next token.
330
- * @returns {string|null} Token or `null` on eof
331
- * @inner
332
- */
333
- function peek() {
334
- if (!stack.length) {
335
- var token = next();
336
- if (token === null)
337
- return null;
338
- push(token);
339
- }
340
- return stack[0];
341
- }
342
-
343
- /**
344
- * Skips a token.
345
- * @param {string} expected Expected token
346
- * @param {boolean} [optional=false] Whether the token is optional
347
- * @returns {boolean} `true` when skipped, `false` if not
348
- * @throws {Error} When a required token is not present
349
- * @inner
350
- */
351
- function skip(expected, optional) {
352
- var actual = peek(),
353
- equals = actual === expected;
354
- if (equals) {
355
- next();
356
- return true;
357
- }
358
- if (!optional)
359
- throw illegal("token '" + actual + "', '" + expected + "' expected");
360
- return false;
361
- }
362
-
363
- /**
364
- * Gets a comment.
365
- * @param {number} [trailingLine] Line number if looking for a trailing comment
366
- * @returns {string|null} Comment text
367
- * @inner
368
- */
369
- function cmnt(trailingLine) {
370
- var ret = null;
371
- if (trailingLine === undefined) {
372
- if (commentLine === line - 1 && (alternateCommentMode || commentType === "*" || commentLineEmpty)) {
373
- ret = commentText;
374
- }
375
- } else {
376
- /* istanbul ignore else */
377
- if (commentLine < trailingLine) {
378
- peek();
379
- }
380
- if (commentLine === trailingLine && !commentLineEmpty && (alternateCommentMode || commentType === "/")) {
381
- ret = commentText;
382
- }
383
- }
384
- return ret;
385
- }
386
-
387
- return Object.defineProperty({
388
- next: next,
389
- peek: peek,
390
- push: push,
391
- skip: skip,
392
- cmnt: cmnt
393
- }, "line", {
394
- get: function() { return line; }
395
- });
396
- /* eslint-enable callback-return */
397
- }
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
+ commentType = null,
107
+ commentText = null,
108
+ commentLine = 0,
109
+ commentLineEmpty = false;
110
+
111
+ var stack = [];
112
+
113
+ var stringDelim = null;
114
+
115
+ /* istanbul ignore next */
116
+ /**
117
+ * Creates an error for illegal syntax.
118
+ * @param {string} subject Subject
119
+ * @returns {Error} Error created
120
+ * @inner
121
+ */
122
+ function illegal(subject) {
123
+ return Error("illegal " + subject + " (line " + line + ")");
124
+ }
125
+
126
+ /**
127
+ * Reads a string till its end.
128
+ * @returns {string} String read
129
+ * @inner
130
+ */
131
+ function readString() {
132
+ var re = stringDelim === "'" ? stringSingleRe : stringDoubleRe;
133
+ re.lastIndex = offset - 1;
134
+ var match = re.exec(source);
135
+ if (!match)
136
+ throw illegal("string");
137
+ offset = re.lastIndex;
138
+ push(stringDelim);
139
+ stringDelim = null;
140
+ return unescape(match[1]);
141
+ }
142
+
143
+ /**
144
+ * Gets the character at `pos` within the source.
145
+ * @param {number} pos Position
146
+ * @returns {string} Character
147
+ * @inner
148
+ */
149
+ function charAt(pos) {
150
+ return source.charAt(pos);
151
+ }
152
+
153
+ /**
154
+ * Sets the current comment text.
155
+ * @param {number} start Start offset
156
+ * @param {number} end End offset
157
+ * @returns {undefined}
158
+ * @inner
159
+ */
160
+ function setComment(start, end) {
161
+ commentType = source.charAt(start++);
162
+ commentLine = line;
163
+ commentLineEmpty = false;
164
+ var lookback;
165
+ if (alternateCommentMode) {
166
+ lookback = 2; // alternate comment parsing: "//" or "/*"
167
+ } else {
168
+ lookback = 3; // "///" or "/**"
169
+ }
170
+ var commentOffset = start - lookback,
171
+ c;
172
+ do {
173
+ if (--commentOffset < 0 ||
174
+ (c = source.charAt(commentOffset)) === "\n") {
175
+ commentLineEmpty = true;
176
+ break;
177
+ }
178
+ } while (c === " " || c === "\t");
179
+ var lines = source
180
+ .substring(start, end)
181
+ .split(setCommentSplitRe);
182
+ for (var i = 0; i < lines.length; ++i)
183
+ lines[i] = lines[i]
184
+ .replace(alternateCommentMode ? setCommentAltRe : setCommentRe, "")
185
+ .trim();
186
+ commentText = lines
187
+ .join("\n")
188
+ .trim();
189
+ }
190
+
191
+ function isDoubleSlashCommentLine(startOffset) {
192
+ var endOffset = findEndOfLine(startOffset);
193
+
194
+ // see if remaining line matches comment pattern
195
+ var lineText = source.substring(startOffset, endOffset);
196
+ // look for 1 or 2 slashes since startOffset would already point past
197
+ // the first slash that started the comment.
198
+ var isComment = /^\s*\/{1,2}/.test(lineText);
199
+ return isComment;
200
+ }
201
+
202
+ function findEndOfLine(cursor) {
203
+ // find end of cursor's line
204
+ var endOffset = cursor;
205
+ while (endOffset < length && charAt(endOffset) !== "\n") {
206
+ endOffset++;
207
+ }
208
+ return endOffset;
209
+ }
210
+
211
+ /**
212
+ * Obtains the next token.
213
+ * @returns {string|null} Next token or `null` on eof
214
+ * @inner
215
+ */
216
+ function next() {
217
+ if (stack.length > 0)
218
+ return stack.shift();
219
+ if (stringDelim)
220
+ return readString();
221
+ var repeat,
222
+ prev,
223
+ curr,
224
+ start,
225
+ isDoc;
226
+ do {
227
+ if (offset === length)
228
+ return null;
229
+ repeat = false;
230
+ while (whitespaceRe.test(curr = charAt(offset))) {
231
+ if (curr === "\n")
232
+ ++line;
233
+ if (++offset === length)
234
+ return null;
235
+ }
236
+
237
+ if (charAt(offset) === "/") {
238
+ if (++offset === length) {
239
+ throw illegal("comment");
240
+ }
241
+ if (charAt(offset) === "/") { // Line
242
+ if (!alternateCommentMode) {
243
+ // check for triple-slash comment
244
+ isDoc = charAt(start = offset + 1) === "/";
245
+
246
+ while (charAt(++offset) !== "\n") {
247
+ if (offset === length) {
248
+ return null;
249
+ }
250
+ }
251
+ ++offset;
252
+ if (isDoc) {
253
+ setComment(start, offset - 1);
254
+ }
255
+ ++line;
256
+ repeat = true;
257
+ } else {
258
+ // check for double-slash comments, consolidating consecutive lines
259
+ start = offset;
260
+ isDoc = false;
261
+ if (isDoubleSlashCommentLine(offset)) {
262
+ isDoc = true;
263
+ do {
264
+ offset = findEndOfLine(offset);
265
+ if (offset === length) {
266
+ break;
267
+ }
268
+ offset++;
269
+ } while (isDoubleSlashCommentLine(offset));
270
+ } else {
271
+ offset = Math.min(length, findEndOfLine(offset) + 1);
272
+ }
273
+ if (isDoc) {
274
+ setComment(start, offset);
275
+ }
276
+ line++;
277
+ repeat = true;
278
+ }
279
+ } else if ((curr = charAt(offset)) === "*") { /* Block */
280
+ // check for /** (regular comment mode) or /* (alternate comment mode)
281
+ start = offset + 1;
282
+ isDoc = alternateCommentMode || charAt(start) === "*";
283
+ do {
284
+ if (curr === "\n") {
285
+ ++line;
286
+ }
287
+ if (++offset === length) {
288
+ throw illegal("comment");
289
+ }
290
+ prev = curr;
291
+ curr = charAt(offset);
292
+ } while (prev !== "*" || curr !== "/");
293
+ ++offset;
294
+ if (isDoc) {
295
+ setComment(start, offset - 2);
296
+ }
297
+ repeat = true;
298
+ } else {
299
+ return "/";
300
+ }
301
+ }
302
+ } while (repeat);
303
+
304
+ // offset !== length if we got here
305
+
306
+ var end = offset;
307
+ delimRe.lastIndex = 0;
308
+ var delim = delimRe.test(charAt(end++));
309
+ if (!delim)
310
+ while (end < length && !delimRe.test(charAt(end)))
311
+ ++end;
312
+ var token = source.substring(offset, offset = end);
313
+ if (token === "\"" || token === "'")
314
+ stringDelim = token;
315
+ return token;
316
+ }
317
+
318
+ /**
319
+ * Pushes a token back to the stack.
320
+ * @param {string} token Token
321
+ * @returns {undefined}
322
+ * @inner
323
+ */
324
+ function push(token) {
325
+ stack.push(token);
326
+ }
327
+
328
+ /**
329
+ * Peeks for the next token.
330
+ * @returns {string|null} Token or `null` on eof
331
+ * @inner
332
+ */
333
+ function peek() {
334
+ if (!stack.length) {
335
+ var token = next();
336
+ if (token === null)
337
+ return null;
338
+ push(token);
339
+ }
340
+ return stack[0];
341
+ }
342
+
343
+ /**
344
+ * Skips a token.
345
+ * @param {string} expected Expected token
346
+ * @param {boolean} [optional=false] Whether the token is optional
347
+ * @returns {boolean} `true` when skipped, `false` if not
348
+ * @throws {Error} When a required token is not present
349
+ * @inner
350
+ */
351
+ function skip(expected, optional) {
352
+ var actual = peek(),
353
+ equals = actual === expected;
354
+ if (equals) {
355
+ next();
356
+ return true;
357
+ }
358
+ if (!optional)
359
+ throw illegal("token '" + actual + "', '" + expected + "' expected");
360
+ return false;
361
+ }
362
+
363
+ /**
364
+ * Gets a comment.
365
+ * @param {number} [trailingLine] Line number if looking for a trailing comment
366
+ * @returns {string|null} Comment text
367
+ * @inner
368
+ */
369
+ function cmnt(trailingLine) {
370
+ var ret = null;
371
+ if (trailingLine === undefined) {
372
+ if (commentLine === line - 1 && (alternateCommentMode || commentType === "*" || commentLineEmpty)) {
373
+ ret = commentText;
374
+ }
375
+ } else {
376
+ /* istanbul ignore else */
377
+ if (commentLine < trailingLine) {
378
+ peek();
379
+ }
380
+ if (commentLine === trailingLine && !commentLineEmpty && (alternateCommentMode || commentType === "/")) {
381
+ ret = commentText;
382
+ }
383
+ }
384
+ return ret;
385
+ }
386
+
387
+ return Object.defineProperty({
388
+ next: next,
389
+ peek: peek,
390
+ push: push,
391
+ skip: skip,
392
+ cmnt: cmnt
393
+ }, "line", {
394
+ get: function() { return line; }
395
+ });
396
+ /* eslint-enable callback-return */
397
+ }