Skip to content

Instantly share code, notes, and snippets.

@PaperclipBadger
Last active June 20, 2016 23:32
Show Gist options
  • Select an option

  • Save PaperclipBadger/a45a2dcd04afd322fdbb9594941e0aee to your computer and use it in GitHub Desktop.

Select an option

Save PaperclipBadger/a45a2dcd04afd322fdbb9594941e0aee to your computer and use it in GitHub Desktop.
use "collections"
trait XMLElement is Seq[XMLElement]
"""A element in an XML tree.
An element is a sequence containing its children. To get the `n`th child
of some `element: XMLElement`, use `element(n)`. To get all of its children,
use `element.values()`.
"""
new initialised
( parent': (XMLElement | None) = None
, children': (ReadSeq[XMLElement] & ReadElement[XMLElement^])
// = Array[XMLElement](0)
, element_type': String = ""
, tags': ReadSeq[(String, String)] = Array[(String, String)](0)
, text': String = ""
)
"""Creates a new element, where some values are already known.
Default values are all "empty".
"""
// Structural navigation
fun parent(): this->(XMLElement | None)
"""Returns the parent of the element, or `None` if there is no parent."""
fun ref update_parent(parent': (XMLElement | None)): (XMLElement^ | None)
"""Updates the parent of this element.
Returns the old parent if there was one, otherwise `None`.
"""
// Properties
fun element_type(): this->String
"""Returns the type of the element."""
fun ref update_element_type(element_type': String): String^
"""Updates the type of the element. Returns the old type."""
fun read_tag(tag_name: String): this->String ?
"""Returns the value of the given tag."""
fun ref update_tag(tag_name: String, value: String): (String^ | None)
"""Updates one of the tags of the element.
Returns the old value of the tag if there was one, otherwise `None`.
"""
fun text(): this->String
"""Returns the text of the element."""
fun ref update_text(text': String): String^
"""Updates the text of the element. Returns the old text."""
class ListXMLElement is XMLElement
"""A element that uses a linked list to keep track of its children."""
var _parent: (XMLElement | None)
let _children: List[XMLElement]
var _element_type: String
let _tags: Map[String, String]
var _text: String
new initialised
( parent': (XMLElement | None) = None
, children': (ReadSeq[XMLElement] & ReadElement[XMLElement^])
// = Array[XMLElement](0)
, element_type': String = ""
, tags': ReadSeq[(String, String)] = Array[(String, String)](0)
, text': String = ""
) =>
"""Creates a new element, where some values are already known.
Default values are all "empty".
"""
_parent = parent'
_children = List[XMLElement]()
_children.append(children')
_element_type = element_type'
_tags = Map[String, String]()
for (tag_name, value) in tags'.values() do
_tags(tag_name) = value
end
_text = text'
// Structural navigation
fun parent(): this->(XMLElement | None) =>
"""Returns the parent of the element, or `None` if there is no parent."""
_parent
fun ref update_parent(parent': (XMLElement | None)): (XMLElement^ | None) =>
"""Updates the parent of this element.
Returns the old parent if there was one, otherwise `None`.
"""
_parent = parent'
// Properties
fun element_type(): this->String =>
"""Returns the type of the element."""
_element_type
fun ref update_element_type(element_type': String): String^ =>
"""Updates the type of the element. Returns the old type."""
_element_type = element_type'
fun read_tag(tag_name: String): this->String ? =>
"""Returns the value of the given tag."""
_tags(tag_name)
fun ref update_tag(tag_name: String, value: String): (String^ | None) =>
"""Updates one of the tags of the element.
Returns the old value of the tag if there was one, otherwise `None`.
"""
_tags(tag_name) = value
fun text(): String =>
"""Returns the text of the element."""
_text
fun ref update_text(text': String): String =>
"""Updates the text of the element. Returns the old text."""
_text = text'
// Delegation to list.
new create(len: USize val = 0) =>
"""Creates a new element with empty values for all attributes."""
_parent = None
_children = List[XMLElement](len)
_element_type = ""
_tags = Map[String, String]()
_text = ""
fun ref reserve(len: USize): ListXMLElement^ =>
_children.reserve(len)
this
fun size(): USize val =>
_children.size()
fun apply(i: USize val): this->XMLElement ? =>
_children(i)
fun ref update(i: USize val, value: XMLElement): XMLElement^ ? =>
_children(i) = value
fun ref clear(): ListXMLElement^ =>
_children.clear()
this
fun ref push(value: XMLElement): ListXMLElement^ =>
_children.push(value)
this
fun ref pop(): XMLElement^ ? =>
_children.pop()
fun ref unshift(value: XMLElement): ListXMLElement^ =>
_children.unshift(value)
this
fun ref shift(): XMLElement^ ? =>
_children.shift()
fun ref append
( seq: (ReadSeq[XMLElement] box & ReadElement[XMLElement])
, offset: USize val = 0
, len: USize val = -1
): ListXMLElement^ =>
_children.append(seq, offset, len)
this
fun ref concat
( iter: Iterator[XMLElement] ref
, offset: USize val = 0
, len: USize val = -1
): ListXMLElement^ =>
_children.concat(iter, offset, len)
this
fun ref truncate(len: USize val): ListXMLElement^ =>
_children.truncate(len)
this
fun values(): Iterator[this->XMLElement]^ =>
_children.values()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment