main.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. 'use strict';
  6. import * as formatter from './impl/format';
  7. import * as edit from './impl/edit';
  8. import * as scanner from './impl/scanner';
  9. import * as parser from './impl/parser';
  10. /**
  11. * Creates a JSON scanner on the given text.
  12. * If ignoreTrivia is set, whitespaces or comments are ignored.
  13. */
  14. export const createScanner = scanner.createScanner;
  15. export var ScanError;
  16. (function (ScanError) {
  17. ScanError[ScanError["None"] = 0] = "None";
  18. ScanError[ScanError["UnexpectedEndOfComment"] = 1] = "UnexpectedEndOfComment";
  19. ScanError[ScanError["UnexpectedEndOfString"] = 2] = "UnexpectedEndOfString";
  20. ScanError[ScanError["UnexpectedEndOfNumber"] = 3] = "UnexpectedEndOfNumber";
  21. ScanError[ScanError["InvalidUnicode"] = 4] = "InvalidUnicode";
  22. ScanError[ScanError["InvalidEscapeCharacter"] = 5] = "InvalidEscapeCharacter";
  23. ScanError[ScanError["InvalidCharacter"] = 6] = "InvalidCharacter";
  24. })(ScanError || (ScanError = {}));
  25. export var SyntaxKind;
  26. (function (SyntaxKind) {
  27. SyntaxKind[SyntaxKind["OpenBraceToken"] = 1] = "OpenBraceToken";
  28. SyntaxKind[SyntaxKind["CloseBraceToken"] = 2] = "CloseBraceToken";
  29. SyntaxKind[SyntaxKind["OpenBracketToken"] = 3] = "OpenBracketToken";
  30. SyntaxKind[SyntaxKind["CloseBracketToken"] = 4] = "CloseBracketToken";
  31. SyntaxKind[SyntaxKind["CommaToken"] = 5] = "CommaToken";
  32. SyntaxKind[SyntaxKind["ColonToken"] = 6] = "ColonToken";
  33. SyntaxKind[SyntaxKind["NullKeyword"] = 7] = "NullKeyword";
  34. SyntaxKind[SyntaxKind["TrueKeyword"] = 8] = "TrueKeyword";
  35. SyntaxKind[SyntaxKind["FalseKeyword"] = 9] = "FalseKeyword";
  36. SyntaxKind[SyntaxKind["StringLiteral"] = 10] = "StringLiteral";
  37. SyntaxKind[SyntaxKind["NumericLiteral"] = 11] = "NumericLiteral";
  38. SyntaxKind[SyntaxKind["LineCommentTrivia"] = 12] = "LineCommentTrivia";
  39. SyntaxKind[SyntaxKind["BlockCommentTrivia"] = 13] = "BlockCommentTrivia";
  40. SyntaxKind[SyntaxKind["LineBreakTrivia"] = 14] = "LineBreakTrivia";
  41. SyntaxKind[SyntaxKind["Trivia"] = 15] = "Trivia";
  42. SyntaxKind[SyntaxKind["Unknown"] = 16] = "Unknown";
  43. SyntaxKind[SyntaxKind["EOF"] = 17] = "EOF";
  44. })(SyntaxKind || (SyntaxKind = {}));
  45. /**
  46. * 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.
  47. */
  48. export const getLocation = parser.getLocation;
  49. /**
  50. * 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.
  51. * Therefore, always check the errors list to find out if the input was valid.
  52. */
  53. export const parse = parser.parse;
  54. /**
  55. * 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.
  56. */
  57. export const parseTree = parser.parseTree;
  58. /**
  59. * Finds the node at the given path in a JSON DOM.
  60. */
  61. export const findNodeAtLocation = parser.findNodeAtLocation;
  62. /**
  63. * Finds the innermost node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
  64. */
  65. export const findNodeAtOffset = parser.findNodeAtOffset;
  66. /**
  67. * Gets the JSON path of the given JSON DOM node
  68. */
  69. export const getNodePath = parser.getNodePath;
  70. /**
  71. * Evaluates the JavaScript object of the given JSON DOM node
  72. */
  73. export const getNodeValue = parser.getNodeValue;
  74. /**
  75. * Parses the given text and invokes the visitor functions for each object, array and literal reached.
  76. */
  77. export const visit = parser.visit;
  78. /**
  79. * Takes JSON with JavaScript-style comments and remove
  80. * them. Optionally replaces every none-newline character
  81. * of comments with a replaceCharacter
  82. */
  83. export const stripComments = parser.stripComments;
  84. export var ParseErrorCode;
  85. (function (ParseErrorCode) {
  86. ParseErrorCode[ParseErrorCode["InvalidSymbol"] = 1] = "InvalidSymbol";
  87. ParseErrorCode[ParseErrorCode["InvalidNumberFormat"] = 2] = "InvalidNumberFormat";
  88. ParseErrorCode[ParseErrorCode["PropertyNameExpected"] = 3] = "PropertyNameExpected";
  89. ParseErrorCode[ParseErrorCode["ValueExpected"] = 4] = "ValueExpected";
  90. ParseErrorCode[ParseErrorCode["ColonExpected"] = 5] = "ColonExpected";
  91. ParseErrorCode[ParseErrorCode["CommaExpected"] = 6] = "CommaExpected";
  92. ParseErrorCode[ParseErrorCode["CloseBraceExpected"] = 7] = "CloseBraceExpected";
  93. ParseErrorCode[ParseErrorCode["CloseBracketExpected"] = 8] = "CloseBracketExpected";
  94. ParseErrorCode[ParseErrorCode["EndOfFileExpected"] = 9] = "EndOfFileExpected";
  95. ParseErrorCode[ParseErrorCode["InvalidCommentToken"] = 10] = "InvalidCommentToken";
  96. ParseErrorCode[ParseErrorCode["UnexpectedEndOfComment"] = 11] = "UnexpectedEndOfComment";
  97. ParseErrorCode[ParseErrorCode["UnexpectedEndOfString"] = 12] = "UnexpectedEndOfString";
  98. ParseErrorCode[ParseErrorCode["UnexpectedEndOfNumber"] = 13] = "UnexpectedEndOfNumber";
  99. ParseErrorCode[ParseErrorCode["InvalidUnicode"] = 14] = "InvalidUnicode";
  100. ParseErrorCode[ParseErrorCode["InvalidEscapeCharacter"] = 15] = "InvalidEscapeCharacter";
  101. ParseErrorCode[ParseErrorCode["InvalidCharacter"] = 16] = "InvalidCharacter";
  102. })(ParseErrorCode || (ParseErrorCode = {}));
  103. export function printParseErrorCode(code) {
  104. switch (code) {
  105. case 1 /* ParseErrorCode.InvalidSymbol */: return 'InvalidSymbol';
  106. case 2 /* ParseErrorCode.InvalidNumberFormat */: return 'InvalidNumberFormat';
  107. case 3 /* ParseErrorCode.PropertyNameExpected */: return 'PropertyNameExpected';
  108. case 4 /* ParseErrorCode.ValueExpected */: return 'ValueExpected';
  109. case 5 /* ParseErrorCode.ColonExpected */: return 'ColonExpected';
  110. case 6 /* ParseErrorCode.CommaExpected */: return 'CommaExpected';
  111. case 7 /* ParseErrorCode.CloseBraceExpected */: return 'CloseBraceExpected';
  112. case 8 /* ParseErrorCode.CloseBracketExpected */: return 'CloseBracketExpected';
  113. case 9 /* ParseErrorCode.EndOfFileExpected */: return 'EndOfFileExpected';
  114. case 10 /* ParseErrorCode.InvalidCommentToken */: return 'InvalidCommentToken';
  115. case 11 /* ParseErrorCode.UnexpectedEndOfComment */: return 'UnexpectedEndOfComment';
  116. case 12 /* ParseErrorCode.UnexpectedEndOfString */: return 'UnexpectedEndOfString';
  117. case 13 /* ParseErrorCode.UnexpectedEndOfNumber */: return 'UnexpectedEndOfNumber';
  118. case 14 /* ParseErrorCode.InvalidUnicode */: return 'InvalidUnicode';
  119. case 15 /* ParseErrorCode.InvalidEscapeCharacter */: return 'InvalidEscapeCharacter';
  120. case 16 /* ParseErrorCode.InvalidCharacter */: return 'InvalidCharacter';
  121. }
  122. return '<unknown ParseErrorCode>';
  123. }
  124. /**
  125. * Computes the edit operations needed to format a JSON document.
  126. *
  127. * @param documentText The input text
  128. * @param range The range to format or `undefined` to format the full content
  129. * @param options The formatting options
  130. * @returns The edit operations describing the formatting changes to the original document following the format described in {@linkcode EditResult}.
  131. * To apply the edit operations to the input, use {@linkcode applyEdits}.
  132. */
  133. export function format(documentText, range, options) {
  134. return formatter.format(documentText, range, options);
  135. }
  136. /**
  137. * Computes the edit operations needed to modify a value in the JSON document.
  138. *
  139. * @param documentText The input text
  140. * @param path The path of the value to change. The path represents either to the document root, a property or an array item.
  141. * If the path points to an non-existing property or item, it will be created.
  142. * @param value The new value for the specified property or item. If the value is undefined,
  143. * the property or item will be removed.
  144. * @param options Options
  145. * @returns The edit operations describing the changes to the original document, following the format described in {@linkcode EditResult}.
  146. * To apply the edit operations to the input, use {@linkcode applyEdits}.
  147. */
  148. export function modify(text, path, value, options) {
  149. return edit.setProperty(text, path, value, options);
  150. }
  151. /**
  152. * Applies edits to an input string.
  153. * @param text The input text
  154. * @param edits Edit operations following the format described in {@linkcode EditResult}.
  155. * @returns The text with the applied edits.
  156. * @throws An error if the edit operations are not well-formed as described in {@linkcode EditResult}.
  157. */
  158. export function applyEdits(text, edits) {
  159. let sortedEdits = edits.slice(0).sort((a, b) => {
  160. const diff = a.offset - b.offset;
  161. if (diff === 0) {
  162. return a.length - b.length;
  163. }
  164. return diff;
  165. });
  166. let lastModifiedOffset = text.length;
  167. for (let i = sortedEdits.length - 1; i >= 0; i--) {
  168. let e = sortedEdits[i];
  169. if (e.offset + e.length <= lastModifiedOffset) {
  170. text = edit.applyEdit(text, e);
  171. }
  172. else {
  173. throw new Error('Overlapping edit');
  174. }
  175. lastModifiedOffset = e.offset;
  176. }
  177. return text;
  178. }