Module: CSS::TokenCursor

Included in:
MediaQueries::Parser, Selectors::AnBParser::Impl, Selectors::Parser
Defined in:
lib/css/token_cursor.rb

Overview

Shared cursor used by the three parsers (‘Selectors::Parser`, `Selectors::AnBParser::Impl`, `MediaQueries::Parser`). Each one walks an array of items with the same primitives — tokens for the selector parsers, mixed Token / SimpleBlock / Function items for the media- query parser. Predicates against the item’s ‘.type` go through `peek_token`, which collapses non-Token items to EOF safely.

Constant Summary collapse

EOF_TOKEN =
Token.new(:eof).freeze

Instance Method Summary collapse

Instance Method Details

#consumeObject



28
29
30
31
32
# File 'lib/css/token_cursor.rb', line 28

def consume
  item = @items[@pos] || EOF_TOKEN
  @pos += 1
  item
end

#eof?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/css/token_cursor.rb', line 40

def eof?
  @pos >= @items.length
end

#init_cursor(items) ⇒ Object



11
12
13
14
# File 'lib/css/token_cursor.rb', line 11

def init_cursor(items)
  @items = items
  @pos   = 0
end

#parse_error!(message) ⇒ Object

Raises:



44
45
46
47
# File 'lib/css/token_cursor.rb', line 44

def parse_error!(message)
  pos = peek.respond_to?(:position) ? peek.position : nil
  raise ParseError.new(message, position: pos)
end

#peek(offset = 0) ⇒ Object



16
17
18
# File 'lib/css/token_cursor.rb', line 16

def peek(offset = 0)
  @items[@pos + offset] || EOF_TOKEN
end

#peek_tokenObject

Returns peek unwrapped to a Token; non-Token items collapse to EOF. Lets media-query code do ‘.type == :colon` against streams that may also hold SimpleBlock / Function items.



23
24
25
26
# File 'lib/css/token_cursor.rb', line 23

def peek_token
  item = peek
  item.is_a?(Token) ? item : EOF_TOKEN
end

#skip_whitespaceObject



34
35
36
37
38
# File 'lib/css/token_cursor.rb', line 34

def skip_whitespace
  while (item = peek).is_a?(Token) && item.type == :whitespace
    @pos += 1
  end
end