src/Pure/PIDE/yxml.scala
author wenzelm
Wed, 16 Apr 2014 11:52:26 +0200
changeset 56600 628e039cc34d
parent 55554 d77090e07000
child 56661 ef623f6f036b
permissions -rw-r--r--
more specific support for sequence of words;
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
45673
cd41e3903fbf separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents: 45667
diff changeset
     2
    Module:     PIDE
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     4
44698
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 44181
diff changeset
     5
Efficient text representation of XML trees.  Suitable for direct
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 44181
diff changeset
     6
inlining into plain text.
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     7
*/
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     8
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
     9
package isabelle
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    10
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    11
40453
wenzelm
parents: 38475
diff changeset
    12
import scala.collection.mutable
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    13
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    14
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
    15
object YXML
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
    16
{
27943
f34ff5e7728f replaced Pattern.split by chunks iterator (more efficient, resembles ML version more closely);
wenzelm
parents: 27930
diff changeset
    17
  /* chunk markers */
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    18
43774
6dfdb70496fe added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents: 43746
diff changeset
    19
  val X = '\5'
6dfdb70496fe added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents: 43746
diff changeset
    20
  val Y = '\6'
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 55554
diff changeset
    21
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 55554
diff changeset
    22
  val is_X = (c: Char) => c == X
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 55554
diff changeset
    23
  val is_Y = (c: Char) => c == Y
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 55554
diff changeset
    24
43774
6dfdb70496fe added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents: 43746
diff changeset
    25
  val X_string = X.toString
6dfdb70496fe added Outer_Syntax.quote_string, which is conceptually a bit different from Token.unparse;
wenzelm
parents: 43746
diff changeset
    26
  val Y_string = Y.toString
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    27
43782
834de29572d5 clarified YXML.detect;
wenzelm
parents: 43774
diff changeset
    28
  def detect(s: String): Boolean = s.exists(c => c == X || c == Y)
834de29572d5 clarified YXML.detect;
wenzelm
parents: 43774
diff changeset
    29
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    30
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    31
  /* string representation */  // FIXME byte array version with pseudo-utf-8 (!?)
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    32
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    33
  def string_of_body(body: XML.Body): String =
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    34
  {
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    35
    val s = new StringBuilder
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    36
    def attrib(p: (String, String)) { s += Y; s ++= p._1; s += '='; s ++= p._2 }
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) =>
55554
d77090e07000 tuned whitespace;
wenzelm
parents: 55551
diff changeset
    40
          s += X; s += Y; s ++= name; atts.foreach(attrib); s += X
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    41
          ts.foreach(tree)
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    42
          s += X; s += Y; s += X
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    43
        case XML.Text(text) => s ++= text
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    44
      }
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    45
    body.foreach(tree)
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    46
    s.toString
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
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    49
  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
    50
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
    51
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    52
  /* parsing */
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    53
27993
6dd90ef9f927 simplified exceptions: use plain error function / RuntimeException;
wenzelm
parents: 27971
diff changeset
    54
  private def err(msg: String) = error("Malformed YXML: " + msg)
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    55
  private def err_attribute() = err("bad attribute")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    56
  private def err_element() = err("bad element")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    57
  private def err_unbalanced(name: String) =
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    58
    if (name == "") err("unbalanced element")
