forked from vuejs/vue-eslint-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.ts
230 lines (214 loc) · 6.67 KB
/
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
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
/* eslint-disable class-methods-use-this, no-empty-function */
import { Tokenizer as HTMLTokenizer } from "../html/tokenizer"
import { ParseError, Token } from "../ast"
const pugLex = require("pug-lexer") //eslint-disable-line no-restricted-globals
/**
*
*/
export class PugTokenizer extends HTMLTokenizer {
private pugTokens: any
private convertedPugTokens: Token[]
private pugPosition: number
private lastToken: any
private lastOpenedTag: any
/**
* Initialize this tokenizer.
* @param text The source code to tokenize.
*/
constructor(text: string, code: string, lastToken: any) {
super(code)
this.lastOpenedTag = null
this.pugPosition = 0
this.lastToken = lastToken
let sourcePosition = lastToken.range[1]
// const initialRangeStart = lastToken.range[1]
// const rangeStart = lastToken.range[1]
try {
pugLex.Lexer.prototype.incrementLine = function(increment: any) {
this.lineno += increment
sourcePosition += increment
if (increment) {
this.colno = 0
}
}
pugLex.Lexer.prototype.incrementColumn = function(increment: any) {
sourcePosition += increment
this.colno += increment
}
pugLex.Lexer.prototype.tok = function(type: any, val: any) {
const res = {
type,
loc: {
start: {
line: this.lineno,
column: this.colno,
},
filename: this.filename,
},
range: [sourcePosition],
val: undefined,
}
if (val !== undefined) {
res.val = val
}
return res
}
pugLex.Lexer.prototype.tokEnd = function(tok: any) {
tok.loc.end = {
line: this.lineno,
column: this.colno,
}
tok.range.push(sourcePosition)
return tok
}
this.pugTokens = pugLex(text, { startingLine: lastToken.loc.end.line, startingColumn: lastToken.loc.end.column })
// plugins: [{
// advance(lexer: any) {
// console.log("LTL: ", lexer.tokens.length)
// if (lexer.tokens.length) {
// const lastProcessedToken = lexer.tokens[lexer.tokens.length - 1]
// const rangeEnd = lexer.originalInput.length - lexer.input.length + initialRangeStart
// lastProcessedToken.range = [rangeStart, rangeEnd]
// rangeStart = rangeEnd
// }
// },
// }]
this.convertedPugTokens = []
this.convertPugTokensToHtmlTokens()
}
catch (e) {
this.errors.push(new ParseError(e.msg, e.code, sourcePosition, e.line, e.column))
this.convertedPugTokens = []
}
}
/**
* Get the next token.
* @returns The next token or null.
*/
public nextToken(): any {
if (this.convertedPugTokens.length > this.pugPosition) {
const result = this.convertedPugTokens[this.pugPosition]
this.pugPosition++
return result
}
return null
}
/**
* Get the next token.
* @returns The next token or null.
*/
private convertPugTokensToHtmlTokens(): any {
try {
console.log(JSON.stringify(this.pugTokens, null, 2))
for (const pt of this.pugTokens) {
(this as any)[pt.type](pt)
if (this.convertedPugTokens.length > 0) {
this.lastToken = this.convertedPugTokens[this.convertedPugTokens.length - 1]
}
}
}
catch (e) {
console.error(e)
}
}
/** */
protected indent() : any {
}
/** */
protected outdent() : any {
}
/** */
protected newline(pt: any) : any {
this.closeLastOpenedTag()
this.convertedPugTokens.push({
type: "HTMLWhitespace",
range: [
pt.range[0] - 1,
pt.range[1],
],
loc: {
start: this.lastToken.loc.end,
end: {
line: pt.loc.end.line,
column: pt.loc.end.column - 1,
},
},
value: "\n",
})
}
/** */
protected tag(pt: any) : any {
this.closeLastOpenedTag()
this.convertedPugTokens.push({
type: "HTMLTagOpen",
range: [
pt.range[0] - 1,
pt.range[1],
],
loc: pt.loc,
value: pt.val,
})
this.lastOpenedTag = pt
}
/** */
protected closeLastOpenedTag() : any {
if (!this.lastOpenedTag) {
return
}
const pt = this.lastOpenedTag
this.convertedPugTokens.push({
type: "HTMLTagClose",
range: [pt.range[1], pt.range[1]],
loc: {
start: pt.loc.end,
end: pt.loc.end,
},
value: "",
})
this.convertedPugTokens.push({
type: "HTMLEndTagOpen",
range: [pt.range[1], pt.range[1]],
loc: {
start: pt.loc.end,
end: pt.loc.end,
},
value: pt.val,
})
this.convertedPugTokens.push({
type: "HTMLTagClose",
range: [pt.range[1], pt.range[1]],
loc: {
start: pt.loc.end,
end: pt.loc.end,
},
value: pt.val,
})
this.lastOpenedTag = null
}
/** */
protected "start-attributes"() : any {
}
/** */
protected "end-attributes"() : any {
this.closeLastOpenedTag()
}
/** */
protected attribute(pt: any) : any {
this.convertedPugTokens.push({
type: "HTMLIdentifier",
range: pt.range,
loc: pt.loc,
value: pt.name,
})
}
/** */
protected eos() : any {
this.closeLastOpenedTag()
}
}
/* eslint-enable class-methods-use-this, no-empty-function */