Class: Caps::Parser

Inherits:
Object
  • Object
show all
Includes:
Consumers, Entrypoints
Defined in:
lib/caps/parser.rb,
lib/caps/parser/infra.rb,
lib/caps/parser/helpers.rb,
lib/caps/parser/consumers.rb,
lib/caps/parser/entrypoints.rb

Defined Under Namespace

Modules: Consumers, Entrypoints, Helpers

Instance Method Summary collapse

Methods included from Entrypoints

#parse_comma_separated_component_values, #parse_component_value, #parse_component_value_list, #parse_declaration, #parse_declaration_list, #parse_full_sheet, #parse_rule, #parse_rule_list, #parse_style_block_contents, #parse_stylesheet

Methods included from Consumers

#consume_at_rule, #consume_component_value, #consume_declaration, #consume_declaration_list, #consume_function, #consume_list_of_rules, #consume_qualified_rule, #consume_simple_block, #consume_style_block_contents, #contains_important_annotation?, #mirror_variant_for, #remove_important_annotation, #resync_invalid_declaration

Constructor Details

#initialize(tokens) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
# File 'lib/caps/parser/infra.rb', line 9

def initialize(tokens)
  @tokens = tokens
  @idx = 0
end

Instance Method Details

#advanceObject



26
27
28
29
30
# File 'lib/caps/parser/infra.rb', line 26

def advance
  return nil if peek.nil?

  peek.tap { @idx += 1 }
end

#consume_whitespaceObject



32
33
34
# File 'lib/caps/parser/infra.rb', line 32

def consume_whitespace
  advance while peek.whitespace?
end

#peekObject



14
15
16
17
18
# File 'lib/caps/parser/infra.rb', line 14

def peek
  return nil if @idx >= @tokens.length

  @tokens[@idx]
end

#prevObject



20
21
22
23
24
# File 'lib/caps/parser/infra.rb', line 20

def prev
  return @tokens.first if @idx.zero?

  @tokens[@idx - 1]
end

#syntax_error!(reason) ⇒ Object



48
49
50
51
52
# File 'lib/caps/parser/infra.rb', line 48

def syntax_error!(reason)
  token = peek.nil? ? prev : peek
  raise "Syntax Error: #{reason} at line #{token.end_line} " \
        "column #{token.end_column}"
end

#trackingObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/caps/parser/infra.rb', line 36

def tracking
  start = peek.dig(:position, :start)
  result = yield
  finish = prev.dig(:position, :end)
  result.merge({
    position: {
      start:,
      end: finish
    }
  })
end