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
Instance Method Summary collapse
- #consume ⇒ Object
- #eof? ⇒ Boolean
- #init_cursor(items) ⇒ Object
- #parse_error!(message) ⇒ Object
- #peek(offset = 0) ⇒ Object
-
#peek_token ⇒ Object
Returns peek unwrapped to a Token; non-Token items collapse to EOF.
- #skip_whitespace ⇒ Object
Instance Method Details
#consume ⇒ Object
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
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
44 45 46 47 |
# File 'lib/css/token_cursor.rb', line 44 def parse_error!() pos = peek.respond_to?(:position) ? peek.position : nil raise ParseError.new(, 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_token ⇒ Object
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_whitespace ⇒ Object
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 |