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.



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

#childrenObject

Returns the value of attribute children.



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

def children
  @children
end

#inputObject

Returns the value of attribute input.



44
45
46
# File 'lib/raabro.rb', line 44

def input
  @input
end

#lengthObject

Returns the value of attribute length.



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

def length
  @length
end

#nameObject

Returns the value of attribute name.



44
45
46
# File 'lib/raabro.rb', line 44

def name
  @name
end

#offsetObject

Returns the value of attribute offset.



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

def offset
  @offset
end

#parterObject

Returns the value of attribute parter.



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

def parter
  @parter
end

#resultObject

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



45
46
47
# File 'lib/raabro.rb', line 45

def result
  @result
end

Instance Method Details

#c0Object



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

def c0; @children[0]; end

#c1Object



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

def c1; @children[1]; end

#c2Object



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

def c2; @children[2]; end

#c3Object



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

def c3; @children[3]; end

#c4Object



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

def c4; @children[4]; end

#clastObject



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

def clast; @children.last; end

#empty?Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/raabro.rb', line 67

def empty?

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

#even_childrenObject



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_errorObject



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)

  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



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_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)



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_childrenObject



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

#stringObject



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

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

#stringdObject Also known as: strind



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

def stringd; string.downcase; end

#stringpdObject Also known as: strinpd



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

def stringpd; strinp.downcase; end

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



72
73
74
75
# File 'lib/raabro.rb', line 72

def successful_children

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

#symbolObject



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

def symbol; strinp.to_sym; end

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