src/Pure/PIDE/yxml.scala
author wenzelm
Sat, 02 Jan 2021 16:39:07 +0100
changeset 73029 64157cae1ba4
parent 73028 95e0f014cd24
child 73030 72a8fdfa185d
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44698
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 44181
diff changeset
     1
/*  Title:      Pure/PIDE/yxml.scala
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     3
44698
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 44181
diff changeset
     4
Efficient text representation of XML trees.  Suitable for direct
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 44181
diff changeset
     5
inlining into plain text.
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     6
*/
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     7
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     8
package isabelle
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     9
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    10
40453
wenzelm
parents: 38475
diff changeset
    11
import scala.collection.mutable
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    12
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    13
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
    14
object YXML
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
    15
{
27943
f34ff5e7728f replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents: 27930
diff changeset
    16
  /* chunk markers */
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    17
56661
ef623f6f036b avoid octal escape literals -- deprecated in scala-2.11.0;
wenzelm
parents: 56600
diff changeset
    18
  val X = '\u0005'
ef623f6f036b avoid octal escape literals -- deprecated in scala-2.11.0;
wenzelm
parents: 56600
diff changeset
    19
  val Y = '\u0006'
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 55554
diff changeset
    20
73029
wenzelm
parents: 73028
diff changeset
    21
  val is_X: Char => Boolean = _ == X
wenzelm
parents: 73028
diff changeset
    22
  val is_Y: Char => Boolean = _ == Y
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 55554
diff changeset
    23
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71534
diff changeset
    24
  val X_string: String = X.toString
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71534
diff changeset
    25
  val Y_string: String = Y.toString
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71534
diff changeset
    26
  val XY_string: String = X_string + Y_string
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71534
diff changeset
    27
  val XYX_string: String = XY_string + X_string
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    28
43782
834de29572d5 clarified YXML.detect;
wenzelm
parents: 43774
diff changeset
    29
  def detect(s: String): Boolean = s.exists(c => c == X || c == Y)
67820
e30d6368c7c8 clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents: 64370
diff changeset
    30
  def detect_elem(s: String): Boolean = s.startsWith(XY_string)
43782
834de29572d5 clarified YXML.detect;
wenzelm
parents: 43774
diff changeset
    31
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    32
73029
wenzelm
parents: 73028
diff changeset
    33
  /* string representation */
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    34
73029
wenzelm
parents: 73028
diff changeset
    35
  def traversal(string: String => Unit, body: XML.Body)
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    36
  {
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    37
    def tree(t: XML.Tree): Unit =
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    38
      t match {
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    39
        case XML.Elem(Markup(name, atts), ts) =>
71534
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    40
          string(XY_string)
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    41
          string(name)
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    42
          for ((a, x) <- atts) { string(Y_string); string(a); string("="); string(x) }
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    43
          string(X_string)
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    44
          ts.foreach(tree)
71534
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    45
          string(XYX_string)
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    46
        case XML.Text(text) => string(text)
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    47
      }
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    48
    body.foreach(tree)
71534
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    49
  }
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    50
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    51
  def string_of_body(body: XML.Body): String =
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    52
  {
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    53
    val s = new StringBuilder
f10bffaa2bae more scalable output of YXML files;
wenzelm
parents: 67820
diff changeset
    54
    traversal(str => s ++= str, body)
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    55
    s.toString
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    56
  }
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    57
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    58
  def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree))
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    59
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    60
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    61
  /* parsing */
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    62
27993
6dd90ef9f927 simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents: 27971
diff changeset
    63
  private def err(msg: String) = error("Malformed YXML: " + msg)
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    64
  private def err_attribute() = err("bad attribute")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    65
  private def err_element() = err("bad element")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    66
  private def err_unbalanced(name: String) =
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    67
    if (name == "") err("unbalanced element")
44181
wenzelm
parents: 43841
diff changeset
    68
    else err("unbalanced element " + quote(name))
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    69
73029
wenzelm
parents: 73028
diff changeset
    70
  private def parse_attrib(source: CharSequence): (String, String) =
