-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathintermediate-tokenizer.ts
592 lines (508 loc) · 16.5 KB
/
intermediate-tokenizer.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
import assert from "assert"
import last from "lodash/last"
import type {
ErrorCode,
HasLocation,
Namespace,
Token,
VAttribute,
} from "../ast/index"
import { ParseError } from "../ast/index"
import { debug } from "../common/debug"
import type { Tokenizer, TokenizerState, TokenType } from "./tokenizer"
const DUMMY_PARENT: any = Object.freeze({})
/**
* Concatenate token values.
* @param text Concatenated text.
* @param token The token to concatenate.
*/
function concat(text: string, token: Token): string {
return text + token.value
}
/**
* The type of intermediate tokens.
*/
export type IntermediateToken = StartTag | EndTag | Text | Mustache
/**
* The type of start tags.
*/
export interface StartTag extends HasLocation {
type: "StartTag"
name: string
rawName: string
selfClosing: boolean
attributes: VAttribute[]
}
/**
* The type of end tags.
*/
export interface EndTag extends HasLocation {
type: "EndTag"
name: string
}
/**
* The type of text chunks.
*/
export interface Text extends HasLocation {
type: "Text"
value: string
}
/**
* The type of text chunks of an expression container.
*/
export interface Mustache extends HasLocation {
type: "Mustache"
value: string
startToken: Token
endToken: Token
}
/**
* The class to create HTML tokens from ESTree-like tokens which are created by a Tokenizer.
*/
export class IntermediateTokenizer {
private tokenizer: Tokenizer
private currentToken: IntermediateToken | null
private attribute: VAttribute | null
private attributeNames: Set<string>
private expressionStartToken: Token | null
private expressionTokens: Token[]
public readonly tokens: Token[]
public readonly comments: Token[]
/**
* The source code text.
*/
public get text(): string {
return this.tokenizer.text
}
/**
* The parse errors.
*/
public get errors(): ParseError[] {
return this.tokenizer.errors
}
/**
* The current state.
*/
public get state(): TokenizerState {
return this.tokenizer.state
}
public set state(value: TokenizerState) {
this.tokenizer.state = value
}
/**
* The current namespace.
*/
public get namespace(): Namespace {
return this.tokenizer.namespace
}
public set namespace(value: Namespace) {
this.tokenizer.namespace = value
}
/**
* The current flag of expression enabled.
*/
public get expressionEnabled(): boolean {
return this.tokenizer.expressionEnabled
}
public set expressionEnabled(value: boolean) {
this.tokenizer.expressionEnabled = value
}
/**
* Initialize this intermediate tokenizer.
* @param tokenizer The tokenizer.
*/
public constructor(tokenizer: Tokenizer) {
this.tokenizer = tokenizer
this.currentToken = null
this.attribute = null
this.attributeNames = new Set<string>()
this.expressionStartToken = null
this.expressionTokens = []
this.tokens = []
this.comments = []
}
/**
* Get the next intermediate token.
* @returns The intermediate token or null.
*/
public nextToken(): IntermediateToken | null {
let token: Token | null = null
let result: IntermediateToken | null = null
while (result == null && (token = this.tokenizer.nextToken()) != null) {
result = this[token.type as TokenType](token)
}
if (result == null && token == null && this.currentToken != null) {
result = this.commit()
}
return result
}
/**
* Commit the current token.
*/
private commit(): IntermediateToken {
assert(this.currentToken != null || this.expressionStartToken != null)
let token = this.currentToken
this.currentToken = null
this.attribute = null
if (this.expressionStartToken != null) {
// VExpressionEnd was not found.
// Concatenate the deferred tokens to the committed token.
const start = this.expressionStartToken
const end = last(this.expressionTokens) || start
const value = this.expressionTokens.reduce(concat, start.value)
this.expressionStartToken = null
this.expressionTokens = []
if (token == null) {
token = {
type: "Text",
range: [start.range[0], end.range[1]],
loc: { start: start.loc.start, end: end.loc.end },
value,
}
} else if (token.type === "Text") {
token.range[1] = end.range[1]
token.loc.end = end.loc.end
token.value += value
} else {
throw new Error("unreachable")
}
}
return token as IntermediateToken
}
/**
* Report an invalid character error.
* @param code The error code.
*/
private reportParseError(token: HasLocation, code: ErrorCode): void {
const error = ParseError.fromCode(
code,
token.range[0],
token.loc.start.line,
token.loc.start.column,
)
this.errors.push(error)
debug("[html] syntax error:", error.message)
}
/**
* Process the given comment token.
* @param token The comment token to process.
*/
private processComment(token: Token): IntermediateToken | null {
this.comments.push(token)
if (this.currentToken?.type === "Text") {
return this.commit()
}
return null
}
/**
* Process the given text token.
* @param token The text token to process.
*/
private processText(token: Token): IntermediateToken | null {
this.tokens.push(token)
let result: IntermediateToken | null = null
if (this.expressionStartToken != null) {
// Defer this token until a VExpressionEnd token or a non-text token appear.
const lastToken =
last(this.expressionTokens) || this.expressionStartToken
if (lastToken.range[1] === token.range[0]) {
this.expressionTokens.push(token)
return null
}
result = this.commit()
} else if (this.currentToken != null) {
// Concatenate this token to the current text token.
if (
this.currentToken.type === "Text" &&
this.currentToken.range[1] === token.range[0]
) {
this.currentToken.value += token.value
this.currentToken.range[1] = token.range[1]
this.currentToken.loc.end = token.loc.end
return null
}
result = this.commit()
}
assert(this.currentToken == null)
this.currentToken = {
type: "Text",
range: [token.range[0], token.range[1]],
loc: { start: token.loc.start, end: token.loc.end },
value: token.value,
}
return result
}
/**
* Process a HTMLAssociation token.
* @param token The token to process.
*/
protected HTMLAssociation(token: Token): IntermediateToken | null {
this.tokens.push(token)
if (this.attribute != null) {
this.attribute.range[1] = token.range[1]
this.attribute.loc.end = token.loc.end
if (
this.currentToken == null ||
this.currentToken.type !== "StartTag"
) {
throw new Error("unreachable")
}
this.currentToken.range[1] = token.range[1]
this.currentToken.loc.end = token.loc.end
}
return null
}
/**
* Process a HTMLBogusComment token.
* @param token The token to process.
*/
protected HTMLBogusComment(token: Token): IntermediateToken | null {
return this.processComment(token)
}
/**
* Process a HTMLCDataText token.
* @param token The token to process.
*/
protected HTMLCDataText(token: Token): IntermediateToken | null {
return this.processText(token)
}
/**
* Process a HTMLComment token.
* @param token The token to process.
*/
protected HTMLComment(token: Token): IntermediateToken | null {
return this.processComment(token)
}
/**
* Process a HTMLEndTagOpen token.
* @param token The token to process.
*/
protected HTMLEndTagOpen(token: Token): IntermediateToken | null {
this.tokens.push(token)
let result: IntermediateToken | null = null
if (this.currentToken != null || this.expressionStartToken != null) {
result = this.commit()
}
this.currentToken = {
type: "EndTag",
range: [token.range[0], token.range[1]],
loc: { start: token.loc.start, end: token.loc.end },
name: token.value,
}
return result
}
/**
* Process a HTMLIdentifier token.
* @param token The token to process.
*/
protected HTMLIdentifier(token: Token): IntermediateToken | null {
this.tokens.push(token)
if (
this.currentToken == null ||
this.currentToken.type === "Text" ||
this.currentToken.type === "Mustache"
) {
throw new Error("unreachable")
}
if (this.currentToken.type === "EndTag") {
this.reportParseError(token, "end-tag-with-attributes")
return null
}
if (this.attributeNames.has(token.value)) {
this.reportParseError(token, "duplicate-attribute")
}
this.attributeNames.add(token.value)
this.attribute = {
type: "VAttribute",
range: [token.range[0], token.range[1]],
loc: { start: token.loc.start, end: token.loc.end },
parent: DUMMY_PARENT,
directive: false,
key: {
type: "VIdentifier",
range: [token.range[0], token.range[1]],
loc: { start: token.loc.start, end: token.loc.end },
parent: DUMMY_PARENT,
name: token.value,
rawName: this.text.slice(token.range[0], token.range[1]),
},
value: null,
}
this.attribute.key.parent = this.attribute
this.currentToken.range[1] = token.range[1]
this.currentToken.loc.end = token.loc.end
this.currentToken.attributes.push(this.attribute)
return null
}
/**
* Process a HTMLLiteral token.
* @param token The token to process.
*/
protected HTMLLiteral(token: Token): IntermediateToken | null {
this.tokens.push(token)
if (this.attribute != null) {
this.attribute.range[1] = token.range[1]
this.attribute.loc.end = token.loc.end
this.attribute.value = {
type: "VLiteral",
range: [token.range[0], token.range[1]],
loc: { start: token.loc.start, end: token.loc.end },
parent: this.attribute,
value: token.value,
}
if (
this.currentToken == null ||
this.currentToken.type !== "StartTag"
) {
throw new Error("unreachable")
}
this.currentToken.range[1] = token.range[1]
this.currentToken.loc.end = token.loc.end
}
return null
}
/**
* Process a HTMLRCDataText token.
* @param token The token to process.
*/
protected HTMLRCDataText(token: Token): IntermediateToken | null {
return this.processText(token)
}
/**
* Process a HTMLRawText token.
* @param token The token to process.
*/
protected HTMLRawText(token: Token): IntermediateToken | null {
return this.processText(token)
}
/**
* Process a HTMLSelfClosingTagClose token.
* @param token The token to process.
*/
protected HTMLSelfClosingTagClose(token: Token): IntermediateToken | null {
this.tokens.push(token)
if (this.currentToken == null || this.currentToken.type === "Text") {
throw new Error("unreachable")
}
if (this.currentToken.type === "StartTag") {
this.currentToken.selfClosing = true
} else {
this.reportParseError(token, "end-tag-with-trailing-solidus")
}
this.currentToken.range[1] = token.range[1]
this.currentToken.loc.end = token.loc.end
return this.commit()
}
/**
* Process a HTMLTagClose token.
* @param token The token to process.
*/
protected HTMLTagClose(token: Token): IntermediateToken | null {
this.tokens.push(token)
if (this.currentToken == null || this.currentToken.type === "Text") {
throw new Error("unreachable")
}
this.currentToken.range[1] = token.range[1]
this.currentToken.loc.end = token.loc.end
return this.commit()
}
/**
* Process a HTMLTagOpen token.
* @param token The token to process.
*/
protected HTMLTagOpen(token: Token): IntermediateToken | null {
this.tokens.push(token)
let result: IntermediateToken | null = null
if (this.currentToken != null || this.expressionStartToken != null) {
result = this.commit()
}
this.currentToken = {
type: "StartTag",
range: [token.range[0], token.range[1]],
loc: { start: token.loc.start, end: token.loc.end },
name: token.value,
rawName: this.text.slice(token.range[0] + 1, token.range[1]),
selfClosing: false,
attributes: [],
}
this.attribute = null
this.attributeNames.clear()
return result
}
/**
* Process a HTMLText token.
* @param token The token to process.
*/
protected HTMLText(token: Token): IntermediateToken | null {
return this.processText(token)
}
/**
* Process a HTMLWhitespace token.
* @param token The token to process.
*/
protected HTMLWhitespace(token: Token): IntermediateToken | null {
return this.processText(token)
}
/**
* Process a VExpressionStart token.
* @param token The token to process.
*/
protected VExpressionStart(token: Token): IntermediateToken | null {
if (this.expressionStartToken != null) {
return this.processText(token)
}
const separated =
this.currentToken != null &&
this.currentToken.range[1] !== token.range[0]
const result = separated ? this.commit() : null
this.tokens.push(token)
this.expressionStartToken = token
return result
}
/**
* Process a VExpressionEnd token.
* @param token The token to process.
*/
protected VExpressionEnd(token: Token): IntermediateToken | null {
if (this.expressionStartToken == null) {
return this.processText(token)
}
const start = this.expressionStartToken
const end = last(this.expressionTokens) || start
// If it's '{{}}', it's handled as a text.
if (token.range[0] === start.range[1]) {
this.tokens.pop()
this.expressionStartToken = null
const result = this.processText(start)
this.processText(token)
return result
}
// If invalid notation `</>` exists directly before this token, separate it.
if (end.range[1] !== token.range[0]) {
const result = this.commit()
this.processText(token)
return result
}
// Clear state.
const value = this.expressionTokens.reduce(concat, "")
this.tokens.push(token)
this.expressionStartToken = null
this.expressionTokens = []
// Create token.
const result = this.currentToken != null ? this.commit() : null
this.currentToken = {
type: "Mustache",
range: [start.range[0], token.range[1]],
loc: { start: start.loc.start, end: token.loc.end },
value,
startToken: start,
endToken: token,
}
return result || this.commit()
}
}