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 *--------------------------------------------------------------------------------------------*/
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';
11 * Creates a JSON scanner on the given text.
12 * If ignoreTrivia is set, whitespaces or comments are ignored.
14 export var createScanner = scanner.createScanner;
16 * 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.
18 export var getLocation = parser.getLocation;
20 * 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.
21 * Therefore, always check the errors list to find out if the input was valid.
23 export var parse = parser.parse;
25 * 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.
27 export var parseTree = parser.parseTree;
29 * Finds the node at the given path in a JSON DOM.
31 export var findNodeAtLocation = parser.findNodeAtLocation;
33 * Finds the innermost node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
35 export var findNodeAtOffset = parser.findNodeAtOffset;
37 * Gets the JSON path of the given JSON DOM node
39 export var getNodePath = parser.getNodePath;
41 * Evaluates the JavaScript object of the given JSON DOM node
43 export var getNodeValue = parser.getNodeValue;
45 * Parses the given text and invokes the visitor functions for each object, array and literal reached.
47 export var visit = parser.visit;
49 * Takes JSON with JavaScript-style comments and remove
50 * them. Optionally replaces every none-newline character
51 * of comments with a replaceCharacter
53 export var stripComments = parser.stripComments;
54 export function printParseErrorCode(code) {
56 case 1 /* InvalidSymbol */: return 'InvalidSymbol';
57 case 2 /* InvalidNumberFormat */: return 'InvalidNumberFormat';
58 case 3 /* PropertyNameExpected */: return 'PropertyNameExpected';
59 case 4 /* ValueExpected */: return 'ValueExpected';
60 case 5 /* ColonExpected */: return 'ColonExpected';
61 case 6 /* CommaExpected */: return 'CommaExpected';
62 case 7 /* CloseBraceExpected */: return 'CloseBraceExpected';
63 case 8 /* CloseBracketExpected */: return 'CloseBracketExpected';
64 case 9 /* EndOfFileExpected */: return 'EndOfFileExpected';
65 case 10 /* InvalidCommentToken */: return 'InvalidCommentToken';
66 case 11 /* UnexpectedEndOfComment */: return 'UnexpectedEndOfComment';
67 case 12 /* UnexpectedEndOfString */: return 'UnexpectedEndOfString';
68 case 13 /* UnexpectedEndOfNumber */: return 'UnexpectedEndOfNumber';
69 case 14 /* InvalidUnicode */: return 'InvalidUnicode';
70 case 15 /* InvalidEscapeCharacter */: return 'InvalidEscapeCharacter';
71 case 16 /* InvalidCharacter */: return 'InvalidCharacter';
73 return '<unknown ParseErrorCode>';
76 * Computes the edits needed to format a JSON document.
78 * @param documentText The input text
79 * @param range The range to format or `undefined` to format the full content
80 * @param options The formatting options
81 * @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or
82 * removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of
83 * text in the original document. However, multiple edits can have
84 * the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first.
85 * To apply edits to an input, you can use `applyEdits`.
87 export function format(documentText, range, options) {
88 return formatter.format(documentText, range, options);
91 * Computes the edits needed to modify a value in the JSON document.
93 * @param documentText The input text
94 * @param path The path of the value to change. The path represents either to the document root, a property or an array item.
95 * If the path points to an non-existing property or item, it will be created.
96 * @param value The new value for the specified property or item. If the value is undefined,
97 * the property or item will be removed.
98 * @param options Options
99 * @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or
100 * removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of
101 * text in the original document. However, multiple edits can have
102 * the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first.
103 * To apply edits to an input, you can use `applyEdits`.
105 export function modify(text, path, value, options) {
106 return edit.setProperty(text, path, value, options);
109 * Applies edits to a input string.
111 export function applyEdits(text, edits) {
112 for (var i = edits.length - 1; i >= 0; i--) {
113 text = edit.applyEdit(text, edits[i]);