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