Class: Depager::LALR::State

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, n = nil) ⇒ State

Returns a new instance of State.



193
194
195
196
197
198
199
# File 'lib/depager/lr.rb', line 193

def initialize(items, n = nil)
  @items = items
  @goto = {}
  @n = n
  @closure = nil
  @_hash = nil
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



191
192
193
# File 'lib/depager/lr.rb', line 191

def items
  @items
end

#nObject

Returns the value of attribute n.



191
192
193
# File 'lib/depager/lr.rb', line 191

def n
  @n
end

Class Method Details

.[](items = []) ⇒ Object



220
221
222
# File 'lib/depager/lr.rb', line 220

def self.[](items = [])
  State.new items
end

Instance Method Details

#closureObject



224
225
226
227
228
229
230
231
232
# File 'lib/depager/lr.rb', line 224

def closure
  return @closure if @closure

  r = []
  @items.each do |i|
    r |= i.closure
  end
  @closure = r
end

#empty?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/depager/lr.rb', line 216

def empty?
  @items.empty?
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


207
208
209
# File 'lib/depager/lr.rb', line 207

def eql?(other)
  @items == other.items
end

#ggObject



234
235
236
# File 'lib/depager/lr.rb', line 234

def gg
  @goto
end

#goto(x) ⇒ Object



248
249
250
# File 'lib/depager/lr.rb', line 248

def goto(x)
  @goto[x] || State.new([])
end

#hashObject



201
202
203
204
205
# File 'lib/depager/lr.rb', line 201

def hash
  return @_hash if @_hash

  @_hash = @items.hash
end

#mkgotoObject



238
239
240
241
242
243
244
245
246
# File 'lib/depager/lr.rb', line 238

def mkgoto
  r = Hash.new { |h, k| h[k] = [] }
  closure.each do |c|
    r[c.dotsym].push LRItem[c.rule, c.n + 1] if c.n < c.rule.rhs.size
  end
  r.map do |k, v|
    [k, (@goto[k] = State.new(v.sort_by { |it| [it.rule.n, it.n] }))]
  end
end

#to_sObject



212
213
214
# File 'lib/depager/lr.rb', line 212

def to_s
  format("I%03i = \n", n) << @items.map(&:to_s).join("\n").lines.map { |i| "  #{i}" }.join
end