(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT)
| 894 | * 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. |
| 895 | */ |
| 896 | export function parseTree(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): Node { |
| 897 | let currentParent: NodeImpl = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root |
| 898 | |
| 899 | function ensurePropertyComplete(endOffset: number) { |
| 900 | if (currentParent.type === 'property') { |
| 901 | currentParent.length = endOffset - currentParent.offset; |
| 902 | currentParent = currentParent.parent!; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | function onValue(valueNode: Node): Node { |
| 907 | currentParent.children!.push(valueNode); |
| 908 | return valueNode; |
| 909 | } |
| 910 | |
| 911 | const visitor: JSONVisitor = { |
| 912 | onObjectBegin: (offset: number) => { |
| 913 | currentParent = onValue({ type: 'object', offset, length: -1, parent: currentParent, children: [] }); |
| 914 | }, |
| 915 | onObjectProperty: (name: string, offset: number, length: number) => { |
| 916 | currentParent = onValue({ type: 'property', offset, length: -1, parent: currentParent, children: [] }); |
| 917 | currentParent.children!.push({ type: 'string', value: name, offset, length, parent: currentParent }); |
| 918 | }, |
| 919 | onObjectEnd: (offset: number, length: number) => { |
| 920 | currentParent.length = offset + length - currentParent.offset; |
| 921 | currentParent = currentParent.parent!; |
| 922 | ensurePropertyComplete(offset + length); |
| 923 | }, |
| 924 | onArrayBegin: (offset: number, length: number) => { |
| 925 | currentParent = onValue({ type: 'array', offset, length: -1, parent: currentParent, children: [] }); |
| 926 | }, |
| 927 | onArrayEnd: (offset: number, length: number) => { |
| 928 | currentParent.length = offset + length - currentParent.offset; |
| 929 | currentParent = currentParent.parent!; |
| 930 | ensurePropertyComplete(offset + length); |
| 931 | }, |
| 932 | onLiteralValue: (value: unknown, offset: number, length: number) => { |
| 933 | onValue({ type: getNodeType(value), offset, length, parent: currentParent, value }); |
| 934 | ensurePropertyComplete(offset + length); |
| 935 | }, |
| 936 | onSeparator: (sep: string, offset: number, length: number) => { |
| 937 | if (currentParent.type === 'property') { |
| 938 | if (sep === ':') { |
| 939 | currentParent.colonOffset = offset; |
| 940 | } else if (sep === ',') { |
| 941 | ensurePropertyComplete(offset); |
| 942 | } |
| 943 | } |
| 944 | }, |
| 945 | onError: (error: ParseErrorCode, offset: number, length: number) => { |
| 946 | errors.push({ error, offset, length }); |
| 947 | } |
| 948 | }; |
| 949 | visit(text, visitor, options); |
| 950 | |
| 951 | const result = currentParent.children![0]; |
| 952 | if (result) { |
| 953 | delete result.parent; |
searching dependent graphs…