class Rley::Formatter::Asciitree
A formatter class that draws parse trees by using characters
Attributes
@return [String] The character pattern used for a blank indentation
@return [String] The character pattern for indentation and nesting continuation.
TODO
@return [String] The character pattern used for rendering a parent - child nesting
Public Class Methods
Source
# File lib/rley/formatter/asciitree.rb, line 32 def initialize(anIO) super(anIO) @curr_path = [] @ranks = [] @nesting_prefix = '+-- ' @blank_indent = ' ' @continuation_indent = '| ' end
Constructor. @param anIO [IO] The output stream to which the parse tree is written.
Public Instance Methods
Source
# File lib/rley/formatter/asciitree.rb, line 73 def after_subnodes(_parent, _children) curr_path.pop ranks.pop end
Method called by a ParseTreeVisitor
to which the formatter subscribed. Notification of a visit event: the visitor completed the visit of the children of a non-terminal node. @param _parent [NonTerminalNode] @param _children [Array] array of children nodes
Source
# File lib/rley/formatter/asciitree.rb, line 56 def before_non_terminal(aNonTerm) emit(aNonTerm) end
Method called by a ParseTreeVisitor
to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a non-terminal node @param aNonTerm [NonTerminalNode]
Source
# File lib/rley/formatter/asciitree.rb, line 47 def before_subnodes(parent, _children) rank_of(parent) curr_path << parent end
Method called by a ParseTreeVisitor
to which the formatter subscribed. Notification of a visit event: the visitor is about to visit the children of a non-terminal node @param parent [NonTerminalNode] @param _children [Array<ParseTreeNode>] array of children nodes
Source
# File lib/rley/formatter/asciitree.rb, line 64 def before_terminal(aTerm) emit(aTerm, ": '#{aTerm.token.lexeme}'") end
Method called by a ParseTreeVisitor
to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a terminal node @param aTerm [TerminalNode]
Private Instance Methods
Source
# File lib/rley/formatter/asciitree.rb, line 125 def emit(aNode, aSuffix = '') output.puts("#{path_prefix}#{aNode.symbol.name}#{aSuffix}") end
Source
# File lib/rley/formatter/asciitree.rb, line 105 def path_prefix return '' if ranks.empty? prefix = +'' @ranks.each_with_index do |rank, i| next if i.zero? case rank when 'first', 'other' prefix << continuation_indent when 'last', 'first_and_last', 'root' prefix << blank_indent end end prefix << nesting_prefix prefix end
‘root’, ‘first’, ‘first_and_last’, ‘last’, ‘other’
Source
# File lib/rley/formatter/asciitree.rb, line 82 def rank_of(aChild) if curr_path.empty? rank = 'root' elsif curr_path[-1].subnodes.size == 1 # steep:ignore rank = 'first_and_last' else # @type var parent : Rley::PTree::NonTerminalNode parent = curr_path[-1] siblings = parent.subnodes siblings_last_index = siblings.size - 1 rank = case siblings.find_index(aChild) when 0 then 'first' when siblings_last_index then 'last' else 'other' end end ranks << rank rank end
Parent node is last node in current path or current path is empty (then aChild is root node)