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