class ParseQueue
The RCTP queue for parser token flow with backtracking.
Constants
- DFB
The default fetch block
- VERSION
Attributes
position[R]
The current read point of the queue.
Public Class Methods
new(&fetch)
click to toggle source
Set up the parser queue.
# File lib/parse_queue.rb, line 17 def initialize(&fetch) @fetch = fetch @buffer = [] @offset = @position = 0 end
Public Instance Methods
fetch_all()
click to toggle source
Fetch all possible items.
# 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_count()
click to toggle source
How many unread items are in this parse queue?
# File lib/parse_queue.rb, line 24 def fwd_count index_limit - @position end
get()
click to toggle source
Get an item from the buffer.
# 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!()
click to toggle source
Get an item and shift the buffer.
# File lib/parse_queue.rb, line 43 def get! result = get shift result end
position=(value)
click to toggle source
Set the position
# File lib/parse_queue.rb, line 64 def position=(value) @position = value validate_position end
rev_count()
click to toggle source
How many already read items are still in this parse queue?
# File lib/parse_queue.rb, line 29 def rev_count @position - @offset end
shift()
click to toggle source
Release any items before the current item.
# File lib/parse_queue.rb, line 76 def shift @buffer.shift(rev_count) @offset = @position end
try(&block)
click to toggle source
Try to process some items with roll back on failure.
# File lib/parse_queue.rb, line 82 def try(&block) save = @position self.position = save unless (result = block.call) result end
try!(&block)
click to toggle source
Process some items with a shift on success and a roll back on failure.
# File lib/parse_queue.rb, line 89 def try!(&block) shift if (result = try(&block)) result end
unget(count=1)
click to toggle source
Undo the last get.
# File lib/parse_queue.rb, line 70 def unget(count=1) @position -= count validate_position end
Private Instance Methods
fetch_one()
click to toggle source
Fetch a single item.
# File lib/parse_queue.rb, line 108 def fetch_one item = @fetch.call unless item @fetch = DFB fail ParseQueueNoFwd end item end
index_limit()
click to toggle source
The first index past the end of the array
# File lib/parse_queue.rb, line 103 def index_limit @buffer.length + @offset end
validate_position()
click to toggle source
Is this a valid position?
# File lib/parse_queue.rb, line 97 def validate_position fail ParseQueueNoRev if @position < @offset fail ParseQueueNoFwd if @position >= index_limit end