Class: CssColorContrast::CommandInterpreter::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/css_color_contrast/command_interpreter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, env = {}) ⇒ Parser

Returns a new instance of Parser.



131
132
133
134
135
136
# File 'lib/css_color_contrast/command_interpreter.rb', line 131

def initialize(line, env = {})
  @scanner = StringScanner.new(line)
  @env = env
  @tokens = []
  @node_tree = []
end

Instance Attribute Details

#node_treeObject (readonly)

Returns the value of attribute node_tree.



129
130
131
# File 'lib/css_color_contrast/command_interpreter.rb', line 129

def node_tree
  @node_tree
end

#tokensObject (readonly)

Returns the value of attribute tokens.



129
130
131
# File 'lib/css_color_contrast/command_interpreter.rb', line 129

def tokens
  @tokens
end

Class Method Details

.parse!(line, env = {}) ⇒ Object



125
126
127
# File 'lib/css_color_contrast/command_interpreter.rb', line 125

def self.parse!(line, env = {})
  new(line, env).parse!.root_node
end

Instance Method Details

#ignore_spaceObject



180
181
182
# File 'lib/css_color_contrast/command_interpreter.rb', line 180

def ignore_space
  @scanner.scan(TokenRe::SPACE)
end

#parse!Object



184
185
186
187
188
189
# File 'lib/css_color_contrast/command_interpreter.rb', line 184

def parse!
  ignore_space
  read_label
  read_separator
  self
end

#read_color_function(cur_pos) ⇒ Object



160
161
162
163
164
165
# File 'lib/css_color_contrast/command_interpreter.rb', line 160

def read_color_function(cur_pos)
  @scanner.pos = cur_pos
  color = Color.as_color(@scanner.scan_until(/\)/))
  source = @scanner.string[cur_pos..(@scanner.pos)]
  Value.assign(source, @env, color)
end

#read_labelObject



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/css_color_contrast/command_interpreter.rb', line 148

def read_label
  cur_pos = @scanner.pos
  label = @scanner.scan(TokenRe::LABEL)
  return unless label

  if TokenRe::COLOR_SCHEME.match?(label)
    @tokens.push read_color_function(cur_pos)
  else
    @tokens.push label
  end
end

#read_separatorObject



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/css_color_contrast/command_interpreter.rb', line 167

def read_separator
  if @scanner.scan(TokenRe::FUNC_HEAD)
    @node_tree.push(Function.create(@tokens.pop, @env))
  elsif @scanner.scan(TokenRe::SPACE) || @scanner.eos?
    token = @tokens.pop
    value = token.is_a?(Value) ? token : Value.assign(token, @env)
    current_node.push(value)
  end

  read_label
  read_separator unless @tokens.empty?
end

#root_nodeObject



138
139
140
# File 'lib/css_color_contrast/command_interpreter.rb', line 138

def root_node
  @node_tree.first
end