grammar Formula

include MathMy

rule top
  "=" meat:(cond  / range / math_exp_full / formula / cell / num)
end

rule primary
  num / cell / formula
end

rule cell
  "-"? "$"? c:[A-Z] "$"? r:[0-9]+ <Extract::Tree::Cell>
end

rule range
  a:cell ":" b:cell <Extract::Tree::Range>
end

rule cell_or_range
  range / cell
end

rule num
  "-"? [0-9]+ ("." [0-9]+)? <Extract::Tree::Num>
end

rule formula
  formula_name "(" formula_args ")" <Extract::Tree::Formula>
end 

rule formula_name
  "THING" / "DOUBLE" / "SUM" / "IF" / "MAX" / "VLOOKUP" / "SQRT" / "COMBIN"
end

rule formula_arg
  cond / math_exp_full / formula / range / cell / num / string
end

rule formula_args
  formula_arg rest:("," fa:formula_arg)* <Extract::Tree::FormulaArgs>
end

rule cond_exp
  a:(num / cell / string / formula / math_exp_full) op:(">=" / "<=" / "=" / ">" / "<") b:(num / cell / string / formula / math_exp_full) <Extract::Tree::CondExp>
end

rule string
  "\"" [^"]+ "\"" <Extract::Tree::String>
end 

rule cond_const
  ("TRUE" / "FALSE") {
    def excel_value
      (text_value == "TRUE") ? true : false
    end
    def deps
      []
    end
  }
end

rule cond
  cond_exp / cond_const
end

end