Class: Walrus::Grammar::StringEnumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/walrus/grammar/string_enumerator.rb

Overview

Unicode-aware (UTF-8) string enumerator. For Unicode support $KCODE must be set to ‘U’ (UTF-8).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ StringEnumerator

Returns a new instance of StringEnumerator.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
# File 'lib/walrus/grammar/string_enumerator.rb', line 28

def initialize(string)
  raise ArgumentError if string.nil?
  @scanner  = StringScanner.new(string)
  @current  = nil
  @last     = nil
end

Instance Attribute Details

#lastObject (readonly)

Returns the char most recently scanned before the last “next” call, or nil if nothing previously scanned.



26
27
28
# File 'lib/walrus/grammar/string_enumerator.rb', line 26

def last
  @last
end

Instance Method Details

#nextObject

This method will only work as expected if $KCODE is set to ‘U’ (UTF-8).



36
37
38
39
# File 'lib/walrus/grammar/string_enumerator.rb', line 36

def next
  @last     = @current
  @current  = @scanner.scan(/./m) # must use multiline mode or "." won't match newlines
end

#peekObject

Take a peek at the next character without actually consuming it. Returns nil if there is no next character. TODO: consider deleting this method as it’s not currently used.



43
44
45
46
47
48
# File 'lib/walrus/grammar/string_enumerator.rb', line 43

def peek
  if char = self.next
    @scanner.unscan
  end
  char
end