wenzelm
parents: 73028
diff changeset
    71
  {
27944
2bf3f30558ed parse_attrib: more efficient due to indexOf('=');
wenzelm
parents: 27943
diff changeset
    72
    val s = source.toString
2bf3f30558ed parse_attrib: more efficient due to indexOf('=');
wenzelm
parents: 27943
diff changeset
    73
    val i = s.indexOf('=')
2bf3f30558ed parse_attrib: more efficient due to indexOf('=');
wenzelm
parents: 27943
diff changeset
    74
    if (i <= 0) err_attribute()
38234
4b610fbb2d83 YXML.parse: refrain from interning, let XML.Cache do it (partially);
wenzelm
parents: 38231
diff changeset
    75
    (s.substring(0, i), s.substring(i + 1))
27944
2bf3f30558ed parse_attrib: more efficient due to indexOf('=');
wenzelm
parents: 27943
diff changeset
    76
  }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    77
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    78
38267
e50c283dd125 type XML.Body as basic data representation language (Scala version);
wenzelm
parents: 38234
diff changeset
    79
  def parse_body(source: CharSequence): XML.Body =
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
    80
  {
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    81
    /* stack operations */
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    82
40453
wenzelm
parents: 38475
diff changeset
    83
    def buffer(): mutable.ListBuffer[XML.Tree] = new mutable.ListBuffer[XML.Tree]
wenzelm
parents: 38475
diff changeset
    84
    var stack: List[(Markup, mutable.ListBuffer[XML.Tree])] = List((Markup.Empty, buffer()))
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    85
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    86
    def add(x: XML.Tree)
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    87
    {
73029
wenzelm
parents: 73028
diff changeset
    88
      (stack: @unchecked) match { case (_, body) :: _ => body += x }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    89
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    90
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    91
    def push(name: String, atts: XML.Attributes)
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    92
    {
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    93
      if (name == "") err_element()
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 36685
diff changeset
    94
      else stack = (Markup(name, atts), buffer()) :: stack
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    95
    }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    96
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    97
    def pop()
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    98
    {
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    99
      (stack: @unchecked) match {
73029
wenzelm
parents: 73028
diff changeset
   100
        case (Markup.Empty, _) :: _ => err_unbalanced("")
wenzelm
parents: 73028
diff changeset
   101
        case (markup, body) :: pending =>
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 36685
diff changeset
   102
          stack = pending
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 36685
diff changeset
   103
          add(XML.Elem(markup, body.toList))
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
   104
      }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   105
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   106
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   107
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   108
    /* parse chunks */
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   109
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 55554
diff changeset
   110
    for (chunk <- Library.separated_chunks(is_X, source) if chunk.length != 0) {
34099
2541de190d92 added decode_chars, with raw character view on byte buffer and adhoc decoding via toString;
wenzelm
parents: 32450
diff changeset
   111
      if (chunk.length == 1 && chunk.charAt(0) == Y) pop()
27945
d2dc5a1903e8 tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents: 27944
diff changeset
   112
      else {
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 55554
diff changeset
   113
        Library.separated_chunks(is_Y, chunk).toList match {
34099
2541de190d92 added decode_chars, with raw character view on byte buffer and adhoc decoding via toString;
wenzelm
parents: 32450
diff changeset
   114
          case ch :: name :: atts if ch.length == 0 =>
38234
4b610fbb2d83 YXML.parse: refrain from interning, let XML.Cache do it (partially);
wenzelm
parents: 38231
diff changeset
   115
            push(name.toString, atts.map(parse_attrib))
27945
d2dc5a1903e8 tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents: 27944
diff changeset
   116
          case txts => for (txt <- txts) add(XML.Text(txt.toString))
d2dc5a1903e8 tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents: 27944
diff changeset
   117
        }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   118
      }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   119
    }
42719
wenzelm
parents: 40453
diff changeset
   120
    (stack: @unchecked) match {
38475
wenzelm
parents: 38268
diff changeset
   121
      case List((Markup.Empty, body)) => body.toList
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 36685
diff changeset
   122
      case (Markup(name, _), _) :: _ => err_unbalanced(name)
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   123
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   124
  }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   125
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
   126
  def parse(source: CharSequence): XML.Tree =
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   127
    parse_body(source) match {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   128
      case List(result) => result
73028
95e0f014cd24 tuned signature;
wenzelm
parents: 71601
diff changeset
   129
      case Nil => XML.no_text
67820
e30d6368c7c8 clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents: 64370
diff changeset
   130
      case _ => err("multiple XML trees")
e30d6368c7c8 clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents: 64370
diff changeset
   131
    }
e30d6368c7c8 clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents: 64370
diff changeset
   132
e30d6368c7c8 clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents: 64370
diff changeset
   133
  def parse_elem(source: CharSequence): XML.Tree =
e30d6368c7c8 clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents: 64370
diff changeset
   134
    parse_body(source) match {
e30d6368c7c8 clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents: 64370
diff changeset
   135
      case List(elem: XML.Elem) => elem
e30d6368c7c8 clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents: 64370
diff changeset
   136
      case _ => err("single XML element expected")
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   137
    }
27960
65b10d8ef0c6 added parse_failsafe;
wenzelm
parents: 27946
diff changeset
   138
29521
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   139
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   140
  /* failsafe parsing */
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   141
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 44698
diff changeset
   142
  private def markup_broken(source: CharSequence) =
55551
4a5f65df29fa tuned signature;
wenzelm
parents: 48996
diff changeset
   143
    XML.Elem(Markup.Broken, List(XML.Text(source.toString)))
29521
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   144
38267
e50c283dd125 type XML.Body as basic data representation language (Scala version);
wenzelm
parents: 38234
diff changeset
   145
  def parse_body_failsafe(source: CharSequence): XML.Body =
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
   146
  {
29521
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   147
    try { parse_body(source) }
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 44698
diff changeset
   148
    catch { case ERROR(_) => List(markup_broken(source)) }
29521
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   149
  }
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   150
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
   151
  def parse_failsafe(source: CharSequence): XML.Tree =
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
   152
  {
27960
65b10d8ef0c6 added parse_failsafe;
wenzelm
parents: 27946
diff changeset
   153
    try { parse(source) }
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 44698
diff changeset
   154
    catch { case ERROR(_) => markup_broken(source) }
27960
65b10d8ef0c6 added parse_failsafe;
wenzelm
parents: 27946
diff changeset
   155
  }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   156
}