Class: LLIP::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/llip/buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scanner) ⇒ Buffer

Returns a new instance of Buffer.



8
9
10
11
12
# File 'lib/llip/buffer.rb', line 8

def initialize(scanner)
  @scanner = scanner
  @current = nil
  @buffer = nil
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



6
7
8
# File 'lib/llip/buffer.rb', line 6

def current
  @current
end

#scannerObject

Returns the value of attribute scanner.



5
6
7
# File 'lib/llip/buffer.rb', line 5

def scanner
  @scanner
end

Instance Method Details

#lookahead(n) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/llip/buffer.rb', line 27

def lookahead(n)
  @buffer ||= []
  while @buffer.size < n	
    @buffer << @scanner.next
  end
  @buffer[n-1]
end

#nextObject



19
20
21
22
23
24
25
# File 'lib/llip/buffer.rb', line 19

def next
  return @current = @scanner.next unless @buffer
  
  @current = @buffer.shift
  @buffer = nil if @buffer.size == 0
  @current
end

#scan(text) ⇒ Object



14
15
16
17
# File 'lib/llip/buffer.rb', line 14

def scan(text)
  @scanner.scan(text)
  self
end