class Mustermann::Visualizer::TreeRenderer
Turns an AST into a Tree
@!visibility private
Constants
- FakeNode
- PREFIX_COLOR
- TEMPLATE
- THEME
Attributes
string[R]
@!visibility private
Public Class Methods
new(string)
click to toggle source
@!visibility private
# File lib/mustermann/visualizer/tree_renderer.rb, line 37 def initialize(string) @string = string end
render(pattern, **options)
click to toggle source
Takes a pattern (or pattern string and option) and turns it into a tree. Runs translation if pattern implements to_ast, otherwise returns single node tree.
@!visibility private
# File lib/mustermann/visualizer/tree_renderer.rb, line 21 def self.render(pattern, **options) pattern &&= Mustermann.new(pattern, **options) renderer = new(pattern.to_s) if pattern.respond_to? :to_ast renderer.translate(pattern.to_ast) else length = renderer.string.length node = FakeNode.new("pattern (not AST based)", 0, length, length) renderer.tree(node) end end
Public Instance Methods
children_for(list)
click to toggle source
Take a hash with trees as values and turn the keys into trees, too. Read again if that didn't make sense. @!visibility private
# File lib/mustermann/visualizer/tree_renderer.rb, line 61 def children_for(list) list.map do |key, value| value = Array(value).flatten if value.any? after = " " * string.inspect.length + " " description = Hansi.render(THEME[:orange], key.to_s) Tree.new(description, *value, after: after, prefix_color: PREFIX_COLOR) end end end
sub(*args)
click to toggle source
access a substring of the pattern, in inspect mode @!visibility private
# File lib/mustermann/visualizer/tree_renderer.rb, line 43 def sub(*args) string[*args].inspect[1..-2] end
tree(node, *children, **typed_children)
click to toggle source
creates a tree node @!visibility private
# File lib/mustermann/visualizer/tree_renderer.rb, line 49 def tree(node, *children, **typed_children) children += children_for(typed_children) children = children.flatten.grep(Tree) infos = sub(0, node.start), sub(node.start, node.length), sub(node.stop..-1) description = Hansi.render(THEME[:green], node.type.to_s.tr("_", " ")) after = Hansi.render(TEMPLATE, *infos, theme: THEME, tags: true) Tree.new(description, *children, after: after, prefix_color: PREFIX_COLOR) end