Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tokenizer (or Expression) should fail on nonsensical input #26

Closed
AlexeyMK opened this issue Sep 10, 2014 · 5 comments
Closed

Tokenizer (or Expression) should fail on nonsensical input #26

AlexeyMK opened this issue Sep 10, 2014 · 5 comments

Comments

@AlexeyMK
Copy link
Contributor

One example of an expression that should fail is 5 4 - two values (or identifiers) not connected with an operator of any sort. This seems like a good cause for a stricter grammar / lexer.

@rubysolo
Copy link
Owner

This would fail at evaluation:

calculator.evaluate!('10 7')
RuntimeError: no rule matched {{10 7}}

Do you feel that it should fail when attempting to construct the expression?

@AlexeyMK
Copy link
Contributor Author

Yeah - ideally this should be a compile-time error and not a run-time error. This is particularly important in my case, where non-technical users write formulas that only get run days later when the formulas are run. It is substantially less painful for debugging if I can tell them there's an error when they write it than when the formula gets run.

My current work-around is to calculate dependencies, mark all the identifiers with the value 0, and run the formula to see if it passes.

@lawso017
Copy link

+1 for this suggestion, or would it be reasonable to expose a parse!() method that could be called to validate a formula?

@rikkipitt
Copy link
Contributor

I was looking for this functionality too, specifically to help validate expressions saved in a model. I came up with this custom validator:

https://gist.github.com/rikkipitt/4551c9b72b35cd7a4756

Please try it out and extend if your case requires!

Rikki

@rubysolo
Copy link
Owner

rubysolo commented Aug 7, 2015

This is fixed in v2.0 -- calculator.ast(expression) will return an AST node or raise a parse error.

@rubysolo rubysolo closed this as completed Aug 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants