markdown2 :: MdNode :: defaultinit
# An abstract node
abstract class MdNode
# Node location in original markdown
var location: MdLocation
# Node parent
var parent: nullable MdNode = null is writable
# First child
var first_child: nullable MdNode = null is writable
# Last child
var last_child: nullable MdNode = null is writable
# Previous node
var prev: nullable MdNode = null is writable
# Next node
var next: nullable MdNode = null is writable
# Children nodes of `self`
fun children: Array[MdNode] do
var nodes = new Array[MdNode]
var node = first_child
while node != null do
nodes.add node
node = node.next
end
return nodes
end
# Append a child to `self`
fun append_child(child: MdNode) do
child.unlink
child.parent = self
if last_child != null then
last_child.as(not null).next = child
child.prev = last_child
last_child = child
else
first_child = child
last_child = child
end
end
# Prepend a child to `self`
fun prepend_child(child: MdNode) do
child.unlink
child.parent = self
if first_child != null then
first_child.as(not null).prev = child
child.next = first_child
first_child = child
else
first_child = child
last_child = child
end
end
# Unlink `self` from its `parent`
fun unlink do
if prev != null then
prev.as(not null).next = next
else if parent != null then
parent.as(not null).first_child = next
end
if next != null then
next.as(not null).prev = prev
else if parent != null then
parent.as(not null).last_child = prev
end
parent = null
next = null
prev = null
end
# Insert `sibling` after `self`.
fun insert_after(sibling: MdNode) do
sibling.unlink
sibling.next = next
if sibling.next != null then
sibling.next.as(not null).prev = sibling
end
sibling.prev = self
next = sibling
sibling.parent = parent
if sibling.next == null then
sibling.parent.as(not null).last_child = sibling
end
end
# Insert `sibling` before `self`.
fun insert_before(sibling: MdNode) do
sibling.unlink
sibling.prev = prev
if sibling.prev != null then
sibling.prev.as(not null).next = sibling
end
sibling.next = self
prev = sibling
sibling.parent = parent
if sibling.prev == null then
sibling.parent.as(not null).first_child = sibling
end
end
# Visit all children or `self`
fun visit_all(v: MdVisitor) do
var node = first_child
while node != null do
var next = node.next
v.visit(node)
node = next
end
end
redef fun to_s do return "{super}\{{to_s_attrs}\}"
# Returns `self` attributes as a String
#
# Mainly used for debug purposes.
fun to_s_attrs: String do return "loc: {location}"
# Print `self` AST
fun debug do
var v = new MdASTPrinter
v.enter_visit(self)
end
end
lib/markdown2/markdown_ast.nit:18,1--149,3