@@ -16,6 +16,154 @@ function createError(message, options) {
1616 return Object . assign ( error , options ) ;
1717}
1818
19+ function simplifyCST ( node ) {
20+ switch ( node . name ) {
21+ case "attribute" : {
22+ const { Name, EQUALS , STRING } = node . children ;
23+
24+ return {
25+ name : "attribute" ,
26+ Name : Name [ 0 ] . image ,
27+ EQUALS : EQUALS [ 0 ] . image ,
28+ STRING : STRING [ 0 ] . image ,
29+ location : node . location
30+ } ;
31+ }
32+ case "chardata" : {
33+ const { SEA_WS , TEXT } = node . children ;
34+
35+ return {
36+ name : "chardata" ,
37+ SEA_WS : SEA_WS ? SEA_WS [ 0 ] . image : null ,
38+ TEXT : TEXT ? TEXT [ 0 ] . image : null ,
39+ location : node . location
40+ } ;
41+ }
42+ case "content" : {
43+ const {
44+ CData,
45+ Comment,
46+ chardata,
47+ element,
48+ PROCESSING_INSTRUCTION ,
49+ reference
50+ } = node . children ;
51+
52+ return {
53+ name : "content" ,
54+ CData : CData || [ ] ,
55+ Comment : Comment || [ ] ,
56+ chardata : ( chardata || [ ] ) . map ( simplifyCST ) ,
57+ element : ( element || [ ] ) . map ( simplifyCST ) ,
58+ PROCESSING_INSTRUCTION : PROCESSING_INSTRUCTION || [ ] ,
59+ reference : ( reference || [ ] ) . map ( simplifyCST ) ,
60+ location : node . location
61+ } ;
62+ }
63+ case "docTypeDecl" : {
64+ const { DocType, Name, externalID, CLOSE } = node . children ;
65+
66+ return {
67+ name : "docTypeDecl" ,
68+ DocType : DocType [ 0 ] . image ,
69+ Name : Name [ 0 ] . image ,
70+ externalID : externalID ? simplifyCST ( externalID [ 0 ] ) : null ,
71+ CLOSE : CLOSE [ 0 ] . image ,
72+ location : node . location
73+ } ;
74+ }
75+ case "document" : {
76+ const { docTypeDecl, element, misc, prolog } = node . children ;
77+
78+ return {
79+ name : "document" ,
80+ docTypeDecl : docTypeDecl ? simplifyCST ( docTypeDecl [ 0 ] ) : null ,
81+ element : element ? simplifyCST ( element [ 0 ] ) : null ,
82+ misc : ( misc || [ ] )
83+ . filter ( ( child ) => ! child . children . SEA_WS )
84+ . map ( simplifyCST ) ,
85+ prolog : prolog ? simplifyCST ( prolog [ 0 ] ) : null ,
86+ location : node . location
87+ } ;
88+ }
89+ case "element" : {
90+ const {
91+ OPEN ,
92+ Name,
93+ attribute,
94+ START_CLOSE ,
95+ content,
96+ SLASH_OPEN ,
97+ END_NAME ,
98+ END ,
99+ SLASH_CLOSE
100+ } = node . children ;
101+
102+ return {
103+ name : "element" ,
104+ OPEN : OPEN [ 0 ] . image ,
105+ Name : Name [ 0 ] . image ,
106+ attribute : ( attribute || [ ] ) . map ( simplifyCST ) ,
107+ START_CLOSE : START_CLOSE ? START_CLOSE [ 0 ] . image : null ,
108+ content : content ? simplifyCST ( content [ 0 ] ) : null ,
109+ SLASH_OPEN : SLASH_OPEN ? SLASH_OPEN [ 0 ] . image : null ,
110+ END_NAME : END_NAME ? END_NAME [ 0 ] . image : null ,
111+ END : END ? END [ 0 ] . image : null ,
112+ SLASH_CLOSE : SLASH_CLOSE ? SLASH_CLOSE [ 0 ] . image : null ,
113+ location : node . location
114+ } ;
115+ }
116+ case "externalID" : {
117+ const { Public, PubIDLiteral, System, SystemLiteral } = node . children ;
118+
119+ return {
120+ name : "externalID" ,
121+ Public : Public ? Public [ 0 ] . image : null ,
122+ PubIDLiteral : PubIDLiteral ? PubIDLiteral [ 0 ] . image : null ,
123+ System : System ? System [ 0 ] . image : null ,
124+ SystemLiteral : SystemLiteral ? SystemLiteral [ 0 ] . image : null ,
125+ location : node . location
126+ } ;
127+ }
128+ case "misc" : {
129+ const { Comment, PROCESSING_INSTRUCTION , SEA_WS } = node . children ;
130+
131+ return {
132+ name : "misc" ,
133+ Comment : Comment ? Comment [ 0 ] . image : null ,
134+ PROCESSING_INSTRUCTION : PROCESSING_INSTRUCTION
135+ ? PROCESSING_INSTRUCTION [ 0 ] . image
136+ : null ,
137+ SEA_WS : SEA_WS ? SEA_WS [ 0 ] . image : null ,
138+ location : node . location
139+ } ;
140+ }
141+ case "prolog" : {
142+ const { XMLDeclOpen, attribute, SPECIAL_CLOSE } = node . children ;
143+
144+ return {
145+ name : "prolog" ,
146+ XMLDeclOpen : XMLDeclOpen [ 0 ] . image ,
147+ attribute : ( attribute || [ ] ) . map ( simplifyCST ) ,
148+ SPECIAL_CLOSE : SPECIAL_CLOSE [ 0 ] . image ,
149+ location : node . location
150+ } ;
151+ }
152+ case "reference" : {
153+ const { CharRef, EntityRef } = node . children ;
154+
155+ return {
156+ name : "reference" ,
157+ CharRef : CharRef ? CharRef [ 0 ] . image : null ,
158+ EntityRef : EntityRef ? EntityRef [ 0 ] . image : null ,
159+ location : node . location
160+ } ;
161+ }
162+ default :
163+ throw new Error ( `Unknown node type: ${ node . name } ` ) ;
164+ }
165+ }
166+
19167const parser = {
20168 parse ( text ) {
21169 const { lexErrors, parseErrors, cst } = xmlToolsParse ( text ) ;
@@ -52,7 +200,7 @@ const parser = {
52200 }
53201
54202 // Otherwise return the CST.
55- return cst ;
203+ return simplifyCST ( cst ) ;
56204 } ,
57205 astFormat : "xml" ,
58206 locStart ( node ) {
0 commit comments