src/Pure/PIDE/yxml.scala
author wenzelm
Sun Sep 04 15:21:50 2011 +0200 (2011-09-04 ago)
changeset 44698 0385292321a0
parent 44181 src/Pure/General/yxml.scala@bbce0417236d
child 45666 d83797ef0d2d
permissions -rw-r--r--
moved XML/YXML to src/Pure/PIDE;
tuned comments;
wenzelm@44698
     1
/*  Title:      Pure/PIDE/yxml.scala
wenzelm@27930
     2
    Author:     Makarius
wenzelm@27930
     3
wenzelm@44698
     4
Efficient text representation of XML trees.  Suitable for direct
wenzelm@44698
     5
inlining into plain text.
wenzelm@27930
     6
*/
wenzelm@27930
     7
wenzelm@27930
     8
package isabelle
wenzelm@27930
     9
wenzelm@27930
    10
wenzelm@40453
    11
import scala.collection.mutable
wenzelm@36684
    12
wenzelm@36684
    13
wenzelm@32450
    14
object YXML
wenzelm@32450
    15
{
wenzelm@27943
    16
  /* chunk markers */
wenzelm@27930
    17
wenzelm@43774
    18
  val X = '\5'
wenzelm@43774
    19
  val Y = '\6'
wenzelm@43774
    20
  val X_string = X.toString
wenzelm@43774
    21
  val Y_string = Y.toString
wenzelm@27930
    22
wenzelm@43782
    23
  def detect(s: String): Boolean = s.exists(c => c == X || c == Y)
wenzelm@43782
    24
wenzelm@27930
    25
wenzelm@38268
    26
  /* string representation */  // FIXME byte array version with pseudo-utf-8 (!?)
wenzelm@38268
    27
wenzelm@38268
    28
  def string_of_body(body: XML.Body): String =
wenzelm@38268
    29
  {
wenzelm@38268
    30
    val s = new StringBuilder
wenzelm@38268
    31
    def attrib(p: (String, String)) { s += Y; s ++= p._1; s += '='; s ++= p._2 }
wenzelm@38268
    32
    def tree(t: XML.Tree): Unit =
wenzelm@38268
    33
      t match {
wenzelm@38268
    34
        case XML.Elem(Markup(name, atts), ts) =>
wenzelm@38268
    35
          s += X; s += Y; s++= name; atts.foreach(attrib); s += X
wenzelm@38268
    36
          ts.foreach(tree)
wenzelm@38268
    37
          s += X; s += Y; s += X
wenzelm@38268
    38
        case XML.Text(text) => s ++= text
wenzelm@38268
    39
      }
wenzelm@38268
    40
    body.foreach(tree)
wenzelm@38268
    41
    s.toString
wenzelm@38268
    42
  }
wenzelm@38268
    43
wenzelm@38268
    44
  def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree))
wenzelm@38268
    45
wenzelm@38268
    46
wenzelm@27930
    47
  /* parsing */
wenzelm@27930
    48
wenzelm@27993
    49
  private def err(msg: String) = error("Malformed YXML: " + msg)
wenzelm@27930
    50
  private def err_attribute() = err("bad attribute")
wenzelm@27930
    51
  private def err_element() = err("bad element")
wenzelm@27930
    52
  private def err_unbalanced(name: String) =
wenzelm@27930
    53
    if (name == "") err("unbalanced element")
wenzelm@44181
    54
    else err("unbalanced element " + quote(name))
wenzelm@27930
    55
wenzelm@27944
    56
  private def parse_attrib(source: CharSequence) = {
wenzelm@27944
    57
    val s = source.toString
wenzelm@27944
    58
    val i = s.indexOf('=')
wenzelm@27944
    59
    if (i <= 0) err_attribute()
wenzelm@38234
    60
    (s.substring(0, i), s.substring(i + 1))
wenzelm@27944
    61
  }
wenzelm@27930
    62
wenzelm@27930
    63
wenzelm@38267
    64
  def parse_body(source: CharSequence): XML.Body =
