Sha256: 278a84aa6a92dcc421aac76bc2cdcfefd1a0ae259f114a5cb3d6d9d256364d94

Contents?: true

Size: 938 Bytes

Versions: 4

Compression:

Stored size: 938 Bytes

Contents

# encoding: utf-8

module Crass

  # Like {Scanner}, but for tokens!
  class TokenScanner
    attr_reader :current, :pos, :tokens

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

    # Executes the given block, collects all tokens that are consumed during its
    # execution, and returns them.
    def collect
      start = @pos
      yield
      @tokens[start...@pos] || []
    end

    # Consumes the next token and returns it, advancing the pointer.
    def consume
      @current = @tokens[@pos]
      @pos += 1 if @current
      @current
    end

    # 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
    def reconsume
      @pos -= 1 if @pos > 0
    end

    # Resets the pointer to the first token in the list.
    def reset
      @current = nil
      @pos     = 0
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
crass-0.2.1 lib/crass/token-scanner.rb
crass-0.2.0 lib/crass/token-scanner.rb
crass-0.1.0 lib/crass/token-scanner.rb
crass-0.0.2 lib/crass/token-scanner.rb