class CachingFormulaParser

Constants

BLANK
FALSE
TRUE

Attributes

functions_used[RW]

Public Class Methods

map(*args) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 67
def self.map(*args)
  instance.map(*args)
end
new() click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 71
def initialize
  @number_cache = {}
  @string_cache = {}
  @percentage_cache = {}
  @error_cache = {}
  @operator_cache = {}
  @comparator_cache = {}
  @sheet_reference_cache = {}
  @functions_used = {}
  @treat_external_references_as_local = false
end
parse(*args) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 63
def self.parse(*args)
  instance.parse(*args)
end

Public Instance Methods

area(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 150
def area(ast)
  ast[1] = ast[1].to_sym
  ast[2] = ast[2].to_sym
  ast
end
blank(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 192
def blank(ast)
  BLANK
end
boolean_false(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 183
def boolean_false(ast)
  FALSE
end
boolean_true(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 179
def boolean_true(ast)
  TRUE
end
cell(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 145
def cell(ast)
  ast[1] = ast[1].to_sym
  ast
end
comparator(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 201
def comparator(ast)
  ast[1] = ast[1].to_sym
  @comparator_cache[ast] ||= ast
end
error(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 187
def error(ast)
  ast[1] = ast[1].to_sym
  @error_cache[ast] ||= ast
end
external_reference(ast) click to toggle source

We can't deal with external references at the moment

# File src/rewrite/caching_formula_parser.rb, line 121
def external_reference(ast)
  raise ExternalReferenceException.new(ast, @full_ast, @text) unless @treat_external_references_as_local
  # We sometimes treat the external reference as local in order to move on
  return ast[2]
end
map(ast) click to toggle source

FIXME: THe function bit in here isn't DRY or consistent

# File src/rewrite/caching_formula_parser.rb, line 97
def map(ast)
  return ast unless ast.is_a?(Array)
  if ast[0] == :function
    ast[1] = ast[1].to_sym
    @functions_used[ast[1]] = true
  end
  if respond_to?(ast[0])
    ast = send(ast[0], ast)
  else
    ast.each.with_index do |a,i|
      next unless a.is_a?(Array)

      if a[0] == :function
        a[1] = a[1].to_sym
        @functions_used[a[1]] = true
      end

      ast[i] = map(a)
    end
  end
  ast
end
number(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 156
def number(ast)
  ast[1] = ast[1].to_f
  @number_cache[ast] ||= ast
end
operator(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 196
def operator(ast)
  ast[1] = ast[1].to_sym
  @operator_cache[ast] ||= ast
end
parse(text, treat_external_references_as_local = false) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 83
def parse(text, treat_external_references_as_local = false)
  @treat_external_references_as_local = treat_external_references_as_local
  ast = Formula.parse(text)
  @text = text # Kept in case of Exception below
  if ast
    @full_ast = ast.to_ast[1] # Kept in case of Exception below
    map(ast.to_ast[1])
  else
    raise ParseFailedException.new(text)
    nil
  end
end
percentage(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 161
def percentage(ast)
  if ast[1].is_a?(Array)
    ast.replace([:arithmetic, map(ast[1]), operator([:operator, :'/']), number([:number, 100])])
  else
    ast[1] = ast[1].to_f / 100.0
    ast[0] = :number
  end
  @percentage_cache[ast] ||= ast
end
sheet_reference(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 127
def sheet_reference(ast)
  # Sheet names shouldn't start with [1], because those are
  # external references
  if ast[1] =~ /^\[\d+\]/
     raise ExternalReferenceException.new(ast, @full_ast, @text) unless @treat_external_references_as_local
     ast[1] = ast[1].gsub(/^\[\d+\]/,'')
  end
  ast[1] = ast[1].to_sym
  ast[2] = map(ast[2])
  # We do this to deal with Control!#REF! style rerferences
  # that occasionally pop up in named references
  if ast[2].first == :error
    return ast[2]
  else
    @sheet_reference_cache[ast] ||= ast
  end
end
string(ast) click to toggle source
# File src/rewrite/caching_formula_parser.rb, line 171
def string(ast)
  return @string_cache[ast] ||= ast
end