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.



48
49
50
51
52
53
54
55
56
57
# File 'lib/raabro.rb', line 48

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.



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

def children
  @children
end

#inputObject

Returns the value of attribute input.



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

def input
  @input
end

#lengthObject

Returns the value of attribute length.



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

def length
  @length
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#offsetObject

Returns the value of attribute offset.



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

def offset
  @offset
end

#parterObject

Returns the value of attribute parter.



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

def parter
  @parter
end

#resultObject

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



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

def result
  @result
end

Instance Method Details

#c0Object



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

def c0; @children[0]; end

#c1Object



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

def c1; @children[1]; end

#c2Object



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

def c2; @children[2]; end

#c3Object



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

def c3; @children[3]; end

#c4Object



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

def c4; @children[4]; end

#clastObject



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

def clast; @children.last; end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?

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

#even_childrenObject



156
157
158
159
# File 'lib/raabro.rb', line 156

def even_children

  cs = []; @children.each_with_index { |c, i| cs << c if i.even? }; cs
end

#extract_errorObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/raabro.rb', line 161

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



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/raabro.rb', line 102

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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/raabro.rb', line 206

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



86
87
88
89
90
91
92
93
# File 'lib/raabro.rb', line 86

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)



198
199
200
201
202
203
204
# File 'lib/raabro.rb', line 198

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



183
184
185
186
187
188
189
190
191
192
# File 'lib/raabro.rb', line 183

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



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

def nonstring(l=7); @input.string[@offset, l]; end

#odd_childrenObject



151
152
153
154
# File 'lib/raabro.rb', line 151

def odd_children

  cs = []; @children.each_with_index { |c, i| cs << c if i.odd? }; cs
end

#prune!Object



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

def prune!

  @children = successful_children
end

#stringObject



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

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

#strinpObject Also known as: strim



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

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

#subgather(name = nil, acc = []) ⇒ Object



115
116
117
118
119
120
# File 'lib/raabro.rb', line 115

def subgather(name=nil, acc=[])

  @children.each { |c| c.gather(name, acc) }

  acc
end

#sublookup(name = nil) ⇒ Object



95
96
97
98
99
100
# File 'lib/raabro.rb', line 95

def sublookup(name=nil)

  @children.each { |c| if n = c.lookup(name); return n; end }

  nil
end

#successful_childrenObject



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

def successful_children

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

#to_a(opts = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/raabro.rb', line 122

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



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/raabro.rb', line 139

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



223
224
225
226
227
# File 'lib/raabro.rb', line 223

def visual(line, column)

  @input.string.split("\n")[line - 1] + "\n" +
  ' ' * (column - 1) + '^---'
end