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
|
/* NSC -- new Scala compiler
* Copyright 2005-2011 LAMP/EPFL
* @author Burak Emir
*/
package scala.tools.nsc
package ast.parser
import scala.collection.mutable
import mutable.{ Buffer, ArrayBuffer, ListBuffer }
import scala.util.control.ControlThrowable
import scala.tools.nsc.util.{SourceFile,CharArrayReader}
import scala.xml.{ Text, TextBuffer }
import scala.xml.parsing.MarkupParserCommon
import scala.xml.Utility.{ isNameStart, isNameChar, isSpace }
import scala.reflect.internal.Chars.{ SU, LF }
// XXX/Note: many/most of the functions in here are almost direct cut and pastes
// from another file - scala.xml.parsing.MarkupParser, it looks like.
// (It was like that when I got here.) They used to be commented "[Duplicate]" but
// since approximately all of them were, I snipped it as noise. As far as I can
// tell this wasn't for any particularly good reason, but slightly different
// compiler and library parser interfaces meant it would take some setup.
//
// I rewrote most of these, but not as yet the library versions: so if you are
// tempted to touch any of these, please be aware of that situation and try not
// to let it get any worse. -- paulp
/** This trait ...
*
* @author Burak Emir
* @version 1.0
*/
trait MarkupParsers {
self: Parsers =>
case object MissingEndTagControl extends ControlThrowable {
override def getMessage = "start tag was here: "
}
case object ConfusedAboutBracesControl extends ControlThrowable {
override def getMessage = " I encountered a '}' where I didn't expect one, maybe this tag isn't closed <"
}
case object TruncatedXMLControl extends ControlThrowable {
override def getMessage = "input ended while parsing XML"
}
import global._
class MarkupParser(parser: SourceFileParser, final val preserveWS: Boolean) extends MarkupParserCommon {
import Tokens.{ EMPTY, LBRACE, RBRACE }
type PositionType = Position
type InputType = CharArrayReader
type ElementType = Tree
type AttributesType = mutable.Map[String, Tree]
type NamespaceType = Any // namespaces ignored
def mkAttributes(name: String, other: NamespaceType): AttributesType = xAttributes
val eof = false
def truncatedError(msg: String): Nothing = throw TruncatedXMLControl
def xHandleError(that: Char, msg: String) =
if (ch == SU) throw TruncatedXMLControl
else reportSyntaxError(msg)
var input : CharArrayReader = _
def lookahead(): BufferedIterator[Char] =
(input.buf drop input.charOffset).iterator.buffered
import parser.{ symbXMLBuilder => handle, o2p, r2p }
def curOffset : Int = input.charOffset - 1
var tmppos : Position = NoPosition
def ch = input.ch
/** this method assign the next character to ch and advances in input */
def nextch() { input.nextChar() }
protected def ch_returning_nextch: Char = {
val result = ch; input.nextChar(); result
}
def mkProcInstr(position: Position, name: String, text: String): ElementType =
parser.symbXMLBuilder.procInstr(position, name, text)
var xEmbeddedBlock = false
private var debugLastStartElement = new mutable.Stack[(Int, String)]
private def debugLastPos = debugLastStartElement.top._1
private def debugLastElem = debugLastStartElement.top._2
private def errorBraces() = {
reportSyntaxError("in XML content, please use '}}' to express '}'")
throw ConfusedAboutBracesControl
}
def errorNoEnd(tag: String) = {
reportSyntaxError("expected closing tag of " + tag)
throw MissingEndTagControl
}
/** checks whether next character starts a Scala block, if yes, skip it.
* @return true if next character starts a scala block
*/
def xCheckEmbeddedBlock: Boolean = {
// attentions, side-effect, used in xText
xEmbeddedBlock = (ch == '{') && { nextch; (ch != '{') }
xEmbeddedBlock
}
/** parse attribute and add it to listmap
* [41] Attributes ::= { S Name Eq AttValue }
* AttValue ::= `'` { _ } `'`
* | `"` { _ } `"`
* | `{` scalablock `}`
*/
def xAttributes = {
val aMap = mutable.LinkedHashMap[String, Tree]()
while (isNameStart(ch)) {
val start = curOffset
val key = xName
xEQ
val delim = ch
val mid = curOffset
val value: Tree = ch match {
case '"' | '\'' =>
val tmp = xAttributeValue(ch_returning_nextch)
try handle.parseAttribute(r2p(start, mid, curOffset), tmp)
catch {
case e: RuntimeException =>
errorAndResult("error parsing attribute value", parser.errorTermTree)
}
case '{' =>
nextch
xEmbeddedExpr
case SU =>
throw TruncatedXMLControl
case _ =>
errorAndResult("' or \" delimited attribute value or '{' scala-expr '}' expected", Literal(Constant("<syntax-error>")))
}
// well-formedness constraint: unique attribute names
if (aMap contains key)
reportSyntaxError("attribute %s may only be defined once" format key)
aMap(key) = value
if (ch != '/' && ch != '>')
xSpace
}
aMap
}
/** '<! CharData ::= [CDATA[ ( {char} - {char}"]]>"{char} ) ']]>'
*
* see [15]
*/
def xCharData: Tree = {
val start = curOffset
xToken("[CDATA[")
val mid = curOffset
xTakeUntil(handle.charData, () => r2p(start, mid, curOffset), "]]>")
}
def xUnparsed: Tree = {
val start = curOffset
xTakeUntil(handle.unparsed, () => r2p(start, start, curOffset), "</xml:unparsed>")
}
/** Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
*
* see [15]
*/
def xComment: Tree = {
val start = curOffset - 2 // Rewinding to include "<!"
xToken("--")
xTakeUntil(handle.comment, () => r2p(start, start, curOffset), "-->")
}
def appendText(pos: Position, ts: Buffer[Tree], txt: String) {
val toAppend =
if (preserveWS) Seq(txt)
else TextBuffer.fromString(txt).toText map (_.text)
toAppend foreach (t => ts append handle.text(pos, t))
}
/** adds entity/character to ts as side-effect
* @precond ch == '&'
*/
def content_AMP(ts: ArrayBuffer[Tree]) {
nextch
val toAppend = ch match {
case '#' => // CharacterRef
nextch
val theChar = handle.text(tmppos, xCharRef)
xToken(';')
theChar
case _ => // EntityRef
val n = xName
xToken(';')
handle.entityRef(tmppos, n)
}
ts append toAppend
}
/**
* @precond ch == '{'
* @postcond: xEmbeddedBlock == false!
*/
def content_BRACE(p: Position, ts: ArrayBuffer[Tree]): Unit =
if (xCheckEmbeddedBlock) ts append xEmbeddedExpr
else appendText(p, ts, xText)
/** Returns true if it encounters an end tag (without consuming it),
* appends trees to ts as side-effect.
*
* @param ts ...
* @return ...
*/
private def content_LT(ts: ArrayBuffer[Tree]): Boolean = {
if (ch == '/')
return true // end tag
val toAppend = ch match {
case '!' => nextch ; if (ch =='[') xCharData else xComment // CDATA or Comment
case '?' => nextch ; xProcInstr // PI
case _ => element // child node
}
ts append toAppend
false
}
def content: Buffer[Tree] = {
val ts = new ArrayBuffer[Tree]
while (true) {
if (xEmbeddedBlock)
ts append xEmbeddedExpr
else {
tmppos = o2p(curOffset)
ch match {
// end tag, cdata, comment, pi or child node
case '<' => nextch ; if (content_LT(ts)) return ts
// either the character '{' or an embedded scala block }
case '{' => content_BRACE(tmppos, ts) // }
// EntityRef or CharRef
case '&' => content_AMP(ts)
case SU => return ts
// text content - here xEmbeddedBlock might be true
case _ => appendText(tmppos, ts, xText)
}
}
}
unreachable
}
/** '<' element ::= xmlTag1 '>' { xmlExpr | '{' simpleExpr '}' } ETag
* | xmlTag1 '/' '>'
*/
def element: Tree = {
val start = curOffset
val (qname, attrMap) = xTag(())
if (ch == '/') { // empty element
xToken("/>")
handle.element(r2p(start, start, curOffset), qname, attrMap, new ListBuffer[Tree])
}
else { // handle content
xToken('>')
if (qname == "xml:unparsed")
return xUnparsed
debugLastStartElement.push((start, qname))
val ts = content
xEndTag(qname)
debugLastStartElement.pop
val pos = r2p(start, start, curOffset)
qname match {
case "xml:group" => handle.group(pos, ts)
case _ => handle.element(pos, qname, attrMap, ts)
}
}
}
/** parse character data.
* precondition: xEmbeddedBlock == false (we are not in a scala block)
*/
private def xText: String = {
assert(!xEmbeddedBlock, "internal error: encountered embedded block")
val buf = new StringBuilder
def done = buf.toString
while (ch != SU) {
if (ch == '}') {
if (charComingAfter(nextch) == '}') nextch
else errorBraces()
}
buf append ch
nextch
if (xCheckEmbeddedBlock || ch == '<' || ch == '&')
return done
}
done
}
/** Some try/catch/finally logic used by xLiteral and xLiteralPattern. */
private def xLiteralCommon(f: () => Tree, ifTruncated: String => Unit): Tree = {
try return f()
catch {
case c @ TruncatedXMLControl =>
ifTruncated(c.getMessage)
case c @ (MissingEndTagControl | ConfusedAboutBracesControl) =>
parser.syntaxError(debugLastPos, c.getMessage + debugLastElem + ">")
case _: ArrayIndexOutOfBoundsException =>
parser.syntaxError(debugLastPos, "missing end tag in XML literal for <%s>" format debugLastElem)
}
finally parser.in resume Tokens.XMLSTART
parser.errorTermTree
}
/** Use a lookahead parser to run speculative body, and return the first char afterward. */
private def charComingAfter(body: => Unit): Char = {
try {
input = input.lookaheadReader
body
ch
}
finally input = parser.in
}
/** xLiteral = element { element }
* @return Scala representation of this xml literal
*/
def xLiteral: Tree = xLiteralCommon(
() => {
input = parser.in
handle.isPattern = false
val ts = new ArrayBuffer[Tree]
val start = curOffset
tmppos = o2p(curOffset) // Iuli: added this line, as it seems content_LT uses tmppos when creating trees
content_LT(ts)
// parse more XML ?
if (charComingAfter(xSpaceOpt) == '<') {
xSpaceOpt
while (ch == '<') {
nextch
ts append element
xSpaceOpt
}
handle.makeXMLseq(r2p(start, start, curOffset), ts)
}
else {
assert(ts.length == 1)
ts(0)
}
},
msg => parser.incompleteInputError(msg)
)
/** @see xmlPattern. resynchronizes after successful parse
* @return this xml pattern
*/
def xLiteralPattern: Tree = xLiteralCommon(
() => {
input = parser.in
saving[Boolean, Tree](handle.isPattern, handle.isPattern = _) {
handle.isPattern = true
val tree = xPattern
xSpaceOpt
tree
}
},
msg => parser.syntaxError(curOffset, msg)
)
def escapeToScala[A](op: => A, kind: String) = {
xEmbeddedBlock = false
val res = saving[List[Int], A](parser.in.sepRegions, parser.in.sepRegions = _) {
parser.in resume LBRACE
op
}
if (parser.in.token != RBRACE)
reportSyntaxError(" expected end of Scala "+kind)
res
}
def xEmbeddedExpr: Tree = escapeToScala(parser.block(), "block")
/** xScalaPatterns ::= patterns
*/
def xScalaPatterns: List[Tree] = escapeToScala(parser.seqPatterns(), "pattern")
def reportSyntaxError(pos: Int, str: String) = parser.syntaxError(pos, str)
def reportSyntaxError(str: String) {
reportSyntaxError(curOffset, "in XML literal: " + str)
nextch()
}
/** '<' xPattern ::= Name [S] { xmlPattern | '{' pattern3 '}' } ETag
* | Name [S] '/' '>'
*/
def xPattern: Tree = {
var start = curOffset
val qname = xName
debugLastStartElement.push((start, qname))
xSpaceOpt
val ts = new ArrayBuffer[Tree]
val isEmptyTag = (ch == '/') && { nextch ; true }
xToken('>')
if (!isEmptyTag) {
// recurses until it hits a termination condition, then returns
def doPattern: Boolean = {
val start1 = curOffset
if (xEmbeddedBlock) ts ++= xScalaPatterns
else ch match {
case '<' => // tag
nextch
if (ch != '/') ts append xPattern // child
else return false // terminate
case '{' => // embedded Scala patterns
while (ch == '{') {
nextch
ts ++= xScalaPatterns
}
assert(!xEmbeddedBlock, "problem with embedded block")
case SU =>
throw TruncatedXMLControl
case _ => // text
appendText(r2p(start1, start1, curOffset), ts, xText)
// here xEmbeddedBlock might be true:
// if (xEmbeddedBlock) throw new ApplicationError("after:"+text); // assert
}
true
}
while (doPattern) { } // call until false
xEndTag(qname)
debugLastStartElement.pop
}
handle.makeXMLpat(r2p(start, start, curOffset), qname, ts)
}
} /* class MarkupParser */
}
|