src/Pure/PIDE/yxml.scala
author wenzelm
Tue Aug 12 18:36:43 2014 +0200 (2014-08-12)
changeset 57916 2c2c24dbf0a4
parent 56661 ef623f6f036b
child 64370 865b39487b5d
permissions -rw-r--r--
generic process wrapping in Prover;
clarified module arrangement;
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@56661
    19
  val X = '\u0005'
wenzelm@56661
    20
  val Y = '\u0006'
wenzelm@56600
    21
wenzelm@56600
    22
  val is_X = (c: Char) => c == X
wenzelm@56600
    23
  val is_Y = (c: Char) => c == Y
wenzelm@56600
    24
wenzelm@43774
    25
  val X_string = X.toString
wenzelm@43774
    26
  val Y_string = Y.toString
wenzelm@27930
    27
wenzelm@43782
    28
  def detect(s: String): Boolean = s.exists(c => c == X || c == Y)
wenzelm@43782
    29
wenzelm@27930
    30
wenzelm@38268
    31
  /* string representation */  // FIXME byte array version with pseudo-utf-8 (!?)
wenzelm@38268
    32
wenzelm@38268
    33
  def string_of_body(body: XML.Body): String =
wenzelm@38268
    34
  {
wenzelm@38268
    35
    val s = new StringBuilder
wenzelm@38268
    36
    def attrib(p: (String, String)) { s += Y; s ++= p._1; s += '='; s ++= p._2 }
wenzelm@38268
    37
    def tree(t: XML.Tree): Unit =
wenzelm@38268
    38
      t match {
wenzelm@38268
    39
        case XML.Elem(Markup(name, atts), ts) =>
wenzelm@55554
    40
          s += X; s += Y; s ++= name; atts.foreach(attrib); s += X
wenzelm@38268
    41
          ts.foreach(tree)
wenzelm@38268
    42
          s += X; s += Y; s += X
wenzelm@38268
    43
        case XML.Text(text) => s ++= text
wenzelm@38268
    44
      }
wenzelm@38268
    45
    body.foreach(tree)
wenzelm@38268
    46
    s.toString
wenzelm@38268
    47
  }
wenzelm@38268
    48
wenzelm@38268
    49
  def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree))
wenzelm@38268
    50
wenzelm@38268
    51
wenzelm@27930
    52
  /* parsing */
wenzelm@27930
    53
wenzelm@27993
    54
  private def err(msg: String) = error("Malformed YXML: " + msg)
wenzelm@27930
    55
  private def err_attribute() = err("bad attribute")
wenzelm@27930
    56
  private def err_element() = err("bad element")
wenzelm@27930
    57
  private def err_unbalanced(name: String) =
wenzelm@27930
    58
    if (name == "") err("unbalanced element")
wenzelm@44181
    59
    else err("unbalanced element " + quote(name))
wenzelm@27930
    60
wenzelm@27944
    61
  private def parse_attrib(source: CharSequence) = {
wenzelm@27944
    62
    val s = source.toString
wenzelm@27944
    63
    val i = s.indexOf('=')
wenzelm@27944
    64
    if (i <= 0) err_attribute()
wenzelm@38234
    65
    (s.substring(0, i), s.substring(i + 1))
wenzelm@27944
    66
  }
wenzelm@27930
    67
wenzelm@27930
    68
wenzelm@38267
    69
  def parse_body(source: CharSequence): XML.Body =
wenzelm@32450
    70
  {
wenzelm@27930
    71
    /* stack operations */
wenzelm@27930
    72
wenzelm@40453
    73
    def buffer(): mutable.ListBuffer[XML.Tree] = new mutable.ListBuffer[XML.Tree]
wenzelm@40453
    74
    var stack: List[(Markup, mutable.ListBuffer[XML.Tree])] = List((Markup.Empty, buffer()))
wenzelm@27930
    75
wenzelm@36684
    76
    def add(x: XML.Tree)
wenzelm@36684
    77
    {
wenzelm@36684
    78
      (stack: @unchecked) match { case ((_, body) :: _) => body += x }
wenzelm@27930
    79
    }
wenzelm@27930
    80
wenzelm@36684
    81
    def push(name: String, atts: XML.Attributes)
wenzelm@36684
    82
    {
wenzelm@27930
    83
      if (name == "") err_element()
wenzelm@38230
    84
      else stack = (Markup(name, atts), buffer()) :: stack
wenzelm@36684
    85
    }
wenzelm@27930
    86
wenzelm@36684
    87
    def pop()
wenzelm@36684
    88
    {
wenzelm@36684
    89
      (stack: @unchecked) match {
wenzelm@38475
    90
        case ((Markup.Empty, _) :: _) => err_unbalanced("")
wenzelm@38230
    91
        case ((markup, body) :: pending) =>
wenzelm@38230
    92
          stack = pending
wenzelm@38230
    93
          add(XML.Elem(markup, body.toList))
wenzelm@36684
    94
      }
wenzelm@27930
    95
    }
wenzelm@27930
    96
wenzelm@27930
    97
wenzelm@27930
    98
    /* parse chunks */
wenzelm@27930
    99
wenzelm@56600
   100
    for (chunk <- Library.separated_chunks(is_X, source) if chunk.length != 0) {
wenzelm@34099
   101
      if (chunk.length == 1 && chunk.charAt(0) == Y) pop()
wenzelm@27945
   102
      else {
wenzelm@56600
   103
        Library.separated_chunks(is_Y, chunk).toList match {
wenzelm@34099
   104
          case ch :: name :: atts if ch.length == 0 =>
wenzelm@38234
   105
            push(name.toString, atts.map(parse_attrib))
wenzelm@27945
   106
          case txts => for (txt <- txts) add(XML.Text(txt.toString))
wenzelm@27945
   107
        }
wenzelm@27930
   108
      }
wenzelm@27930
   109
    }
wenzelm@42719
   110
    (stack: @unchecked) match {
wenzelm@38475
   111
      case List((Markup.Empty, body)) => body.toList
wenzelm@38230
   112
      case (Markup(name, _), _) :: _ => err_unbalanced(name)
wenzelm@27930
   113
    }
wenzelm@27930
   114
  }
wenzelm@27930
   115
wenzelm@32450
   116
  def parse(source: CharSequence): XML.Tree =
wenzelm@27930
   117
    parse_body(source) match {
wenzelm@27930
   118
      case List(result) => result
wenzelm@27930
   119
      case Nil => XML.Text("")
wenzelm@27930
   120
      case _ => err("multiple results")
wenzelm@27930
   121
    }
wenzelm@27960
   122
wenzelm@29521
   123
wenzelm@29521
   124
  /* failsafe parsing */
wenzelm@29521
   125
wenzelm@45666
   126
  private def markup_broken(source: CharSequence) =
wenzelm@55551
   127
    XML.Elem(Markup.Broken, List(XML.Text(source.toString)))
wenzelm@29521
   128
wenzelm@38267
   129
  def parse_body_failsafe(source: CharSequence): XML.Body =
wenzelm@32450
   130
  {
wenzelm@29521
   131
    try { parse_body(source) }
wenzelm@45666
   132
    catch { case ERROR(_) => List(markup_broken(source)) }
wenzelm@29521
   133
  }
wenzelm@29521
   134
wenzelm@32450
   135
  def parse_failsafe(source: CharSequence): XML.Tree =
wenzelm@32450
   136
  {
wenzelm@27960
   137
    try { parse(source) }
wenzelm@45666
   138
    catch { case ERROR(_) => markup_broken(source) }
wenzelm@27960
   139
  }
wenzelm@27930
   140
}