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.



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

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

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



5
6
7
# File 'lib/crass/token-scanner.rb', line 5

def current
  @current
end

#posObject (readonly)

Returns the value of attribute pos.



5
6
7
# File 'lib/crass/token-scanner.rb', line 5

def pos
  @pos
end

#tokensObject (readonly)

Returns the value of attribute tokens.



5
6
7
# File 'lib/crass/token-scanner.rb', line 5

def tokens
  @tokens
end

Instance Method Details

#collectObject

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



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

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

#consumeObject

Consumes the next token and returns it, advancing the pointer.



21
22
23
24
25
# File 'lib/crass/token-scanner.rb', line 21

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

#reconsumeObject

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

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



30
31
32
# File 'lib/crass/token-scanner.rb', line 30

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

#resetObject

Resets the pointer to the first token in the list.



35
36
37
38
# File 'lib/crass/token-scanner.rb', line 35

def reset
  @current = nil
  @pos     = 0
end