/
XmlLexer.scala
251 lines (231 loc) · 6.57 KB
/
XmlLexer.scala
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
package scalariform.lexer
import scala.annotation.{ switch, tailrec }
import scalariform.lexer.Tokens._
import scalariform.utils.Utils
trait XmlLexer extends Lexer {
import ScalaLexer._
import CharConstants._
private def xmlMode: XmlMode = modeStack.head.asInstanceOf[XmlMode]
abstract sealed trait TagState
case object InStartTag extends TagState
case object InEndTag extends TagState
case object Normal extends TagState
private def tagMode = xmlMode.isTagMode
private def tagMode_=(isTagMode: Boolean) {
xmlMode.isTagMode = isTagMode
}
protected def isXmlMode = modeStack.head.isInstanceOf[XmlMode]
class XmlMode(private var tagNestLevel: Int = 0, var isTagMode: Boolean = false, var tagState: TagState = Normal) extends LexerMode {
def nestTag() { tagNestLevel += 1 }
def unnestTag(): Int = {
tagNestLevel -= 1
tagNestLevel
}
def nestingLevel = tagNestLevel
}
protected def fetchXmlToken() {
(ch: @switch) match {
case '<' ⇒ {
if (ch(1) == '/') {
nextChar()
nextChar()
token(XML_END_OPEN)
xmlMode.isTagMode = true
xmlMode.tagState = InEndTag
} else if (ch(1) == '!') {
if (ch(2) == '-') {
getXmlComment()
if (xmlMode.nestingLevel == 0)
modeStack.pop() // Go back to Scala; TODO -- scan for more elems
} else if (ch(2) == '[') {
getXmlCDATA()
if (xmlMode.nestingLevel == 0)
modeStack.pop() // Go back to Scala; TODO -- scan for more elems
} else
throw new ScalaLexerException("Bad XML")
} else if (ch(1) == '?') {
getXmlProcessingInstruction()
if (xmlMode.nestingLevel == 0)
modeStack.pop() // Go back to Scala; TODO -- scan for more elems
} else if (lookaheadIs("<xml:unparsed")) {
getXmlUnparsed()
if (xmlMode.nestingLevel == 0)
modeStack.pop() // Go back to Scala; TODO -- scan for more elems
} else {
nextChar()
token(XML_START_OPEN)
xmlMode.isTagMode = true
xmlMode.tagState = InStartTag
}
// xmlMode.nestTag()
}
case '/' ⇒ {
if (tagMode) {
if (ch(1) == '>') {
nextChar()
nextChar()
token(XML_EMPTY_CLOSE)
xmlMode.isTagMode = false
xmlMode.tagState = Normal
if (xmlMode.nestingLevel == 0)
modeStack.pop() // Go back to Scala; TODO -- scan for more elems
} else
getXmlCharData()
} else
getXmlCharData()
}
case '>' ⇒
if (tagMode) {
nextChar()
token(XML_TAG_CLOSE)
xmlMode.isTagMode = false
xmlMode.tagState match {
case InStartTag ⇒ xmlMode.nestTag()
case InEndTag ⇒ {
val nestingLevel = xmlMode.unnestTag()
if (nestingLevel == 0)
modeStack.pop() // Go back to Scala; TODO -- scan for more elems
}
case Normal ⇒ throw new AssertionError("shouldn't reach here")
}
} else
getXmlCharData()
case '=' ⇒
if (tagMode) {
nextChar()
token(XML_ATTR_EQ)
} else {
getXmlCharData()
}
case '\'' ⇒
if (tagMode) {
getXmlAttributeValue('\'')
} else {
getXmlCharData()
}
case '"' ⇒
if (tagMode) {
getXmlAttributeValue('"')
} else {
getXmlCharData()
}
case '{' ⇒
if (ch(1) != '{')
switchToScalaModeAndFetchToken
else
getXmlCharData() // TODO: tagMode?
case EOF_CHAR ⇒ token(EOF)
case _ ⇒
if (tagMode && isNameStart(ch.asInstanceOf[Char])) {
getXmlName()
} else if (tagMode && isSpace(ch.asInstanceOf[Char])) {
getXmlSpace()
} else {
getXmlCharData()
// throw new ScalaLexerException("illegal character in xml: " + Character.valueOf(ch.asInstanceOf[Char]))
// TODO
}
}
}
private def getXmlCDATA() {
munch("<![CDATA[")
var continue = true
while (continue) {
if (lookaheadIs("]]>")) {
munch("]]>")
continue = false
} else if (ch == EOF_CHAR)
throw new ScalaLexerException("Malformed XML CDATA")
else
nextChar()
}
token(XML_CDATA)
}
private def getXmlComment() {
munch("<!--")
var continue = true
while (continue) {
if (ch == '-' && ch(1) == '-') {
nextChar()
nextChar()
if (ch != '>')
throw new ScalaLexerException("Malformed XML comment")
nextChar()
continue = false
} else if (ch == EOF_CHAR)
throw new ScalaLexerException("Malformed XML comment")
else
nextChar()
}
token(XML_COMMENT)
}
private def getXmlCharData() {
var continue = true
while (continue) {
if (ch == EOF_CHAR || ch == '<')
continue = false
else if (ch == '{')
if (ch(1) == '{') {
nextChar(); nextChar()
} else
continue = false
else
nextChar()
}
token(XML_PCDATA)
}
// S
private def getXmlSpace() {
require(isSpace(ch.asInstanceOf[Char]))
nextChar()
while (ch != EOF_CHAR && isSpace(ch.asInstanceOf[Char]))
nextChar()
token(XML_WHITESPACE)
}
private def getXmlName() {
require(isNameStart(ch.asInstanceOf[Char]))
nextChar()
while (ch != EOF_CHAR && isNameChar(ch.asInstanceOf[Char]))
nextChar()
// TODO endswith colon?
token(XML_NAME)
}
private def getXmlAttributeValue(quote: Int) {
require(quote == '\'' || quote == '\"')
require(ch == quote)
nextChar()
while (ch != quote) {
if (ch == EOF_CHAR) // TODO: line seps etc
throw new ScalaLexerException("Unterminated attribute value")
else
nextChar()
}
require(ch == quote)
nextChar()
token(XML_ATTR_VALUE)
}
private def getXmlUnparsed() {
munch("<xml:unparsed")
var continue = true
while (continue) {
if (lookaheadIs("</xml:unparsed>")) {
munch("</xml:unparsed>")
continue = false
} else
nextChar()
}
token(XML_UNPARSED)
}
private def getXmlProcessingInstruction() {
munch("<?")
var continue = true
while (continue) {
if (lookaheadIs("?>")) {
munch("?>")
continue = false
} else
nextChar()
}
token(XML_PROCESSING_INSTRUCTION)
}
}