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.
-
#peek ⇒ Object
Returns the next token without consuming it, or
nil
if there is no next token. -
#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.
9 10 11 12 |
# File 'lib/crass/token-scanner.rb', line 9 def initialize(tokens) @tokens = tokens.to_a reset end |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
7 8 9 |
# File 'lib/crass/token-scanner.rb', line 7 def current @current end |
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
7 8 9 |
# File 'lib/crass/token-scanner.rb', line 7 def pos @pos end |
#tokens ⇒ Object (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
#collect ⇒ Object
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 |
#consume ⇒ Object
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 |
#peek ⇒ Object
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 |
#reconsume ⇒ Object
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 |
#reset ⇒ Object
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 |