Class: ParseQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/parse_queue.rb,
lib/parse_queue/version.rb

Overview

The RCTP queue for parser token flow with backtracking.

Constant Summary collapse

DFB =

The default fetch block

Proc.new { false }
VERSION =
"0.2.5".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&fetch) ⇒ ParseQueue

Set up the parser queue.



17
18
19
20
21
# File 'lib/parse_queue.rb', line 17

def initialize(&fetch)
  @fetch  = fetch
  @buffer = []
  @offset = @position = 0
end

Instance Attribute Details

#positionObject

The current read point of the queue.



11
12
13
# File 'lib/parse_queue.rb', line 11

def position
  @position
end

Instance Method Details

#fetch_allObject

Fetch all possible items.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/parse_queue.rb', line 50

def fetch_all
  loop do
    item = @fetch.call

    unless item
      @fetch = DFB
      return
    end

    @buffer << item
  end
end

#fwd_countObject

How many unread items are in this parse queue?



24
25
26
# File 'lib/parse_queue.rb', line 24

def fwd_count
  index_limit - @position
end

#getObject

Get an item from the buffer.



34
35
36
37
38
39
40
# File 'lib/parse_queue.rb', line 34

def get
  @buffer << fetch_one if @position == index_limit

  result = @buffer[rev_count]
  @position += 1
  result
end

#get!Object

Get an item and shift the buffer.



43
44
45
46
47
# File 'lib/parse_queue.rb', line 43

def get!
  result = get
  shift
  result
end

#rev_countObject

How many already read items are still in this parse queue?



29
30
31
# File 'lib/parse_queue.rb', line 29

def rev_count
  @position - @offset
end

#shiftObject

Release any items before the current item.



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

def shift
  @buffer.shift(rev_count)
  @offset = @position
end

#try(&block) ⇒ Object

Try to process some items with roll back on failure.



82
83
84
85
86
# File 'lib/parse_queue.rb', line 82

def try(&block)
  save = @position
  self.position = save unless (result = block.call)
  result
end

#try!(&block) ⇒ Object

Process some items with a shift on success and a roll back on failure.



89
90
91
92
# File 'lib/parse_queue.rb', line 89

def try!(&block)
  shift if (result = try(&block))
  result
end

#unget(count = 1) ⇒ Object

Undo the last get.



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

def unget(count=1)
  @position -= count
  validate_position
end