wenzelm@32450
    65
  {
wenzelm@27930
    66
    /* stack operations */
wenzelm@27930
    67
wenzelm@40453
    68
    def buffer(): mutable.ListBuffer[XML.Tree] = new mutable.ListBuffer[XML.Tree]
wenzelm@40453
    69
    var stack: List[(Markup, mutable.ListBuffer[XML.Tree])] = List((Markup.Empty, buffer()))
wenzelm@27930
    70
wenzelm@36684
    71
    def add(x: XML.Tree)
wenzelm@36684
    72
    {
wenzelm@36684
    73
      (stack: @unchecked) match { case ((_, body) :: _) => body += x }
wenzelm@27930
    74
    }
wenzelm@27930
    75
wenzelm@36684
    76
    def push(name: String, atts: XML.Attributes)
wenzelm@36684
    77
    {
wenzelm@27930
    78
      if (name == "") err_element()
wenzelm@38230
    79
      else stack = (Markup(name, atts), buffer()) :: stack
wenzelm@36684
    80
    }
wenzelm@27930
    81
wenzelm@36684
    82
    def pop()
wenzelm@36684
    83
    {
wenzelm@36684
    84
      (stack: @unchecked) match {
wenzelm@38475
    85
        case ((Markup.Empty, _) :: _) => err_unbalanced("")
wenzelm@38230
    86
        case ((markup, body) :: pending) =>
wenzelm@38230
    87
          stack = pending
wenzelm@38230
    88
          add(XML.Elem(markup, body.toList))
wenzelm@36684
    89
      }
wenzelm@27930
    90
    }
wenzelm@27930
    91
wenzelm@27930
    92
wenzelm@27930
    93
    /* parse chunks */
wenzelm@27930
    94
wenzelm@36685
    95
    for (chunk <- Library.chunks(source, X) if chunk.length != 0) {
wenzelm@34099
    96
      if (chunk.length == 1 && chunk.charAt(0) == Y) pop()
wenzelm@27945
    97
      else {
wenzelm@36685
    98
        Library.chunks(chunk, Y).toList match {
wenzelm@34099
    99
          case ch :: name :: atts if ch.length == 0 =>
wenzelm@38234
   100
            push(name.toString, atts.map(parse_attrib))
wenzelm@27945
   101
          case txts => for (txt <- txts) add(XML.Text(txt.toString))
wenzelm@27945
   102
        }
wenzelm@27930
   103
      }
wenzelm@27930
   104
    }
wenzelm@42719
   105
    (stack: @unchecked) match {
wenzelm@38475
   106
      case List((Markup.Empty, body)) => body.toList
wenzelm@38230
   107
      case (Markup(name, _), _) :: _ => err_unbalanced(name)
wenzelm@27930
   108
    }
wenzelm@27930
   109
  }
wenzelm@27930
   110
wenzelm@32450
   111
  def parse(source: CharSequence): XML.Tree =
wenzelm@27930
   112
    parse_body(source) match {
wenzelm@27930
   113
      case List(result) => result
wenzelm@27930
   114
      case Nil => XML.Text("")
wenzelm@27930
   115
      case _ => err("multiple results")
wenzelm@27930
   116
    }
wenzelm@27960
   117
wenzelm@29521
   118
wenzelm@29521
   119
  /* failsafe parsing */
wenzelm@29521
   120
wenzelm@43841
   121
  private def markup_malformed(source: CharSequence) =
wenzelm@43841
   122
    XML.elem(Markup.MALFORMED, List(XML.Text(source.toString)))
wenzelm@29521
   123
wenzelm@38267
   124
  def parse_body_failsafe(source: CharSequence): XML.Body =
wenzelm@32450
   125
  {
wenzelm@29521
   126
    try { parse_body(source) }
wenzelm@43841
   127
    catch { case ERROR(_) => List(markup_malformed(source)) }
wenzelm@29521
   128
  }
wenzelm@29521
   129
wenzelm@32450
   130
  def parse_failsafe(source: CharSequence): XML.Tree =
wenzelm@32450
   131
  {
wenzelm@27960
   132
    try { parse(source) }
wenzelm@43841
   133
    catch { case ERROR(_) => markup_malformed(source) }
wenzelm@27960
   134
  }
wenzelm@27930
   135
}