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
json.ts
898
>
899
>
function ensurePropertyComplete(endOffset: number) {
900
>
if (currentParent.type === 'property') {
901
currentParent.length = endOffset - currentParent.offset;
902
currentParent = currentParent.parent!;
903
}
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) => {
json.ts
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) => {
json.ts
920
currentParent.length = offset + length - currentParent.offset;
921
currentParent = currentParent.parent!;
922
ensurePropertyComplete(offset + length);
923
},
924
>
onArrayBegin: (offset: number, length: number) => {
json.ts
925
currentParent = onValue({ type: 'array', offset, length: -1, parent: currentParent, children: [] });
926
},
927
>
onArrayEnd: (offset: number, length: number) => {
json.ts
928
currentParent.length = offset + length - currentParent.offset;
929
currentParent = currentParent.parent!;
930
ensurePropertyComplete(offset + length);
931
},
932
>
onLiteralValue: (value: unknown, offset: number, length: number) => {
json.ts
933
onValue({ type: getNodeType(value), offset, length, parent: currentParent, value });
934
ensurePropertyComplete(offset + length);
935
},
936
>
onSeparator: (sep: string, offset: number, length: number) => {
json.ts
937
if (currentParent.type === 'property') {
938
if (sep === ':') {