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