Class: Crass::TokenScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/crass/token-scanner.rb

Overview

Like Scanner, but for tokens!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ TokenScanner

Returns a new instance of TokenScanner.



9
10
11
12
# File 'lib/crass/token-scanner.rb', line 9

def initialize(tokens)
  @tokens = tokens.to_a
  reset
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



7
8
9
# File 'lib/crass/token-scanner.rb', line 7

def current
  @current
end

#posObject (readonly)

Returns the value of attribute pos.



7
8
9
# File 'lib/crass/token-scanner.rb', line 7

def pos
  @pos
end

#tokensObject (readonly)

Returns the value of attribute tokens.



7
8
9
# File 'lib/crass/token-scanner.rb', line 7

def tokens
  @tokens
end

Instance Method Details

#collectObject

Executes the given block, collects all tokens that are consumed during its execution, and returns them.



16
17
18
19
20
# File 'lib/crass/token-scanner.rb', line 16

def collect
  start = @pos
  yield
  @tokens[start...@pos] || []
end

#consumeObject

Consumes the next token and returns it, advancing the pointer. Returns nil if there is no next token.



24
25
26
27
28
# File 'lib/crass/token-scanner.rb', line 24

def consume
  @current = @tokens[@pos]
  @pos += 1 if @current
  @current
end

#peekObject

Returns the next token without consuming it, or nil if there is no next token.



32
33
34
# File 'lib/crass/token-scanner.rb', line 32

def peek
  @tokens[@pos]
end

#reconsumeObject

Reconsumes the current token, moving the pointer back one position.

http://www.w3.org/TR/2013/WD-css-syntax-3-20130919/#reconsume-the-current-input-token



39
40
41
# File 'lib/crass/token-scanner.rb', line 39

def reconsume
  @pos -= 1 if @pos > 0
end

#resetObject

Resets the pointer to the first token in the list.



44
45
46
47
# File 'lib/crass/token-scanner.rb', line 44

def reset
  @current = nil
  @pos     = 0
end