Urara-Blog/node_modules/.pnpm-store/v3/files/07/358f527ecc773bc1de68c5bea180e73a0678f8b78176edda7e5250ffd33c40bc3f1a8aae5a9eb102d786ffe5b30b2fca67d5b2c32118ebcac6c350a5b3cf41
2022-08-14 01:14:53 +08:00

374 lines
16 KiB
Text

(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.createScanner = void 0;
/**
* Creates a JSON scanner on the given text.
* If ignoreTrivia is set, whitespaces or comments are ignored.
*/
function createScanner(text, ignoreTrivia) {
if (ignoreTrivia === void 0) { ignoreTrivia = false; }
var len = text.length;
var pos = 0, value = '', tokenOffset = 0, token = 16 /* SyntaxKind.Unknown */, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0 /* ScanError.None */;
function scanHexDigits(count, exact) {
var digits = 0;
var value = 0;
while (digits < count || !exact) {
var ch = text.charCodeAt(pos);
if (ch >= 48 /* CharacterCodes._0 */ && ch <= 57 /* CharacterCodes._9 */) {
value = value * 16 + ch - 48 /* CharacterCodes._0 */;
}
else if (ch >= 65 /* CharacterCodes.A */ && ch <= 70 /* CharacterCodes.F */) {
value = value * 16 + ch - 65 /* CharacterCodes.A */ + 10;
}
else if (ch >= 97 /* CharacterCodes.a */ && ch <= 102 /* CharacterCodes.f */) {
value = value * 16 + ch - 97 /* CharacterCodes.a */ + 10;
}
else {
break;
}
pos++;
digits++;
}
if (digits < count) {
value = -1;
}
return value;
}
function setPosition(newPosition) {
pos = newPosition;
value = '';
tokenOffset = 0;
token = 16 /* SyntaxKind.Unknown */;
scanError = 0 /* ScanError.None */;
}
function scanNumber() {
var start = pos;
if (text.charCodeAt(pos) === 48 /* CharacterCodes._0 */) {
pos++;
}
else {
pos++;
while (pos < text.length && isDigit(text.charCodeAt(pos))) {
pos++;
}
}
if (pos < text.length && text.charCodeAt(pos) === 46 /* CharacterCodes.dot */) {
pos++;
if (pos < text.length && isDigit(text.charCodeAt(pos))) {
pos++;
while (pos < text.length && isDigit(text.charCodeAt(pos))) {
pos++;
}
}
else {
scanError = 3 /* ScanError.UnexpectedEndOfNumber */;
return text.substring(start, pos);
}
}
var end = pos;
if (pos < text.length && (text.charCodeAt(pos) === 69 /* CharacterCodes.E */ || text.charCodeAt(pos) === 101 /* CharacterCodes.e */)) {
pos++;
if (pos < text.length && text.charCodeAt(pos) === 43 /* CharacterCodes.plus */ || text.charCodeAt(pos) === 45 /* CharacterCodes.minus */) {
pos++;
}
if (pos < text.length && isDigit(text.charCodeAt(pos))) {
pos++;
while (pos < text.length && isDigit(text.charCodeAt(pos))) {
pos++;
}
end = pos;
}
else {
scanError = 3 /* ScanError.UnexpectedEndOfNumber */;
}
}
return text.substring(start, end);
}
function scanString() {
var result = '', start = pos;
while (true) {
if (pos >= len) {
result += text.substring(start, pos);
scanError = 2 /* ScanError.UnexpectedEndOfString */;
break;
}
var ch = text.charCodeAt(pos);
if (ch === 34 /* CharacterCodes.doubleQuote */) {
result += text.substring(start, pos);
pos++;
break;
}
if (ch === 92 /* CharacterCodes.backslash */) {
result += text.substring(start, pos);
pos++;
if (pos >= len) {
scanError = 2 /* ScanError.UnexpectedEndOfString */;
break;
}
var ch2 = text.charCodeAt(pos++);
switch (ch2) {
case 34 /* CharacterCodes.doubleQuote */:
result += '\"';
break;
case 92 /* CharacterCodes.backslash */:
result += '\\';
break;
case 47 /* CharacterCodes.slash */:
result += '/';
break;
case 98 /* CharacterCodes.b */:
result += '\b';
break;
case 102 /* CharacterCodes.f */:
result += '\f';
break;
case 110 /* CharacterCodes.n */:
result += '\n';
break;
case 114 /* CharacterCodes.r */:
result += '\r';
break;
case 116 /* CharacterCodes.t */:
result += '\t';
break;
case 117 /* CharacterCodes.u */:
var ch3 = scanHexDigits(4, true);
if (ch3 >= 0) {
result += String.fromCharCode(ch3);
}
else {
scanError = 4 /* ScanError.InvalidUnicode */;
}
break;
default:
scanError = 5 /* ScanError.InvalidEscapeCharacter */;
}
start = pos;
continue;
}
if (ch >= 0 && ch <= 0x1f) {
if (isLineBreak(ch)) {
result += text.substring(start, pos);
scanError = 2 /* ScanError.UnexpectedEndOfString */;
break;
}
else {
scanError = 6 /* ScanError.InvalidCharacter */;
// mark as error but continue with string
}
}
pos++;
}
return result;
}
function scanNext() {
value = '';
scanError = 0 /* ScanError.None */;
tokenOffset = pos;
lineStartOffset = lineNumber;
prevTokenLineStartOffset = tokenLineStartOffset;
if (pos >= len) {
// at the end
tokenOffset = len;
return token = 17 /* SyntaxKind.EOF */;
}
var code = text.charCodeAt(pos);
// trivia: whitespace
if (isWhiteSpace(code)) {
do {
pos++;
value += String.fromCharCode(code);
code = text.charCodeAt(pos);
} while (isWhiteSpace(code));
return token = 15 /* SyntaxKind.Trivia */;
}
// trivia: newlines
if (isLineBreak(code)) {
pos++;
value += String.fromCharCode(code);
if (code === 13 /* CharacterCodes.carriageReturn */ && text.charCodeAt(pos) === 10 /* CharacterCodes.lineFeed */) {
pos++;
value += '\n';
}
lineNumber++;
tokenLineStartOffset = pos;
return token = 14 /* SyntaxKind.LineBreakTrivia */;
}
switch (code) {
// tokens: []{}:,
case 123 /* CharacterCodes.openBrace */:
pos++;
return token = 1 /* SyntaxKind.OpenBraceToken */;
case 125 /* CharacterCodes.closeBrace */:
pos++;
return token = 2 /* SyntaxKind.CloseBraceToken */;
case 91 /* CharacterCodes.openBracket */:
pos++;
return token = 3 /* SyntaxKind.OpenBracketToken */;
case 93 /* CharacterCodes.closeBracket */:
pos++;
return token = 4 /* SyntaxKind.CloseBracketToken */;
case 58 /* CharacterCodes.colon */:
pos++;
return token = 6 /* SyntaxKind.ColonToken */;
case 44 /* CharacterCodes.comma */:
pos++;
return token = 5 /* SyntaxKind.CommaToken */;
// strings
case 34 /* CharacterCodes.doubleQuote */:
pos++;
value = scanString();
return token = 10 /* SyntaxKind.StringLiteral */;
// comments
case 47 /* CharacterCodes.slash */:
var start = pos - 1;
// Single-line comment
if (text.charCodeAt(pos + 1) === 47 /* CharacterCodes.slash */) {
pos += 2;
while (pos < len) {
if (isLineBreak(text.charCodeAt(pos))) {
break;
}
pos++;
}
value = text.substring(start, pos);
return token = 12 /* SyntaxKind.LineCommentTrivia */;
}
// Multi-line comment
if (text.charCodeAt(pos + 1) === 42 /* CharacterCodes.asterisk */) {
pos += 2;
var safeLength = len - 1; // For lookahead.
var commentClosed = false;
while (pos < safeLength) {
var ch = text.charCodeAt(pos);
if (ch === 42 /* CharacterCodes.asterisk */ && text.charCodeAt(pos + 1) === 47 /* CharacterCodes.slash */) {
pos += 2;
commentClosed = true;
break;
}
pos++;
if (isLineBreak(ch)) {
if (ch === 13 /* CharacterCodes.carriageReturn */ && text.charCodeAt(pos) === 10 /* CharacterCodes.lineFeed */) {
pos++;
}
lineNumber++;
tokenLineStartOffset = pos;
}
}
if (!commentClosed) {
pos++;
scanError = 1 /* ScanError.UnexpectedEndOfComment */;
}
value = text.substring(start, pos);
return token = 13 /* SyntaxKind.BlockCommentTrivia */;
}
// just a single slash
value += String.fromCharCode(code);
pos++;
return token = 16 /* SyntaxKind.Unknown */;
// numbers
case 45 /* CharacterCodes.minus */:
value += String.fromCharCode(code);
pos++;
if (pos === len || !isDigit(text.charCodeAt(pos))) {
return token = 16 /* SyntaxKind.Unknown */;
}
// found a minus, followed by a number so
// we fall through to proceed with scanning
// numbers
case 48 /* CharacterCodes._0 */:
case 49 /* CharacterCodes._1 */:
case 50 /* CharacterCodes._2 */:
case 51 /* CharacterCodes._3 */:
case 52 /* CharacterCodes._4 */:
case 53 /* CharacterCodes._5 */:
case 54 /* CharacterCodes._6 */:
case 55 /* CharacterCodes._7 */:
case 56 /* CharacterCodes._8 */:
case 57 /* CharacterCodes._9 */:
value += scanNumber();
return token = 11 /* SyntaxKind.NumericLiteral */;
// literals and unknown symbols
default:
// is a literal? Read the full word.
while (pos < len && isUnknownContentCharacter(code)) {
pos++;
code = text.charCodeAt(pos);
}
if (tokenOffset !== pos) {
value = text.substring(tokenOffset, pos);
// keywords: true, false, null
switch (value) {
case 'true': return token = 8 /* SyntaxKind.TrueKeyword */;
case 'false': return token = 9 /* SyntaxKind.FalseKeyword */;
case 'null': return token = 7 /* SyntaxKind.NullKeyword */;
}
return token = 16 /* SyntaxKind.Unknown */;
}
// some
value += String.fromCharCode(code);
pos++;
return token = 16 /* SyntaxKind.Unknown */;
}
}
function isUnknownContentCharacter(code) {
if (isWhiteSpace(code) || isLineBreak(code)) {
return false;
}
switch (code) {
case 125 /* CharacterCodes.closeBrace */:
case 93 /* CharacterCodes.closeBracket */:
case 123 /* CharacterCodes.openBrace */:
case 91 /* CharacterCodes.openBracket */:
case 34 /* CharacterCodes.doubleQuote */:
case 58 /* CharacterCodes.colon */:
case 44 /* CharacterCodes.comma */:
case 47 /* CharacterCodes.slash */:
return false;
}
return true;
}
function scanNextNonTrivia() {
var result;
do {
result = scanNext();
} while (result >= 12 /* SyntaxKind.LineCommentTrivia */ && result <= 15 /* SyntaxKind.Trivia */);
return result;
}
return {
setPosition: setPosition,
getPosition: function () { return pos; },
scan: ignoreTrivia ? scanNextNonTrivia : scanNext,
getToken: function () { return token; },
getTokenValue: function () { return value; },
getTokenOffset: function () { return tokenOffset; },
getTokenLength: function () { return pos - tokenOffset; },
getTokenStartLine: function () { return lineStartOffset; },
getTokenStartCharacter: function () { return tokenOffset - prevTokenLineStartOffset; },
getTokenError: function () { return scanError; },
};
}
exports.createScanner = createScanner;
function isWhiteSpace(ch) {
return ch === 32 /* CharacterCodes.space */ || ch === 9 /* CharacterCodes.tab */;
}
function isLineBreak(ch) {
return ch === 10 /* CharacterCodes.lineFeed */ || ch === 13 /* CharacterCodes.carriageReturn */;
}
function isDigit(ch) {
return ch >= 48 /* CharacterCodes._0 */ && ch <= 57 /* CharacterCodes._9 */;
}
});