sealedabstractclass Tree extends Trav { overridedef toString: String = string_of_tree(this)
} type Body = List[Tree] caseclass Elem(markup: Markup, body: Body) extends Tree with Trav { privatelazyval hash: Int = (markup, body).hashCode() overridedef hashCode(): Int = hash
def name: String = markup.name
def update_attributes(more_attributes: Attributes): Elem = if (more_attributes.isEmpty) this else Elem(markup.update_properties(more_attributes), body)
def + (att: Attribute): Elem = Elem(markup + att, body)
} caseclass Text(content: String) extends Tree with Trav { privatelazyval hash: Int = content.hashCode() overridedef hashCode(): Int = hash
}
trait Traversal { def text(s: String): Unit def elem(markup: Markup, end: Boolean = false): Unit def end_elem(name: String): Unit
def traverse(trees: List[Tree]): Unit = {
@tailrec def trav_atomic(list: List[Trav]): List[Trav] =
list match { case Text(s) :: rest => text(s); trav_atomic(rest) case Elem(markup, Nil) :: rest => if (!markup.is_empty) elem(markup, end = true)
trav_atomic(rest) case End(name) :: rest => end_elem(name); trav_atomic(rest) case _ => list
}
@tailrec def trav(list: List[Trav]): Unit =
(trav_atomic(list) : @unchecked) match { case Nil => case Elem(markup, body) :: rest if body.nonEmpty => if (markup.is_empty) trav(trav_atomic(body) ::: rest) else { elem(markup); trav(trav_atomic(body) ::: End(markup.name) :: rest) }
}
def unapply(tree: Tree): Option[(Markup, Body, Body)] =
tree match { case
XML.Elem(Markup(XML_ELEM, (XML_NAME, name) :: props),
XML.Elem(Markup(XML_BODY, Nil), body1) :: body2) =>
Some(Markup(name, props), body1, body2) case _ => None
}
}
object Wrapped_Elem_Body { def unapply(tree: Tree): Option[Body] =
tree match { case
XML.Elem(Markup(XML_ELEM, (XML_NAME, _) :: _),
XML.Elem(Markup(XML_BODY, Nil), _) :: body) =>
Some(body) case _ => None
}
}
object Root_Elem { def apply(body: Body): XML.Elem = XML.Elem(Markup(XML_ELEM, Nil), body) def unapply(tree: Tree): Option[Body] =
tree match { case XML.Elem(Markup(XML_ELEM, Nil), body) => Some(body) case _ => None
}
}
/* filter markup elements */
def filter_elements(xml: XML.Body,
remove: Markup.Elements = Markup.Elements.empty,
expose: Markup.Elements = Markup.Elements.empty
): XML.Body = { def filter(ts: XML.Body): XML.Body =
ts flatMap { case XML.Wrapped_Elem(markup, body1, body2) => if (remove(markup.name)) Nil elseif (expose(markup.name)) filter(body2) else List(XML.Wrapped_Elem(markup, body1, filter(body2))) case XML.Elem(markup, body) => if (remove(markup.name)) Nil elseif (expose(markup.name)) filter(body) else List(XML.Elem(markup, filter(body))) case t => List(t)
}
filter(xml)
}
/* traverse text */
def traverse_text[A](body: Body, a: A, op: (A, String) => A): A = {
@tailrec def trav(x: A, list: List[Tree]): A =
list match { case Nil => x case XML.Wrapped_Elem_Body(body) :: rest => trav(x, body ::: rest) case XML.Elem(_, body) :: rest => trav(x, body ::: rest) case XML.Text(s) :: rest => trav(op(x, s), rest)
}
trav(a, body)
}
def text_length(body: Body): Int = traverse_text(body, 0, (n, s) => n + s.length) def symbol_length(body: Body): Int = traverse_text(body, 0, (n, s) => n + Symbol.length(s))
object Cache { def make(
compress: Compress.Cache = Compress.Cache.make(),
max_string: Int = isabelle.Cache.default_max_string,
initial_size: Int = isabelle.Cache.default_initial_size): Cache = new Cache(compress, max_string, initial_size)
val none: Cache = make(Compress.Cache.none, max_string = 0)
}
class Cache(val compress: Compress.Cache, max_string: Int, initial_size: Int) extends isabelle.Cache(max_string, initial_size) { protecteddef cache_props(x: Properties.T): Properties.T = { if (x.isEmpty) x else
lookup(x) match { case Some(y) => y case None => store(x.map(p => (Library.isolate_substring(p._1).intern, cache_string(p._2))))
}
}
protecteddef cache_markup(x: Markup): Markup = {
lookup(x) match { case Some(y) => y case None =>
x match { case Markup(name, props) =>
store(Markup(cache_string(name), cache_props(props)))
}
}
}
protecteddef cache_tree(x: XML.Tree): XML.Tree = {
lookup(x) match { case Some(y) => y case None =>
x match { case XML.Elem(markup, body) =>
store(XML.Elem(cache_markup(markup), cache_body(body))) case XML.Text(text) => store(XML.Text(cache_string(text)))
}
}
}
protecteddef cache_body(x: XML.Body): XML.Body = { if (x.isEmpty) x else
lookup(x) match { case Some(y) => y case None => x.map(cache_tree)
}
}
// support hash-consing def tree0(x: XML.Tree): XML.Tree = if (no_cache) x else synchronized { lookup(x) getOrElse store(x) }
// main methods def props(x: Properties.T): Properties.T = if (no_cache) x else synchronized { cache_props(x) } def markup(x: Markup): Markup = if (no_cache) x else synchronized { cache_markup(x) } def tree(x: XML.Tree): XML.Tree = if (no_cache) x else synchronized { cache_tree(x) } def body(x: XML.Body): XML.Body = if (no_cache) x else synchronized { cache_body(x) } def elem(x: XML.Elem): XML.Elem = if (no_cache) x else synchronized { cache_tree(x).asInstanceOf[XML.Elem] }
}
/** XML as data representation language **/
abstractclass Error(s: String) extends Exception(s) class XML_Atom(s: String) extends Error(s) class XML_Body(body: XML.Body) extends Error("")
object Encode { type T[A] = A => XML.Body type V[A] = PartialFunction[A, (List[String], XML.Body)] type P[A] = PartialFunction[A, List[String]]
def option[A](f: T[A]): T[Option[A]] = { case Nil => None case List(t) => Some(f(node(t))) case ts => thrownew XML_Body(ts)
}
def variant[A](fs: List[V[A]]): T[A] = { case List(t) => val (tag, (xs, ts)) = tagged(t) val f = try { fs(tag) } catch { case _: IndexOutOfBoundsException => thrownew XML_Body(List(t)) }
f(xs, ts) case ts => thrownew XML_Body(ts)
}
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.14Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.