Class: Dommy::Internal::CSS::CalcParser

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/internal/css/calc.rb

Overview

Recursive-descent evaluator over calc_tokenize's output. Values are [kind, number] where kind is :length (px) or :number. Operators follow CSS Values 4 calc unit algebra: +/- need matching kinds, * needs a number operand, / needs a number divisor.

Instance Method Summary collapse

Constructor Details

#initialize(tokens, ctx) ⇒ CalcParser

Returns a new instance of CalcParser.



98
99
100
101
102
# File 'lib/dommy/internal/css/calc.rb', line 98

def initialize(tokens, ctx)
  @tokens = tokens
  @ctx = ctx
  @pos = 0
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


104
# File 'lib/dommy/internal/css/calc.rb', line 104

def done? = @pos >= @tokens.length

#parse_valueObject

= | ( ) | [+-] |



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dommy/internal/css/calc.rb', line 107

def parse_value
  token = peek
  if token.is_a?(Hash) && token[:fn]
    parse_function
  elsif token == "("
    advance
    value = parse_sum
    expect(")")
    value
  elsif token == "+" || token == "-"
    advance
    kind, number = parse_value
    [kind, token == "-" ? -number : number]
  elsif token.is_a?(Hash) && token[:num]
    advance
    dimension(token)
  else
    raise PropertyRegistry::CalcUnresolvable
  end
end