| 19 | * @returns The parsed tree. |
| 20 | */ |
| 21 | export function parseJSON(text: string, options?: ParseOptions, parentMeta?: ParentMeta): Tree { |
| 22 | const { nodeMap, nestNodeMap, parseErrors } = doParseJSON(text, options, parentMeta); |
| 23 | const errors: ContextError[] = |
| 24 | parseErrors.map((e) => ({ |
| 25 | ...e, |
| 26 | context: [ |
| 27 | text.slice(0, e.offset).slice(-50), |
| 28 | text.slice(e.offset, e.offset + e.length), |
| 29 | text.slice(e.offset + e.length).slice(0, 50), |
| 30 | ], |
| 31 | })) ?? []; |
| 32 | |
| 33 | for (const id in nodeMap) { |
| 34 | nodeMap[id].path = undefined!; |
| 35 | nodeMap[id].parent = undefined; |
| 36 | nodeMap[id].childrenOffset = undefined; |
| 37 | } |
| 38 | |
| 39 | return Tree.fromObject({ nodeMap, text, errors }, nestNodeMap); |
| 40 | } |
| 41 | |
| 42 | function doParseJSON(text: string, options?: ParseOptions, parentMeta?: ParentMeta) { |
| 43 | const visitor = new Visitor(text, options, parentMeta); |