Class: RipperTree
- Inherits:
-
Object
- Object
- RipperTree
- Defined in:
- lib/ripper_tree.rb,
lib/ripper_tree/version.rb
Constant Summary collapse
- OPTIONS =
{ space_size: 8, color: true }
- T_LINE =
'├────'- I_LINE =
'│'- L_LINE =
'└────'- SCANNER_EVENT =
/^(@CHAR|@const|@cvar|@float|@gvar|@ident|@imaginary|@int|@ivar|@kw|@label|@op|@period|@rational|@regexp_end|@tstring_content)$/- VERSION =
'0.1.3'
Class Method Summary collapse
Instance Method Summary collapse
- #get_line(end_line: nil, space: '') ⇒ Object
- #get_space(end_line: nil) ⇒ Object
-
#initialize ⇒ RipperTree
constructor
A new instance of RipperTree.
- #output_event_id(id) ⇒ Object
- #output_value_node(node) ⇒ Object
- #parse(parent, space: ' ') ⇒ Object
- #parse_method_arguments(parent, space: ' ') ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ RipperTree
Returns a new instance of RipperTree.
18 19 20 |
# File 'lib/ripper_tree.rb', line 18 def initialize @queue = [] end |
Class Method Details
.create(code) ⇒ Object
12 13 14 15 16 |
# File 'lib/ripper_tree.rb', line 12 def self.create(code) new.tap do |rtree| rtree.parse(Ripper.sexp(code)) end end |
Instance Method Details
#get_line(end_line: nil, space: '') ⇒ Object
26 27 28 |
# File 'lib/ripper_tree.rb', line 26 def get_line(end_line: nil, space: '') end_line ? "#{space}#{L_LINE} " : "#{space}#{T_LINE} " end |
#get_space(end_line: nil) ⇒ Object
30 31 32 |
# File 'lib/ripper_tree.rb', line 30 def get_space(end_line: nil) end_line ? ' ' * RipperTree::OPTIONS[:space_size] : I_LINE + ' ' * (RipperTree::OPTIONS[:space_size]-1) end |
#output_event_id(id) ⇒ Object
34 35 36 |
# File 'lib/ripper_tree.rb', line 34 def output_event_id(id) "#{id.inspect}\n".colorize(:magenta) end |
#output_value_node(node) ⇒ Object
38 39 40 |
# File 'lib/ripper_tree.rb', line 38 def output_value_node(node) "#{node[0].inspect.colorize(:blue)} [#{node[1].inspect.colorize(:red)}] #{node[2].join(':')}\n" end |
#parse(parent, space: ' ') ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ripper_tree.rb', line 111 def parse(parent, space: ' ') event_id, *nodes = parent if event_id.instance_of?(Array) parse(parent.first, space: space) return end case event_id when SCANNER_EVENT @queue << output_value_node(parent) return when /array/ @queue << output_event_id(event_id) nodes = parent[1].nil? ? [] : parent[1] until nodes.empty? node = nodes.shift next if node == :args_add_star next if node == [] @queue << get_line(end_line: nodes.empty?, space: space) parse(node, space: space + get_space(end_line: nodes.empty?)) end return when /params/ parse_method_arguments(parent, space: space) return when /void_stmt/ @queue << output_event_id(event_id) return else @queue << output_event_id(event_id) until nodes.empty? node = nodes.shift if node.instance_of?(Array) && !node.empty? if node.all? { |e| e.instance_of?(Array) } until node.empty? event = node.shift @queue << get_line(end_line: nodes.empty? && node.empty?, space: space) parse(event, space: space + get_space(end_line: nodes.empty? && node.empty?)) end else @queue << get_line(end_line: nodes.empty?, space: space) parse(node, space: space + get_space(end_line: nodes.empty?)) end else @queue << get_line(end_line: nodes.empty?, space: space) @queue << "#{node.inspect}\n" end end end end |
#parse_method_arguments(parent, space: ' ') ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ripper_tree.rb', line 42 def parse_method_arguments(parent, space: ' ') id = parent.first @queue << output_event_id(id) params = parent[1..-1].zip(%i(pars opts rest pars2 kws kwrest blk)) until params.empty? param, arg_type = params.shift @queue << get_line(end_line: params.empty?, space: space) if param.instance_of?(Array) @queue << output_event_id(arg_type) if arg_type == :kwrest s = space + get_space(end_line: params.empty? && param.empty?) @queue << "#{s}#{L_LINE} " @queue << output_value_node(param) next end arg_count = 0 until param.empty? e = param.shift arg_count += 1 next if e == :rest_param next if e == :blockarg next if e.nil? s = space + get_space(end_line: params.empty? && param.empty?) @queue << get_line(end_line: param.empty?, space: s) case arg_type when :opts ident = e.first val = e.last @queue << "arg#{arg_count}\n" ss = s + get_space(end_line: param.empty?) @queue << get_line(end_line: false, space: ss) @queue << output_value_node(ident) @queue << get_line(end_line: true, space: ss) case val.first when /var_ref/ v = val.last @queue << output_value_node(v) when /symbol_literal|string_literal|array|hash|call|paren/ sss = ss + get_space(end_line: param.empty?) parse(val, space: sss) else @queue << output_value_node(val) end else parse(e, space: space + s) end end else if param =~ /\d+/ @queue << ":kwrest\n" else @queue << "#{param.inspect}\n" end end end end |
#to_s ⇒ Object
22 23 24 |
# File 'lib/ripper_tree.rb', line 22 def to_s RipperTree::OPTIONS[:color] ? @queue.join : @queue.join.uncolorize end |