src/Pure/PIDE/xml.scala
author wenzelm
Thu, 04 Mar 2021 15:41:46 +0100
changeset 73359 d8a0e996614b
parent 73340 0ffcad1f6130
child 73528 c337c798f64c
permissions -rw-r--r--
tuned --- fewer warnings;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44698
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 44697
diff changeset
     1
/*  Title:      Pure/PIDE/xml.scala
27931
b533a9de87a7 Minimalistic XML tree values.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
b533a9de87a7 Minimalistic XML tree values.
wenzelm
parents:
diff changeset
     3
44698
0385292321a0 moved XML/YXML to src/Pure/PIDE;
wenzelm
parents: 44697
diff changeset
     4
Untyped XML trees and basic data representation.
27931
b533a9de87a7 Minimalistic XML tree values.
wenzelm
parents:
diff changeset
     5
*/
b533a9de87a7 Minimalistic XML tree values.
wenzelm
parents:
diff changeset
     6
b533a9de87a7 Minimalistic XML tree values.
wenzelm
parents:
diff changeset
     7
package isabelle
b533a9de87a7 Minimalistic XML tree values.
wenzelm
parents:
diff changeset
     8
55618
995162143ef4 tuned imports;
wenzelm
parents: 52890
diff changeset
     9
29203
0c4effb73518 override toString method;
wenzelm
parents: 29140
diff changeset
    10
