diff --git a/packages/parser/README.md b/packages/parser/README.md index 94dfae1..be4a480 100644 --- a/packages/parser/README.md +++ b/packages/parser/README.md @@ -43,6 +43,102 @@ const { cst, lexErrors, parseErrors } = parse(xmlText); console.log(cst.children["element"][0].children["Name"][0].image); // -> note ``` +### CST Structure + +The parser outputs a Concrete Syntax Tree (CST) using **Chevrotain**. + +For example, given the following XML input: + +```xml + + Hello World + + +``` + +The resulting `cst` object for the document has the following structure (whitespace `chardata` nodes omitted for brevity): + +```jsonc +{ + "name": "document", + "children": { + "element": [ + // top-level element(s) + { + "name": "element", + "children": { + "OPEN": [{ "image": "<" }], + "Name": [{ "image": "root" }], + "START_CLOSE": [{ "image": ">" }], + + "content": [ + // child content lives here + { + "name": "content", + "children": { + "element": [ + // nested child elements + { + "name": "element", + "children": { + "OPEN": [{ "image": "<" }], + "Name": [{ "image": "child" }], + "attribute": [ + { + "name": "attribute", + "children": { + "Name": [{ "image": "attr" }], + "EQUALS": [{ "image": "=" }], + "STRING": [{ "image": "\"value\"" }] + } + } + ], + "START_CLOSE": [{ "image": ">" }], + "content": [ + { + "name": "content", + "children": { + "chardata": [ + { + "name": "chardata", + "children": { + "TEXT": [{ "image": "Hello World" }] + } + } + ] + } + } + ], + "SLASH_OPEN": [{ "image": "" }] + } + }, + { + "name": "element", // self-closing: + "children": { + "OPEN": [{ "image": "<" }], + "Name": [{ "image": "empty" }], + "SLASH_CLOSE": [{ "image": "/>" }] + } + } + ] + } + } + ], + + "SLASH_OPEN": [{ "image": "" }] + } + } + ] + } +} +``` + +Every property in `children` is an array of matched tokens (`IToken`) or nested rule nodes (`CstNode`). For full details on all node types (such as `prolog`, `docTypeDecl`, `content`, and `chardata`), see the [TypeScript Definitions](./api.d.ts). + ## Support Please open [issues](https://github.com/SAP/xml-tols/issues) on github.