Class: Raabro::Tree
- Inherits:
-
Object
- Object
- Raabro::Tree
- Defined in:
- lib/raabro.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#input ⇒ Object
Returns the value of attribute input.
-
#length ⇒ Object
Returns the value of attribute length.
-
#name ⇒ Object
Returns the value of attribute name.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#parter ⇒ Object
Returns the value of attribute parter.
-
#result ⇒ Object
((-1 error,)) 0 nomatch, 1 success.
Instance Method Summary collapse
- #c0 ⇒ Object
- #c1 ⇒ Object
- #c2 ⇒ Object
- #c3 ⇒ Object
- #c4 ⇒ Object
- #clast ⇒ Object
- #empty? ⇒ Boolean
- #even_children ⇒ Object
- #extract_error ⇒ Object
- #gather(name = nil, acc = []) ⇒ Object
-
#initialize(name, parter, input) ⇒ Tree
constructor
A new instance of Tree.
- #line_and_column(offset) ⇒ Object
- #lookup(name = nil) ⇒ Object
-
#lookup_all_error ⇒ Object
Not “lookup all errors” but “lookup all error”, in other words lookup the point up until which the parser stopped (not consuming all the input).
- #lookup_error(stack = []) ⇒ Object
- #nonstring(l = 7) ⇒ Object
- #odd_children ⇒ Object
- #prune! ⇒ Object
- #string ⇒ Object
- #stringd ⇒ Object (also: #strind)
- #stringpd ⇒ Object (also: #strinpd)
- #strinp ⇒ Object (also: #strim)
- #subgather(name = nil, acc = []) ⇒ Object
- #sublookup(name = nil) ⇒ Object
- #successful_children ⇒ Object
- #symbol ⇒ Object
- #symbold ⇒ Object (also: #symbod)
- #to_a(opts = {}) ⇒ Object
- #to_s(depth = 0, io = StringIO.new) ⇒ Object
- #visual(line, column) ⇒ Object
Constructor Details
#initialize(name, parter, input) ⇒ Tree
Returns a new instance of Tree.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/raabro.rb', line 49 def initialize(name, parter, input) @result = 0 @name = name @parter = parter @input = input @offset = input.offset @length = 0 @children = [] end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
47 48 49 |
# File 'lib/raabro.rb', line 47 def children @children end |
#input ⇒ Object
Returns the value of attribute input.
44 45 46 |
# File 'lib/raabro.rb', line 44 def input @input end |
#length ⇒ Object
Returns the value of attribute length.
46 47 48 |
# File 'lib/raabro.rb', line 46 def length @length end |
#name ⇒ Object
Returns the value of attribute name.
44 45 46 |
# File 'lib/raabro.rb', line 44 def name @name end |
#offset ⇒ Object
Returns the value of attribute offset.
46 47 48 |
# File 'lib/raabro.rb', line 46 def offset @offset end |
#parter ⇒ Object
Returns the value of attribute parter.
47 48 49 |
# File 'lib/raabro.rb', line 47 def parter @parter end |
#result ⇒ Object
((-1 error,)) 0 nomatch, 1 success
45 46 47 |
# File 'lib/raabro.rb', line 45 def result @result end |
Instance Method Details
#c0 ⇒ Object
60 |
# File 'lib/raabro.rb', line 60 def c0; @children[0]; end |
#c1 ⇒ Object
61 |
# File 'lib/raabro.rb', line 61 def c1; @children[1]; end |
#c2 ⇒ Object
62 |
# File 'lib/raabro.rb', line 62 def c2; @children[2]; end |
#c3 ⇒ Object
63 |
# File 'lib/raabro.rb', line 63 def c3; @children[3]; end |
#c4 ⇒ Object
64 |
# File 'lib/raabro.rb', line 64 def c4; @children[4]; end |
#clast ⇒ Object
65 |
# File 'lib/raabro.rb', line 65 def clast; @children.last; end |
#empty? ⇒ Boolean
67 68 69 70 |
# File 'lib/raabro.rb', line 67 def empty? @result == 1 && @length == 0 end |
#even_children ⇒ Object
166 167 168 169 |
# File 'lib/raabro.rb', line 166 def even_children cs = []; @children.each_with_index { |c, i| cs << c if i.even? }; cs end |
#extract_error ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/raabro.rb', line 171 def extract_error #Raabro.pp(self, colors: true) err_tree, stack = lookup_error || lookup_all_error line, column = line_and_column(err_tree.offset) = if stack path = stack .compact.reverse.take(3).reverse .collect(&:inspect).join('/') "parsing failed .../#{path}" else 'parsing failed, not all input was consumed' end visual = visual(line, column) [ line, column, err_tree.offset, , visual ] end |
#gather(name = nil, acc = []) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/raabro.rb', line 112 def gather(name=nil, acc=[]) name = name ? name.to_s : nil if (@name && name == nil) || (@name.to_s == name) acc << self else subgather(name, acc) end acc end |
#line_and_column(offset) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/raabro.rb', line 216 def line_and_column(offset) line = 1 column = 0 (0..offset).each do |off| column += 1 next unless @input.at(off) == "\n" line += 1 column = 0 end [ line, column ] end |
#lookup(name = nil) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/raabro.rb', line 96 def lookup(name=nil) name = name ? name.to_s : nil return self if @name && name == nil return self if @name.to_s == name sublookup(name) end |
#lookup_all_error ⇒ Object
Not “lookup all errors” but “lookup all error”, in other words lookup the point up until which the parser stopped (not consuming all the input)
208 209 210 211 212 213 214 |
# File 'lib/raabro.rb', line 208 def lookup_all_error #print "lae(): "; Raabro.pp(self, colors: true) @children.each { |c| return [ c, nil ] if c.result == 0 } @children.reverse.each { |c| es = c.lookup_all_error; return es if es } nil end |
#lookup_error(stack = []) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/raabro.rb', line 193 def lookup_error(stack=[]) #print 'le(): '; Raabro.pp(self, colors: true) return nil if @result != 0 return [ self, stack ] if @children.empty? @children.each { |c| es = c.lookup_error(stack.dup.push(self.name)) return es if es } nil end |
#nonstring(l = 7) ⇒ Object
85 |
# File 'lib/raabro.rb', line 85 def nonstring(l=7); @input.string[@offset, l]; end |
#odd_children ⇒ Object
161 162 163 164 |
# File 'lib/raabro.rb', line 161 def odd_children cs = []; @children.each_with_index { |c, i| cs << c if i.odd? }; cs end |
#prune! ⇒ Object
77 78 79 80 |
# File 'lib/raabro.rb', line 77 def prune! @children = successful_children end |
#string ⇒ Object
82 |
# File 'lib/raabro.rb', line 82 def string; @input.string[@offset, @length]; end |
#stringd ⇒ Object Also known as: strind
87 |
# File 'lib/raabro.rb', line 87 def stringd; string.downcase; end |
#stringpd ⇒ Object Also known as: strinpd
89 |
# File 'lib/raabro.rb', line 89 def stringpd; strinp.downcase; end |
#strinp ⇒ Object Also known as: strim
83 |
# File 'lib/raabro.rb', line 83 def strinp; string.strip; end |
#subgather(name = nil, acc = []) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/raabro.rb', line 125 def subgather(name=nil, acc=[]) @children.each { |c| c.gather(name, acc) } acc end |
#sublookup(name = nil) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/raabro.rb', line 105 def sublookup(name=nil) @children.each { |c| if n = c.lookup(name); return n; end } nil end |
#successful_children ⇒ Object
72 73 74 75 |
# File 'lib/raabro.rb', line 72 def successful_children @children.select { |c| c.result == 1 } end |
#symbol ⇒ Object
92 |
# File 'lib/raabro.rb', line 92 def symbol; strinp.to_sym; end |
#symbold ⇒ Object Also known as: symbod
93 |
# File 'lib/raabro.rb', line 93 def symbold; symbol.downcase; end |
#to_a(opts = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/raabro.rb', line 132 def to_a(opts={}) opts = Array(opts).inject({}) { |h, e| h[e] = true; h } \ unless opts.is_a?(Hash) cn = if opts[:leaves] && (@result == 1) && @children.empty? string elsif opts[:children] != false @children.collect { |e| e.to_a(opts) } else @children.length end [ @name, @result, @offset, @length, @note, @parter, cn ] end |
#to_s(depth = 0, io = StringIO.new) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/raabro.rb', line 149 def to_s(depth=0, io=StringIO.new) io.print "\n" if depth > 0 io.print ' ' * depth io.print "#{@result} #{@name.inspect} #{@offset},#{@length}" io.print result == 1 && children.size == 0 ? ' ' + string.inspect : '' @children.each { |c| c.to_s(depth + 1, io) } depth == 0 ? io.string : nil end |
#visual(line, column) ⇒ Object
233 234 235 236 237 |
# File 'lib/raabro.rb', line 233 def visual(line, column) @input.string.split("\n")[line - 1] + "\n" + ' ' * (column - 1) + '^---' end |