Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions packages/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<root>
<child attr="value">Hello World</child>
<empty/>
</root>
```

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": "</" }],
"END_NAME": [{ "image": "child" }],
"END": [{ "image": ">" }]
}
},
{
"name": "element", // self-closing: <empty/>
"children": {
"OPEN": [{ "image": "<" }],
"Name": [{ "image": "empty" }],
"SLASH_CLOSE": [{ "image": "/>" }]
}
}
]
}
}
],

"SLASH_OPEN": [{ "image": "</" }],
"END_NAME": [{ "image": "root" }],
"END": [{ "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.
Expand Down