Class: Raabro::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/raabro.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject

Returns the value of attribute children.



50
51
52
# File 'lib/raabro.rb', line 50

def children
  @children
end

#inputObject

Returns the value of attribute input.



47
48
49
# File 'lib/raabro.rb', line 47

def input
  @input
end

#lengthObject

Returns the value of attribute length.



49
50
51
# File 'lib/raabro.rb', line 49

def length
  @length
end

#nameObject

Returns the value of attribute name.



47
48
49
# File 'lib/raabro.rb', line 47

def name
  @name
end

#offsetObject

Returns the value of attribute offset.



49
50
51
# File 'lib/raabro.rb', line 49

def offset
  @offset
end

#parterObject

Returns the value of attribute parter.



50
51
52
# File 'lib/raabro.rb', line 50

def parter
  @parter
end

#resultObject

((-1 error,)) 0 nomatch, 1 success



48
49
50
# File 'lib/raabro.rb', line 48

def result
  @result
end

Instance Method Details

#c0Object



63
# File 'lib/raabro.rb', line 63

def c0; @children[0]; end

#c1Object



64
# File 'lib/raabro.rb', line 64

def c1; @children[1]; end

#c2Object



65
# File 'lib/raabro.rb', line 65

def c2; @children[2]; end

#c3Object



66
# File 'lib/raabro.rb', line 66

def c3; @children[3]; end

#c4Object



67
# File 'lib/raabro.rb', line 67

def c4; @children[4]; end

#clastObject



68
# File 'lib/raabro.rb', line 68

def clast; @children.last; end

#empty?Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/raabro.rb', line 70

def empty?

  @result == 1 && @length == 0
end

#even_childrenObject



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_errorObject



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)

  err_message =
    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, err_message, 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_errorObject

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_childrenObject



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

#stringObject



85
# File 'lib/raabro.rb', line 85

def string; @input.string[@offset, @length]; end

#stringdObject Also known as: strind



90
# File 'lib/raabro.rb', line 90

def stringd; string.downcase; end

#stringpdObject Also known as: strinpd



92
# File 'lib/raabro.rb', line 92

def stringpd; strinp.downcase; end

#strinpObject 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_childrenObject



75
76
77
78
# File 'lib/raabro.rb', line 75

def successful_children

  @children.select { |c| c.result == 1 }
end

#symbolObject



95
# File 'lib/raabro.rb', line 95

def symbol; strinp.to_sym; end

#symboldObject 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