Class: Rubycli::Arguments::TokenStream

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycli/arguments/token_stream.rb

Overview

Lightweight mutable cursor over CLI tokens.

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ TokenStream

Returns a new instance of TokenStream.



7
8
9
10
# File 'lib/rubycli/arguments/token_stream.rb', line 7

def initialize(tokens)
  @tokens = Array(tokens).dup
  @index = 0
end

Instance Method Details

#advance(count = 1) ⇒ Object



20
21
22
# File 'lib/rubycli/arguments/token_stream.rb', line 20

def advance(count = 1)
  @index += count
end

#consumeObject



24
25
26
27
28
# File 'lib/rubycli/arguments/token_stream.rb', line 24

def consume
  value = current
  advance
  value
end

#consume_remainingObject



30
31
32
33
34
# File 'lib/rubycli/arguments/token_stream.rb', line 30

def consume_remaining
  remaining = @tokens[@index..] || []
  @index = @tokens.length
  remaining
end

#currentObject



12
13
14
# File 'lib/rubycli/arguments/token_stream.rb', line 12

def current
  @tokens[@index]
end

#finished?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rubycli/arguments/token_stream.rb', line 36

def finished?
  @index >= @tokens.length
end

#peek(offset = 1) ⇒ Object



16
17
18
# File 'lib/rubycli/arguments/token_stream.rb', line 16

def peek(offset = 1)
  @tokens[@index + offset]
end