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";
     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   val XY_string = X_string + Y_string
    27 
    28   def detect(s: String): Boolean = s.exists(c => c == X || c == Y)
    29   def detect_elem(s: String): Boolean = s.startsWith(XY_string)
    30 
    31 
    32   /* string representation */  // FIXME byte array version with pseudo-utf-8 (!?)
    33 
    34   def string_of_body(body: XML.Body): String =
    35   {
    36     val s = new StringBuilder
    37     def attrib(p: (String, String)) { s += Y; s ++= p._1; s += '='; s ++= p._2 }
    38     def tree(t: XML.Tree): Unit =
    39       t match {
    40         case XML.Elem(Markup(name, atts), ts) =>
    41           s += X; s += Y; s ++= name; atts.foreach(attrib); s += X
    42           ts.foreach(tree)
    43           s += X; s += Y; s += X
    44         case XML.Text(text) => s ++= text
    45       }
    46     body.foreach(tree)
    47     s.toString
    48   }
    49 
    50   def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree))
    51 
    52 
    53   /* parsing */
    54 
    55   private def err(msg: String) = error("Malformed YXML: " + msg)
    56   private def err_attribute() = err("bad attribute")
    57   private def err_element() = err("bad element")
    58   private def err_unbalanced(name: String) =
    59     if (name == "") err("unbalanced element")
    60     else err("unbalanced element " + quote(name))
    61 
    62   private def parse_attrib(source: CharSequence) = {
    63     val s = source.toString
    64     val i = s.indexOf('=')
    65     if (i <= 0) err_attribute()
    66     (s.substring(0, i), s.substring(i + 1))
    67   }
    68 
    69 
    70   def parse_body(source: CharSequence): XML.Body =
    71   {
    72     /* stack operations */
    73 
    74     def buffer(): mutable.ListBuffer[XML.Tree] = new mutable.ListBuffer[XML.Tree]
    75     var stack: List[(Markup, mutable.ListBuffer[XML.Tree])] = List((Markup.Empty, buffer()))
    76 
    77     def add(x: XML.Tree)
    78     {
    79       (stack: @unchecked) match { case ((_, body) :: _) => body += x }
    80     }
    81 
    82     def push(name: String, atts: XML.Attributes)
    83     {
    84       if (name == "") err_element()
    85       else stack = (Markup(name, atts), buffer()) :: stack
    86     }
    87 
    88     def pop()
    89     {
    90       (stack: @unchecked) match {
    91         case ((Markup.Empty, _) :: _) => err_unbalanced("")
    92         case ((markup, body) :: pending) =>
    93           stack = pending
    94           add(XML.Elem(markup, body.toList))
    95       }
    96     }
    97 
    98 
    99     /* parse chunks */
   100 
   101     for (chunk <- Library.separated_chunks(is_X, source) if chunk.length != 0) {
   102       if (chunk.length == 1 && chunk.charAt(0) == Y) pop()
   103       else {
   104         Library.separated_chunks(is_Y, chunk).toList match {
   105           case ch :: name :: atts if ch.length == 0 =>
   106             push(name.toString, atts.map(parse_attrib))
   107           case txts => for (txt <- txts) add(XML.Text(txt.toString))
   108         }
   109       }
   110     }
   111     (stack: @unchecked) match {
   112       case List((Markup.Empty, body)) => body.toList
   113       case (Markup(name, _), _) :: _ => err_unbalanced(name)
   114     }
   115   }
   116 
   117   def parse(source: CharSequence): XML.Tree =
   118     parse_body(source) match {
   119       case List(result) => result
   120       case Nil => XML.Text("")
   121       case _ => err("multiple XML trees")
   122     }
   123 
   124   def parse_elem(source: CharSequence): XML.Tree =
   125     parse_body(source) match {
   126       case List(elem: XML.Elem) => elem
   127       case _ => err("single XML element expected")
   128     }
   129 
   130 
   131   /* failsafe parsing */
   132 
   133   private def markup_broken(source: CharSequence) =
   134     XML.Elem(Markup.Broken, List(XML.Text(source.toString)))
   135 
   136   def parse_body_failsafe(source: CharSequence): XML.Body =
   137   {
   138     try { parse_body(source) }
   139     catch { case ERROR(_) => List(markup_broken(source)) }
   140   }
   141 
   142   def parse_failsafe(source: CharSequence): XML.Tree =
   143   {
   144     try { parse(source) }
   145     catch { case ERROR(_) => markup_broken(source) }
   146   }
   147 }