jsonc-parser-umd

Code - github/microsoft/node-jsonc-parser

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/486088/1320058/jsonc-parser-umd.js

  1. // ==UserScript==
  2. // @name jsonc-parser-umd
  3. // @namespace flomk.userscripts
  4. // @version 3.2.1
  5. // @description Code - github/microsoft/node-jsonc-parser
  6. // @match *
  7. // @license MIT
  8. // ==/UserScript==
  9.  
  10. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jsoncParser = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  11. (function (factory) {
  12. if (typeof module === "object" && typeof module.exports === "object") {
  13. var v = factory(require, exports);
  14. if (v !== undefined) module.exports = v;
  15. }
  16. else if (typeof define === "function" && define.amd) {
  17. define(["require", "exports", "./format", "./parser"], factory);
  18. }
  19. })(function (require, exports) {
  20. /*---------------------------------------------------------------------------------------------
  21. * Copyright (c) Microsoft Corporation. All rights reserved.
  22. * Licensed under the MIT License. See License.txt in the project root for license information.
  23. *--------------------------------------------------------------------------------------------*/
  24. 'use strict';
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.isWS = exports.applyEdit = exports.setProperty = exports.removeProperty = void 0;
  27. const format_1 = require("./format");
  28. const parser_1 = require("./parser");
  29. function removeProperty(text, path, options) {
  30. return setProperty(text, path, void 0, options);
  31. }
  32. exports.removeProperty = removeProperty;
  33. function setProperty(text, originalPath, value, options) {
  34. const path = originalPath.slice();
  35. const errors = [];
  36. const root = (0, parser_1.parseTree)(text, errors);
  37. let parent = void 0;
  38. let lastSegment = void 0;
  39. while (path.length > 0) {
  40. lastSegment = path.pop();
  41. parent = (0, parser_1.findNodeAtLocation)(root, path);
  42. if (parent === void 0 && value !== void 0) {
  43. if (typeof lastSegment === 'string') {
  44. value = { [lastSegment]: value };
  45. }
  46. else {
  47. value = [value];
  48. }
  49. }
  50. else {
  51. break;
  52. }
  53. }
  54. if (!parent) {
  55. // empty document
  56. if (value === void 0) { // delete
  57. throw new Error('Can not delete in empty document');
  58. }
  59. return withFormatting(text, { offset: root ? root.offset : 0, length: root ? root.length : 0, content: JSON.stringify(value) }, options);
  60. }
  61. else if (parent.type === 'object' && typeof lastSegment === 'string' && Array.isArray(parent.children)) {
  62. const existing = (0, parser_1.findNodeAtLocation)(parent, [lastSegment]);
  63. if (existing !== void 0) {
  64. if (value === void 0) { // delete
  65. if (!existing.parent) {
  66. throw new Error('Malformed AST');
  67. }
  68. const propertyIndex = parent.children.indexOf(existing.parent);
  69. let removeBegin;
  70. let removeEnd = existing.parent.offset + existing.parent.length;
  71. if (propertyIndex > 0) {
  72. // remove the comma of the previous node
  73. let previous = parent.children[propertyIndex - 1];
  74. removeBegin = previous.offset + previous.length;
  75. }
  76. else {
  77. removeBegin = parent.offset + 1;
  78. if (parent.children.length > 1) {
  79. // remove the comma of the next node
  80. let next = parent.children[1];
  81. removeEnd = next.offset;
  82. }
  83. }
  84. return withFormatting(text, { offset: removeBegin, length: removeEnd - removeBegin, content: '' }, options);
  85. }
  86. else {
  87. // set value of existing property
  88. return withFormatting(text, { offset: existing.offset, length: existing.length, content: JSON.stringify(value) }, options);
  89. }
  90. }
  91. else {
  92. if (value === void 0) { // delete
  93. return []; // property does not exist, nothing to do
  94. }
  95. const newProperty = `${JSON.stringify(lastSegment)}: ${JSON.stringify(value)}`;
  96. const index = options.getInsertionIndex ? options.getInsertionIndex(parent.children.map(p => p.children[0].value)) : parent.children.length;
  97. let edit;
  98. if (index > 0) {
  99. let previous = parent.children[index - 1];
  100. edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };
  101. }
  102. else if (parent.children.length === 0) {
  103. edit = { offset: parent.offset + 1, length: 0, content: newProperty };
  104. }
  105. else {
  106. edit = { offset: parent.offset + 1, length: 0, content: newProperty + ',' };
  107. }
  108. return withFormatting(text, edit, options);
  109. }
  110. }
  111. else if (parent.type === 'array' && typeof lastSegment === 'number' && Array.isArray(parent.children)) {
  112. const insertIndex = lastSegment;
  113. if (insertIndex === -1) {
  114. // Insert
  115. const newProperty = `${JSON.stringify(value)}`;
  116. let edit;
  117. if (parent.children.length === 0) {
  118. edit = { offset: parent.offset + 1, length: 0, content: newProperty };
  119. }
  120. else {
  121. const previous = parent.children[parent.children.length - 1];
  122. edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };
  123. }
  124. return withFormatting(text, edit, options);
  125. }
  126. else if (value === void 0 && parent.children.length >= 0) {
  127. // Removal
  128. const removalIndex = lastSegment;
  129. const toRemove = parent.children[removalIndex];
  130. let edit;
  131. if (parent.children.length === 1) {
  132. // only item
  133. edit = { offset: parent.offset + 1, length: parent.length - 2, content: '' };
  134. }
  135. else if (parent.children.length - 1 === removalIndex) {
  136. // last item
  137. let previous = parent.children[removalIndex - 1];
  138. let offset = previous.offset + previous.length;
  139. let parentEndOffset = parent.offset + parent.length;
  140. edit = { offset, length: parentEndOffset - 2 - offset, content: '' };
  141. }
  142. else {
  143. edit = { offset: toRemove.offset, length: parent.children[removalIndex + 1].offset - toRemove.offset, content: '' };
  144. }
  145. return withFormatting(text, edit, options);
  146. }
  147. else if (value !== void 0) {
  148. let edit;
  149. const newProperty = `${JSON.stringify(value)}`;
  150. if (!options.isArrayInsertion && parent.children.length > lastSegment) {
  151. const toModify = parent.children[lastSegment];
  152. edit = { offset: toModify.offset, length: toModify.length, content: newProperty };
  153. }
  154. else if (parent.children.length === 0 || lastSegment === 0) {
  155. edit = { offset: parent.offset + 1, length: 0, content: parent.children.length === 0 ? newProperty : newProperty + ',' };
  156. }
  157. else {
  158. const index = lastSegment > parent.children.length ? parent.children.length : lastSegment;
  159. const previous = parent.children[index - 1];
  160. edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };
  161. }
  162. return withFormatting(text, edit, options);
  163. }
  164. else {
  165. throw new Error(`Can not ${value === void 0 ? 'remove' : (options.isArrayInsertion ? 'insert' : 'modify')} Array index ${insertIndex} as length is not sufficient`);
  166. }
  167. }
  168. else {
  169. throw new Error(`Can not add ${typeof lastSegment !== 'number' ? 'index' : 'property'} to parent of type ${parent.type}`);
  170. }
  171. }
  172. exports.setProperty = setProperty;
  173. function withFormatting(text, edit, options) {
  174. if (!options.formattingOptions) {
  175. return [edit];
  176. }
  177. // apply the edit
  178. let newText = applyEdit(text, edit);
  179. // format the new text
  180. let begin = edit.offset;
  181. let end = edit.offset + edit.content.length;
  182. if (edit.length === 0 || edit.content.length === 0) { // insert or remove
  183. while (begin > 0 && !(0, format_1.isEOL)(newText, begin - 1)) {
  184. begin--;
  185. }
  186. while (end < newText.length && !(0, format_1.isEOL)(newText, end)) {
  187. end++;
  188. }
  189. }
  190. const edits = (0, format_1.format)(newText, { offset: begin, length: end - begin }, { ...options.formattingOptions, keepLines: false });
  191. // apply the formatting edits and track the begin and end offsets of the changes
  192. for (let i = edits.length - 1; i >= 0; i--) {
  193. const edit = edits[i];
  194. newText = applyEdit(newText, edit);
  195. begin = Math.min(begin, edit.offset);
  196. end = Math.max(end, edit.offset + edit.length);
  197. end += edit.content.length - edit.length;
  198. }
  199. // create a single edit with all changes
  200. const editLength = text.length - (newText.length - end) - begin;
  201. return [{ offset: begin, length: editLength, content: newText.substring(begin, end) }];
  202. }
  203. function applyEdit(text, edit) {
  204. return text.substring(0, edit.offset) + edit.content + text.substring(edit.offset + edit.length);
  205. }
  206. exports.applyEdit = applyEdit;
  207. function isWS(text, offset) {
  208. return '\r\n \t'.indexOf(text.charAt(offset)) !== -1;
  209. }
  210. exports.isWS = isWS;
  211. });
  212.  
  213. },{"./format":2,"./parser":3}],2:[function(require,module,exports){
  214. (function (factory) {
  215. if (typeof module === "object" && typeof module.exports === "object") {
  216. var v = factory(require, exports);
  217. if (v !== undefined) module.exports = v;
  218. }
  219. else if (typeof define === "function" && define.amd) {
  220. define(["require", "exports", "./scanner", "./string-intern"], factory);
  221. }
  222. })(function (require, exports) {
  223. /*---------------------------------------------------------------------------------------------
  224. * Copyright (c) Microsoft Corporation. All rights reserved.
  225. * Licensed under the MIT License. See License.txt in the project root for license information.
  226. *--------------------------------------------------------------------------------------------*/
  227. 'use strict';
  228. Object.defineProperty(exports, "__esModule", { value: true });
  229. exports.isEOL = exports.format = void 0;
  230. const scanner_1 = require("./scanner");
  231. const string_intern_1 = require("./string-intern");
  232. function format(documentText, range, options) {
  233. let initialIndentLevel;
  234. let formatText;
  235. let formatTextStart;
  236. let rangeStart;
  237. let rangeEnd;
  238. if (range) {
  239. rangeStart = range.offset;
  240. rangeEnd = rangeStart + range.length;
  241. formatTextStart = rangeStart;
  242. while (formatTextStart > 0 && !isEOL(documentText, formatTextStart - 1)) {
  243. formatTextStart--;
  244. }
  245. let endOffset = rangeEnd;
  246. while (endOffset < documentText.length && !isEOL(documentText, endOffset)) {
  247. endOffset++;
  248. }
  249. formatText = documentText.substring(formatTextStart, endOffset);
  250. initialIndentLevel = computeIndentLevel(formatText, options);
  251. }
  252. else {
  253. formatText = documentText;
  254. initialIndentLevel = 0;
  255. formatTextStart = 0;
  256. rangeStart = 0;
  257. rangeEnd = documentText.length;
  258. }
  259. const eol = getEOL(options, documentText);
  260. const eolFastPathSupported = string_intern_1.supportedEols.includes(eol);
  261. let numberLineBreaks = 0;
  262. let indentLevel = 0;
  263. let indentValue;
  264. if (options.insertSpaces) {
  265. indentValue = string_intern_1.cachedSpaces[options.tabSize || 4] ?? repeat(string_intern_1.cachedSpaces[1], options.tabSize || 4);
  266. }
  267. else {
  268. indentValue = '\t';
  269. }
  270. const indentType = indentValue === '\t' ? '\t' : ' ';
  271. let scanner = (0, scanner_1.createScanner)(formatText, false);
  272. let hasError = false;
  273. function newLinesAndIndent() {
  274. if (numberLineBreaks > 1) {
  275. return repeat(eol, numberLineBreaks) + repeat(indentValue, initialIndentLevel + indentLevel);
  276. }
  277. const amountOfSpaces = indentValue.length * (initialIndentLevel + indentLevel);
  278. if (!eolFastPathSupported || amountOfSpaces > string_intern_1.cachedBreakLinesWithSpaces[indentType][eol].length) {
  279. return eol + repeat(indentValue, initialIndentLevel + indentLevel);
  280. }
  281. if (amountOfSpaces <= 0) {
  282. return eol;
  283. }
  284. return string_intern_1.cachedBreakLinesWithSpaces[indentType][eol][amountOfSpaces];
  285. }
  286. function scanNext() {
  287. let token = scanner.scan();
  288. numberLineBreaks = 0;
  289. while (token === 15 /* SyntaxKind.Trivia */ || token === 14 /* SyntaxKind.LineBreakTrivia */) {
  290. if (token === 14 /* SyntaxKind.LineBreakTrivia */ && options.keepLines) {
  291. numberLineBreaks += 1;
  292. }
  293. else if (token === 14 /* SyntaxKind.LineBreakTrivia */) {
  294. numberLineBreaks = 1;
  295. }
  296. token = scanner.scan();
  297. }
  298. hasError = token === 16 /* SyntaxKind.Unknown */ || scanner.getTokenError() !== 0 /* ScanError.None */;
  299. return token;
  300. }
  301. const editOperations = [];
  302. function addEdit(text, startOffset, endOffset) {
  303. if (!hasError && (!range || (startOffset < rangeEnd && endOffset > rangeStart)) && documentText.substring(startOffset, endOffset) !== text) {
  304. editOperations.push({ offset: startOffset, length: endOffset - startOffset, content: text });
  305. }
  306. }
  307. let firstToken = scanNext();
  308. if (options.keepLines && numberLineBreaks > 0) {
  309. addEdit(repeat(eol, numberLineBreaks), 0, 0);
  310. }
  311. if (firstToken !== 17 /* SyntaxKind.EOF */) {
  312. let firstTokenStart = scanner.getTokenOffset() + formatTextStart;
  313. let initialIndent = (indentValue.length * initialIndentLevel < 20) && options.insertSpaces
  314. ? string_intern_1.cachedSpaces[indentValue.length * initialIndentLevel]
  315. : repeat(indentValue, initialIndentLevel);
  316. addEdit(initialIndent, formatTextStart, firstTokenStart);
  317. }
  318. while (firstToken !== 17 /* SyntaxKind.EOF */) {
  319. let firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart;
  320. let secondToken = scanNext();
  321. let replaceContent = '';
  322. let needsLineBreak = false;
  323. while (numberLineBreaks === 0 && (secondToken === 12 /* SyntaxKind.LineCommentTrivia */ || secondToken === 13 /* SyntaxKind.BlockCommentTrivia */)) {
  324. let commentTokenStart = scanner.getTokenOffset() + formatTextStart;
  325. addEdit(string_intern_1.cachedSpaces[1], firstTokenEnd, commentTokenStart);
  326. firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart;
  327. needsLineBreak = secondToken === 12 /* SyntaxKind.LineCommentTrivia */;
  328. replaceContent = needsLineBreak ? newLinesAndIndent() : '';
  329. secondToken = scanNext();
  330. }
  331. if (secondToken === 2 /* SyntaxKind.CloseBraceToken */) {
  332. if (firstToken !== 1 /* SyntaxKind.OpenBraceToken */) {
  333. indentLevel--;
  334. }
  335. ;
  336. if (options.keepLines && numberLineBreaks > 0 || !options.keepLines && firstToken !== 1 /* SyntaxKind.OpenBraceToken */) {
  337. replaceContent = newLinesAndIndent();
  338. }
  339. else if (options.keepLines) {
  340. replaceContent = string_intern_1.cachedSpaces[1];
  341. }
  342. }
  343. else if (secondToken === 4 /* SyntaxKind.CloseBracketToken */) {
  344. if (firstToken !== 3 /* SyntaxKind.OpenBracketToken */) {
  345. indentLevel--;
  346. }
  347. ;
  348. if (options.keepLines && numberLineBreaks > 0 || !options.keepLines && firstToken !== 3 /* SyntaxKind.OpenBracketToken */) {
  349. replaceContent = newLinesAndIndent();
  350. }
  351. else if (options.keepLines) {
  352. replaceContent = string_intern_1.cachedSpaces[1];
  353. }
  354. }
  355. else {
  356. switch (firstToken) {
  357. case 3 /* SyntaxKind.OpenBracketToken */:
  358. case 1 /* SyntaxKind.OpenBraceToken */:
  359. indentLevel++;
  360. if (options.keepLines && numberLineBreaks > 0 || !options.keepLines) {
  361. replaceContent = newLinesAndIndent();
  362. }
  363. else {
  364. replaceContent = string_intern_1.cachedSpaces[1];
  365. }
  366. break;
  367. case 5 /* SyntaxKind.CommaToken */:
  368. if (options.keepLines && numberLineBreaks > 0 || !options.keepLines) {
  369. replaceContent = newLinesAndIndent();
  370. }
  371. else {
  372. replaceContent = string_intern_1.cachedSpaces[1];
  373. }
  374. break;
  375. case 12 /* SyntaxKind.LineCommentTrivia */:
  376. replaceContent = newLinesAndIndent();
  377. break;
  378. case 13 /* SyntaxKind.BlockCommentTrivia */:
  379. if (numberLineBreaks > 0) {
  380. replaceContent = newLinesAndIndent();
  381. }
  382. else if (!needsLineBreak) {
  383. replaceContent = string_intern_1.cachedSpaces[1];
  384. }
  385. break;
  386. case 6 /* SyntaxKind.ColonToken */:
  387. if (options.keepLines && numberLineBreaks > 0) {
  388. replaceContent = newLinesAndIndent();
  389. }
  390. else if (!needsLineBreak) {
  391. replaceContent = string_intern_1.cachedSpaces[1];
  392. }
  393. break;
  394. case 10 /* SyntaxKind.StringLiteral */:
  395. if (options.keepLines && numberLineBreaks > 0) {
  396. replaceContent = newLinesAndIndent();
  397. }
  398. else if (secondToken === 6 /* SyntaxKind.ColonToken */ && !needsLineBreak) {
  399. replaceContent = '';
  400. }
  401. break;
  402. case 7 /* SyntaxKind.NullKeyword */:
  403. case 8 /* SyntaxKind.TrueKeyword */:
  404. case 9 /* SyntaxKind.FalseKeyword */:
  405. case 11 /* SyntaxKind.NumericLiteral */:
  406. case 2 /* SyntaxKind.CloseBraceToken */:
  407. case 4 /* SyntaxKind.CloseBracketToken */:
  408. if (options.keepLines && numberLineBreaks > 0) {
  409. replaceContent = newLinesAndIndent();
  410. }
  411. else {
  412. if ((secondToken === 12 /* SyntaxKind.LineCommentTrivia */ || secondToken === 13 /* SyntaxKind.BlockCommentTrivia */) && !needsLineBreak) {
  413. replaceContent = string_intern_1.cachedSpaces[1];
  414. }
  415. else if (secondToken !== 5 /* SyntaxKind.CommaToken */ && secondToken !== 17 /* SyntaxKind.EOF */) {
  416. hasError = true;
  417. }
  418. }
  419. break;
  420. case 16 /* SyntaxKind.Unknown */:
  421. hasError = true;
  422. break;
  423. }
  424. if (numberLineBreaks > 0 && (secondToken === 12 /* SyntaxKind.LineCommentTrivia */ || secondToken === 13 /* SyntaxKind.BlockCommentTrivia */)) {
  425. replaceContent = newLinesAndIndent();
  426. }
  427. }
  428. if (secondToken === 17 /* SyntaxKind.EOF */) {
  429. if (options.keepLines && numberLineBreaks > 0) {
  430. replaceContent = newLinesAndIndent();
  431. }
  432. else {
  433. replaceContent = options.insertFinalNewline ? eol : '';
  434. }
  435. }
  436. const secondTokenStart = scanner.getTokenOffset() + formatTextStart;
  437. addEdit(replaceContent, firstTokenEnd, secondTokenStart);
  438. firstToken = secondToken;
  439. }
  440. return editOperations;
  441. }
  442. exports.format = format;
  443. function repeat(s, count) {
  444. let result = '';
  445. for (let i = 0; i < count; i++) {
  446. result += s;
  447. }
  448. return result;
  449. }
  450. function computeIndentLevel(content, options) {
  451. let i = 0;
  452. let nChars = 0;
  453. const tabSize = options.tabSize || 4;
  454. while (i < content.length) {
  455. let ch = content.charAt(i);
  456. if (ch === string_intern_1.cachedSpaces[1]) {
  457. nChars++;
  458. }
  459. else if (ch === '\t') {
  460. nChars += tabSize;
  461. }
  462. else {
  463. break;
  464. }
  465. i++;
  466. }
  467. return Math.floor(nChars / tabSize);
  468. }
  469. function getEOL(options, text) {
  470. for (let i = 0; i < text.length; i++) {
  471. const ch = text.charAt(i);
  472. if (ch === '\r') {
  473. if (i + 1 < text.length && text.charAt(i + 1) === '\n') {
  474. return '\r\n';
  475. }
  476. return '\r';
  477. }
  478. else if (ch === '\n') {
  479. return '\n';
  480. }
  481. }
  482. return (options && options.eol) || '\n';
  483. }
  484. function isEOL(text, offset) {
  485. return '\r\n'.indexOf(text.charAt(offset)) !== -1;
  486. }
  487. exports.isEOL = isEOL;
  488. });
  489.  
  490. },{"./scanner":4,"./string-intern":5}],3:[function(require,module,exports){
  491. (function (factory) {
  492. if (typeof module === "object" && typeof module.exports === "object") {
  493. var v = factory(require, exports);
  494. if (v !== undefined) module.exports = v;
  495. }
  496. else if (typeof define === "function" && define.amd) {
  497. define(["require", "exports", "./scanner"], factory);
  498. }
  499. })(function (require, exports) {
  500. /*---------------------------------------------------------------------------------------------
  501. * Copyright (c) Microsoft Corporation. All rights reserved.
  502. * Licensed under the MIT License. See License.txt in the project root for license information.
  503. *--------------------------------------------------------------------------------------------*/
  504. 'use strict';
  505. Object.defineProperty(exports, "__esModule", { value: true });
  506. exports.getNodeType = exports.stripComments = exports.visit = exports.findNodeAtOffset = exports.contains = exports.getNodeValue = exports.getNodePath = exports.findNodeAtLocation = exports.parseTree = exports.parse = exports.getLocation = void 0;
  507. const scanner_1 = require("./scanner");
  508. var ParseOptions;
  509. (function (ParseOptions) {
  510. ParseOptions.DEFAULT = {
  511. allowTrailingComma: false
  512. };
  513. })(ParseOptions || (ParseOptions = {}));
  514. /**
  515. * For a given offset, evaluate the location in the JSON document. Each segment in the location path is either a property name or an array index.
  516. */
  517. function getLocation(text, position) {
  518. const segments = []; // strings or numbers
  519. const earlyReturnException = new Object();
  520. let previousNode = undefined;
  521. const previousNodeInst = {
  522. value: {},
  523. offset: 0,
  524. length: 0,
  525. type: 'object',
  526. parent: undefined
  527. };
  528. let isAtPropertyKey = false;
  529. function setPreviousNode(value, offset, length, type) {
  530. previousNodeInst.value = value;
  531. previousNodeInst.offset = offset;
  532. previousNodeInst.length = length;
  533. previousNodeInst.type = type;
  534. previousNodeInst.colonOffset = undefined;
  535. previousNode = previousNodeInst;
  536. }
  537. try {
  538. visit(text, {
  539. onObjectBegin: (offset, length) => {
  540. if (position <= offset) {
  541. throw earlyReturnException;
  542. }
  543. previousNode = undefined;
  544. isAtPropertyKey = position > offset;
  545. segments.push(''); // push a placeholder (will be replaced)
  546. },
  547. onObjectProperty: (name, offset, length) => {
  548. if (position < offset) {
  549. throw earlyReturnException;
  550. }
  551. setPreviousNode(name, offset, length, 'property');
  552. segments[segments.length - 1] = name;
  553. if (position <= offset + length) {
  554. throw earlyReturnException;
  555. }
  556. },
  557. onObjectEnd: (offset, length) => {
  558. if (position <= offset) {
  559. throw earlyReturnException;
  560. }
  561. previousNode = undefined;
  562. segments.pop();
  563. },
  564. onArrayBegin: (offset, length) => {
  565. if (position <= offset) {
  566. throw earlyReturnException;
  567. }
  568. previousNode = undefined;
  569. segments.push(0);
  570. },
  571. onArrayEnd: (offset, length) => {
  572. if (position <= offset) {
  573. throw earlyReturnException;
  574. }
  575. previousNode = undefined;
  576. segments.pop();
  577. },
  578. onLiteralValue: (value, offset, length) => {
  579. if (position < offset) {
  580. throw earlyReturnException;
  581. }
  582. setPreviousNode(value, offset, length, getNodeType(value));
  583. if (position <= offset + length) {
  584. throw earlyReturnException;
  585. }
  586. },
  587. onSeparator: (sep, offset, length) => {
  588. if (position <= offset) {
  589. throw earlyReturnException;
  590. }
  591. if (sep === ':' && previousNode && previousNode.type === 'property') {
  592. previousNode.colonOffset = offset;
  593. isAtPropertyKey = false;
  594. previousNode = undefined;
  595. }
  596. else if (sep === ',') {
  597. const last = segments[segments.length - 1];
  598. if (typeof last === 'number') {
  599. segments[segments.length - 1] = last + 1;
  600. }
  601. else {
  602. isAtPropertyKey = true;
  603. segments[segments.length - 1] = '';
  604. }
  605. previousNode = undefined;
  606. }
  607. }
  608. });
  609. }
  610. catch (e) {
  611. if (e !== earlyReturnException) {
  612. throw e;
  613. }
  614. }
  615. return {
  616. path: segments,
  617. previousNode,
  618. isAtPropertyKey,
  619. matches: (pattern) => {
  620. let k = 0;
  621. for (let i = 0; k < pattern.length && i < segments.length; i++) {
  622. if (pattern[k] === segments[i] || pattern[k] === '*') {
  623. k++;
  624. }
  625. else if (pattern[k] !== '**') {
  626. return false;
  627. }
  628. }
  629. return k === pattern.length;
  630. }
  631. };
  632. }
  633. exports.getLocation = getLocation;
  634. /**
  635. * Parses the given text and returns the object the JSON content represents. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
  636. * Therefore always check the errors list to find out if the input was valid.
  637. */
  638. function parse(text, errors = [], options = ParseOptions.DEFAULT) {
  639. let currentProperty = null;
  640. let currentParent = [];
  641. const previousParents = [];
  642. function onValue(value) {
  643. if (Array.isArray(currentParent)) {
  644. currentParent.push(value);
  645. }
  646. else if (currentProperty !== null) {
  647. currentParent[currentProperty] = value;
  648. }
  649. }
  650. const visitor = {
  651. onObjectBegin: () => {
  652. const object = {};
  653. onValue(object);
  654. previousParents.push(currentParent);
  655. currentParent = object;
  656. currentProperty = null;
  657. },
  658. onObjectProperty: (name) => {
  659. currentProperty = name;
  660. },
  661. onObjectEnd: () => {
  662. currentParent = previousParents.pop();
  663. },
  664. onArrayBegin: () => {
  665. const array = [];
  666. onValue(array);
  667. previousParents.push(currentParent);
  668. currentParent = array;
  669. currentProperty = null;
  670. },
  671. onArrayEnd: () => {
  672. currentParent = previousParents.pop();
  673. },
  674. onLiteralValue: onValue,
  675. onError: (error, offset, length) => {
  676. errors.push({ error, offset, length });
  677. }
  678. };
  679. visit(text, visitor, options);
  680. return currentParent[0];
  681. }
  682. exports.parse = parse;
  683. /**
  684. * Parses the given text and returns a tree representation the JSON content. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
  685. */
  686. function parseTree(text, errors = [], options = ParseOptions.DEFAULT) {
  687. let currentParent = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root
  688. function ensurePropertyComplete(endOffset) {
  689. if (currentParent.type === 'property') {
  690. currentParent.length = endOffset - currentParent.offset;
  691. currentParent = currentParent.parent;
  692. }
  693. }
  694. function onValue(valueNode) {
  695. currentParent.children.push(valueNode);
  696. return valueNode;
  697. }
  698. const visitor = {
  699. onObjectBegin: (offset) => {
  700. currentParent = onValue({ type: 'object', offset, length: -1, parent: currentParent, children: [] });
  701. },
  702. onObjectProperty: (name, offset, length) => {
  703. currentParent = onValue({ type: 'property', offset, length: -1, parent: currentParent, children: [] });
  704. currentParent.children.push({ type: 'string', value: name, offset, length, parent: currentParent });
  705. },
  706. onObjectEnd: (offset, length) => {
  707. ensurePropertyComplete(offset + length); // in case of a missing value for a property: make sure property is complete
  708. currentParent.length = offset + length - currentParent.offset;
  709. currentParent = currentParent.parent;
  710. ensurePropertyComplete(offset + length);
  711. },
  712. onArrayBegin: (offset, length) => {
  713. currentParent = onValue({ type: 'array', offset, length: -1, parent: currentParent, children: [] });
  714. },
  715. onArrayEnd: (offset, length) => {
  716. currentParent.length = offset + length - currentParent.offset;
  717. currentParent = currentParent.parent;
  718. ensurePropertyComplete(offset + length);
  719. },
  720. onLiteralValue: (value, offset, length) => {
  721. onValue({ type: getNodeType(value), offset, length, parent: currentParent, value });
  722. ensurePropertyComplete(offset + length);
  723. },
  724. onSeparator: (sep, offset, length) => {
  725. if (currentParent.type === 'property') {
  726. if (sep === ':') {
  727. currentParent.colonOffset = offset;
  728. }
  729. else if (sep === ',') {
  730. ensurePropertyComplete(offset);
  731. }
  732. }
  733. },
  734. onError: (error, offset, length) => {
  735. errors.push({ error, offset, length });
  736. }
  737. };
  738. visit(text, visitor, options);
  739. const result = currentParent.children[0];
  740. if (result) {
  741. delete result.parent;
  742. }
  743. return result;
  744. }
  745. exports.parseTree = parseTree;
  746. /**
  747. * Finds the node at the given path in a JSON DOM.
  748. */
  749. function findNodeAtLocation(root, path) {
  750. if (!root) {
  751. return undefined;
  752. }
  753. let node = root;
  754. for (let segment of path) {
  755. if (typeof segment === 'string') {
  756. if (node.type !== 'object' || !Array.isArray(node.children)) {
  757. return undefined;
  758. }
  759. let found = false;
  760. for (const propertyNode of node.children) {
  761. if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment && propertyNode.children.length === 2) {
  762. node = propertyNode.children[1];
  763. found = true;
  764. break;
  765. }
  766. }
  767. if (!found) {
  768. return undefined;
  769. }
  770. }
  771. else {
  772. const index = segment;
  773. if (node.type !== 'array' || index < 0 || !Array.isArray(node.children) || index >= node.children.length) {
  774. return undefined;
  775. }
  776. node = node.children[index];
  777. }
  778. }
  779. return node;
  780. }
  781. exports.findNodeAtLocation = findNodeAtLocation;
  782. /**
  783. * Gets the JSON path of the given JSON DOM node
  784. */
  785. function getNodePath(node) {
  786. if (!node.parent || !node.parent.children) {
  787. return [];
  788. }
  789. const path = getNodePath(node.parent);
  790. if (node.parent.type === 'property') {
  791. const key = node.parent.children[0].value;
  792. path.push(key);
  793. }
  794. else if (node.parent.type === 'array') {
  795. const index = node.parent.children.indexOf(node);
  796. if (index !== -1) {
  797. path.push(index);
  798. }
  799. }
  800. return path;
  801. }
  802. exports.getNodePath = getNodePath;
  803. /**
  804. * Evaluates the JavaScript object of the given JSON DOM node
  805. */
  806. function getNodeValue(node) {
  807. switch (node.type) {
  808. case 'array':
  809. return node.children.map(getNodeValue);
  810. case 'object':
  811. const obj = Object.create(null);
  812. for (let prop of node.children) {
  813. const valueNode = prop.children[1];
  814. if (valueNode) {
  815. obj[prop.children[0].value] = getNodeValue(valueNode);
  816. }
  817. }
  818. return obj;
  819. case 'null':
  820. case 'string':
  821. case 'number':
  822. case 'boolean':
  823. return node.value;
  824. default:
  825. return undefined;
  826. }
  827. }
  828. exports.getNodeValue = getNodeValue;
  829. function contains(node, offset, includeRightBound = false) {
  830. return (offset >= node.offset && offset < (node.offset + node.length)) || includeRightBound && (offset === (node.offset + node.length));
  831. }
  832. exports.contains = contains;
  833. /**
  834. * Finds the most inner node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
  835. */
  836. function findNodeAtOffset(node, offset, includeRightBound = false) {
  837. if (contains(node, offset, includeRightBound)) {
  838. const children = node.children;
  839. if (Array.isArray(children)) {
  840. for (let i = 0; i < children.length && children[i].offset <= offset; i++) {
  841. const item = findNodeAtOffset(children[i], offset, includeRightBound);
  842. if (item) {
  843. return item;
  844. }
  845. }
  846. }
  847. return node;
  848. }
  849. return undefined;
  850. }
  851. exports.findNodeAtOffset = findNodeAtOffset;
  852. /**
  853. * Parses the given text and invokes the visitor functions for each object, array and literal reached.
  854. */
  855. function visit(text, visitor, options = ParseOptions.DEFAULT) {
  856. const _scanner = (0, scanner_1.createScanner)(text, false);
  857. // Important: Only pass copies of this to visitor functions to prevent accidental modification, and
  858. // to not affect visitor functions which stored a reference to a previous JSONPath
  859. const _jsonPath = [];
  860. function toNoArgVisit(visitFunction) {
  861. return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true;
  862. }
  863. function toNoArgVisitWithPath(visitFunction) {
  864. return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true;
  865. }
  866. function toOneArgVisit(visitFunction) {
  867. return visitFunction ? (arg) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true;
  868. }
  869. function toOneArgVisitWithPath(visitFunction) {
  870. return visitFunction ? (arg) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true;
  871. }
  872. const onObjectBegin = toNoArgVisitWithPath(visitor.onObjectBegin), onObjectProperty = toOneArgVisitWithPath(visitor.onObjectProperty), onObjectEnd = toNoArgVisit(visitor.onObjectEnd), onArrayBegin = toNoArgVisitWithPath(visitor.onArrayBegin), onArrayEnd = toNoArgVisit(visitor.onArrayEnd), onLiteralValue = toOneArgVisitWithPath(visitor.onLiteralValue), onSeparator = toOneArgVisit(visitor.onSeparator), onComment = toNoArgVisit(visitor.onComment), onError = toOneArgVisit(visitor.onError);
  873. const disallowComments = options && options.disallowComments;
  874. const allowTrailingComma = options && options.allowTrailingComma;
  875. function scanNext() {
  876. while (true) {
  877. const token = _scanner.scan();
  878. switch (_scanner.getTokenError()) {
  879. case 4 /* ScanError.InvalidUnicode */:
  880. handleError(14 /* ParseErrorCode.InvalidUnicode */);
  881. break;
  882. case 5 /* ScanError.InvalidEscapeCharacter */:
  883. handleError(15 /* ParseErrorCode.InvalidEscapeCharacter */);
  884. break;
  885. case 3 /* ScanError.UnexpectedEndOfNumber */:
  886. handleError(13 /* ParseErrorCode.UnexpectedEndOfNumber */);
  887. break;
  888. case 1 /* ScanError.UnexpectedEndOfComment */:
  889. if (!disallowComments) {
  890. handleError(11 /* ParseErrorCode.UnexpectedEndOfComment */);
  891. }
  892. break;
  893. case 2 /* ScanError.UnexpectedEndOfString */:
  894. handleError(12 /* ParseErrorCode.UnexpectedEndOfString */);
  895. break;
  896. case 6 /* ScanError.InvalidCharacter */:
  897. handleError(16 /* ParseErrorCode.InvalidCharacter */);
  898. break;
  899. }
  900. switch (token) {
  901. case 12 /* SyntaxKind.LineCommentTrivia */:
  902. case 13 /* SyntaxKind.BlockCommentTrivia */:
  903. if (disallowComments) {
  904. handleError(10 /* ParseErrorCode.InvalidCommentToken */);
  905. }
  906. else {
  907. onComment();
  908. }
  909. break;
  910. case 16 /* SyntaxKind.Unknown */:
  911. handleError(1 /* ParseErrorCode.InvalidSymbol */);
  912. break;
  913. case 15 /* SyntaxKind.Trivia */:
  914. case 14 /* SyntaxKind.LineBreakTrivia */:
  915. break;
  916. default:
  917. return token;
  918. }
  919. }
  920. }
  921. function handleError(error, skipUntilAfter = [], skipUntil = []) {
  922. onError(error);
  923. if (skipUntilAfter.length + skipUntil.length > 0) {
  924. let token = _scanner.getToken();
  925. while (token !== 17 /* SyntaxKind.EOF */) {
  926. if (skipUntilAfter.indexOf(token) !== -1) {
  927. scanNext();
  928. break;
  929. }
  930. else if (skipUntil.indexOf(token) !== -1) {
  931. break;
  932. }
  933. token = scanNext();
  934. }
  935. }
  936. }
  937. function parseString(isValue) {
  938. const value = _scanner.getTokenValue();
  939. if (isValue) {
  940. onLiteralValue(value);
  941. }
  942. else {
  943. onObjectProperty(value);
  944. // add property name afterwards
  945. _jsonPath.push(value);
  946. }
  947. scanNext();
  948. return true;
  949. }
  950. function parseLiteral() {
  951. switch (_scanner.getToken()) {
  952. case 11 /* SyntaxKind.NumericLiteral */:
  953. const tokenValue = _scanner.getTokenValue();
  954. let value = Number(tokenValue);
  955. if (isNaN(value)) {
  956. handleError(2 /* ParseErrorCode.InvalidNumberFormat */);
  957. value = 0;
  958. }
  959. onLiteralValue(value);
  960. break;
  961. case 7 /* SyntaxKind.NullKeyword */:
  962. onLiteralValue(null);
  963. break;
  964. case 8 /* SyntaxKind.TrueKeyword */:
  965. onLiteralValue(true);
  966. break;
  967. case 9 /* SyntaxKind.FalseKeyword */:
  968. onLiteralValue(false);
  969. break;
  970. default:
  971. return false;
  972. }
  973. scanNext();
  974. return true;
  975. }
  976. function parseProperty() {
  977. if (_scanner.getToken() !== 10 /* SyntaxKind.StringLiteral */) {
  978. handleError(3 /* ParseErrorCode.PropertyNameExpected */, [], [2 /* SyntaxKind.CloseBraceToken */, 5 /* SyntaxKind.CommaToken */]);
  979. return false;
  980. }
  981. parseString(false);
  982. if (_scanner.getToken() === 6 /* SyntaxKind.ColonToken */) {
  983. onSeparator(':');
  984. scanNext(); // consume colon
  985. if (!parseValue()) {
  986. handleError(4 /* ParseErrorCode.ValueExpected */, [], [2 /* SyntaxKind.CloseBraceToken */, 5 /* SyntaxKind.CommaToken */]);
  987. }
  988. }
  989. else {
  990. handleError(5 /* ParseErrorCode.ColonExpected */, [], [2 /* SyntaxKind.CloseBraceToken */, 5 /* SyntaxKind.CommaToken */]);
  991. }
  992. _jsonPath.pop(); // remove processed property name
  993. return true;
  994. }
  995. function parseObject() {
  996. onObjectBegin();
  997. scanNext(); // consume open brace
  998. let needsComma = false;
  999. while (_scanner.getToken() !== 2 /* SyntaxKind.CloseBraceToken */ && _scanner.getToken() !== 17 /* SyntaxKind.EOF */) {
  1000. if (_scanner.getToken() === 5 /* SyntaxKind.CommaToken */) {
  1001. if (!needsComma) {
  1002. handleError(4 /* ParseErrorCode.ValueExpected */, [], []);
  1003. }
  1004. onSeparator(',');
  1005. scanNext(); // consume comma
  1006. if (_scanner.getToken() === 2 /* SyntaxKind.CloseBraceToken */ && allowTrailingComma) {
  1007. break;
  1008. }
  1009. }
  1010. else if (needsComma) {
  1011. handleError(6 /* ParseErrorCode.CommaExpected */, [], []);
  1012. }
  1013. if (!parseProperty()) {
  1014. handleError(4 /* ParseErrorCode.ValueExpected */, [], [2 /* SyntaxKind.CloseBraceToken */, 5 /* SyntaxKind.CommaToken */]);
  1015. }
  1016. needsComma = true;
  1017. }
  1018. onObjectEnd();
  1019. if (_scanner.getToken() !== 2 /* SyntaxKind.CloseBraceToken */) {
  1020. handleError(7 /* ParseErrorCode.CloseBraceExpected */, [2 /* SyntaxKind.CloseBraceToken */], []);
  1021. }
  1022. else {
  1023. scanNext(); // consume close brace
  1024. }
  1025. return true;
  1026. }
  1027. function parseArray() {
  1028. onArrayBegin();
  1029. scanNext(); // consume open bracket
  1030. let isFirstElement = true;
  1031. let needsComma = false;
  1032. while (_scanner.getToken() !== 4 /* SyntaxKind.CloseBracketToken */ && _scanner.getToken() !== 17 /* SyntaxKind.EOF */) {
  1033. if (_scanner.getToken() === 5 /* SyntaxKind.CommaToken */) {
  1034. if (!needsComma) {
  1035. handleError(4 /* ParseErrorCode.ValueExpected */, [], []);
  1036. }
  1037. onSeparator(',');
  1038. scanNext(); // consume comma
  1039. if (_scanner.getToken() === 4 /* SyntaxKind.CloseBracketToken */ && allowTrailingComma) {
  1040. break;
  1041. }
  1042. }
  1043. else if (needsComma) {
  1044. handleError(6 /* ParseErrorCode.CommaExpected */, [], []);
  1045. }
  1046. if (isFirstElement) {
  1047. _jsonPath.push(0);
  1048. isFirstElement = false;
  1049. }
  1050. else {
  1051. _jsonPath[_jsonPath.length - 1]++;
  1052. }
  1053. if (!parseValue()) {
  1054. handleError(4 /* ParseErrorCode.ValueExpected */, [], [4 /* SyntaxKind.CloseBracketToken */, 5 /* SyntaxKind.CommaToken */]);
  1055. }
  1056. needsComma = true;
  1057. }
  1058. onArrayEnd();
  1059. if (!isFirstElement) {
  1060. _jsonPath.pop(); // remove array index
  1061. }
  1062. if (_scanner.getToken() !== 4 /* SyntaxKind.CloseBracketToken */) {
  1063. handleError(8 /* ParseErrorCode.CloseBracketExpected */, [4 /* SyntaxKind.CloseBracketToken */], []);
  1064. }
  1065. else {
  1066. scanNext(); // consume close bracket
  1067. }
  1068. return true;
  1069. }
  1070. function parseValue() {
  1071. switch (_scanner.getToken()) {
  1072. case 3 /* SyntaxKind.OpenBracketToken */:
  1073. return parseArray();
  1074. case 1 /* SyntaxKind.OpenBraceToken */:
  1075. return parseObject();
  1076. case 10 /* SyntaxKind.StringLiteral */:
  1077. return parseString(true);
  1078. default:
  1079. return parseLiteral();
  1080. }
  1081. }
  1082. scanNext();
  1083. if (_scanner.getToken() === 17 /* SyntaxKind.EOF */) {
  1084. if (options.allowEmptyContent) {
  1085. return true;
  1086. }
  1087. handleError(4 /* ParseErrorCode.ValueExpected */, [], []);
  1088. return false;
  1089. }
  1090. if (!parseValue()) {
  1091. handleError(4 /* ParseErrorCode.ValueExpected */, [], []);
  1092. return false;
  1093. }
  1094. if (_scanner.getToken() !== 17 /* SyntaxKind.EOF */) {
  1095. handleError(9 /* ParseErrorCode.EndOfFileExpected */, [], []);
  1096. }
  1097. return true;
  1098. }
  1099. exports.visit = visit;
  1100. /**
  1101. * Takes JSON with JavaScript-style comments and remove
  1102. * them. Optionally replaces every none-newline character
  1103. * of comments with a replaceCharacter
  1104. */
  1105. function stripComments(text, replaceCh) {
  1106. let _scanner = (0, scanner_1.createScanner)(text), parts = [], kind, offset = 0, pos;
  1107. do {
  1108. pos = _scanner.getPosition();
  1109. kind = _scanner.scan();
  1110. switch (kind) {
  1111. case 12 /* SyntaxKind.LineCommentTrivia */:
  1112. case 13 /* SyntaxKind.BlockCommentTrivia */:
  1113. case 17 /* SyntaxKind.EOF */:
  1114. if (offset !== pos) {
  1115. parts.push(text.substring(offset, pos));
  1116. }
  1117. if (replaceCh !== undefined) {
  1118. parts.push(_scanner.getTokenValue().replace(/[^\r\n]/g, replaceCh));
  1119. }
  1120. offset = _scanner.getPosition();
  1121. break;
  1122. }
  1123. } while (kind !== 17 /* SyntaxKind.EOF */);
  1124. return parts.join('');
  1125. }
  1126. exports.stripComments = stripComments;
  1127. function getNodeType(value) {
  1128. switch (typeof value) {
  1129. case 'boolean': return 'boolean';
  1130. case 'number': return 'number';
  1131. case 'string': return 'string';
  1132. case 'object': {
  1133. if (!value) {
  1134. return 'null';
  1135. }
  1136. else if (Array.isArray(value)) {
  1137. return 'array';
  1138. }
  1139. return 'object';
  1140. }
  1141. default: return 'null';
  1142. }
  1143. }
  1144. exports.getNodeType = getNodeType;
  1145. });
  1146.  
  1147. },{"./scanner":4}],4:[function(require,module,exports){
  1148. (function (factory) {
  1149. if (typeof module === "object" && typeof module.exports === "object") {
  1150. var v = factory(require, exports);
  1151. if (v !== undefined) module.exports = v;
  1152. }
  1153. else if (typeof define === "function" && define.amd) {
  1154. define(["require", "exports"], factory);
  1155. }
  1156. })(function (require, exports) {
  1157. /*---------------------------------------------------------------------------------------------
  1158. * Copyright (c) Microsoft Corporation. All rights reserved.
  1159. * Licensed under the MIT License. See License.txt in the project root for license information.
  1160. *--------------------------------------------------------------------------------------------*/
  1161. 'use strict';
  1162. Object.defineProperty(exports, "__esModule", { value: true });
  1163. exports.createScanner = void 0;
  1164. /**
  1165. * Creates a JSON scanner on the given text.
  1166. * If ignoreTrivia is set, whitespaces or comments are ignored.
  1167. */
  1168. function createScanner(text, ignoreTrivia = false) {
  1169. const len = text.length;
  1170. let pos = 0, value = '', tokenOffset = 0, token = 16 /* SyntaxKind.Unknown */, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0 /* ScanError.None */;
  1171. function scanHexDigits(count, exact) {
  1172. let digits = 0;
  1173. let value = 0;
  1174. while (digits < count || !exact) {
  1175. let ch = text.charCodeAt(pos);
  1176. if (ch >= 48 /* CharacterCodes._0 */ && ch <= 57 /* CharacterCodes._9 */) {
  1177. value = value * 16 + ch - 48 /* CharacterCodes._0 */;
  1178. }
  1179. else if (ch >= 65 /* CharacterCodes.A */ && ch <= 70 /* CharacterCodes.F */) {
  1180. value = value * 16 + ch - 65 /* CharacterCodes.A */ + 10;
  1181. }
  1182. else if (ch >= 97 /* CharacterCodes.a */ && ch <= 102 /* CharacterCodes.f */) {
  1183. value = value * 16 + ch - 97 /* CharacterCodes.a */ + 10;
  1184. }
  1185. else {
  1186. break;
  1187. }
  1188. pos++;
  1189. digits++;
  1190. }
  1191. if (digits < count) {
  1192. value = -1;
  1193. }
  1194. return value;
  1195. }
  1196. function setPosition(newPosition) {
  1197. pos = newPosition;
  1198. value = '';
  1199. tokenOffset = 0;
  1200. token = 16 /* SyntaxKind.Unknown */;
  1201. scanError = 0 /* ScanError.None */;
  1202. }
  1203. function scanNumber() {
  1204. let start = pos;
  1205. if (text.charCodeAt(pos) === 48 /* CharacterCodes._0 */) {
  1206. pos++;
  1207. }
  1208. else {
  1209. pos++;
  1210. while (pos < text.length && isDigit(text.charCodeAt(pos))) {
  1211. pos++;
  1212. }
  1213. }
  1214. if (pos < text.length && text.charCodeAt(pos) === 46 /* CharacterCodes.dot */) {
  1215. pos++;
  1216. if (pos < text.length && isDigit(text.charCodeAt(pos))) {
  1217. pos++;
  1218. while (pos < text.length && isDigit(text.charCodeAt(pos))) {
  1219. pos++;
  1220. }
  1221. }
  1222. else {
  1223. scanError = 3 /* ScanError.UnexpectedEndOfNumber */;
  1224. return text.substring(start, pos);
  1225. }
  1226. }
  1227. let end = pos;
  1228. if (pos < text.length && (text.charCodeAt(pos) === 69 /* CharacterCodes.E */ || text.charCodeAt(pos) === 101 /* CharacterCodes.e */)) {
  1229. pos++;
  1230. if (pos < text.length && text.charCodeAt(pos) === 43 /* CharacterCodes.plus */ || text.charCodeAt(pos) === 45 /* CharacterCodes.minus */) {
  1231. pos++;
  1232. }
  1233. if (pos < text.length && isDigit(text.charCodeAt(pos))) {
  1234. pos++;
  1235. while (pos < text.length && isDigit(text.charCodeAt(pos))) {
  1236. pos++;
  1237. }
  1238. end = pos;
  1239. }
  1240. else {
  1241. scanError = 3 /* ScanError.UnexpectedEndOfNumber */;
  1242. }
  1243. }
  1244. return text.substring(start, end);
  1245. }
  1246. function scanString() {
  1247. let result = '', start = pos;
  1248. while (true) {
  1249. if (pos >= len) {
  1250. result += text.substring(start, pos);
  1251. scanError = 2 /* ScanError.UnexpectedEndOfString */;
  1252. break;
  1253. }
  1254. const ch = text.charCodeAt(pos);
  1255. if (ch === 34 /* CharacterCodes.doubleQuote */) {
  1256. result += text.substring(start, pos);
  1257. pos++;
  1258. break;
  1259. }
  1260. if (ch === 92 /* CharacterCodes.backslash */) {
  1261. result += text.substring(start, pos);
  1262. pos++;
  1263. if (pos >= len) {
  1264. scanError = 2 /* ScanError.UnexpectedEndOfString */;
  1265. break;
  1266. }
  1267. const ch2 = text.charCodeAt(pos++);
  1268. switch (ch2) {
  1269. case 34 /* CharacterCodes.doubleQuote */:
  1270. result += '\"';
  1271. break;
  1272. case 92 /* CharacterCodes.backslash */:
  1273. result += '\\';
  1274. break;
  1275. case 47 /* CharacterCodes.slash */:
  1276. result += '/';
  1277. break;
  1278. case 98 /* CharacterCodes.b */:
  1279. result += '\b';
  1280. break;
  1281. case 102 /* CharacterCodes.f */:
  1282. result += '\f';
  1283. break;
  1284. case 110 /* CharacterCodes.n */:
  1285. result += '\n';
  1286. break;
  1287. case 114 /* CharacterCodes.r */:
  1288. result += '\r';
  1289. break;
  1290. case 116 /* CharacterCodes.t */:
  1291. result += '\t';
  1292. break;
  1293. case 117 /* CharacterCodes.u */:
  1294. const ch3 = scanHexDigits(4, true);
  1295. if (ch3 >= 0) {
  1296. result += String.fromCharCode(ch3);
  1297. }
  1298. else {
  1299. scanError = 4 /* ScanError.InvalidUnicode */;
  1300. }
  1301. break;
  1302. default:
  1303. scanError = 5 /* ScanError.InvalidEscapeCharacter */;
  1304. }
  1305. start = pos;
  1306. continue;
  1307. }
  1308. if (ch >= 0 && ch <= 0x1f) {
  1309. if (isLineBreak(ch)) {
  1310. result += text.substring(start, pos);
  1311. scanError = 2 /* ScanError.UnexpectedEndOfString */;
  1312. break;
  1313. }
  1314. else {
  1315. scanError = 6 /* ScanError.InvalidCharacter */;
  1316. // mark as error but continue with string
  1317. }
  1318. }
  1319. pos++;
  1320. }
  1321. return result;
  1322. }
  1323. function scanNext() {
  1324. value = '';
  1325. scanError = 0 /* ScanError.None */;
  1326. tokenOffset = pos;
  1327. lineStartOffset = lineNumber;
  1328. prevTokenLineStartOffset = tokenLineStartOffset;
  1329. if (pos >= len) {
  1330. // at the end
  1331. tokenOffset = len;
  1332. return token = 17 /* SyntaxKind.EOF */;
  1333. }
  1334. let code = text.charCodeAt(pos);
  1335. // trivia: whitespace
  1336. if (isWhiteSpace(code)) {
  1337. do {
  1338. pos++;
  1339. value += String.fromCharCode(code);
  1340. code = text.charCodeAt(pos);
  1341. } while (isWhiteSpace(code));
  1342. return token = 15 /* SyntaxKind.Trivia */;
  1343. }
  1344. // trivia: newlines
  1345. if (isLineBreak(code)) {
  1346. pos++;
  1347. value += String.fromCharCode(code);
  1348. if (code === 13 /* CharacterCodes.carriageReturn */ && text.charCodeAt(pos) === 10 /* CharacterCodes.lineFeed */) {
  1349. pos++;
  1350. value += '\n';
  1351. }
  1352. lineNumber++;
  1353. tokenLineStartOffset = pos;
  1354. return token = 14 /* SyntaxKind.LineBreakTrivia */;
  1355. }
  1356. switch (code) {
  1357. // tokens: []{}:,
  1358. case 123 /* CharacterCodes.openBrace */:
  1359. pos++;
  1360. return token = 1 /* SyntaxKind.OpenBraceToken */;
  1361. case 125 /* CharacterCodes.closeBrace */:
  1362. pos++;
  1363. return token = 2 /* SyntaxKind.CloseBraceToken */;
  1364. case 91 /* CharacterCodes.openBracket */:
  1365. pos++;
  1366. return token = 3 /* SyntaxKind.OpenBracketToken */;
  1367. case 93 /* CharacterCodes.closeBracket */:
  1368. pos++;
  1369. return token = 4 /* SyntaxKind.CloseBracketToken */;
  1370. case 58 /* CharacterCodes.colon */:
  1371. pos++;
  1372. return token = 6 /* SyntaxKind.ColonToken */;
  1373. case 44 /* CharacterCodes.comma */:
  1374. pos++;
  1375. return token = 5 /* SyntaxKind.CommaToken */;
  1376. // strings
  1377. case 34 /* CharacterCodes.doubleQuote */:
  1378. pos++;
  1379. value = scanString();
  1380. return token = 10 /* SyntaxKind.StringLiteral */;
  1381. // comments
  1382. case 47 /* CharacterCodes.slash */:
  1383. const start = pos - 1;
  1384. // Single-line comment
  1385. if (text.charCodeAt(pos + 1) === 47 /* CharacterCodes.slash */) {
  1386. pos += 2;
  1387. while (pos < len) {
  1388. if (isLineBreak(text.charCodeAt(pos))) {
  1389. break;
  1390. }
  1391. pos++;
  1392. }
  1393. value = text.substring(start, pos);
  1394. return token = 12 /* SyntaxKind.LineCommentTrivia */;
  1395. }
  1396. // Multi-line comment
  1397. if (text.charCodeAt(pos + 1) === 42 /* CharacterCodes.asterisk */) {
  1398. pos += 2;
  1399. const safeLength = len - 1; // For lookahead.
  1400. let commentClosed = false;
  1401. while (pos < safeLength) {
  1402. const ch = text.charCodeAt(pos);
  1403. if (ch === 42 /* CharacterCodes.asterisk */ && text.charCodeAt(pos + 1) === 47 /* CharacterCodes.slash */) {
  1404. pos += 2;
  1405. commentClosed = true;
  1406. break;
  1407. }
  1408. pos++;
  1409. if (isLineBreak(ch)) {
  1410. if (ch === 13 /* CharacterCodes.carriageReturn */ && text.charCodeAt(pos) === 10 /* CharacterCodes.lineFeed */) {
  1411. pos++;
  1412. }
  1413. lineNumber++;
  1414. tokenLineStartOffset = pos;
  1415. }
  1416. }
  1417. if (!commentClosed) {
  1418. pos++;
  1419. scanError = 1 /* ScanError.UnexpectedEndOfComment */;
  1420. }
  1421. value = text.substring(start, pos);
  1422. return token = 13 /* SyntaxKind.BlockCommentTrivia */;
  1423. }
  1424. // just a single slash
  1425. value += String.fromCharCode(code);
  1426. pos++;
  1427. return token = 16 /* SyntaxKind.Unknown */;
  1428. // numbers
  1429. case 45 /* CharacterCodes.minus */:
  1430. value += String.fromCharCode(code);
  1431. pos++;
  1432. if (pos === len || !isDigit(text.charCodeAt(pos))) {
  1433. return token = 16 /* SyntaxKind.Unknown */;
  1434. }
  1435. // found a minus, followed by a number so
  1436. // we fall through to proceed with scanning
  1437. // numbers
  1438. case 48 /* CharacterCodes._0 */:
  1439. case 49 /* CharacterCodes._1 */:
  1440. case 50 /* CharacterCodes._2 */:
  1441. case 51 /* CharacterCodes._3 */:
  1442. case 52 /* CharacterCodes._4 */:
  1443. case 53 /* CharacterCodes._5 */:
  1444. case 54 /* CharacterCodes._6 */:
  1445. case 55 /* CharacterCodes._7 */:
  1446. case 56 /* CharacterCodes._8 */:
  1447. case 57 /* CharacterCodes._9 */:
  1448. value += scanNumber();
  1449. return token = 11 /* SyntaxKind.NumericLiteral */;
  1450. // literals and unknown symbols
  1451. default:
  1452. // is a literal? Read the full word.
  1453. while (pos < len && isUnknownContentCharacter(code)) {
  1454. pos++;
  1455. code = text.charCodeAt(pos);
  1456. }
  1457. if (tokenOffset !== pos) {
  1458. value = text.substring(tokenOffset, pos);
  1459. // keywords: true, false, null
  1460. switch (value) {
  1461. case 'true': return token = 8 /* SyntaxKind.TrueKeyword */;
  1462. case 'false': return token = 9 /* SyntaxKind.FalseKeyword */;
  1463. case 'null': return token = 7 /* SyntaxKind.NullKeyword */;
  1464. }
  1465. return token = 16 /* SyntaxKind.Unknown */;
  1466. }
  1467. // some
  1468. value += String.fromCharCode(code);
  1469. pos++;
  1470. return token = 16 /* SyntaxKind.Unknown */;
  1471. }
  1472. }
  1473. function isUnknownContentCharacter(code) {
  1474. if (isWhiteSpace(code) || isLineBreak(code)) {
  1475. return false;
  1476. }
  1477. switch (code) {
  1478. case 125 /* CharacterCodes.closeBrace */:
  1479. case 93 /* CharacterCodes.closeBracket */:
  1480. case 123 /* CharacterCodes.openBrace */:
  1481. case 91 /* CharacterCodes.openBracket */:
  1482. case 34 /* CharacterCodes.doubleQuote */:
  1483. case 58 /* CharacterCodes.colon */:
  1484. case 44 /* CharacterCodes.comma */:
  1485. case 47 /* CharacterCodes.slash */:
  1486. return false;
  1487. }
  1488. return true;
  1489. }
  1490. function scanNextNonTrivia() {
  1491. let result;
  1492. do {
  1493. result = scanNext();
  1494. } while (result >= 12 /* SyntaxKind.LineCommentTrivia */ && result <= 15 /* SyntaxKind.Trivia */);
  1495. return result;
  1496. }
  1497. return {
  1498. setPosition: setPosition,
  1499. getPosition: () => pos,
  1500. scan: ignoreTrivia ? scanNextNonTrivia : scanNext,
  1501. getToken: () => token,
  1502. getTokenValue: () => value,
  1503. getTokenOffset: () => tokenOffset,
  1504. getTokenLength: () => pos - tokenOffset,
  1505. getTokenStartLine: () => lineStartOffset,
  1506. getTokenStartCharacter: () => tokenOffset - prevTokenLineStartOffset,
  1507. getTokenError: () => scanError,
  1508. };
  1509. }
  1510. exports.createScanner = createScanner;
  1511. function isWhiteSpace(ch) {
  1512. return ch === 32 /* CharacterCodes.space */ || ch === 9 /* CharacterCodes.tab */;
  1513. }
  1514. function isLineBreak(ch) {
  1515. return ch === 10 /* CharacterCodes.lineFeed */ || ch === 13 /* CharacterCodes.carriageReturn */;
  1516. }
  1517. function isDigit(ch) {
  1518. return ch >= 48 /* CharacterCodes._0 */ && ch <= 57 /* CharacterCodes._9 */;
  1519. }
  1520. var CharacterCodes;
  1521. (function (CharacterCodes) {
  1522. CharacterCodes[CharacterCodes["lineFeed"] = 10] = "lineFeed";
  1523. CharacterCodes[CharacterCodes["carriageReturn"] = 13] = "carriageReturn";
  1524. CharacterCodes[CharacterCodes["space"] = 32] = "space";
  1525. CharacterCodes[CharacterCodes["_0"] = 48] = "_0";
  1526. CharacterCodes[CharacterCodes["_1"] = 49] = "_1";
  1527. CharacterCodes[CharacterCodes["_2"] = 50] = "_2";
  1528. CharacterCodes[CharacterCodes["_3"] = 51] = "_3";
  1529. CharacterCodes[CharacterCodes["_4"] = 52] = "_4";
  1530. CharacterCodes[CharacterCodes["_5"] = 53] = "_5";
  1531. CharacterCodes[CharacterCodes["_6"] = 54] = "_6";
  1532. CharacterCodes[CharacterCodes["_7"] = 55] = "_7";
  1533. CharacterCodes[CharacterCodes["_8"] = 56] = "_8";
  1534. CharacterCodes[CharacterCodes["_9"] = 57] = "_9";
  1535. CharacterCodes[CharacterCodes["a"] = 97] = "a";
  1536. CharacterCodes[CharacterCodes["b"] = 98] = "b";
  1537. CharacterCodes[CharacterCodes["c"] = 99] = "c";
  1538. CharacterCodes[CharacterCodes["d"] = 100] = "d";
  1539. CharacterCodes[CharacterCodes["e"] = 101] = "e";
  1540. CharacterCodes[CharacterCodes["f"] = 102] = "f";
  1541. CharacterCodes[CharacterCodes["g"] = 103] = "g";
  1542. CharacterCodes[CharacterCodes["h"] = 104] = "h";
  1543. CharacterCodes[CharacterCodes["i"] = 105] = "i";
  1544. CharacterCodes[CharacterCodes["j"] = 106] = "j";
  1545. CharacterCodes[CharacterCodes["k"] = 107] = "k";
  1546. CharacterCodes[CharacterCodes["l"] = 108] = "l";
  1547. CharacterCodes[CharacterCodes["m"] = 109] = "m";
  1548. CharacterCodes[CharacterCodes["n"] = 110] = "n";
  1549. CharacterCodes[CharacterCodes["o"] = 111] = "o";
  1550. CharacterCodes[CharacterCodes["p"] = 112] = "p";
  1551. CharacterCodes[CharacterCodes["q"] = 113] = "q";
  1552. CharacterCodes[CharacterCodes["r"] = 114] = "r";
  1553. CharacterCodes[CharacterCodes["s"] = 115] = "s";
  1554. CharacterCodes[CharacterCodes["t"] = 116] = "t";
  1555. CharacterCodes[CharacterCodes["u"] = 117] = "u";
  1556. CharacterCodes[CharacterCodes["v"] = 118] = "v";
  1557. CharacterCodes[CharacterCodes["w"] = 119] = "w";
  1558. CharacterCodes[CharacterCodes["x"] = 120] = "x";
  1559. CharacterCodes[CharacterCodes["y"] = 121] = "y";
  1560. CharacterCodes[CharacterCodes["z"] = 122] = "z";
  1561. CharacterCodes[CharacterCodes["A"] = 65] = "A";
  1562. CharacterCodes[CharacterCodes["B"] = 66] = "B";
  1563. CharacterCodes[CharacterCodes["C"] = 67] = "C";
  1564. CharacterCodes[CharacterCodes["D"] = 68] = "D";
  1565. CharacterCodes[CharacterCodes["E"] = 69] = "E";
  1566. CharacterCodes[CharacterCodes["F"] = 70] = "F";
  1567. CharacterCodes[CharacterCodes["G"] = 71] = "G";
  1568. CharacterCodes[CharacterCodes["H"] = 72] = "H";
  1569. CharacterCodes[CharacterCodes["I"] = 73] = "I";
  1570. CharacterCodes[CharacterCodes["J"] = 74] = "J";
  1571. CharacterCodes[CharacterCodes["K"] = 75] = "K";
  1572. CharacterCodes[CharacterCodes["L"] = 76] = "L";
  1573. CharacterCodes[CharacterCodes["M"] = 77] = "M";
  1574. CharacterCodes[CharacterCodes["N"] = 78] = "N";
  1575. CharacterCodes[CharacterCodes["O"] = 79] = "O";
  1576. CharacterCodes[CharacterCodes["P"] = 80] = "P";
  1577. CharacterCodes[CharacterCodes["Q"] = 81] = "Q";
  1578. CharacterCodes[CharacterCodes["R"] = 82] = "R";
  1579. CharacterCodes[CharacterCodes["S"] = 83] = "S";
  1580. CharacterCodes[CharacterCodes["T"] = 84] = "T";
  1581. CharacterCodes[CharacterCodes["U"] = 85] = "U";
  1582. CharacterCodes[CharacterCodes["V"] = 86] = "V";
  1583. CharacterCodes[CharacterCodes["W"] = 87] = "W";
  1584. CharacterCodes[CharacterCodes["X"] = 88] = "X";
  1585. CharacterCodes[CharacterCodes["Y"] = 89] = "Y";
  1586. CharacterCodes[CharacterCodes["Z"] = 90] = "Z";
  1587. CharacterCodes[CharacterCodes["asterisk"] = 42] = "asterisk";
  1588. CharacterCodes[CharacterCodes["backslash"] = 92] = "backslash";
  1589. CharacterCodes[CharacterCodes["closeBrace"] = 125] = "closeBrace";
  1590. CharacterCodes[CharacterCodes["closeBracket"] = 93] = "closeBracket";
  1591. CharacterCodes[CharacterCodes["colon"] = 58] = "colon";
  1592. CharacterCodes[CharacterCodes["comma"] = 44] = "comma";
  1593. CharacterCodes[CharacterCodes["dot"] = 46] = "dot";
  1594. CharacterCodes[CharacterCodes["doubleQuote"] = 34] = "doubleQuote";
  1595. CharacterCodes[CharacterCodes["minus"] = 45] = "minus";
  1596. CharacterCodes[CharacterCodes["openBrace"] = 123] = "openBrace";
  1597. CharacterCodes[CharacterCodes["openBracket"] = 91] = "openBracket";
  1598. CharacterCodes[CharacterCodes["plus"] = 43] = "plus";
  1599. CharacterCodes[CharacterCodes["slash"] = 47] = "slash";
  1600. CharacterCodes[CharacterCodes["formFeed"] = 12] = "formFeed";
  1601. CharacterCodes[CharacterCodes["tab"] = 9] = "tab";
  1602. })(CharacterCodes || (CharacterCodes = {}));
  1603. });
  1604.  
  1605. },{}],5:[function(require,module,exports){
  1606. (function (factory) {
  1607. if (typeof module === "object" && typeof module.exports === "object") {
  1608. var v = factory(require, exports);
  1609. if (v !== undefined) module.exports = v;
  1610. }
  1611. else if (typeof define === "function" && define.amd) {
  1612. define(["require", "exports"], factory);
  1613. }
  1614. })(function (require, exports) {
  1615. "use strict";
  1616. Object.defineProperty(exports, "__esModule", { value: true });
  1617. exports.supportedEols = exports.cachedBreakLinesWithSpaces = exports.cachedSpaces = void 0;
  1618. exports.cachedSpaces = new Array(20).fill(0).map((_, index) => {
  1619. return ' '.repeat(index);
  1620. });
  1621. const maxCachedValues = 200;
  1622. exports.cachedBreakLinesWithSpaces = {
  1623. ' ': {
  1624. '\n': new Array(maxCachedValues).fill(0).map((_, index) => {
  1625. return '\n' + ' '.repeat(index);
  1626. }),
  1627. '\r': new Array(maxCachedValues).fill(0).map((_, index) => {
  1628. return '\r' + ' '.repeat(index);
  1629. }),
  1630. '\r\n': new Array(maxCachedValues).fill(0).map((_, index) => {
  1631. return '\r\n' + ' '.repeat(index);
  1632. }),
  1633. },
  1634. '\t': {
  1635. '\n': new Array(maxCachedValues).fill(0).map((_, index) => {
  1636. return '\n' + '\t'.repeat(index);
  1637. }),
  1638. '\r': new Array(maxCachedValues).fill(0).map((_, index) => {
  1639. return '\r' + '\t'.repeat(index);
  1640. }),
  1641. '\r\n': new Array(maxCachedValues).fill(0).map((_, index) => {
  1642. return '\r\n' + '\t'.repeat(index);
  1643. }),
  1644. }
  1645. };
  1646. exports.supportedEols = ['\n', '\r', '\r\n'];
  1647. });
  1648.  
  1649. },{}],"jsonc-parser":[function(require,module,exports){
  1650. (function (factory) {
  1651. if (typeof module === "object" && typeof module.exports === "object") {
  1652. var v = factory(require, exports);
  1653. if (v !== undefined) module.exports = v;
  1654. }
  1655. else if (typeof define === "function" && define.amd) {
  1656. define(["require", "exports", "./impl/format", "./impl/edit", "./impl/scanner", "./impl/parser"], factory);
  1657. }
  1658. })(function (require, exports) {
  1659. /*---------------------------------------------------------------------------------------------
  1660. * Copyright (c) Microsoft Corporation. All rights reserved.
  1661. * Licensed under the MIT License. See License.txt in the project root for license information.
  1662. *--------------------------------------------------------------------------------------------*/
  1663. 'use strict';
  1664. Object.defineProperty(exports, "__esModule", { value: true });
  1665. exports.applyEdits = exports.modify = exports.format = exports.printParseErrorCode = exports.ParseErrorCode = exports.stripComments = exports.visit = exports.getNodeValue = exports.getNodePath = exports.findNodeAtOffset = exports.findNodeAtLocation = exports.parseTree = exports.parse = exports.getLocation = exports.SyntaxKind = exports.ScanError = exports.createScanner = void 0;
  1666. const formatter = require("./impl/format");
  1667. const edit = require("./impl/edit");
  1668. const scanner = require("./impl/scanner");
  1669. const parser = require("./impl/parser");
  1670. /**
  1671. * Creates a JSON scanner on the given text.
  1672. * If ignoreTrivia is set, whitespaces or comments are ignored.
  1673. */
  1674. exports.createScanner = scanner.createScanner;
  1675. var ScanError;
  1676. (function (ScanError) {
  1677. ScanError[ScanError["None"] = 0] = "None";
  1678. ScanError[ScanError["UnexpectedEndOfComment"] = 1] = "UnexpectedEndOfComment";
  1679. ScanError[ScanError["UnexpectedEndOfString"] = 2] = "UnexpectedEndOfString";
  1680. ScanError[ScanError["UnexpectedEndOfNumber"] = 3] = "UnexpectedEndOfNumber";
  1681. ScanError[ScanError["InvalidUnicode"] = 4] = "InvalidUnicode";
  1682. ScanError[ScanError["InvalidEscapeCharacter"] = 5] = "InvalidEscapeCharacter";
  1683. ScanError[ScanError["InvalidCharacter"] = 6] = "InvalidCharacter";
  1684. })(ScanError || (exports.ScanError = ScanError = {}));
  1685. var SyntaxKind;
  1686. (function (SyntaxKind) {
  1687. SyntaxKind[SyntaxKind["OpenBraceToken"] = 1] = "OpenBraceToken";
  1688. SyntaxKind[SyntaxKind["CloseBraceToken"] = 2] = "CloseBraceToken";
  1689. SyntaxKind[SyntaxKind["OpenBracketToken"] = 3] = "OpenBracketToken";
  1690. SyntaxKind[SyntaxKind["CloseBracketToken"] = 4] = "CloseBracketToken";
  1691. SyntaxKind[SyntaxKind["CommaToken"] = 5] = "CommaToken";
  1692. SyntaxKind[SyntaxKind["ColonToken"] = 6] = "ColonToken";
  1693. SyntaxKind[SyntaxKind["NullKeyword"] = 7] = "NullKeyword";
  1694. SyntaxKind[SyntaxKind["TrueKeyword"] = 8] = "TrueKeyword";
  1695. SyntaxKind[SyntaxKind["FalseKeyword"] = 9] = "FalseKeyword";
  1696. SyntaxKind[SyntaxKind["StringLiteral"] = 10] = "StringLiteral";
  1697. SyntaxKind[SyntaxKind["NumericLiteral"] = 11] = "NumericLiteral";
  1698. SyntaxKind[SyntaxKind["LineCommentTrivia"] = 12] = "LineCommentTrivia";
  1699. SyntaxKind[SyntaxKind["BlockCommentTrivia"] = 13] = "BlockCommentTrivia";
  1700. SyntaxKind[SyntaxKind["LineBreakTrivia"] = 14] = "LineBreakTrivia";
  1701. SyntaxKind[SyntaxKind["Trivia"] = 15] = "Trivia";
  1702. SyntaxKind[SyntaxKind["Unknown"] = 16] = "Unknown";
  1703. SyntaxKind[SyntaxKind["EOF"] = 17] = "EOF";
  1704. })(SyntaxKind || (exports.SyntaxKind = SyntaxKind = {}));
  1705. /**
  1706. * For a given offset, evaluate the location in the JSON document. Each segment in the location path is either a property name or an array index.
  1707. */
  1708. exports.getLocation = parser.getLocation;
  1709. /**
  1710. * Parses the given text and returns the object the JSON content represents. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
  1711. * Therefore, always check the errors list to find out if the input was valid.
  1712. */
  1713. exports.parse = parser.parse;
  1714. /**
  1715. * Parses the given text and returns a tree representation the JSON content. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
  1716. */
  1717. exports.parseTree = parser.parseTree;
  1718. /**
  1719. * Finds the node at the given path in a JSON DOM.
  1720. */
  1721. exports.findNodeAtLocation = parser.findNodeAtLocation;
  1722. /**
  1723. * Finds the innermost node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
  1724. */
  1725. exports.findNodeAtOffset = parser.findNodeAtOffset;
  1726. /**
  1727. * Gets the JSON path of the given JSON DOM node
  1728. */
  1729. exports.getNodePath = parser.getNodePath;
  1730. /**
  1731. * Evaluates the JavaScript object of the given JSON DOM node
  1732. */
  1733. exports.getNodeValue = parser.getNodeValue;
  1734. /**
  1735. * Parses the given text and invokes the visitor functions for each object, array and literal reached.
  1736. */
  1737. exports.visit = parser.visit;
  1738. /**
  1739. * Takes JSON with JavaScript-style comments and remove
  1740. * them. Optionally replaces every none-newline character
  1741. * of comments with a replaceCharacter
  1742. */
  1743. exports.stripComments = parser.stripComments;
  1744. var ParseErrorCode;
  1745. (function (ParseErrorCode) {
  1746. ParseErrorCode[ParseErrorCode["InvalidSymbol"] = 1] = "InvalidSymbol";
  1747. ParseErrorCode[ParseErrorCode["InvalidNumberFormat"] = 2] = "InvalidNumberFormat";
  1748. ParseErrorCode[ParseErrorCode["PropertyNameExpected"] = 3] = "PropertyNameExpected";
  1749. ParseErrorCode[ParseErrorCode["ValueExpected"] = 4] = "ValueExpected";
  1750. ParseErrorCode[ParseErrorCode["ColonExpected"] = 5] = "ColonExpected";
  1751. ParseErrorCode[ParseErrorCode["CommaExpected"] = 6] = "CommaExpected";
  1752. ParseErrorCode[ParseErrorCode["CloseBraceExpected"] = 7] = "CloseBraceExpected";
  1753. ParseErrorCode[ParseErrorCode["CloseBracketExpected"] = 8] = "CloseBracketExpected";
  1754. ParseErrorCode[ParseErrorCode["EndOfFileExpected"] = 9] = "EndOfFileExpected";
  1755. ParseErrorCode[ParseErrorCode["InvalidCommentToken"] = 10] = "InvalidCommentToken";
  1756. ParseErrorCode[ParseErrorCode["UnexpectedEndOfComment"] = 11] = "UnexpectedEndOfComment";
  1757. ParseErrorCode[ParseErrorCode["UnexpectedEndOfString"] = 12] = "UnexpectedEndOfString";
  1758. ParseErrorCode[ParseErrorCode["UnexpectedEndOfNumber"] = 13] = "UnexpectedEndOfNumber";
  1759. ParseErrorCode[ParseErrorCode["InvalidUnicode"] = 14] = "InvalidUnicode";
  1760. ParseErrorCode[ParseErrorCode["InvalidEscapeCharacter"] = 15] = "InvalidEscapeCharacter";
  1761. ParseErrorCode[ParseErrorCode["InvalidCharacter"] = 16] = "InvalidCharacter";
  1762. })(ParseErrorCode || (exports.ParseErrorCode = ParseErrorCode = {}));
  1763. function printParseErrorCode(code) {
  1764. switch (code) {
  1765. case 1 /* ParseErrorCode.InvalidSymbol */: return 'InvalidSymbol';
  1766. case 2 /* ParseErrorCode.InvalidNumberFormat */: return 'InvalidNumberFormat';
  1767. case 3 /* ParseErrorCode.PropertyNameExpected */: return 'PropertyNameExpected';
  1768. case 4 /* ParseErrorCode.ValueExpected */: return 'ValueExpected';
  1769. case 5 /* ParseErrorCode.ColonExpected */: return 'ColonExpected';
  1770. case 6 /* ParseErrorCode.CommaExpected */: return 'CommaExpected';
  1771. case 7 /* ParseErrorCode.CloseBraceExpected */: return 'CloseBraceExpected';
  1772. case 8 /* ParseErrorCode.CloseBracketExpected */: return 'CloseBracketExpected';
  1773. case 9 /* ParseErrorCode.EndOfFileExpected */: return 'EndOfFileExpected';
  1774. case 10 /* ParseErrorCode.InvalidCommentToken */: return 'InvalidCommentToken';
  1775. case 11 /* ParseErrorCode.UnexpectedEndOfComment */: return 'UnexpectedEndOfComment';
  1776. case 12 /* ParseErrorCode.UnexpectedEndOfString */: return 'UnexpectedEndOfString';
  1777. case 13 /* ParseErrorCode.UnexpectedEndOfNumber */: return 'UnexpectedEndOfNumber';
  1778. case 14 /* ParseErrorCode.InvalidUnicode */: return 'InvalidUnicode';
  1779. case 15 /* ParseErrorCode.InvalidEscapeCharacter */: return 'InvalidEscapeCharacter';
  1780. case 16 /* ParseErrorCode.InvalidCharacter */: return 'InvalidCharacter';
  1781. }
  1782. return '<unknown ParseErrorCode>';
  1783. }
  1784. exports.printParseErrorCode = printParseErrorCode;
  1785. /**
  1786. * Computes the edit operations needed to format a JSON document.
  1787. *
  1788. * @param documentText The input text
  1789. * @param range The range to format or `undefined` to format the full content
  1790. * @param options The formatting options
  1791. * @returns The edit operations describing the formatting changes to the original document following the format described in {@linkcode EditResult}.
  1792. * To apply the edit operations to the input, use {@linkcode applyEdits}.
  1793. */
  1794. function format(documentText, range, options) {
  1795. return formatter.format(documentText, range, options);
  1796. }
  1797. exports.format = format;
  1798. /**
  1799. * Computes the edit operations needed to modify a value in the JSON document.
  1800. *
  1801. * @param documentText The input text
  1802. * @param path The path of the value to change. The path represents either to the document root, a property or an array item.
  1803. * If the path points to an non-existing property or item, it will be created.
  1804. * @param value The new value for the specified property or item. If the value is undefined,
  1805. * the property or item will be removed.
  1806. * @param options Options
  1807. * @returns The edit operations describing the changes to the original document, following the format described in {@linkcode EditResult}.
  1808. * To apply the edit operations to the input, use {@linkcode applyEdits}.
  1809. */
  1810. function modify(text, path, value, options) {
  1811. return edit.setProperty(text, path, value, options);
  1812. }
  1813. exports.modify = modify;
  1814. /**
  1815. * Applies edits to an input string.
  1816. * @param text The input text
  1817. * @param edits Edit operations following the format described in {@linkcode EditResult}.
  1818. * @returns The text with the applied edits.
  1819. * @throws An error if the edit operations are not well-formed as described in {@linkcode EditResult}.
  1820. */
  1821. function applyEdits(text, edits) {
  1822. let sortedEdits = edits.slice(0).sort((a, b) => {
  1823. const diff = a.offset - b.offset;
  1824. if (diff === 0) {
  1825. return a.length - b.length;
  1826. }
  1827. return diff;
  1828. });
  1829. let lastModifiedOffset = text.length;
  1830. for (let i = sortedEdits.length - 1; i >= 0; i--) {
  1831. let e = sortedEdits[i];
  1832. if (e.offset + e.length <= lastModifiedOffset) {
  1833. text = edit.applyEdit(text, e);
  1834. }
  1835. else {
  1836. throw new Error('Overlapping edit');
  1837. }
  1838. lastModifiedOffset = e.offset;
  1839. }
  1840. return text;
  1841. }
  1842. exports.applyEdits = applyEdits;
  1843. });
  1844.  
  1845. },{"./impl/edit":1,"./impl/format":2,"./impl/parser":3,"./impl/scanner":4}]},{},[])("jsonc-parser")
  1846. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址