Class: YTools::Path::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/ytools/path/lexer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Lexer

Returns a new instance of Lexer.



23
24
25
26
27
# File 'lib/ytools/path/lexer.rb', line 23

def initialize(path)
  @path = path
  @offset = 0
  @buffer = []
end

Instance Attribute Details

#offsetObject (readonly)

Returns the value of attribute offset.



21
22
23
# File 'lib/ytools/path/lexer.rb', line 21

def offset
  @offset
end

#pathObject (readonly)

Returns the value of attribute path.



21
22
23
# File 'lib/ytools/path/lexer.rb', line 21

def path
  @path
end

Instance Method Details

#[](index) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ytools/path/lexer.rb', line 29

def [](index)
  if offset + index >= path.length
    nil
  else
    @path[offset + index]
  end
end

#has_next?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ytools/path/lexer.rb', line 57

def has_next?
  !peek.nil?
end

#nextObject



37
38
39
40
41
42
43
# File 'lib/ytools/path/lexer.rb', line 37

def next
  if @buffer.length > 0
     @buffer.pop
  else
    token
  end
end

#peek(count = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ytools/path/lexer.rb', line 45

def peek(count=nil)
  count ||= 0

  if count >= @buffer.length
    (@buffer.length - count).downto(0) do
      @buffer.push(token)
    end
  end

  @buffer[count]
end