-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathParser.ts
More file actions
333 lines (286 loc) · 8.64 KB
/
Parser.ts
File metadata and controls
333 lines (286 loc) · 8.64 KB
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
324
325
326
327
328
329
330
331
332
333
import {
TagType,
CODE,
STATE,
isWhitespaceCode,
type Range,
type ParserOptions as Options,
getLines,
getLocation,
getPosition,
ErrorCode,
} from "../internal";
export interface Meta extends Range {
parent: Meta;
state: StateDefinition;
}
export interface StateDefinition<P extends Meta = Meta> {
name: string;
enter: (
this: Parser,
parent: Meta,
pos: number,
) => Partial<P & { state: unknown }>;
exit: (this: Parser, activeRange: P) => void;
char: (this: Parser, code: number, activeRange: P) => void;
eol: (this: Parser, length: number, activeRange: P) => void;
eof: (this: Parser, activeRange: P) => void;
return: (this: Parser, child: Meta, activeRange: P) => void;
}
export class Parser {
declare public pos: number;
declare public maxPos: number;
declare public data: string;
declare public activeState: StateDefinition;
declare public activeRange: Meta;
declare public forward: number;
declare public activeTag: STATE.OpenTagMeta | undefined; // Used to reference the closest open tag
declare public activeAttr: STATE.AttrMeta | undefined; // Used to reference the current attribute that is being parsed
declare public indent: string; // Used to build the indent for the current concise line
declare public isConcise: boolean; // Set to true if parser is currently in concise mode
declare public beginMixedMode?: boolean; // Used as a flag to mark that the next HTML block should enter the parser into HTML mode
declare public endingMixedModeAtEOL?: boolean; // Used as a flag to record that the next EOL to exit HTML mode and go back to concise
declare public textPos: number; // Used to buffer text that is found within the body of a tag
declare public lines: undefined | number[]; // Keeps track of line indexes to provide line/column info.
constructor(public options: Options) {}
read(range: Range) {
return this.data.slice(range.start, range.end);
}
positionAt(offset: number) {
return getPosition(
this.lines || (this.lines = getLines(this.data)),
offset,
);
}
locationAt(range: Range) {
return getLocation(
this.lines || (this.lines = getLines(this.data)),
range.start,
range.end,
);
}
enterState<P extends Meta>(state: StateDefinition<P>): P {
this.activeState = state as unknown as StateDefinition;
return (this.activeRange = state.enter.call(
this,
this.activeRange,
this.pos,
) as P);
}
exitState() {
const { activeRange, activeState } = this;
const parent = (this.activeRange = activeRange.parent);
this.activeState = parent.state;
this.forward = 0;
activeRange.end = this.pos;
activeState.exit.call(this, activeRange);
this.activeState.return.call(this, activeRange, parent);
}
/**
* Compare a position in the source to either another position, or a string.
*/
matchAtPos(a: Range, b: Range | string) {
const aPos = a.start;
const aLen = a.end - aPos;
let bPos = 0;
let bLen = 0;
let bSource = this.data;
if (typeof b === "string") {
bLen = b.length;
bSource = b;
} else {
bPos = b.start;
bLen = b.end - bPos;
}
if (aLen !== bLen) return false;
for (let i = 0; i < aLen; i++) {
if (this.data.charAt(aPos + i) !== bSource.charAt(bPos + i)) {
return false;
}
}
return true;
}
matchAnyAtPos(a: Range, list: (Range | string)[]) {
for (const item of list) {
if (this.matchAtPos(a, item)) return true;
}
return false;
}
/**
* Look ahead to see if the given str matches the substring sequence
* beyond
*/
lookAheadFor(str: string, startPos = this.pos + 1) {
let i = str.length;
if (startPos + i <= this.maxPos) {
const { data } = this;
for (; i--; ) {
if (str[i] !== data[startPos + i]) {
return undefined;
}
}
return str;
}
}
lookAtCharCodeAhead(offset: number, startPos = this.pos) {
return this.data.charCodeAt(startPos + offset);
}
startText() {
if (this.textPos === -1) {
this.textPos = this.pos;
}
}
endText() {
const start = this.textPos;
if (start !== -1) {
if (start !== this.pos) {
this.options.onText?.({ start, end: this.pos });
}
this.textPos = -1;
}
}
/**
* This is used to enter into "HTML" parsing mode instead
* of concise HTML. We push a block on to the stack so that we know when
* return back to the previous parsing mode and to ensure that all
* tags within a block are properly closed.
*/
beginHtmlBlock(delimiter: string | undefined, singleLine: boolean) {
const content = this.enterState(
this.activeTag?.type === TagType.text
? STATE.PARSED_TEXT_CONTENT
: STATE.HTML_CONTENT,
);
content.singleLine = singleLine;
content.delimiter = delimiter;
content.indent = this.indent;
}
emitError(range: number | Range, code: ErrorCode, message: string) {
let start, end;
if (typeof range === "number") {
start = end = range;
} else {
start = range.start;
end = range.end;
}
this.options.onError?.({
start,
end,
code,
message,
});
this.pos = this.maxPos + 1;
}
closeTagEnd(start: number, end: number, name: Range | undefined) {
const { beginMixedMode, parentTag } = this.activeTag!;
if (beginMixedMode) this.endingMixedModeAtEOL = true;
this.activeTag = parentTag;
if (name) this.options.onCloseTagName?.(name);
this.options.onCloseTagEnd?.({ start, end });
}
// --------------------------
consumeWhitespaceIfBefore(str: string, start = 0) {
const { pos, data } = this;
let cur = pos + start;
while (isWhitespaceCode(data.charCodeAt(cur))) cur++;
if (this.lookAheadFor(str, cur)) {
this.pos = cur;
if (this.forward > 1) this.forward = 1;
return true;
}
return false;
}
getPreviousNonWhitespaceCharCode(start = -1) {
let behind = start;
while (isWhitespaceCode(this.lookAtCharCodeAhead(behind))) behind--;
return this.lookAtCharCodeAhead(behind);
}
onlyWhitespaceRemainsOnLine(start = 1) {
const maxOffset = this.maxPos - this.pos;
let ahead = start;
while (ahead < maxOffset) {
const code = this.lookAtCharCodeAhead(ahead);
if (isWhitespaceCode(code)) {
switch (code) {
case CODE.CARRIAGE_RETURN:
case CODE.NEWLINE:
return true;
}
} else {
return false;
}
ahead++;
}
return true;
}
consumeWhitespaceOnLine(start = 1) {
const maxOffset = this.maxPos - this.pos;
let ahead = start;
while (ahead < maxOffset) {
const code = this.lookAtCharCodeAhead(ahead);
if (isWhitespaceCode(code)) {
switch (code) {
case CODE.CARRIAGE_RETURN:
case CODE.NEWLINE:
this.pos += ahead;
return true;
}
} else {
this.pos += ahead;
return false;
}
ahead++;
}
this.pos = this.maxPos;
return true;
}
consumeWhitespace() {
const maxOffset = this.maxPos - this.pos;
let ahead = 0;
while (
ahead < maxOffset &&
isWhitespaceCode(this.lookAtCharCodeAhead(ahead))
) {
ahead++;
}
this.pos += ahead;
}
parse(data: string) {
const maxPos = (this.maxPos = data.length);
this.data = data;
this.indent = "";
this.textPos = -1;
this.forward = 1;
this.isConcise = true;
this.beginMixedMode = this.endingMixedModeAtEOL = false;
this.lines = this.activeTag = this.activeAttr = undefined;
// Skip the byte order mark (BOM) sequence
// at the beginning of the file if there is one:
// - https://en.wikipedia.org/wiki/Byte_order_mark
// > The Unicode Standard permits the BOM in UTF-8, but does not require or recommend its use.
this.pos = data.charCodeAt(0) === 0xfeff ? 1 : 0;
this.enterState(STATE.CONCISE_HTML_CONTENT);
while (this.pos < maxPos) {
const code = data.charCodeAt(this.pos);
if (code === CODE.NEWLINE) {
this.forward = 1;
this.activeState.eol.call(this, 1, this.activeRange);
} else if (
code === CODE.CARRIAGE_RETURN &&
data.charCodeAt(this.pos + 1) === CODE.NEWLINE
) {
this.forward = 2;
this.activeState.eol.call(this, 2, this.activeRange);
} else {
this.forward = 1;
this.activeState.char.call(this, code, this.activeRange);
}
this.pos += this.forward;
}
while (this.pos === this.maxPos) {
this.forward = 1;
this.activeState.eof.call(this, this.activeRange);
if (this.forward !== 0) break;
}
}
}