Class: Crass::TokenScanner
- Inherits:
-
Object
- Object
- Crass::TokenScanner
- Defined in:
- lib/crass/token-scanner.rb
Overview
Like Scanner, but for tokens!
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
-
#collect ⇒ Object
Executes the given block, collects all tokens that are consumed during its execution, and returns them.
-
#consume ⇒ Object
Consumes the next token and returns it, advancing the pointer.
-
#initialize(tokens) ⇒ TokenScanner
constructor
A new instance of TokenScanner.
-
#reconsume ⇒ Object
Reconsumes the current token, moving the pointer back one position.
-
#reset ⇒ Object
Resets the pointer to the first token in the list.
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
#current ⇒ Object (readonly)
Returns the value of attribute current.
5 6 7 |
# File 'lib/crass/token-scanner.rb', line 5 def current @current end |
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
5 6 7 |
# File 'lib/crass/token-scanner.rb', line 5 def pos @pos end |
#tokens ⇒ Object (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
#collect ⇒ Object
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 |
#consume ⇒ Object
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 |
#reconsume ⇒ Object
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 |
#reset ⇒ Object
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 |