Class: Walrus::Grammar::StringEnumerator
- Inherits:
-
Object
- Object
- Walrus::Grammar::StringEnumerator
- 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
-
#last ⇒ Object
readonly
Returns the char most recently scanned before the last “next” call, or nil if nothing previously scanned.
Instance Method Summary collapse
-
#initialize(string) ⇒ StringEnumerator
constructor
A new instance of StringEnumerator.
-
#next ⇒ Object
This method will only work as expected if $KCODE is set to ‘U’ (UTF-8).
-
#peek ⇒ Object
Take a peek at the next character without actually consuming it.
Constructor Details
#initialize(string) ⇒ StringEnumerator
Returns a new instance of StringEnumerator.
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
#last ⇒ Object (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
#next ⇒ Object
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 |
#peek ⇒ Object
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 |