2014-08-15 21:29:48 +02:00
|
|
|
# We transform objects into a tree of nodes
|
|
|
|
|
|
|
|
module Sof
|
2014-08-16 11:34:25 +02:00
|
|
|
#abstract base class for nodes in the tree
|
2014-08-18 12:53:05 +02:00
|
|
|
# may be referenced (should be a simple name or number)
|
2014-08-15 21:29:48 +02:00
|
|
|
class Node
|
2014-08-18 12:53:05 +02:00
|
|
|
def initialize referenced
|
|
|
|
@referenced = referenced
|
|
|
|
end
|
2014-08-18 12:01:52 +02:00
|
|
|
include Util
|
2014-08-16 11:34:25 +02:00
|
|
|
# must be able to output to a stream
|
|
|
|
def out io ,level
|
2014-08-18 12:53:05 +02:00
|
|
|
io.write "&#{referenced} " if referenced
|
2014-08-16 11:34:25 +02:00
|
|
|
end
|
2014-08-18 12:53:05 +02:00
|
|
|
attr_reader :referenced
|
2014-08-16 11:34:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
class SimpleNode < Node
|
2014-08-16 14:16:07 +02:00
|
|
|
def initialize data
|
2014-08-18 12:53:05 +02:00
|
|
|
super(nil)
|
2014-08-16 14:16:07 +02:00
|
|
|
@data = data
|
2014-08-15 21:29:48 +02:00
|
|
|
end
|
2014-08-18 12:01:52 +02:00
|
|
|
attr_reader :data
|
2014-08-16 10:43:41 +02:00
|
|
|
def out io , level
|
2014-08-18 07:19:48 +02:00
|
|
|
io.write(data)
|
2014-08-16 10:43:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-18 07:19:48 +02:00
|
|
|
class ObjectNode < Node
|
2014-08-18 12:53:05 +02:00
|
|
|
def initialize data , ref
|
|
|
|
super(ref)
|
2014-08-18 07:19:48 +02:00
|
|
|
@data = data
|
|
|
|
@children = []
|
|
|
|
end
|
2014-08-18 12:01:52 +02:00
|
|
|
attr_reader :children , :data
|
2014-08-18 07:19:48 +02:00
|
|
|
def add k , v
|
|
|
|
@children << [k,v]
|
|
|
|
end
|
|
|
|
def out io , level = 0
|
2014-08-18 12:53:05 +02:00
|
|
|
super
|
2014-08-18 07:19:48 +02:00
|
|
|
io.write(@data)
|
2014-08-18 11:49:38 +02:00
|
|
|
indent = " " * (level + 1)
|
2014-08-18 07:19:48 +02:00
|
|
|
@children.each_with_index do |child , i|
|
|
|
|
k , v = child
|
2014-08-18 11:49:38 +02:00
|
|
|
io.write "\n#{indent}"
|
|
|
|
k.out(io , level + 2)
|
|
|
|
io.write " "
|
|
|
|
v.out(io , level + 2)
|
2014-08-18 07:19:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-08-15 21:29:48 +02:00
|
|
|
end
|