版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

633 строки
24 KiB

  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 { createScanner } from './scanner';
  7. var ParseOptions;
  8. (function (ParseOptions) {
  9. ParseOptions.DEFAULT = {
  10. allowTrailingComma: false
  11. };
  12. })(ParseOptions || (ParseOptions = {}));
  13. /**
  14. * 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.
  15. */
  16. export function getLocation(text, position) {
  17. const segments = []; // strings or numbers
  18. const earlyReturnException = new Object();
  19. let previousNode = undefined;
  20. const previousNodeInst = {
  21. value: {},
  22. offset: 0,
  23. length: 0,
  24. type: 'object',
  25. parent: undefined
  26. };
  27. let isAtPropertyKey = false;
  28. function setPreviousNode(value, offset, length, type) {
  29. previousNodeInst.value = value;
  30. previousNodeInst.offset = offset;
  31. previousNodeInst.length = length;
  32. previousNodeInst.type = type;
  33. previousNodeInst.colonOffset = undefined;
  34. previousNode = previousNodeInst;
  35. }
  36. try {
  37. visit(text, {
  38. onObjectBegin: (offset, length) => {
  39. if (position <= offset) {
  40. throw earlyReturnException;
  41. }
  42. previousNode = undefined;
  43. isAtPropertyKey = position > offset;
  44. segments.push(''); // push a placeholder (will be replaced)
  45. },
  46. onObjectProperty: (name, offset, length) => {
  47. if (position < offset) {
  48. throw earlyReturnException;
  49. }
  50. setPreviousNode(name, offset, length, 'property');
  51. segments[segments.length - 1] = name;
  52. if (position <= offset + length) {
  53. throw earlyReturnException;
  54. }
  55. },
  56. onObjectEnd: (offset, length) => {
  57. if (position <= offset) {
  58. throw earlyReturnException;
  59. }
  60. previousNode = undefined;
  61. segments.pop();
  62. },
  63. onArrayBegin: (offset, length) => {
  64. if (position <= offset) {
  65. throw earlyReturnException;
  66. }
  67. previousNode = undefined;
  68. segments.push(0);
  69. },
  70. onArrayEnd: (offset, length) => {
  71. if (position <= offset) {
  72. throw earlyReturnException;
  73. }
  74. previousNode = undefined;
  75. segments.pop();
  76. },
  77. onLiteralValue: (value, offset, length) => {
  78. if (position < offset) {
  79. throw earlyReturnException;
  80. }
  81. setPreviousNode(value, offset, length, getNodeType(value));
  82. if (position <= offset + length) {
  83. throw earlyReturnException;
  84. }
  85. },
  86. onSeparator: (sep, offset, length) => {
  87. if (position <= offset) {
  88. throw earlyReturnException;
  89. }
  90. if (sep === ':' && previousNode && previousNode.type === 'property') {
  91. previousNode.colonOffset = offset;
  92. isAtPropertyKey = false;
  93. previousNode = undefined;
  94. }
  95. else if (sep === ',') {
  96. const last = segments[segments.length - 1];
  97. if (typeof last === 'number') {
  98. segments[segments.length - 1] = last + 1;
  99. }
  100. else {
  101. isAtPropertyKey = true;
  102. segments[segments.length - 1] = '';
  103. }
  104. previousNode = undefined;
  105. }
  106. }
  107. });
  108. }
  109. catch (e) {
  110. if (e !== earlyReturnException) {
  111. throw e;
  112. }
  113. }
  114. return {
  115. path: segments,
  116. previousNode,
  117. isAtPropertyKey,
  118. matches: (pattern) => {
  119. let k = 0;
  120. for (let i = 0; k < pattern.length && i < segments.length; i++) {
  121. if (pattern[k] === segments[i] || pattern[k] === '*') {
  122. k++;
  123. }
  124. else if (pattern[k] !== '**') {
  125. return false;
  126. }
  127. }
  128. return k === pattern.length;
  129. }
  130. };
  131. }
  132. /**
  133. * 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.
  134. * Therefore always check the errors list to find out if the input was valid.
  135. */
  136. export function parse(text, errors = [], options = ParseOptions.DEFAULT) {
  137. let currentProperty = null;
  138. let currentParent = [];
  139. const previousParents = [];
  140. function onValue(value) {
  141. if (Array.isArray(currentParent)) {
  142. currentParent.push(value);
  143. }
  144. else if (currentProperty !== null) {
  145. currentParent[currentProperty] = value;
  146. }
  147. }
  148. const visitor = {
  149. onObjectBegin: () => {
  150. const object = {};
  151. onValue(object);
  152. previousParents.push(currentParent);
  153. currentParent = object;
  154. currentProperty = null;
  155. },
  156. onObjectProperty: (name) => {
  157. currentProperty = name;
  158. },
  159. onObjectEnd: () => {
  160. currentParent = previousParents.pop();
  161. },
  162. onArrayBegin: () => {
  163. const array = [];
  164. onValue(array);
  165. previousParents.push(currentParent);
  166. currentParent = array;
  167. currentProperty = null;
  168. },
  169. onArrayEnd: () => {
  170. currentParent = previousParents.pop();
  171. },
  172. onLiteralValue: onValue,
  173. onError: (error, offset, length) => {
  174. errors.push({ error, offset, length });
  175. }
  176. };
  177. visit(text, visitor, options);
  178. return currentParent[0];
  179. }
  180. /**
  181. * 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.
  182. */
  183. export function parseTree(text, errors = [], options = ParseOptions.DEFAULT) {
  184. let currentParent = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root
  185. function ensurePropertyComplete(endOffset) {
  186. if (currentParent.type === 'property') {
  187. currentParent.length = endOffset - currentParent.offset;
  188. currentParent = currentParent.parent;
  189. }
  190. }
  191. function onValue(valueNode) {
  192. currentParent.children.push(valueNode);
  193. return valueNode;
  194. }
  195. const visitor = {
  196. onObjectBegin: (offset) => {
  197. currentParent = onValue({ type: 'object', offset, length: -1, parent: currentParent, children: [] });
  198. },
  199. onObjectProperty: (name, offset, length) => {
  200. currentParent = onValue({ type: 'property', offset, length: -1, parent: currentParent, children: [] });
  201. currentParent.children.push({ type: 'string', value: name, offset, length, parent: currentParent });
  202. },
  203. onObjectEnd: (offset, length) => {
  204. ensurePropertyComplete(offset + length); // in case of a missing value for a property: make sure property is complete
  205. currentParent.length = offset + length - currentParent.offset;
  206. currentParent = currentParent.parent;
  207. ensurePropertyComplete(offset + length);
  208. },
  209. onArrayBegin: (offset, length) => {
  210. currentParent = onValue({ type: 'array', offset, length: -1, parent: currentParent, children: [] });
  211. },
  212. onArrayEnd: (offset, length) => {
  213. currentParent.length = offset + length - currentParent.offset;
  214. currentParent = currentParent.parent;
  215. ensurePropertyComplete(offset + length);
  216. },
  217. onLiteralValue: (value, offset, length) => {
  218. onValue({ type: getNodeType(value), offset, length, parent: currentParent, value });
  219. ensurePropertyComplete(offset + length);
  220. },
  221. onSeparator: (sep, offset, length) => {
  222. if (currentParent.type === 'property') {
  223. if (sep === ':') {
  224. currentParent.colonOffset = offset;
  225. }
  226. else if (sep === ',') {
  227. ensurePropertyComplete(offset);
  228. }
  229. }
  230. },
  231. onError: (error, offset, length) => {
  232. errors.push({ error, offset, length });
  233. }
  234. };
  235. visit(text, visitor, options);
  236. const result = currentParent.children[0];
  237. if (result) {
  238. delete result.parent;
  239. }
  240. return result;
  241. }
  242. /**
  243. * Finds the node at the given path in a JSON DOM.
  244. */
  245. export function findNodeAtLocation(root, path) {
  246. if (!root) {
  247. return undefined;
  248. }
  249. let node = root;
  250. for (let segment of path) {
  251. if (typeof segment === 'string') {
  252. if (node.type !== 'object' || !Array.isArray(node.children)) {
  253. return undefined;
  254. }
  255. let found = false;
  256. for (const propertyNode of node.children) {
  257. if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment && propertyNode.children.length === 2) {
  258. node = propertyNode.children[1];
  259. found = true;
  260. break;
  261. }
  262. }
  263. if (!found) {
  264. return undefined;
  265. }
  266. }
  267. else {
  268. const index = segment;
  269. if (node.type !== 'array' || index < 0 || !Array.isArray(node.children) || index >= node.children.length) {
  270. return undefined;
  271. }
  272. node = node.children[index];
  273. }
  274. }
  275. return node;
  276. }
  277. /**
  278. * Gets the JSON path of the given JSON DOM node
  279. */
  280. export function getNodePath(node) {
  281. if (!node.parent || !node.parent.children) {
  282. return [];
  283. }
  284. const path = getNodePath(node.parent);
  285. if (node.parent.type === 'property') {
  286. const key = node.parent.children[0].value;
  287. path.push(key);
  288. }
  289. else if (node.parent.type === 'array') {
  290. const index = node.parent.children.indexOf(node);
  291. if (index !== -1) {
  292. path.push(index);
  293. }
  294. }
  295. return path;
  296. }
  297. /**
  298. * Evaluates the JavaScript object of the given JSON DOM node
  299. */
  300. export function getNodeValue(node) {
  301. switch (node.type) {
  302. case 'array':
  303. return node.children.map(getNodeValue);
  304. case 'object':
  305. const obj = Object.create(null);
  306. for (let prop of node.children) {
  307. const valueNode = prop.children[1];
  308. if (valueNode) {
  309. obj[prop.children[0].value] = getNodeValue(valueNode);
  310. }
  311. }
  312. return obj;
  313. case 'null':
  314. case 'string':
  315. case 'number':
  316. case 'boolean':
  317. return node.value;
  318. default:
  319. return undefined;
  320. }
  321. }
  322. export function contains(node, offset, includeRightBound = false) {
  323. return (offset >= node.offset && offset < (node.offset + node.length)) || includeRightBound && (offset === (node.offset + node.length));
  324. }
  325. /**
  326. * Finds the most inner node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
  327. */
  328. export function findNodeAtOffset(node, offset, includeRightBound = false) {
  329. if (contains(node, offset, includeRightBound)) {
  330. const children = node.children;
  331. if (Array.isArray(children)) {
  332. for (let i = 0; i < children.length && children[i].offset <= offset; i++) {
  333. const item = findNodeAtOffset(children[i], offset, includeRightBound);
  334. if (item) {
  335. return item;
  336. }
  337. }
  338. }
  339. return node;
  340. }
  341. return undefined;
  342. }
  343. /**
  344. * Parses the given text and invokes the visitor functions for each object, array and literal reached.
  345. */
  346. export function visit(text, visitor, options = ParseOptions.DEFAULT) {
  347. const _scanner = createScanner(text, false);
  348. // Important: Only pass copies of this to visitor functions to prevent accidental modification, and
  349. // to not affect visitor functions which stored a reference to a previous JSONPath
  350. const _jsonPath = [];
  351. function toNoArgVisit(visitFunction) {
  352. return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true;
  353. }
  354. function toNoArgVisitWithPath(visitFunction) {
  355. return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true;
  356. }
  357. function toOneArgVisit(visitFunction) {
  358. return visitFunction ? (arg) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true;
  359. }
  360. function toOneArgVisitWithPath(visitFunction) {
  361. return visitFunction ? (arg) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true;
  362. }
  363. 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);
  364. const disallowComments = options && options.disallowComments;
  365. const allowTrailingComma = options && options.allowTrailingComma;
  366. function scanNext() {
  367. while (true) {
  368. const token = _scanner.scan();
  369. switch (_scanner.getTokenError()) {
  370. case 4 /* ScanError.InvalidUnicode */:
  371. handleError(14 /* ParseErrorCode.InvalidUnicode */);
  372. break;
  373. case 5 /* ScanError.InvalidEscapeCharacter */:
  374. handleError(15 /* ParseErrorCode.InvalidEscapeCharacter */);
  375. break;
  376. case 3 /* ScanError.UnexpectedEndOfNumber */:
  377. handleError(13 /* ParseErrorCode.UnexpectedEndOfNumber */);
  378. break;
  379. case 1 /* ScanError.UnexpectedEndOfComment */:
  380. if (!disallowComments) {
  381. handleError(11 /* ParseErrorCode.UnexpectedEndOfComment */);
  382. }
  383. break;
  384. case 2 /* ScanError.UnexpectedEndOfString */:
  385. handleError(12 /* ParseErrorCode.UnexpectedEndOfString */);
  386. break;
  387. case 6 /* ScanError.InvalidCharacter */:
  388. handleError(16 /* ParseErrorCode.InvalidCharacter */);
  389. break;
  390. }
  391. switch (token) {
  392. case 12 /* SyntaxKind.LineCommentTrivia */:
  393. case 13 /* SyntaxKind.BlockCommentTrivia */:
  394. if (disallowComments) {
  395. handleError(10 /* ParseErrorCode.InvalidCommentToken */);
  396. }
  397. else {
  398. onComment();
  399. }
  400. break;
  401. case 16 /* SyntaxKind.Unknown */:
  402. handleError(1 /* ParseErrorCode.InvalidSymbol */);
  403. break;
  404. case 15 /* SyntaxKind.Trivia */:
  405. case 14 /* SyntaxKind.LineBreakTrivia */:
  406. break;
  407. default:
  408. return token;
  409. }
  410. }
  411. }
  412. function handleError(error, skipUntilAfter = [], skipUntil = []) {
  413. onError(error);
  414. if (skipUntilAfter.length + skipUntil.length > 0) {
  415. let token = _scanner.getToken();
  416. while (token !== 17 /* SyntaxKind.EOF */) {
  417. if (skipUntilAfter.indexOf(token) !== -1) {
  418. scanNext();
  419. break;
  420. }
  421. else if (skipUntil.indexOf(token) !== -1) {
  422. break;
  423. }
  424. token = scanNext();
  425. }
  426. }
  427. }
  428. function parseString(isValue) {
  429. const value = _scanner.getTokenValue();
  430. if (isValue) {
  431. onLiteralValue(value);
  432. }
  433. else {
  434. onObjectProperty(value);
  435. // add property name afterwards
  436. _jsonPath.push(value);
  437. }
  438. scanNext();
  439. return true;
  440. }
  441. function parseLiteral() {
  442. switch (_scanner.getToken()) {
  443. case 11 /* SyntaxKind.NumericLiteral */:
  444. const tokenValue = _scanner.getTokenValue();
  445. let value = Number(tokenValue);
  446. if (isNaN(value)) {
  447. handleError(2 /* ParseErrorCode.InvalidNumberFormat */);
  448. value = 0;
  449. }
  450. onLiteralValue(value);
  451. break;
  452. case 7 /* SyntaxKind.NullKeyword */:
  453. onLiteralValue(null);
  454. break;
  455. case 8 /* SyntaxKind.TrueKeyword */:
  456. onLiteralValue(true);
  457. break;
  458. case 9 /* SyntaxKind.FalseKeyword */:
  459. onLiteralValue(false);
  460. break;
  461. default:
  462. return false;
  463. }
  464. scanNext();
  465. return true;
  466. }
  467. function parseProperty() {
  468. if (_scanner.getToken() !== 10 /* SyntaxKind.StringLiteral */) {
  469. handleError(3 /* ParseErrorCode.PropertyNameExpected */, [], [2 /* SyntaxKind.CloseBraceToken */, 5 /* SyntaxKind.CommaToken */]);
  470. return false;
  471. }
  472. parseString(false);
  473. if (_scanner.getToken() === 6 /* SyntaxKind.ColonToken */) {
  474. onSeparator(':');
  475. scanNext(); // consume colon
  476. if (!parseValue()) {
  477. handleError(4 /* ParseErrorCode.ValueExpected */, [], [2 /* SyntaxKind.CloseBraceToken */, 5 /* SyntaxKind.CommaToken */]);
  478. }
  479. }
  480. else {
  481. handleError(5 /* ParseErrorCode.ColonExpected */, [], [2 /* SyntaxKind.CloseBraceToken */, 5 /* SyntaxKind.CommaToken */]);
  482. }
  483. _jsonPath.pop(); // remove processed property name
  484. return true;
  485. }
  486. function parseObject() {
  487. onObjectBegin();
  488. scanNext(); // consume open brace
  489. let needsComma = false;
  490. while (_scanner.getToken() !== 2 /* SyntaxKind.CloseBraceToken */ && _scanner.getToken() !== 17 /* SyntaxKind.EOF */) {
  491. if (_scanner.getToken() === 5 /* SyntaxKind.CommaToken */) {
  492. if (!needsComma) {
  493. handleError(4 /* ParseErrorCode.ValueExpected */, [], []);
  494. }
  495. onSeparator(',');
  496. scanNext(); // consume comma
  497. if (_scanner.getToken() === 2 /* SyntaxKind.CloseBraceToken */ && allowTrailingComma) {
  498. break;
  499. }
  500. }
  501. else if (needsComma) {
  502. handleError(6 /* ParseErrorCode.CommaExpected */, [], []);
  503. }
  504. if (!parseProperty()) {
  505. handleError(4 /* ParseErrorCode.ValueExpected */, [], [2 /* SyntaxKind.CloseBraceToken */, 5 /* SyntaxKind.CommaToken */]);
  506. }
  507. needsComma = true;
  508. }
  509. onObjectEnd();
  510. if (_scanner.getToken() !== 2 /* SyntaxKind.CloseBraceToken */) {
  511. handleError(7 /* ParseErrorCode.CloseBraceExpected */, [2 /* SyntaxKind.CloseBraceToken */], []);
  512. }
  513. else {
  514. scanNext(); // consume close brace
  515. }
  516. return true;
  517. }
  518. function parseArray() {
  519. onArrayBegin();
  520. scanNext(); // consume open bracket
  521. let isFirstElement = true;
  522. let needsComma = false;
  523. while (_scanner.getToken() !== 4 /* SyntaxKind.CloseBracketToken */ && _scanner.getToken() !== 17 /* SyntaxKind.EOF */) {
  524. if (_scanner.getToken() === 5 /* SyntaxKind.CommaToken */) {
  525. if (!needsComma) {
  526. handleError(4 /* ParseErrorCode.ValueExpected */, [], []);
  527. }
  528. onSeparator(',');
  529. scanNext(); // consume comma
  530. if (_scanner.getToken() === 4 /* SyntaxKind.CloseBracketToken */ && allowTrailingComma) {
  531. break;
  532. }
  533. }
  534. else if (needsComma) {
  535. handleError(6 /* ParseErrorCode.CommaExpected */, [], []);
  536. }
  537. if (isFirstElement) {
  538. _jsonPath.push(0);
  539. isFirstElement = false;
  540. }
  541. else {
  542. _jsonPath[_jsonPath.length - 1]++;
  543. }
  544. if (!parseValue()) {
  545. handleError(4 /* ParseErrorCode.ValueExpected */, [], [4 /* SyntaxKind.CloseBracketToken */, 5 /* SyntaxKind.CommaToken */]);
  546. }
  547. needsComma = true;
  548. }
  549. onArrayEnd();
  550. if (!isFirstElement) {
  551. _jsonPath.pop(); // remove array index
  552. }
  553. if (_scanner.getToken() !== 4 /* SyntaxKind.CloseBracketToken */) {
  554. handleError(8 /* ParseErrorCode.CloseBracketExpected */, [4 /* SyntaxKind.CloseBracketToken */], []);
  555. }
  556. else {
  557. scanNext(); // consume close bracket
  558. }
  559. return true;
  560. }
  561. function parseValue() {
  562. switch (_scanner.getToken()) {
  563. case 3 /* SyntaxKind.OpenBracketToken */:
  564. return parseArray();
  565. case 1 /* SyntaxKind.OpenBraceToken */:
  566. return parseObject();
  567. case 10 /* SyntaxKind.StringLiteral */:
  568. return parseString(true);
  569. default:
  570. return parseLiteral();
  571. }
  572. }
  573. scanNext();
  574. if (_scanner.getToken() === 17 /* SyntaxKind.EOF */) {
  575. if (options.allowEmptyContent) {
  576. return true;
  577. }
  578. handleError(4 /* ParseErrorCode.ValueExpected */, [], []);
  579. return false;
  580. }
  581. if (!parseValue()) {
  582. handleError(4 /* ParseErrorCode.ValueExpected */, [], []);
  583. return false;
  584. }
  585. if (_scanner.getToken() !== 17 /* SyntaxKind.EOF */) {
  586. handleError(9 /* ParseErrorCode.EndOfFileExpected */, [], []);
  587. }
  588. return true;
  589. }
  590. /**
  591. * Takes JSON with JavaScript-style comments and remove
  592. * them. Optionally replaces every none-newline character
  593. * of comments with a replaceCharacter
  594. */
  595. export function stripComments(text, replaceCh) {
  596. let _scanner = createScanner(text), parts = [], kind, offset = 0, pos;
  597. do {
  598. pos = _scanner.getPosition();
  599. kind = _scanner.scan();
  600. switch (kind) {
  601. case 12 /* SyntaxKind.LineCommentTrivia */:
  602. case 13 /* SyntaxKind.BlockCommentTrivia */:
  603. case 17 /* SyntaxKind.EOF */:
  604. if (offset !== pos) {
  605. parts.push(text.substring(offset, pos));
  606. }
  607. if (replaceCh !== undefined) {
  608. parts.push(_scanner.getTokenValue().replace(/[^\r\n]/g, replaceCh));
  609. }
  610. offset = _scanner.getPosition();
  611. break;
  612. }
  613. } while (kind !== 17 /* SyntaxKind.EOF */);
  614. return parts.join('');
  615. }
  616. export function getNodeType(value) {
  617. switch (typeof value) {
  618. case 'boolean': return 'boolean';
  619. case 'number': return 'number';
  620. case 'string': return 'string';
  621. case 'object': {
  622. if (!value) {
  623. return 'null';
  624. }
  625. else if (Array.isArray(value)) {
  626. return 'array';
  627. }
  628. return 'object';
  629. }
  630. default: return 'null';
  631. }
  632. }