Class: Coppertone::MacroString::MacroParser

Inherits:
Object
  • Object
show all
Defined in:
lib/coppertone/macro_string/macro_parser.rb

Overview

A internal class that parses the macro string template into an object that can later be evaluated (or ‘expanded’) in the context of a particular SPF check.

Constant Summary collapse

SIMPLE_MACRO_LETTERS =
%w[% _ -].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ MacroParser

Returns a new instance of MacroParser.



12
13
14
15
16
# File 'lib/coppertone/macro_string/macro_parser.rb', line 12

def initialize(s)
  @s = s.dup
  @macros = []
  parse_macro_array
end

Instance Attribute Details

#macrosObject (readonly)

Returns the value of attribute macros.



11
12
13
# File 'lib/coppertone/macro_string/macro_parser.rb', line 11

def macros
  @macros
end

Instance Method Details

#parse_contextual_interpolated_macroObject



32
33
34
35
36
37
38
39
# File 'lib/coppertone/macro_string/macro_parser.rb', line 32

def parse_contextual_interpolated_macro
  raise MacroStringParsingError unless @s[1] == '{'
  closing_index = @s.index('}')
  raise MacroStringParsingError unless closing_index
  interpolated_body = @s[2, closing_index - 2]
  @macros << MacroExpand.new(interpolated_body)
  @s = @s[(closing_index + 1)..-1]
end

#parse_interpolated_macroObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/coppertone/macro_string/macro_parser.rb', line 42

def parse_interpolated_macro
  raise MacroStringParsingError if @s.length == 1
  macro_code = @s[0, 2]
  if MacroStaticExpand.exists_for?(macro_code)
    @macros << MacroStaticExpand.macro_for(macro_code)
    @s = @s[2..-1]
  else
    parse_contextual_interpolated_macro
  end
end

#parse_macro_arrayObject



18
19
20
21
22
23
24
25
26
# File 'lib/coppertone/macro_string/macro_parser.rb', line 18

def parse_macro_array
  while @s && !@s.empty?
    if starting_macro?
      parse_interpolated_macro
    else
      parse_macro_literal
    end
  end
end

#parse_macro_literalObject



53
54
55
56
57
58
59
# File 'lib/coppertone/macro_string/macro_parser.rb', line 53

def parse_macro_literal
  new_idx = @s.index('%')
  new_idx ||= @s.length
  @macros << MacroLiteral.new(@s[0, new_idx])
  @s = @s[new_idx..-1]
  new_idx
end

#starting_macro?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/coppertone/macro_string/macro_parser.rb', line 28

def starting_macro?
  @s && @s.length >= 1 && (@s[0] == '%')
end