2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports"], factory);
9 })(function (require, exports) {
10 /*---------------------------------------------------------------------------------------------
11 * Copyright (c) Microsoft Corporation. All rights reserved.
12 * Licensed under the MIT License. See License.txt in the project root for license information.
13 *--------------------------------------------------------------------------------------------*/
15 Object.defineProperty(exports, "__esModule", { value: true });
16 exports.createScanner = void 0;
18 * Creates a JSON scanner on the given text.
19 * If ignoreTrivia is set, whitespaces or comments are ignored.
21 function createScanner(text, ignoreTrivia) {
22 if (ignoreTrivia === void 0) { ignoreTrivia = false; }
23 var len = text.length;
24 var pos = 0, value = '', tokenOffset = 0, token = 16 /* Unknown */, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0 /* None */;
25 function scanHexDigits(count, exact) {
28 while (digits < count || !exact) {
29 var ch = text.charCodeAt(pos);
30 if (ch >= 48 /* _0 */ && ch <= 57 /* _9 */) {
31 value = value * 16 + ch - 48 /* _0 */;
33 else if (ch >= 65 /* A */ && ch <= 70 /* F */) {
34 value = value * 16 + ch - 65 /* A */ + 10;
36 else if (ch >= 97 /* a */ && ch <= 102 /* f */) {
37 value = value * 16 + ch - 97 /* a */ + 10;
50 function setPosition(newPosition) {
54 token = 16 /* Unknown */;
55 scanError = 0 /* None */;
57 function scanNumber() {
59 if (text.charCodeAt(pos) === 48 /* _0 */) {
64 while (pos < text.length && isDigit(text.charCodeAt(pos))) {
68 if (pos < text.length && text.charCodeAt(pos) === 46 /* dot */) {
70 if (pos < text.length && isDigit(text.charCodeAt(pos))) {
72 while (pos < text.length && isDigit(text.charCodeAt(pos))) {
77 scanError = 3 /* UnexpectedEndOfNumber */;
78 return text.substring(start, pos);
82 if (pos < text.length && (text.charCodeAt(pos) === 69 /* E */ || text.charCodeAt(pos) === 101 /* e */)) {
84 if (pos < text.length && text.charCodeAt(pos) === 43 /* plus */ || text.charCodeAt(pos) === 45 /* minus */) {
87 if (pos < text.length && isDigit(text.charCodeAt(pos))) {
89 while (pos < text.length && isDigit(text.charCodeAt(pos))) {
95 scanError = 3 /* UnexpectedEndOfNumber */;
98 return text.substring(start, end);
100 function scanString() {
101 var result = '', start = pos;
104 result += text.substring(start, pos);
105 scanError = 2 /* UnexpectedEndOfString */;
108 var ch = text.charCodeAt(pos);
109 if (ch === 34 /* doubleQuote */) {
110 result += text.substring(start, pos);
114 if (ch === 92 /* backslash */) {
115 result += text.substring(start, pos);
118 scanError = 2 /* UnexpectedEndOfString */;
121 var ch2 = text.charCodeAt(pos++);
123 case 34 /* doubleQuote */:
126 case 92 /* backslash */:
148 var ch3 = scanHexDigits(4, true);
150 result += String.fromCharCode(ch3);
153 scanError = 4 /* InvalidUnicode */;
157 scanError = 5 /* InvalidEscapeCharacter */;
162 if (ch >= 0 && ch <= 0x1f) {
163 if (isLineBreak(ch)) {
164 result += text.substring(start, pos);
165 scanError = 2 /* UnexpectedEndOfString */;
169 scanError = 6 /* InvalidCharacter */;
170 // mark as error but continue with string
177 function scanNext() {
179 scanError = 0 /* None */;
181 lineStartOffset = lineNumber;
182 prevTokenLineStartOffset = tokenLineStartOffset;
186 return token = 17 /* EOF */;
188 var code = text.charCodeAt(pos);
189 // trivia: whitespace
190 if (isWhiteSpace(code)) {
193 value += String.fromCharCode(code);
194 code = text.charCodeAt(pos);
195 } while (isWhiteSpace(code));
196 return token = 15 /* Trivia */;
199 if (isLineBreak(code)) {
201 value += String.fromCharCode(code);
202 if (code === 13 /* carriageReturn */ && text.charCodeAt(pos) === 10 /* lineFeed */) {
207 tokenLineStartOffset = pos;
208 return token = 14 /* LineBreakTrivia */;
212 case 123 /* openBrace */:
214 return token = 1 /* OpenBraceToken */;
215 case 125 /* closeBrace */:
217 return token = 2 /* CloseBraceToken */;
218 case 91 /* openBracket */:
220 return token = 3 /* OpenBracketToken */;
221 case 93 /* closeBracket */:
223 return token = 4 /* CloseBracketToken */;
226 return token = 6 /* ColonToken */;
229 return token = 5 /* CommaToken */;
231 case 34 /* doubleQuote */:
233 value = scanString();
234 return token = 10 /* StringLiteral */;
238 // Single-line comment
239 if (text.charCodeAt(pos + 1) === 47 /* slash */) {
242 if (isLineBreak(text.charCodeAt(pos))) {
247 value = text.substring(start, pos);
248 return token = 12 /* LineCommentTrivia */;
250 // Multi-line comment
251 if (text.charCodeAt(pos + 1) === 42 /* asterisk */) {
253 var safeLength = len - 1; // For lookahead.
254 var commentClosed = false;
255 while (pos < safeLength) {
256 var ch = text.charCodeAt(pos);
257 if (ch === 42 /* asterisk */ && text.charCodeAt(pos + 1) === 47 /* slash */) {
259 commentClosed = true;
263 if (isLineBreak(ch)) {
264 if (ch === 13 /* carriageReturn */ && text.charCodeAt(pos) === 10 /* lineFeed */) {
268 tokenLineStartOffset = pos;
271 if (!commentClosed) {
273 scanError = 1 /* UnexpectedEndOfComment */;
275 value = text.substring(start, pos);
276 return token = 13 /* BlockCommentTrivia */;
278 // just a single slash
279 value += String.fromCharCode(code);
281 return token = 16 /* Unknown */;
284 value += String.fromCharCode(code);
286 if (pos === len || !isDigit(text.charCodeAt(pos))) {
287 return token = 16 /* Unknown */;
289 // found a minus, followed by a number so
290 // we fall through to proceed with scanning
302 value += scanNumber();
303 return token = 11 /* NumericLiteral */;
304 // literals and unknown symbols
306 // is a literal? Read the full word.
307 while (pos < len && isUnknownContentCharacter(code)) {
309 code = text.charCodeAt(pos);
311 if (tokenOffset !== pos) {
312 value = text.substring(tokenOffset, pos);
313 // keywords: true, false, null
315 case 'true': return token = 8 /* TrueKeyword */;
316 case 'false': return token = 9 /* FalseKeyword */;
317 case 'null': return token = 7 /* NullKeyword */;
319 return token = 16 /* Unknown */;
322 value += String.fromCharCode(code);
324 return token = 16 /* Unknown */;
327 function isUnknownContentCharacter(code) {
328 if (isWhiteSpace(code) || isLineBreak(code)) {
332 case 125 /* closeBrace */:
333 case 93 /* closeBracket */:
334 case 123 /* openBrace */:
335 case 91 /* openBracket */:
336 case 34 /* doubleQuote */:
344 function scanNextNonTrivia() {
348 } while (result >= 12 /* LineCommentTrivia */ && result <= 15 /* Trivia */);
352 setPosition: setPosition,
353 getPosition: function () { return pos; },
354 scan: ignoreTrivia ? scanNextNonTrivia : scanNext,
355 getToken: function () { return token; },
356 getTokenValue: function () { return value; },
357 getTokenOffset: function () { return tokenOffset; },
358 getTokenLength: function () { return pos - tokenOffset; },
359 getTokenStartLine: function () { return lineStartOffset; },
360 getTokenStartCharacter: function () { return tokenOffset - prevTokenLineStartOffset; },
361 getTokenError: function () { return scanError; },
364 exports.createScanner = createScanner;
365 function isWhiteSpace(ch) {
366 return ch === 32 /* space */ || ch === 9 /* tab */ || ch === 11 /* verticalTab */ || ch === 12 /* formFeed */ ||
367 ch === 160 /* nonBreakingSpace */ || ch === 5760 /* ogham */ || ch >= 8192 /* enQuad */ && ch <= 8203 /* zeroWidthSpace */ ||
368 ch === 8239 /* narrowNoBreakSpace */ || ch === 8287 /* mathematicalSpace */ || ch === 12288 /* ideographicSpace */ || ch === 65279 /* byteOrderMark */;
370 function isLineBreak(ch) {
371 return ch === 10 /* lineFeed */ || ch === 13 /* carriageReturn */ || ch === 8232 /* lineSeparator */ || ch === 8233 /* paragraphSeparator */;
373 function isDigit(ch) {
374 return ch >= 48 /* _0 */ && ch <= 57 /* _9 */;