44181
wenzelm
parents: 43841
diff changeset
    59
    else err("unbalanced element " + quote(name))
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    60
27944
2bf3f30558ed parse_attrib: more efficient due to indexOf('=');
wenzelm
parents: 27943
diff changeset
    61
  private def parse_attrib(source: CharSequence) = {
2bf3f30558ed parse_attrib: more efficient due to indexOf('=');
wenzelm
parents: 27943
diff changeset
    62
    val s = source.toString
2bf3f30558ed parse_attrib: more efficient due to indexOf('=');
wenzelm
parents: 27943
diff changeset
    63
    val i = s.indexOf('=')
2bf3f30558ed parse_attrib: more efficient due to indexOf('=');
wenzelm
parents: 27943
diff changeset
    64
    if (i <= 0) err_attribute()
38234
4b610fbb2d83 YXML.parse: refrain from interning, let XML.Cache do it (partially);
wenzelm
parents: 38231
diff changeset
    65
    (s.substring(0, i), s.substring(i + 1))
27944
2bf3f30558ed parse_attrib: more efficient due to indexOf('=');
wenzelm
parents: 27943
diff changeset
    66
  }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    67
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    68
38267
e50c283dd125 type XML.Body as basic data representation language (Scala version);
wenzelm
parents: 38234
diff changeset
    69
  def parse_body(source: CharSequence): XML.Body =
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
    70
  {
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    71
    /* stack operations */
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    72
40453
wenzelm
parents: 38475
diff changeset
    73
    def buffer(): mutable.ListBuffer[XML.Tree] = new mutable.ListBuffer[XML.Tree]
wenzelm
parents: 38475
diff changeset
    74
    var stack: List[(Markup, mutable.ListBuffer[XML.Tree])] = List((Markup.Empty, buffer()))
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    75
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    76
    def add(x: XML.Tree)
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    77
    {
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    78
      (stack: @unchecked) match { case ((_, body) :: _) => body += x }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    79
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    80
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    81
    def push(name: String, atts: XML.Attributes)
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    82
    {
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    83
      if (name == "") err_element()
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 36685
diff changeset
    84
      else stack = (Markup(name, atts), buffer()) :: stack
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    85
    }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    86
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    87
    def pop()
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    88
    {
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    89
      (stack: @unchecked) match {
38475
wenzelm
parents: 38268
diff changeset
    90
        case ((Markup.Empty, _) :: _) => err_unbalanced("")
38230
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 36685
diff changeset
    91
        case ((markup, body) :: pending) =>
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 36685
diff changeset
    92
          stack = pending
ed147003de4b simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents: 36685
diff changeset
    93
          add(XML.Elem(markup, body.toList))
36684
943f1ca7b375 misc tuning -- accumulate body via ListBuffer;
wenzelm
parents: 34201
diff changeset
    94
      }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    95
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    96
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    97
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    98
    /* parse chunks */
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
    99
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 55554
diff changeset
   100
    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
   101
      if (chunk.length == 1 && chunk.charAt(0) == Y) pop()
27945
d2dc5a1903e8 tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents: 27944
diff changeset
   102
      else {
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 55554
diff changeset
   103
        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
   104
          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
   105
            push(name.toString, atts.map(parse_attrib))
27945
d2dc5a1903e8 tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents: 27944
diff changeset
   106
          case txts => for (txt <- txts) add(XML.Text(txt.toString))
d2dc5a1903e8 tuned parse performance: avoid splitting terminal Y chunk;
wenzelm
parents: 27944
diff changeset
   107
        }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   108
      }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   109
    }
42719
wenzelm
parents: 40453
diff changeset
   110
    (stack: @unchecked) match {
38475
wenzelm
parents: 38268
diff changeset
   111
      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
   112
      case (Markup(name, _), _) :: _ => err_unbalanced(name)
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   113
    }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   114
  }
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   115
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
   116
  def parse(source: CharSequence): XML.Tree =
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   117
    parse_body(source) match {
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   118
      case List(result) => result
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   119
      case Nil => XML.Text("")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   120
      case _ => err("multiple results")
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   121
    }
27960
65b10d8ef0c6 added parse_failsafe;
wenzelm
parents: 27946
diff changeset
   122
29521
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   123
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   124
  /* failsafe parsing */
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   125
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 44698
diff changeset
   126
  private def markup_broken(source: CharSequence) =
55551
4a5f65df29fa tuned signature;
wenzelm
parents: 48996
diff changeset
   127
    XML.Elem(Markup.Broken, List(XML.Text(source.toString)))
29521
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   128
38267
e50c283dd125 type XML.Body as basic data representation language (Scala version);
wenzelm
parents: 38234
diff changeset
   129
  def parse_body_failsafe(source: CharSequence): XML.Body =
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
   130
  {
29521
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   131
    try { parse_body(source) }
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 44698
diff changeset
   132
    catch { case ERROR(_) => List(markup_broken(source)) }
29521
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   133
  }
736bf7117153 added parse_body_failsafe;
wenzelm
parents: 29180
diff changeset
   134
32450
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
   135
  def parse_failsafe(source: CharSequence): XML.Tree =
375db037f4d2 misc tuning;
wenzelm
parents: 31521
diff changeset
   136
  {
27960
65b10d8ef0c6 added parse_failsafe;
wenzelm
parents: 27946
diff changeset
   137
    try { parse(source) }
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 44698
diff changeset
   138
    catch { case ERROR(_) => markup_broken(source) }
27960
65b10d8ef0c6 added parse_failsafe;
wenzelm
parents: 27946
diff changeset
   139
  }
27930
2b44df907cc2 Efficient text representation of XML trees.
wenzelm
parents:
diff changeset
   140
}