summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala
blob: 1d6b1319a787431d16f458f397f6b5957fcf085e (plain) (blame)
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
/* NSC -- new Scala compiler
 * Copyright 2005-2006 LAMP/EPFL
 * @author Burak Emir
 */
// $Id$

package scala.tools.nsc.ast.parser

import scala.collection.immutable.{Map, ListMap}
import scala.collection.mutable
import scala.tools.nsc.util.Position
import scala.xml.{EntityRef, Text, TextBuffer}
import symtab.Flags.MUTABLE

/** This class builds instance of Tree that represent XML.
 *
 *  @author  Burak Emir
 *  @version 1.0
 */
abstract class SymbolicXMLBuilder(make: TreeBuilder, p: Parsers # Parser, preserveWS: Boolean) {

  val global: Global
  import global._
  import RequiresIntsAsPositions._
  import global.posAssigner.atPos
  //import scala.tools.scalac.ast.{TreeList => myTreeList}

  var isPattern:Boolean = _

  def _Attribute           = global.newTypeName("Attribute")
  def _MetaData            = global.newTypeName("MetaData")
  def _NamespaceBinding    = global.newTypeName("NamespaceBinding")
  def _NodeBuffer          = global.newTypeName("NodeBuffer")
  def _Null                = global.newTermName("Null")

  def _PrefixedAttribute   = global.newTypeName("PrefixedAttribute")
  def _UnprefixedAttribute = global.newTypeName("UnprefixedAttribute")
  def _Elem                = global.newTypeName("Elem")
  def _Group               = global.newTypeName("Group")
  def _Unparsed            = global.newTypeName("Unparsed")
  def _Seq                 = global.newTypeName("Seq")
  def _immutable           = global.newTermName("immutable")
  def _mutable             = global.newTermName("mutable")
  def _append              = global.newTermName("append")
  def _plus                = global.newTermName("$amp$plus")
  def _collection          = global.newTermName("collection")
  def _toList              = global.newTermName("toList")
  def _xml                 = global.newTermName("xml")
  def _Comment             = global.newTypeName("Comment")
  def _Node                = global.newTypeName("Node")
  def _None                = global.newTermName("None")
  def _Some                = global.newTypeName("Some")
  def _ProcInstr           = global.newTypeName("ProcInstr")
  def _Text                = global.newTypeName("Text")
  def _EntityRef           = global.newTypeName("EntityRef")
  final def _md = global.newTermName("$md")
  final def _scope = global.newTermName("$scope")
  final def _tmpscope = global.newTermName("$tmpscope")

  // convenience methods
  private def LL[A](x:A*):List[List[A]] = List(List(x:_*))

  private def _scala(name: Name) =
    Select(Select(Ident(nme.ROOTPKG), nme.scala_), name)

  private def _scala_Seq   = _scala(_Seq)
  private def _scala_xml(name: Name) = Select(_scala(_xml), name)

  private def _scala_xml_MetaData           = _scala_xml(_MetaData)
  private def _scala_xml_NamespaceBinding   = _scala_xml(_NamespaceBinding)
  private def _scala_xml_Null               = _scala_xml(_Null)
  private def _scala_xml_PrefixedAttribute  = _scala_xml(_PrefixedAttribute)
  private def _scala_xml_UnprefixedAttribute= _scala_xml(_UnprefixedAttribute)
  private def _scala_xml_Node               = _scala_xml(_Node)
  private def _scala_xml_NodeBuffer         = _scala_xml(_NodeBuffer)
  private def _scala_xml_EntityRef          = _scala_xml(_EntityRef)
  private def _scala_xml_Comment            = _scala_xml(_Comment)
  private def _scala_xml_ProcInstr          = _scala_xml(_ProcInstr)
  private def _scala_xml_Text               = _scala_xml(_Text)
  private def _scala_xml_Elem               = _scala_xml(_Elem)
  private def _scala_xml_Attribute          = _scala_xml(_Attribute)
  private def _scala_xml_Group              = _scala_xml(_Group)
  private def _scala_xml_Unparsed           = _scala_xml(_Unparsed)

  /*
  private def bufferToArray(buf: mutable.Buffer[Tree]): Array[Tree] = {
    val arr = new Array[Tree](buf.length)
    var i = 0
    for (val x <- buf.elements) { arr(i) = x; i = i + 1; }
    arr
  }
  */

  // create scala xml tree

  /**
   *  @arg  namespace: a Tree of type defs.STRING_TYPE
   *  @arg  label:     a Tree of type defs.STRING_TYPE
   *  @todo map:       a map of attributes !!!
   */

  protected def mkXML(pos: int, isPattern: boolean, pre: Tree, label: Tree, attrs: /*Array[*/Tree/*]*/ , scope:Tree, children: mutable.Buffer[Tree]): Tree = {
    if (isPattern) {
      convertToTextPat(children)
      atPos (pos) { //@todo maybe matching on attributes, scope?
        Apply( _scala_xml_Elem, List(
          pre, label, Ident( nme.WILDCARD ) /* md */ , Ident( nme.WILDCARD )) /* scope */ ::: children.toList )
      }
    } else {
      var ab = List(pre, label, attrs, scope);
      if (children.length > 0)
        ab = ab ::: List(Typed(makeXMLseq(pos, children), Ident(nme.WILDCARD_STAR.toTypeName)));
      atPos(pos) { New( _scala_xml_Elem, List(ab) )}
    }
  }

  final def entityRef(pos: int, n: String) = {
    atPos(pos) { New( _scala_xml_EntityRef, LL(Literal(Constant( n )))) }

  };
  // create scala.xml.Text here <: scala.xml.Node
  final def text(pos: Int, txt:String): Tree =  {
    //makeText( isPattern, gen.mkStringLit( txt ))
    val txt1 = Literal(Constant(txt))
    atPos(pos) {
      if (isPattern)
        makeTextPat(txt1)
      else
        makeText1(txt1)
    }
  }

  // create scala.xml.Text here <: scala.xml.Node
  def makeTextPat(txt: Tree) = Apply(_scala_xml_Text, List(txt))

  def makeText1(txt: Tree) =
    New(_scala_xml_Text, LL(txt))

  // create
  def comment(pos: int, text: String): Tree =
    atPos(pos) { Comment( Literal(Constant(text))) }

  // create
  def charData(pos: int, txt: String): Tree =
    atPos(pos) { makeText1(Literal(Constant(txt))) }; //{ CharData( Literal(Constant(txt))) };

  // create scala.xml.Text here <: scala.xml.Node
  def procInstr( pos: int, target: String, txt: String ) =
    atPos(pos) { ProcInstr(Literal(Constant(target)), Literal(Constant(txt))) }

  protected def Comment(txt: Tree) = New(_scala_xml_Comment, LL(txt))

  protected def ProcInstr(target: Tree, txt: Tree) =
    New(_scala_xml_ProcInstr, LL(target, txt))

  /** @todo: attributes */
  def makeXMLpat(pos: int, n: String, args: mutable.Buffer[Tree]): Tree =
    mkXML(pos,
          true,
          Ident( nme.WILDCARD ),
          Literal(Constant(n)),
          null, //Array[Tree](),
          null,
          args);

  protected def convertToTextPat(t: Tree): Tree = t match {
    case _:Literal => makeTextPat(t)
    case _ => t
  }

  def parseAttribute(pos: Int, s: String): Tree = {
    val ns = xml.Utility.parseAttributeValue(s)
    val ts:collection.mutable.ListBuffer[Tree] = new collection.mutable.ListBuffer
    val it = ns.elements
    while(it.hasNext) it.next match {
      case Text(s)      => ts += text(pos, s) // makeText1(Literal(Constant(s)))
      case EntityRef(s) => ts += entityRef(pos, s)
    }
    ts.length match {
      case 0 => gen.mkNil
      case 1 => val t = ts(0); ts.clear; t
      case _ => makeXMLseq(pos, ts)
    }
  }
  protected def convertToTextPat(buf: mutable.Buffer[Tree]): Unit = {
    var i = 0; while (i < buf.length) {
      val t1 = buf(i)
      val t2 = convertToTextPat(t1)
      if (!t1.eq(t2)) {
        buf.remove(i)
        buf.insert(i, t2)
      }
      i = i + 1
    }
  }

  def freshName(prefix: String): Name

  def isEmptyText(t:Tree) = t match {
    case Literal(Constant("")) => true
    case _ => false
  }

  def makeXMLseq( pos:int, args:mutable.Buffer[Tree] ) = {
    var _buffer = New( _scala_xml_NodeBuffer, List(Nil))
    val it = args.elements
    while (it.hasNext) {
      val t = it.next
      if (!isEmptyText(t)) {
        _buffer = Apply(Select(_buffer, _plus), List(t))
      }
    }
    atPos(pos) { Select(_buffer, _toList) }
  }

  /** returns Some(prefix) if pre:name, None otherwise */
  def getPrefix(name: String): Option[String] = {
    val i = name.indexOf(':')
    if (i != -1) Some(name.substring(0, i)) else None
  }

  def group(pos: int, args: mutable.Buffer[Tree]): Tree = {
    atPos(pos) { New( _scala_xml_Group, LL( makeXMLseq(pos, args))) }
  }

  /** code that constructs an unparsed node
  */
  def unparsed(pos: int, str: String): Tree = {
    atPos(pos) { New( _scala_xml_Unparsed, LL( Literal(Constant(str)))) }
  }

  /** makes an element */
  def element(pos: int, qname: String, attrMap: mutable.Map[String,Tree], args: mutable.Buffer[Tree]): Tree = {
    //Console.println("SymbolicXMLBuilder::element("+pos+","+qname+","+attrMap+","+args+")");
    var setNS = new mutable.HashMap[String, Tree]

    var tlist: List[Tree] = List()

    /* pre can be null */
    def handleNamespaceBinding(pre:String , uri1: Tree): Unit = uri1 match {
      case Apply(_,List(uri @ Literal(Constant(_)))) => //text
        val t = Assign(Ident(_tmpscope), New( _scala_xml_NamespaceBinding,
                                             LL(Literal(Constant(pre)), uri, Ident( _tmpscope))));
        tlist = t :: tlist
      //Console.println("SymbolicXMLBuilder::handleNamespaceBinding:");
      //Console.println(t.toString());
    }

    /* DEBUG */
    val attrIt = attrMap.keys
    while (attrIt.hasNext) {
      val z = attrIt.next
      if (z.startsWith("xmlns")) {  // handle namespace
        val i = z.indexOf(':')
        if( i == -1 )
          handleNamespaceBinding(null, attrMap(z))
          //setNS.update("default", attrMap(z))
        else {
          val zz = z.substring(i+1, z.length())
          //setNS.update( zz, attrMap( z ) );
          handleNamespaceBinding(zz, attrMap(z))
        }
        attrMap -= z
      }
    }

    val moreNamespaces = (0 < tlist.length)

    /* */
    val i = qname.indexOf(':')

    var newlabel = qname
    val pre = getPrefix(qname) match {
      case Some(p) =>
        newlabel = qname.substring(p.length()+1, qname.length())
        p
      case None =>
        null
    }
    var tlist2: List[Tree] = List()

    // make attributes

    def handlePrefixedAttribute(pre:String, key:String, value:Tree): Unit = {
      val t = atPos(pos) {
        Assign(Ident(_md), New( _scala_xml_PrefixedAttribute,
                                       LL(
                                         Literal(Constant(pre)),
                                         Literal(Constant(key)),
                                         value,
                                         Ident(_md)
                                       )))};
      tlist2 = t :: tlist2;
     // Console.println("SymbolicXMLBuilder::handlePrefixed :");
     // Console.println(t.toString());
    }

    def handleUnprefixedAttribute(key:String, value:Tree): Unit = {
      val t = atPos(pos) {
        Assign(Ident(_md), New(_scala_xml_UnprefixedAttribute,
                               LL(Literal(Constant(key)),value,Ident(_md))
                             ))};
      tlist2 = t :: tlist2;
    }

    var it = attrMap.elements
    while (it.hasNext) {
      val ansk = it.next
      getPrefix(ansk._1) match {
        case Some(pre) =>
          val key = ansk._1.substring(pre.length()+1, ansk._1.length())
          handlePrefixedAttribute(pre, key, ansk._2)
        case None      =>
          handleUnprefixedAttribute(ansk._1, ansk._2)
      }
    }
    //  attrs

    val moreAttributes = (0 < tlist2.length)

    var ts: List[Tree] = tlist
    var ts2: List[Tree] = List()

    if (moreAttributes) {
      ts2 = atPos(pos) {ValDef(Modifiers(MUTABLE),
                              _md,
                              _scala_xml_MetaData,
                              _scala_xml_Null)} :: tlist2;
    }
    if (moreNamespaces) {
      ts = atPos(pos) {
      ValDef(Modifiers(MUTABLE),
             _tmpscope,
             _scala_xml_NamespaceBinding,
             Ident(_scope))} :: ts;

      ts2 = ValDef(NoMods, _scope, _scala_xml_NamespaceBinding, Ident(_tmpscope)) :: ts2;
    }

    val makeSymbolicAttrs =
      if (moreAttributes) Ident(_md) else _scala_xml_Null

    var t = mkXML(pos,
                  false,
                  Literal(Constant(pre)) /* can be null */ ,
                  Literal(Constant(newlabel)): Tree,
                  makeSymbolicAttrs,
                  Ident(_scope),
                  args);

    atPos(pos) { Block(ts, Block(ts2, t)) }
  }
}