Sha256: 52e82960f84a404e01425d93a8833c94a3c0406455dae8db0712f97a08bec605

Contents?: true

Size: 1.09 KB

Versions: 43

Compression:

Stored size: 1.09 KB

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. Returns
    # `nil` if there is no next token.
    def consume
      @current = @tokens[@pos]
      @pos += 1 if @current
      @current
    end

    # Returns the next token without consuming it, or `nil` if there is no next
    # token.
    def peek
      @tokens[@pos]
    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

43 entries across 39 versions & 16 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/crass-1.0.6/lib/crass/token-scanner.rb
crass-1.0.6 lib/crass/token-scanner.rb