-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.ts
323 lines (300 loc) · 10.6 KB
/
parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2020, 2024 Masataka Kurihara. All rights reserved. MIT license.
import {
XMLParseHandler,
XMLParseContext,
XMLParseEvent,
XMLParseError,
XMLLocator,
XMLPosition,
ElementInfo,
} from './context.ts';
import {
handleBeforeDocument,
handleGeneralStuff,
handleFoundLT,
handleProcInst,
handleProcInstEnding,
handleSgmlDecl,
handleCdata,
handleCdataEnding,
handleCdataEnding2,
handleComment,
handleCommentEnding,
handleCommentEnding2,
handleDoctype,
handleStartTag,
handleStartTagStuff,
handleEmptyElementTag,
handleAttributeName,
handleAttributeNameSawWhite,
handleAttributeEqual,
handleAttributeValueStart,
handleAttributeValueEnd,
handleEndTag,
handleEndTagSawWhite,
handleAfterDocument,
} from './handler.ts';
export abstract class ParserBase implements XMLLocator {
private _cx = new XMLParseContext(this);
private _handlers: { [state: string]: XMLParseHandler } = {};
private _chunk = '';
private _index = -1;
private _position: XMLPosition = { line: 1, column: 0 };
/*
The basic logic of this XML parser was obtained by reading the source code of sax-js.
Thanks & see: https://github.com/isaacs/sax-js
STATE XML
------------------------ ------------------
BEFORE_DOCUMENT
GENERAL_STUFF
FOUND_LT <
PROC_INST <?
PROC_INST_ENDING <? proc ?
SGML_DECL <!
CDATA <![CDATA[
CDATA_ENDING <![CDATA[ cdata ]
CDATA_ENDING_2 <![CDATA[ cdata ]]
COMMENT <!--
COMMENT_ENDING <!-- comment -
COMMENT_ENDING_2 <!-- comment --
DOCTYPE <!DOCTYPE
START_TAG <element
START_TAG_STUFF <element%20
EMPTY_ELEMENT_TAG <element/
ATTRIBUTE_NAME <element a
ATTRIBUTE_NAME_SAW_WHITE <element a%20
ATTRIBUTE_EQUAL <element a=
ATTRIBUTE_VALUE_START <element a="
ATTRIBUTE_VALUE_END <element a="value"
END_TAG </element
END_TAG_SAW_WHITE </element%20
AFTER_DOCUMENT
*/
constructor() {
this.appendHandler('BEFORE_DOCUMENT', handleBeforeDocument);
this.appendHandler('GENERAL_STUFF', handleGeneralStuff);
this.appendHandler('FOUND_LT', handleFoundLT);
this.appendHandler('PROC_INST', handleProcInst);
this.appendHandler('PROC_INST_ENDING', handleProcInstEnding);
this.appendHandler('SGML_DECL', handleSgmlDecl);
this.appendHandler('CDATA', handleCdata);
this.appendHandler('CDATA_ENDING', handleCdataEnding);
this.appendHandler('CDATA_ENDING_2', handleCdataEnding2);
this.appendHandler('COMMENT', handleComment);
this.appendHandler('COMMENT_ENDING', handleCommentEnding);
this.appendHandler('COMMENT_ENDING_2', handleCommentEnding2);
this.appendHandler('DOCTYPE', handleDoctype);
this.appendHandler('START_TAG', handleStartTag);
this.appendHandler('START_TAG_STUFF', handleStartTagStuff);
this.appendHandler('EMPTY_ELEMENT_TAG', handleEmptyElementTag);
this.appendHandler('ATTRIBUTE_NAME', handleAttributeName);
this.appendHandler('ATTRIBUTE_NAME_SAW_WHITE', handleAttributeNameSawWhite);
this.appendHandler('ATTRIBUTE_EQUAL', handleAttributeEqual);
this.appendHandler('ATTRIBUTE_VALUE_START', handleAttributeValueStart);
this.appendHandler('ATTRIBUTE_VALUE_END', handleAttributeValueEnd);
this.appendHandler('END_TAG', handleEndTag);
this.appendHandler('END_TAG_SAW_WHITE', handleEndTagSawWhite);
this.appendHandler('AFTER_DOCUMENT', handleAfterDocument);
}
protected get cx(): XMLParseContext {
return this._cx;
}
protected appendHandler(state: string, handler: XMLParseHandler): this {
this._handlers[state] = handler;
return this;
}
protected get handlers(): { [state: string]: XMLParseHandler } {
return this._handlers;
}
protected set chunk(chunk: string) {
this._chunk = chunk;
this._index = -1;
}
protected hasNext(): boolean {
return this._index < this._chunk.length - 1;
}
protected readNext(): string {
this._index += 1;
const c = this._chunk[this._index];
if (c === '\n') {
this._position.line += 1;
this._position.column = 0;
} else {
this._position.column += 1;
}
return c;
}
get position(): XMLPosition {
return this._position;
}
}
/**
* A catalog of events.
*/
export interface SAXEvent {
start_document: () => void;
processing_instruction: (procInst: string) => void;
sgml_declaration: (sgmlDecl: string) => void;
text: (text: string, element: ElementInfo, cdata: boolean) => void;
doctype: (doctype: string) => void;
start_prefix_mapping: (ns: string, uri: string) => void;
start_element: (element: ElementInfo) => void;
comment: (comment: string) => void;
end_element: (element: ElementInfo) => void;
end_prefix_mapping: (ns: string, uri: string) => void;
end_document: () => void;
error: (error: XMLParseError) => void;
}
/**
* SAX-style XML parser.
*/
export class SAXParser extends ParserBase implements UnderlyingSink<Uint8Array> {
// deno-lint-ignore no-explicit-any
private _listeners: { [name: string]: ((...arg: any[]) => void)[] } = {};
private _controller?: WritableStreamDefaultController;
private _encoding?: string;
protected fireListeners(event: XMLParseEvent) {
const [name, ...args] = event;
const list = this._listeners[name] || [];
for (const listener of list) {
listener.call(this, ...args);
}
}
protected run() {
try {
while(this.hasNext()) {
const state = this.cx.state;
const handler = this.handlers[state];
if (!handler) {
throw new Error(`Handler for ${state} not found`);
}
const events = handler(this.cx, this.readNext());
for (const event of events) {
this.fireListeners(event);
}
}
} catch(e) {
if (e instanceof XMLParseError) {
this.fireListeners(['error', e]);
this._controller?.error(e);
} else {
throw e;
}
}
}
/**
* implements UnderlyingSink<Uint8Array>
* @param chunk XML data chunk
* @param controller error reporter, Deno writable stream uses internal.
*/
write(chunk: Uint8Array, controller?: WritableStreamDefaultController) {
try {
this._controller = controller;
// TextDecoder can resolve BOM.
this.chunk = new TextDecoder(this._encoding).decode(chunk);
this.run();
} finally {
this._controller = undefined;
}
}
/**
* Execute XML pull parsing.
* @param source Target XML.
* @param encoding When the source is Deno.Reader or Uint8Array, specify the encoding.
*/
async parse(source: ReadableStream<unknown> | Uint8Array | string, encoding?: string) {
this._encoding = encoding;
if (typeof source === 'string') {
this.chunk = source;
this.run();
} else if (source instanceof Uint8Array) {
this.write(source);
} else {
await source.pipeThrough(
new TextDecoderStream(this._encoding)
).pipeTo(
new WritableStream<string>({ write: str => this.parse(str, encoding) }),
);
}
}
on<K extends keyof SAXEvent>(event: K, listener: SAXEvent[K]): this {
const list = this._listeners[event] || [];
list.push(listener);
this._listeners[event] = list;
return this;
}
}
/**
* PullParser returns a iterator of this.
*/
export interface PullResult {
/** event name */
name: string;
// known properties
procInst?: string;
sgmlDecl?: string;
text?: string;
element?: ElementInfo;
cdata?: boolean;
doctype?: string;
ns?: string;
uri?: string;
comment?: string;
error?: XMLParseError;
}
/**
* Pull-style XML parser. This Pull parser is implemented using the ES6 Generator / Iterator mechanism.
*/
export class PullParser extends ParserBase {
protected marshallEvent(event: XMLParseEvent): PullResult {
const name = event[0];
const result: PullResult = { name };
if (name === 'processing_instruction') {
result['procInst'] = event[1];
} else if (name === 'sgml_declaration') {
result['sgmlDecl'] = event[1];
} else if (name === 'text') {
result['text'] = event[1];
result['element'] = event[2];
result['cdata'] = event[3];
} else if (name === 'doctype') {
result['doctype'] = event[1];
} else if (name === 'start_prefix_mapping' || name === 'end_prefix_mapping') {
result['ns'] = event[1];
result['uri'] = event[2];
} else if (name === 'start_element' || name === 'end_element') {
result['element'] = event[1];
} else if (name === 'comment') {
result['comment'] = event[1];
}
return result;
}
/**
* Execute XML pull parsing. this is the ES6 Generator.
* @param source Target XML.
* @param encoding When the source is Uint8Array, specify the encoding.
* @return ES6 Iterator, "value" property is a XML event object typed {@code PullResult} .
*/
* parse(source: Uint8Array | string, encoding?: string) {
this.chunk = typeof source === 'string' ? source : new TextDecoder(encoding).decode(source);
try {
while(this.hasNext()) {
const state = this.cx.state;
const handler = this.handlers[state];
if (!handler) {
throw new Error(`Handler for ${state} not found`);
}
const events = handler(this.cx, this.readNext());
for (const event of events) {
yield this.marshallEvent(event);
}
}
} catch(e) {
if (e instanceof XMLParseError) {
yield { name: 'error', error: e };
} else {
throw e;
}
}
}
}