Skip to content

Commit b210a26

Browse files
committed
docs(parser): add CST Structure section to README
Add a new 'CST Structure' subsection under 'Usage' with a concrete JSON example showing the Chevrotain CST output for a self-closing element (<xs:element name="api" />). The example accurately reflects the grammar rules in parser.js and the type definitions in api.d.ts. Closes #470
1 parent 929b51d commit b210a26

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

packages/parser/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,102 @@ const { cst, lexErrors, parseErrors } = parse(xmlText);
4343
console.log(cst.children["element"][0].children["Name"][0].image); // -> note
4444
```
4545

46+
### CST Structure
47+
48+
The parser outputs a Concrete Syntax Tree (CST) using **Chevrotain**.
49+
50+
For example, given the following XML input:
51+
52+
```xml
53+
<root>
54+
<child attr="value">Hello World</child>
55+
<empty/>
56+
</root>
57+
```
58+
59+
The resulting `cst` object for the document has the following structure (whitespace `chardata` nodes omitted for brevity):
60+
61+
```jsonc
62+
{
63+
"name": "document",
64+
"children": {
65+
"element": [
66+
// top-level element(s)
67+
{
68+
"name": "element",
69+
"children": {
70+
"OPEN": [{ "image": "<" }],
71+
"Name": [{ "image": "root" }],
72+
"START_CLOSE": [{ "image": ">" }],
73+
74+
"content": [
75+
// child content lives here
76+
{
77+
"name": "content",
78+
"children": {
79+
"element": [
80+
// nested child elements
81+
{
82+
"name": "element",
83+
"children": {
84+
"OPEN": [{ "image": "<" }],
85+
"Name": [{ "image": "child" }],
86+
"attribute": [
87+
{
88+
"name": "attribute",
89+
"children": {
90+
"Name": [{ "image": "attr" }],
91+
"EQUALS": [{ "image": "=" }],
92+
"STRING": [{ "image": "\"value\"" }],
93+
},
94+
},
95+
],
96+
"START_CLOSE": [{ "image": ">" }],
97+
"content": [
98+
{
99+
"name": "content",
100+
"children": {
101+
"chardata": [
102+
{
103+
"name": "chardata",
104+
"children": {
105+
"TEXT": [{ "image": "Hello World" }],
106+
},
107+
},
108+
],
109+
},
110+
},
111+
],
112+
"SLASH_OPEN": [{ "image": "</" }],
113+
"END_NAME": [{ "image": "child" }],
114+
"END": [{ "image": ">" }],
115+
},
116+
},
117+
{
118+
"name": "element", // self-closing: <empty/>
119+
"children": {
120+
"OPEN": [{ "image": "<" }],
121+
"Name": [{ "image": "empty" }],
122+
"SLASH_CLOSE": [{ "image": "/>" }],
123+
},
124+
},
125+
],
126+
},
127+
},
128+
],
129+
130+
"SLASH_OPEN": [{ "image": "</" }],
131+
"END_NAME": [{ "image": "root" }],
132+
"END": [{ "image": ">" }],
133+
},
134+
},
135+
],
136+
},
137+
}
138+
```
139+
140+
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).
141+
46142
## Support
47143

48144
Please open [issues](https://github.com/SAP/xml-tols/issues) on github.

0 commit comments

Comments
 (0)