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