object XML
0c4effb73518 override toString method;
wenzelm
parents: 29140
diff changeset
    11
{
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
    12
  /** XML trees **/
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
    13
27947
b6dc0a396857 tuned comments;
wenzelm
parents: 27942
diff changeset
    14
  /* datatype representation */
b6dc0a396857 tuned comments;
wenzelm
parents: 27942
diff changeset
    15
65753
787e5ee6ef53 more operations;
wenzelm
parents: 65334
diff changeset
    16
  type Attribute = Properties.Entry
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents: 43778
diff changeset
    17
  type Attributes = Properties.T
27931
b533a9de87a7 Minimalistic XML tree values.
wenzelm
parents:
diff changeset
    18
57912
wenzelm
parents: 57909
diff changeset
    19
  sealed abstract class Tree { override def toString: String = string_of_tree(this) }
64354
wenzelm
parents: 61026
diff changeset
    20
  type Body = List[Tree]
wenzelm
parents: 61026
diff changeset
    21
  case class Elem(markup: Markup, body: Body) extends Tree
52890
36e2c0c308eb tuned signature;
wenzelm
parents: 51987
diff changeset
    22
  {
73032
72b13af7f266 persistent hash code: much faster caching;
wenzelm
parents: 73031
diff changeset
    23
    private lazy val hash: Int = (markup, body).hashCode()
72b13af7f266 persistent hash code: much faster caching;
wenzelm
parents: 73031
diff changeset
    24
    override def hashCode(): Int = hash
72b13af7f266 persistent hash code: much faster caching;
wenzelm
parents: 73031
diff changeset
    25
52890
36e2c0c308eb tuned signature;
wenzelm
parents: 51987
diff changeset
    26
    def name: String = markup.name
65753
787e5ee6ef53 more operations;
wenzelm
parents: 65334
diff changeset
    27
64358
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 64354
diff changeset
    28
    def update_attributes(more_attributes: Attributes): Elem =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 64354
diff changeset
    29
      if (more_attributes.isEmpty) this
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 64354
diff changeset
    30
      else Elem(markup.update_properties(more_attributes), body)
65753
787e5ee6ef53 more operations;
wenzelm
parents: 65334
diff changeset
    31
65772
368399c5d87f proper type for iterated application;
wenzelm
parents: 65753
diff changeset
    32
    def + (att: Attribute): Elem = Elem(markup + att, body)
52890
36e2c0c308eb tuned signature;
wenzelm
parents: 51987
diff changeset
    33
  }
29204
wenzelm
parents: 29203
diff changeset
    34
  case class Text(content: String) extends Tree
73032
72b13af7f266 persistent hash code: much faster caching;
wenzelm
parents: 73031
diff changeset
    35
  {
72b13af7f266 persistent hash code: much faster caching;
wenzelm
parents: 73031
diff changeset
    36
    private lazy val hash: Int = content.hashCode()
72b13af7f266 persistent hash code: much faster caching;
wenzelm
parents: 73031
diff changeset
    37
    override def hashCode(): Int = hash
72b13af7f266 persistent hash code: much faster caching;
wenzelm
parents: 73031
diff changeset
    38
  }
29203
0c4effb73518 override toString method;
wenzelm
parents: 29140
diff changeset
    39
66196
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 65991
diff changeset
    40
  def elem(markup: Markup): XML.Elem = XML.Elem(markup, Nil)
64354
wenzelm
parents: 61026
diff changeset
    41
  def elem(name: String, body: Body): XML.Elem = XML.Elem(Markup(name, Nil), body)
wenzelm
parents: 61026
diff changeset
    42
  def elem(name: String): XML.Elem = XML.Elem(Markup(name, Nil), Nil)
38267
e50c283dd125 type XML.Body as basic data representation language (Scala version);
wenzelm
parents: 38263
diff changeset
    43
73028
95e0f014cd24 tuned signature;
wenzelm
parents: 73024
diff changeset
    44
  val no_text: Text = Text("")
69867
wenzelm
parents: 69805
diff changeset
    45
  val newline: Text = Text("\n")
wenzelm
parents: 69805
diff changeset
    46
29203
0c4effb73518 override toString method;
wenzelm
parents: 29140
diff changeset
    47
69805
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    48
  /* name space */
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    49
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    50
  object Namespace
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    51
  {
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    52
    def apply(prefix: String, target: String): Namespace =
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    53
      new Namespace(prefix, target)
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    54
  }
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    55
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    56
  final class Namespace private(prefix: String, target: String)
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    57
  {
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    58
    def apply(name: String): String = prefix + ":" + name
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    59
    val attribute: XML.Attribute = ("xmlns:" + prefix, target)
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    60
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    61
    override def toString: String = attribute.toString
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    62
  }
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    63
a8debe27c36c support for XML name spaces;
wenzelm
parents: 69804
diff changeset
    64
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    65
  /* wrapped elements */
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    66
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 57912
diff changeset
    67
  val XML_ELEM = "xml_elem"
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 57912
diff changeset
    68
  val XML_NAME = "xml_name"
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 57912
diff changeset
    69
  val XML_BODY = "xml_body"
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    70
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    71
  object Wrapped_Elem
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    72
  {
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    73
    def apply(markup: Markup, body1: Body, body2: Body): XML.Elem =
61026
397354b29935 tuned signature;
wenzelm
parents: 60215
diff changeset
    74
      XML.Elem(Markup(XML_ELEM, (XML_NAME, markup.name) :: markup.properties),
397354b29935 tuned signature;
wenzelm
parents: 60215
diff changeset
    75
        XML.Elem(Markup(XML_BODY, Nil), body1) :: body2)
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    76
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    77
    def unapply(tree: Tree): Option[(Markup, Body, Body)] =
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    78
      tree match {
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    79
        case
61026
397354b29935 tuned signature;
wenzelm
parents: 60215
diff changeset
    80
          XML.Elem(Markup(XML_ELEM, (XML_NAME, name) :: props),
397354b29935 tuned signature;
wenzelm
parents: 60215
diff changeset
    81
            XML.Elem(Markup(XML_BODY, Nil), body1) :: body2) =>
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    82
          Some(Markup(name, props), body1, body2)
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    83
        case _ => None
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    84
      }
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    85
  }
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    86
67818
2457bea123e4 convenience to represent XML.Body as single XML.Elem;
wenzelm
parents: 67113
diff changeset
    87
  object Root_Elem
2457bea123e4 convenience to represent XML.Body as single XML.Elem;
wenzelm
parents: 67113
diff changeset
    88
  {
2457bea123e4 convenience to represent XML.Body as single XML.Elem;
wenzelm
parents: 67113
diff changeset
    89
    def apply(body: Body): XML.Elem = XML.Elem(Markup(XML_ELEM, Nil), body)
2457bea123e4 convenience to represent XML.Body as single XML.Elem;
wenzelm
parents: 67113
diff changeset
    90
    def unapply(tree: Tree): Option[Body] =
2457bea123e4 convenience to represent XML.Body as single XML.Elem;
wenzelm
parents: 67113
diff changeset
    91
      tree match {
2457bea123e4 convenience to represent XML.Body as single XML.Elem;
wenzelm
parents: 67113
diff changeset
    92
        case XML.Elem(Markup(XML_ELEM, Nil), body) => Some(body)
2457bea123e4 convenience to represent XML.Body as single XML.Elem;
wenzelm
parents: 67113
diff changeset
    93
        case _ => None
2457bea123e4 convenience to represent XML.Body as single XML.Elem;
wenzelm
parents: 67113
diff changeset
    94
      }
2457bea123e4 convenience to represent XML.Body as single XML.Elem;
wenzelm
parents: 67113
diff changeset
    95
  }
2457bea123e4 convenience to represent XML.Body as single XML.Elem;
wenzelm
parents: 67113
diff changeset
    96
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    97
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    98
  /* traverse text */
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
    99
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   100
  def traverse_text[A](body: Body)(a: A)(op: (A, String) => A): A =
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   101
  {
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   102
    def traverse(x: A, t: Tree): A =
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   103
      t match {
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   104
        case XML.Wrapped_Elem(_, _, ts) => ts.foldLeft(x)(traverse)
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   105
        case XML.Elem(_, ts) => ts.foldLeft(x)(traverse)
61026
397354b29935 tuned signature;
wenzelm
parents: 60215
diff changeset
   106
        case XML.Text(s) => op(x, s)
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   107
      }
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   108
    body.foldLeft(a)(traverse)
49650
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   109
  }
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   110
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   111
  def text_length(body: Body): Int = traverse_text(body)(0) { case (n, s) => n + s.length }
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   112
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   113
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   114
  /* text content */
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   115
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   116
  def content(body: Body): String =
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   117
  {
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   118
    val text = new StringBuilder(text_length(body))
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   119
    traverse_text(body)(()) { case (_, s) => text.append(s) }
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   120
    text.toString
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   121
  }
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   122
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   123
  def content(tree: Tree): String = content(List(tree))
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   124
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   125
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   126
9fad6480300d support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents: 49613
diff changeset
   127
  /** string representation **/
29203
0c4effb73518 override toString method;
wenzelm
parents: 29140
diff changeset
   128
69804
9efccbad7d42 uniform XML header;
wenzelm
parents: 68265
diff changeset
   129
  val header: String = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
9efccbad7d42 uniform XML header;
wenzelm
parents: 68265
diff changeset
   130
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73204
diff changeset
   131
  def output_char(s: StringBuilder, c: Char, permissive: Boolean = false): Unit =
65990
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   132
  {
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   133
    c match {
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   134
      case '<' => s ++= "&lt;"
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   135
      case '>' => s ++= "&gt;"
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   136
      case '&' => s ++= "&amp;"
73203
9c10b4fa17b5 clarified signature;
wenzelm
parents: 73202
diff changeset
   137
      case '"' if !permissive => s ++= "&quot;"
9c10b4fa17b5 clarified signature;
wenzelm
parents: 73202
diff changeset
   138
      case '\'' if !permissive => s ++= "&apos;"
65990
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   139
      case _ => s += c
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   140
    }
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   141
  }
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   142
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73204
diff changeset
   143
  def output_string(s: StringBuilder, str: String, permissive: Boolean = false): Unit =
65990
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   144
  {
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   145
    if (str == null) s ++= str
73203
9c10b4fa17b5 clarified signature;
wenzelm
parents: 73202
diff changeset
   146
    else str.iterator.foreach(output_char(s, _, permissive = permissive))
65990
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   147
  }
868089ee9d60 uniform output of HTML as XML;
wenzelm
parents: 65903
diff changeset
   148
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73204
diff changeset
   149
  def output_elem(s: StringBuilder, markup: Markup, end: Boolean = false): Unit =
73204
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   150
  {
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   151
    s += '<'
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   152
    s ++= markup.name
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   153
    for ((a, b) <- markup.properties) {
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   154
      s += ' '
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   155
      s ++= a
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   156
      s += '='
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   157
      s += '"'
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   158
      output_string(s, b)
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   159
      s += '"'
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   160
    }
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   161
    if (end) s += '/'
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   162
    s += '>'
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   163
  }
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   164
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73204
diff changeset
   165
  def output_elem_end(s: StringBuilder, name: String): Unit =
73204
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   166
  {
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   167
    s += '<'
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   168
    s += '/'
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   169
    s ++= name
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   170
    s += '>'
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   171
  }
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   172
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   173
  def string_of_body(body: Body): String =
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   174
  {
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   175
    val s = new StringBuilder
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   176
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   177
    def tree(t: Tree): Unit =
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   178
      t match {
61026
397354b29935 tuned signature;
wenzelm
parents: 60215
diff changeset
   179
        case XML.Elem(markup, Nil) =>
73204
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   180
          output_elem(s, markup, end = true)
61026
397354b29935 tuned signature;
wenzelm
parents: 60215
diff changeset
   181
        case XML.Elem(markup, ts) =>
73204
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   182
          output_elem(s, markup)
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   183
          ts.foreach(tree)
73204
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   184
          output_elem_end(s, markup.name)
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   185
        case XML.Text(txt) => output_string(s, txt)
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   186
      }
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   187
    body.foreach(tree)
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   188
    s.toString
29203
0c4effb73518 override toString method;
wenzelm
parents: 29140
diff changeset
   189
  }
0c4effb73518 override toString method;
wenzelm
parents: 29140
diff changeset
   190
38268
beb86b805590 more uniform XML/YXML string_of_body/string_of_tree;
wenzelm
parents: 38267
diff changeset
   191
  def string_of_tree(tree: XML.Tree): String = string_of_body(List(tree))
27941
b4656b671cce added iterator over content;
wenzelm
parents: 27931
diff changeset
   192
b4656b671cce added iterator over content;
wenzelm
parents: 27931
diff changeset
   193
44808
05b8997899a2 XML.cache for partial sharing (strings only);
wenzelm
parents: 44721
diff changeset
   194
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   195
  /** cache **/
34108
54d48ca8708f cache for partial sharing;
wenzelm
parents: 34047
diff changeset
   196
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   197
  object Cache
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   198
  {
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   199
    def make(
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73030
diff changeset
   200
      xz: XZ.Cache = XZ.Cache.make(),
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73030
diff changeset
   201
      max_string: Int = isabelle.Cache.default_max_string,
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   202
        initial_size: Int = isabelle.Cache.default_initial_size): Cache =
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73030
diff changeset
   203
      new Cache(xz, max_string, initial_size)
68169
395432e7516e tuned signature;
wenzelm
parents: 67827
diff changeset
   204
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73030
diff changeset
   205
    val none: Cache = make(XZ.Cache.none, max_string = 0)
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   206
  }
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   207
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73030
diff changeset
   208
  class Cache private[XML](val xz: XZ.Cache, max_string: Int, initial_size: Int)
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   209
    extends isabelle.Cache(max_string, initial_size)
34108
54d48ca8708f cache for partial sharing;
wenzelm
parents: 34047
diff changeset
   210
  {
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   211
    protected def cache_props(x: Properties.T): Properties.T =
44704
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   212
    {
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   213
      if (x.isEmpty) x
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   214
      else
34133
wenzelm
parents: 34119
diff changeset
   215
        lookup(x) match {
wenzelm
parents: 34119
diff changeset
   216
          case Some(y) => y
65903
692e428803c8 clarified signature;
wenzelm
parents: 65772
diff changeset
   217
          case None => store(x.map(p => (Library.isolate_substring(p._1).intern, cache_string(p._2))))
34133
wenzelm
parents: 34119
diff changeset
   218
        }
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   219
    }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   220
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   221
    protected def cache_markup(x: Markup): Markup =
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   222
    {
44704
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   223
      lookup(x) match {
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   224
        case Some(y) => y
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   225
        case None =>
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   226
          x match {
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   227
            case Markup(name, props) =>
51663
098f3cf6c809 tuned signature;
wenzelm
parents: 51223
diff changeset
   228
              store(Markup(cache_string(name), cache_props(props)))
44704
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   229
          }
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   230
      }
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   231
    }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   232
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   233
    protected def cache_tree(x: XML.Tree): XML.Tree =
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   234
    {
44704
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   235
      lookup(x) match {
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   236
        case Some(y) => y
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   237
        case None =>
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   238
          x match {
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   239
            case XML.Elem(markup, body) =>
51663
098f3cf6c809 tuned signature;
wenzelm
parents: 51223
diff changeset
   240
              store(XML.Elem(cache_markup(markup), cache_body(body)))
098f3cf6c809 tuned signature;
wenzelm
parents: 51223
diff changeset
   241
            case XML.Text(text) => store(XML.Text(cache_string(text)))
44704
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   242
          }
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   243
      }
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   244
    }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   245
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   246
    protected def cache_body(x: XML.Body): XML.Body =
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   247
    {
44704
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   248
      if (x.isEmpty) x
528d635ef6f0 synchronous XML.Cache without actor -- potentially more efficient on machines with few cores;
wenzelm
parents: 44698
diff changeset
   249
      else
34133
wenzelm
parents: 34119
diff changeset
   250
        lookup(x) match {
wenzelm
parents: 34119
diff changeset
   251
          case Some(y) => y
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 70828
diff changeset
   252
          case None => x.map(cache_tree)
34133
wenzelm
parents: 34119
diff changeset
   253
        }
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 68169
diff changeset
   254
    }
38446
9d59dab38fef XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents: 38268
diff changeset
   255
73030
72a8fdfa185d support more direct hash-consing via XML.Cache;
wenzelm
parents: 73028
diff changeset
   256
    // support hash-consing
72a8fdfa185d support more direct hash-consing via XML.Cache;
wenzelm
parents: 73028
diff changeset
   257
    def tree0(x: XML.Tree): XML.Tree =
72a8fdfa185d support more direct hash-consing via XML.Cache;
wenzelm
parents: 73028
diff changeset
   258
      if (no_cache) x else synchronized { lookup(x) getOrElse store(x) }
72a8fdfa185d support more direct hash-consing via XML.Cache;
wenzelm
parents: 73028
diff changeset
   259
38446
9d59dab38fef XML.Cache: pipe-lined (thread-safe) version using actor;
wenzelm
parents: 38268
diff changeset
   260
    // main methods
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   261
    def props(x: Properties.T): Properties.T =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   262
      if (no_cache) x else synchronized { cache_props(x) }
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   263
    def markup(x: Markup): Markup =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   264
      if (no_cache) x else synchronized { cache_markup(x) }
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   265
    def tree(x: XML.Tree): XML.Tree =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   266
      if (no_cache) x else synchronized { cache_tree(x) }
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   267
    def body(x: XML.Body): XML.Body =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   268
      if (no_cache) x else synchronized { cache_body(x) }
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   269
    def elem(x: XML.Elem): XML.Elem =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71601
diff changeset
   270
      if (no_cache) x else synchronized { cache_tree(x).asInstanceOf[XML.Elem] }
34108
54d48ca8708f cache for partial sharing;
wenzelm
parents: 34047
diff changeset
   271
  }
54d48ca8708f cache for partial sharing;
wenzelm
parents: 34047
diff changeset
   272
54d48ca8708f cache for partial sharing;
wenzelm
parents: 34047
diff changeset
   273
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   274
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   275
  /** XML as data representation language **/
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   276
51987
7d8e0e3c553b tuned signature;
wenzelm
parents: 51663
diff changeset
   277
  abstract class Error(s: String) extends Exception(s)
7d8e0e3c553b tuned signature;
wenzelm
parents: 51663
diff changeset
   278
  class XML_Atom(s: String) extends Error(s)
7d8e0e3c553b tuned signature;
wenzelm
parents: 51663
diff changeset
   279
  class XML_Body(body: XML.Body) extends Error("")
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   280
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   281
  object Encode
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   282
  {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   283
    type T[A] = A => XML.Body
65334
wenzelm
parents: 65333
diff changeset
   284
    type V[A] = PartialFunction[A, (List[String], XML.Body)]
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 69867
diff changeset
   285
    type P[A] = PartialFunction[A, List[String]]
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   286
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   287
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   288
    /* atomic values */
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   289
57909
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 55618
diff changeset
   290
    def long_atom(i: Long): String = Library.signed_string_of_long(i)
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   291
57909
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 55618
diff changeset
   292
    def int_atom(i: Int): String = Library.signed_string_of_int(i)
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   293
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   294
    def bool_atom(b: Boolean): String = if (b) "1" else "0"
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   295
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   296
    def unit_atom(u: Unit) = ""
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   297
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   298
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   299
    /* structural nodes */
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   300
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   301
    private def node(ts: XML.Body): XML.Tree = XML.Elem(Markup(":", Nil), ts)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   302
43781
d43e5f79bdc2 retain some terminology of "XML attributes";
wenzelm
parents: 43780
diff changeset
   303
    private def vector(xs: List[String]): XML.Attributes =
46839
f7232c078fa5 simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents: 45673
diff changeset
   304
      xs.zipWithIndex.map({ case (x, i) => (int_atom(i), x) })
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   305
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   306
    private def tagged(tag: Int, data: (List[String], XML.Body)): XML.Tree =
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   307
      XML.Elem(Markup(int_atom(tag), vector(data._1)), data._2)
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   308
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   309
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   310
    /* representation of standard types */
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   311
65333
289561ca4fa3 more operations;
wenzelm
parents: 64820
diff changeset
   312
    val tree: T[XML.Tree] = (t => List(t))
289561ca4fa3 more operations;
wenzelm
parents: 64820
diff changeset
   313
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents: 43778
diff changeset
   314
    val properties: T[Properties.T] =
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   315
      (props => List(XML.Elem(Markup(":", props), Nil)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   316
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   317
    val string: T[String] = (s => if (s.isEmpty) Nil else List(XML.Text(s)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   318
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   319
    val long: T[Long] = (x => string(long_atom(x)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   320
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   321
    val int: T[Int] = (x => string(int_atom(x)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   322
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   323
    val bool: T[Boolean] = (x => string(bool_atom(x)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   324
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   325
    val unit: T[Unit] = (x => string(unit_atom(x)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   326
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   327
    def pair[A, B](f: T[A], g: T[B]): T[(A, B)] =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   328
      (x => List(node(f(x._1)), node(g(x._2))))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   329
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   330
    def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   331
      (x => List(node(f(x._1)), node(g(x._2)), node(h(x._3))))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   332
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   333
    def list[A](f: T[A]): T[List[A]] =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   334
      (xs => xs.map((x: A) => node(f(x))))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   335
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   336
    def option[A](f: T[A]): T[Option[A]] =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   337
    {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   338
      case None => Nil
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   339
      case Some(x) => List(node(f(x)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   340
    }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   341
65334
wenzelm
parents: 65333
diff changeset
   342
    def variant[A](fs: List[V[A]]): T[A] =
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   343
    {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   344
      case x =>
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   345
        val (f, tag) = fs.iterator.zipWithIndex.find(p => p._1.isDefinedAt(x)).get
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   346
        List(tagged(tag, f(x)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   347
    }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   348
  }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   349
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   350
  object Decode
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   351
  {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   352
    type T[A] = XML.Body => A
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   353
    type V[A] = (List[String], XML.Body) => A
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 69867
diff changeset
   354
    type P[A] = PartialFunction[List[String], A]
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   355
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   356
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   357
    /* atomic values */
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   358
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   359
    def long_atom(s: String): Long =
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   360
      try { java.lang.Long.parseLong(s) }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   361
      catch { case e: NumberFormatException => throw new XML_Atom(s) }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   362
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   363
    def int_atom(s: String): Int =
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   364
      try { Integer.parseInt(s) }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   365
      catch { case e: NumberFormatException => throw new XML_Atom(s) }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   366
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   367
    def bool_atom(s: String): Boolean =
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   368
      if (s == "1") true
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   369
      else if (s == "0") false
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   370
      else throw new XML_Atom(s)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   371
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   372
    def unit_atom(s: String): Unit =
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   373
      if (s == "") () else throw new XML_Atom(s)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   374
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   375
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   376
    /* structural nodes */
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   377
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   378
    private def node(t: XML.Tree): XML.Body =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   379
      t match {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   380
        case XML.Elem(Markup(":", Nil), ts) => ts
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   381
        case _ => throw new XML_Body(List(t))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   382
      }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   383
43781
d43e5f79bdc2 retain some terminology of "XML attributes";
wenzelm
parents: 43780
diff changeset
   384
    private def vector(atts: XML.Attributes): List[String] =
46839
f7232c078fa5 simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents: 45673
diff changeset
   385
      atts.iterator.zipWithIndex.map(
f7232c078fa5 simplified -- plain map_index is sufficient (pointed out by Enrico Tassi);
wenzelm
parents: 45673
diff changeset
   386
        { case ((a, x), i) => if (int_atom(a) == i) x else throw new XML_Atom(a) }).toList
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   387
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   388
    private def tagged(t: XML.Tree): (Int, (List[String], XML.Body)) =
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   389
      t match {
43781
d43e5f79bdc2 retain some terminology of "XML attributes";
wenzelm
parents: 43780
diff changeset
   390
        case XML.Elem(Markup(name, atts), ts) => (int_atom(name), (vector(atts), ts))
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   391
        case _ => throw new XML_Body(List(t))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   392
      }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   393
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   394
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   395
    /* representation of standard types */
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   396
65333
289561ca4fa3 more operations;
wenzelm
parents: 64820
diff changeset
   397
    val tree: T[XML.Tree] =
289561ca4fa3 more operations;
wenzelm
parents: 64820
diff changeset
   398
    {
289561ca4fa3 more operations;
wenzelm
parents: 64820
diff changeset
   399
      case List(t) => t
289561ca4fa3 more operations;
wenzelm
parents: 64820
diff changeset
   400
      case ts => throw new XML_Body(ts)
289561ca4fa3 more operations;
wenzelm
parents: 64820
diff changeset
   401
    }
289561ca4fa3 more operations;
wenzelm
parents: 64820
diff changeset
   402
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents: 43778
diff changeset
   403
    val properties: T[Properties.T] =
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   404
    {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   405
      case List(XML.Elem(Markup(":", props), Nil)) => props
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   406
      case ts => throw new XML_Body(ts)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   407
    }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   408
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   409
    val string: T[String] =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   410
    {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   411
      case Nil => ""
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   412
      case List(XML.Text(s)) => s
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   413
      case ts => throw new XML_Body(ts)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   414
    }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   415
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   416
    val long: T[Long] = (x => long_atom(string(x)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   417
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   418
    val int: T[Int] = (x => int_atom(string(x)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   419
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   420
    val bool: T[Boolean] = (x => bool_atom(string(x)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   421
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   422
    val unit: T[Unit] = (x => unit_atom(string(x)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   423
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   424
    def pair[A, B](f: T[A], g: T[B]): T[(A, B)] =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   425
    {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   426
      case List(t1, t2) => (f(node(t1)), g(node(t2)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   427
      case ts => throw new XML_Body(ts)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   428
    }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   429
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   430
    def triple[A, B, C](f: T[A], g: T[B], h: T[C]): T[(A, B, C)] =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   431
    {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   432
      case List(t1, t2, t3) => (f(node(t1)), g(node(t2)), h(node(t3)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   433
      case ts => throw new XML_Body(ts)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   434
    }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   435
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   436
    def list[A](f: T[A]): T[List[A]] =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   437
      (ts => ts.map(t => f(node(t))))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   438
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   439
    def option[A](f: T[A]): T[Option[A]] =
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   440
    {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   441
      case Nil => None
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   442
      case List(t) => Some(f(node(t)))
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   443
      case ts => throw new XML_Body(ts)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   444
    }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   445
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   446
    def variant[A](fs: List[V[A]]): T[A] =
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   447
    {
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   448
      case List(t) =>
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   449
        val (tag, (xs, ts)) = tagged(t)
43768
d52ab827d62b more precise exceptions;
wenzelm
parents: 43767
diff changeset
   450
        val f =
d52ab827d62b more precise exceptions;
wenzelm
parents: 43767
diff changeset
   451
          try { fs(tag) }
d52ab827d62b more precise exceptions;
wenzelm
parents: 43767
diff changeset
   452
          catch { case _: IndexOutOfBoundsException => throw new XML_Body(List(t)) }
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43768
diff changeset
   453
        f(xs, ts)
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   454
      case ts => throw new XML_Body(ts)
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   455
    }
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43747
diff changeset
   456
  }
27931
b533a9de87a7 Minimalistic XML tree values.
wenzelm
parents:
diff changeset
   457
}