-
Notifications
You must be signed in to change notification settings - Fork 59
/
lexer.js
210 lines (183 loc) · 4.06 KB
/
lexer.js
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
const EOF = void 0
function HTMLLexicalParser (syntaxer) {
let state = data
let token = null
let attribute = null
let characterReference = ''
this.receiveInput = function (char) {
if (state == null) {
throw new Error('there is an error')
} else {
state = state(char)
}
}
this.reset = function () {
state = data
}
function data (c) {
switch (c) {
case '&':
return characterReferenceInData
case '<':
return tagOpen
// perhaps will not encounter in javascript?
// case '\0':
// error()
// emitToken(c)
// return data
// can be handle by default case
// case EOF:
// emitToken(EOF)
// return data
default:
emitToken(c)
return data
}
}
// only handle right character reference
function characterReferenceInData (c) {
if (c === ';') {
characterReference += c
emitToken(characterReference)
characterReference = ''
return data
} else {
characterReference += c
return characterReferenceInData
}
}
function tagOpen (c) {
if (c === '/') {
return endTagOpen
}
if (/[a-zA-Z]/.test(c)) {
token = new StartTagToken()
token.name = c.toLowerCase()
return tagName
}
// no need to handle this
// if (c === '?') {
// return bogusComment
// }
return error(c)
}
function tagName (c) {
if (c === '/') {
return selfClosingTag
}
if (/[\t \f\n]/.test(c)) {
return beforeAttributeName
}
if (c === '>') {
emitToken(token)
return data
}
if (/[a-zA-Z]/.test(c)) {
token.name += c.toLowerCase()
return tagName
}
}
function beforeAttributeName (c) {
if (/[\t \f\n]/.test(c)) {
return beforeAttributeName
}
if (c === '/') {
return selfClosingTag
}
if (c === '>') {
emitToken(token)
return data
}
if (/["'<]/.test(c)) {
return error(c)
}
attribute = new Attribute()
attribute.name = c.toLowerCase()
attribute.value = ''
return attributeName
}
function attributeName (c) {
if (c === '/') {
token[attribute.name] = attribute.value
return selfClosingTag
}
if (c === '=') {
return beforeAttributeValue
}
if (/[\t \f\n]/.test(c)) {
return beforeAttributeName
}
attribute.name += c.toLowerCase()
return attributeName
}
function beforeAttributeValue (c) {
if (c === '"') {
return attributeValueDoubleQuoted
}
if (c === "'") {
return attributeValueSingleQuoted
}
if (/\t \f\n/.test(c)) {
return beforeAttributeValue
}
attribute.value += c
return attributeValueUnquoted
}
function attributeValueDoubleQuoted (c) {
if (c === '"') {
token[attribute.name] = attribute.value
return beforeAttributeName
}
attribute.value += c
return attributeValueDoubleQuoted
}
function attributeValueSingleQuoted (c) {
if (c === "'") {
token[attribute.name] = attribute.value
return beforeAttributeName
}
attribute.value += c
return attributeValueSingleQuoted
}
function attributeValueUnquoted (c) {
if (/[\t \f\n]/.test(c)) {
token[attribute.name] = attribute.value
return beforeAttributeName
}
attribute.value += c
return attributeValueUnquoted
}
function selfClosingTag (c) {
if (c === '>') {
emitToken(token)
endToken = new EndTagToken()
endToken.name = token.name
emitToken(endToken)
return data
}
}
function endTagOpen (c) {
if (/[a-zA-Z]/.test(c)) {
token = new EndTagToken()
token.name = c.toLowerCase()
return tagName
}
if (c === '>') {
return error(c)
}
}
function emitToken (token) {
syntaxer.receiveInput(token)
}
function error (c) {
console.log(`warn: unexpected char '${c}'`)
}
}
class StartTagToken {}
class EndTagToken {}
class Attribute {}
module.exports = {
HTMLLexicalParser,
StartTagToken,
EndTagToken
}