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.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/raabro.rb', line 52 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.
50 51 52 |
# File 'lib/raabro.rb', line 50 def children @children end |
#input ⇒ Object
Returns the value of attribute input.
47 48 49 |
# File 'lib/raabro.rb', line 47 def input @input end |
#length ⇒ Object
Returns the value of attribute length.
49 50 51 |
# File 'lib/raabro.rb', line 49 def length @length end |
#name ⇒ Object
Returns the value of attribute name.
47 48 49 |
# File 'lib/raabro.rb', line 47 def name @name end |
#offset ⇒ Object
Returns the value of attribute offset.
49 50 51 |
# File 'lib/raabro.rb', line 49 def offset @offset end |
#parter ⇒ Object
Returns the value of attribute parter.
50 51 52 |
# File 'lib/raabro.rb', line 50 def parter @parter end |
#result ⇒ Object
((-1 error,)) 0 nomatch, 1 success
48 49 50 |
# File 'lib/raabro.rb', line 48 def result @result end |
Instance Method Details
#c0 ⇒ Object
63 |
# File 'lib/raabro.rb', line 63 def c0; @children[0]; end |
#c1 ⇒ Object
64 |
# File 'lib/raabro.rb', line 64 def c1; @children[1]; end |
#c2 ⇒ Object
65 |
# File 'lib/raabro.rb', line 65 def c2; @children[2]; end |
#c3 ⇒ Object
66 |
# File 'lib/raabro.rb', line 66 def c3; @children[3]; end |
#c4 ⇒ Object
67 |
# File 'lib/raabro.rb', line 67 def c4; @children[4]; end |
#clast ⇒ Object
68 |
# File 'lib/raabro.rb', line 68 def clast; @children.last; end |
#empty? ⇒ Boolean
70 71 72 73 |
# File 'lib/raabro.rb', line 70 def empty? @result == 1 && @length == 0 end |
#even_children ⇒ Object
169 170 171 172 |
# File 'lib/raabro.rb', line 169 def even_children cs = []; @children.each_with_index { |c, i| cs << c if i.even? }; cs end |
#extract_error ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/raabro.rb', line 174 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
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/raabro.rb', line 115 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
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/raabro.rb', line 219 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
99 100 101 102 103 104 105 106 |
# File 'lib/raabro.rb', line 99 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)
211 212 213 214 215 216 217 |
# File 'lib/raabro.rb', line 211 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
196 197 198 199 200 201 202 203 204 205 |
# File 'lib/raabro.rb', line 196 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
88 |
# File 'lib/raabro.rb', line 88 def nonstring(l=7); @input.string[@offset, l]; end |
#odd_children ⇒ Object
164 165 166 167 |
# File 'lib/raabro.rb', line 164 def odd_children cs = []; @children.each_with_index { |c, i| cs << c if i.odd? }; cs end |
#prune! ⇒ Object
80 81 82 83 |
# File 'lib/raabro.rb', line 80 def prune! @children = successful_children end |
#string ⇒ Object
85 |
# File 'lib/raabro.rb', line 85 def string; @input.string[@offset, @length]; end |
#stringd ⇒ Object Also known as: strind
90 |
# File 'lib/raabro.rb', line 90 def stringd; string.downcase; end |
#stringpd ⇒ Object Also known as: strinpd
92 |
# File 'lib/raabro.rb', line 92 def stringpd; strinp.downcase; end |
#strinp ⇒ Object Also known as: strim
86 |
# File 'lib/raabro.rb', line 86 def strinp; string.strip; end |
#subgather(name = nil, acc = []) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/raabro.rb', line 128 def subgather(name=nil, acc=[]) @children.each { |c| c.gather(name, acc) } acc end |
#sublookup(name = nil) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/raabro.rb', line 108 def sublookup(name=nil) @children.each { |c| if n = c.lookup(name); return n; end } nil end |
#successful_children ⇒ Object
75 76 77 78 |
# File 'lib/raabro.rb', line 75 def successful_children @children.select { |c| c.result == 1 } end |
#symbol ⇒ Object
95 |
# File 'lib/raabro.rb', line 95 def symbol; strinp.to_sym; end |
#symbold ⇒ Object Also known as: symbod
96 |
# File 'lib/raabro.rb', line 96 def symbold; symbol.downcase; end |
#to_a(opts = {}) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/raabro.rb', line 135 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
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/raabro.rb', line 152 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
236 237 238 239 240 |
# File 'lib/raabro.rb', line 236 def visual(line, column) @input.string.split("\n")[line - 1] + "\n" + ' ' * (column - 1) + '^---' end |