UNPKG

alm

Version:

The best IDE for TypeScript

154 lines (153 loc) 5.96 kB
/** * From https://raw.githubusercontent.com/Microsoft/monaco-json/master/src/tokenization.ts * * Just redirected the jsonc-parser dependency 🌹 */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var json = require("./core/jsonc-parser"); function createTokenizationSupport(supportComments) { return { getInitialState: function () { return new JSONState(null, null, false); }, tokenize: function (line, state) { return tokenize(supportComments, line, state, undefined, undefined); } }; } exports.createTokenizationSupport = createTokenizationSupport; exports.TOKEN_DELIM_OBJECT = 'punctuation.bracket.json'; exports.TOKEN_DELIM_ARRAY = 'punctuation.array.json'; exports.TOKEN_DELIM_COLON = 'punctuation.colon.json'; exports.TOKEN_DELIM_COMMA = 'punctuation.comma.json'; exports.TOKEN_VALUE_BOOLEAN = 'support.property-value.keyword.json'; exports.TOKEN_VALUE_NULL = 'support.property-value.constant.other.json'; exports.TOKEN_VALUE_STRING = 'support.property-value.string.value.json'; exports.TOKEN_VALUE_NUMBER = 'support.property-value.constant.numeric.json'; exports.TOKEN_PROPERTY_NAME = 'support.type.property-name.json'; exports.TOKEN_COMMENT_BLOCK = 'comment.block.json'; exports.TOKEN_COMMENT_LINE = 'comment.line.json'; var JSONState = /** @class */ (function () { function JSONState(state, scanError, lastWasColon) { this._state = state; this.scanError = scanError; this.lastWasColon = lastWasColon; } JSONState.prototype.clone = function () { return new JSONState(this._state, this.scanError, this.lastWasColon); }; JSONState.prototype.equals = function (other) { if (other === this) { return true; } if (!other || !(other instanceof JSONState)) { return false; } return this.scanError === other.scanError && this.lastWasColon === other.lastWasColon; }; JSONState.prototype.getStateData = function () { return this._state; }; JSONState.prototype.setStateData = function (state) { this._state = state; }; return JSONState; }()); function tokenize(comments, line, state, offsetDelta, stopAtOffset) { if (offsetDelta === void 0) { offsetDelta = 0; } // handle multiline strings and block comments var numberOfInsertedCharacters = 0, adjustOffset = false; switch (state.scanError) { case json.ScanError.UnexpectedEndOfString: line = '"' + line; numberOfInsertedCharacters = 1; break; case json.ScanError.UnexpectedEndOfComment: line = '/*' + line; numberOfInsertedCharacters = 2; break; } var scanner = json.createScanner(line), kind, ret, lastWasColon = state.lastWasColon; ret = { tokens: [], endState: state.clone() }; while (true) { var offset = offsetDelta + scanner.getPosition(), type = ''; kind = scanner.scan(); if (kind === json.SyntaxKind.EOF) { break; } // Check that the scanner has advanced if (offset === offsetDelta + scanner.getPosition()) { throw new Error('Scanner did not advance, next 3 characters are: ' + line.substr(scanner.getPosition(), 3)); } // In case we inserted /* or " character, we need to // adjust the offset of all tokens (except the first) if (adjustOffset) { offset -= numberOfInsertedCharacters; } adjustOffset = numberOfInsertedCharacters > 0; // brackets and type switch (kind) { case json.SyntaxKind.OpenBraceToken: type = exports.TOKEN_DELIM_OBJECT; lastWasColon = false; break; case json.SyntaxKind.CloseBraceToken: type = exports.TOKEN_DELIM_OBJECT; lastWasColon = false; break; case json.SyntaxKind.OpenBracketToken: type = exports.TOKEN_DELIM_ARRAY; lastWasColon = false; break; case json.SyntaxKind.CloseBracketToken: type = exports.TOKEN_DELIM_ARRAY; lastWasColon = false; break; case json.SyntaxKind.ColonToken: type = exports.TOKEN_DELIM_COLON; lastWasColon = true; break; case json.SyntaxKind.CommaToken: type = exports.TOKEN_DELIM_COMMA; lastWasColon = false; break; case json.SyntaxKind.TrueKeyword: case json.SyntaxKind.FalseKeyword: type = exports.TOKEN_VALUE_BOOLEAN; lastWasColon = false; break; case json.SyntaxKind.NullKeyword: type = exports.TOKEN_VALUE_NULL; lastWasColon = false; break; case json.SyntaxKind.StringLiteral: type = lastWasColon ? exports.TOKEN_VALUE_STRING : exports.TOKEN_PROPERTY_NAME; lastWasColon = false; break; case json.SyntaxKind.NumericLiteral: type = exports.TOKEN_VALUE_NUMBER; lastWasColon = false; break; } // comments, iff enabled if (comments) { switch (kind) { case json.SyntaxKind.LineCommentTrivia: type = exports.TOKEN_COMMENT_LINE; break; case json.SyntaxKind.BlockCommentTrivia: type = exports.TOKEN_COMMENT_BLOCK; break; } } ret.endState = new JSONState(state.getStateData(), scanner.getTokenError(), lastWasColon); ret.tokens.push({ startIndex: offset, scopes: type }); } return ret; }