rubysolo / dentaku

math and logic formula parser and evaluator
MIT License
876 stars 161 forks source link

Question regarding "reserved" variables #55

Closed glanotte closed 9 years ago

glanotte commented 9 years ago

Hi, we are using Dentaku for calculating statistics for sports. One of our default statistics has the acronym or and that appears to be read by dentaku as the logical operation first. These are customer entered and we are trying out dentaku as a replacement, so we already have a good deal of data entered and backtracking to have reserved words would be challenging at best.

I am looking at the code to see if I can figure out a fix but I was curious if you might have thoughts or something that I am not seeing?

Thanks!

rubysolo commented 9 years ago

Do you have any formulas that use and or or to combine logical values? If not, my first thought would be to refactor the TokenScanner class so that you can customize the list of scanners. If you disabled the "combinator" scanner, then and and or should be tokenized as identifiers.

glanotte commented 9 years ago

no, we are pretty much only using basic arithmetic, that is a great idea I will get on that. Thanks for the suggestion!

rubysolo commented 9 years ago
[2] pry(main)> Dentaku::TokenScanner.scanners = Dentaku::TokenScanner.available_scanners - [:combinator]
=> [:whitespace, :numeric, :double_quoted_string, :single_quoted_string, :negate, :operator, :grouping, :comparator, :boolean, :function, :identifier]
[3] pry(main)> Dentaku("or + and", or: 1, and: 2)
=> 3

:tada:

muescha commented 9 years ago

what about (optional): 1) to allow variables only with a prefix $or or @or 2) only uppercase tokens are dentaku keywords

rubysolo commented 8 years ago

Since a5eb4d4bda16832259004600c57051251dd262de, you can add your own custom scanners, which would let you achieve these results. The test defines a custom scanner that looks for identifiers starting with #.