in src/impl/parser.ts [212:274]
export function parseTree(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): Node | undefined {
let currentParent: NodeImpl = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root
function ensurePropertyComplete(endOffset: number) {
if (currentParent.type === 'property') {
currentParent.length = endOffset - currentParent.offset;
currentParent = currentParent.parent!;
}
}
function onValue(valueNode: Node): Node {
currentParent.children!.push(valueNode);
return valueNode;
}
const visitor: JSONVisitor = {
onObjectBegin: (offset: number) => {
currentParent = onValue({ type: 'object', offset, length: -1, parent: currentParent, children: [] });
},
onObjectProperty: (name: string, offset: number, length: number) => {
currentParent = onValue({ type: 'property', offset, length: -1, parent: currentParent, children: [] });
currentParent.children!.push({ type: 'string', value: name, offset, length, parent: currentParent });
},
onObjectEnd: (offset: number, length: number) => {
ensurePropertyComplete(offset + length); // in case of a missing value for a property: make sure property is complete
currentParent.length = offset + length - currentParent.offset;
currentParent = currentParent.parent!;
ensurePropertyComplete(offset + length);
},
onArrayBegin: (offset: number, length: number) => {
currentParent = onValue({ type: 'array', offset, length: -1, parent: currentParent, children: [] });
},
onArrayEnd: (offset: number, length: number) => {
currentParent.length = offset + length - currentParent.offset;
currentParent = currentParent.parent!;
ensurePropertyComplete(offset + length);
},
onLiteralValue: (value: any, offset: number, length: number) => {
onValue({ type: getNodeType(value), offset, length, parent: currentParent, value });
ensurePropertyComplete(offset + length);
},
onSeparator: (sep: string, offset: number, length: number) => {
if (currentParent.type === 'property') {
if (sep === ':') {
currentParent.colonOffset = offset;
} else if (sep === ',') {
ensurePropertyComplete(offset);
}
}
},
onError: (error: ParseErrorCode, offset: number, length: number) => {
errors.push({ error, offset, length });
}
};
visit(text, visitor, options);
const result = currentParent.children![0];
if (result) {
delete result.parent;
}
return result;
}