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

Issues when variables have special characters #715

Closed
shivambarsaley opened this Issue Sep 21, 2016 · 7 comments

Comments

Projects
None yet
4 participants
@shivambarsaley

shivambarsaley commented Sep 21, 2016

We have an application using mathJS to perform evaluation. We are facing issues when operands start having special characters (like $, :, ; or ^).

For Instance
let math = require('mathjs');
math.eval('a * 10',{"a":1}) //This would successfully give a value 10

But if we do
math.eval('$a * 10',{"$a":1}) // This gives an error "SyntaxError: Syntax error in part "$a * 10" (char 1)"

Is there something we are missing out on?

Adding to it, We were able to get around the issue by specifying the expression as '"$a" * 10', And later during parsing and compiling, we convert $a to a symbol node.

Though the workaround worked, But it doesn't seem as the best way to solve the problem, and it now stops us from doing string based operations in an expression, Something like
math.eval('"a"=="s"',{a:"s"})

@josdejong

This comment has been minimized.

Owner

josdejong commented Sep 21, 2016

the allowed characters for variables are indeed limited. Which characters would you exactly like to use in variable names? The characters :, ; and ^ are not allowed since they are operators. The $ character isn't (yet) used as operator and isn't allowed in variable names either. We could consider allowing this character for variable names like in many programming languages.

@FSMaxB

This comment has been minimized.

Collaborator

FSMaxB commented Sep 22, 2016

Actually I don't know any programming language that allows $ in their variable names. At least I don't think there's a major one.

PHP and Perl use the dollar sign, but only at the beginning of a variable name literal, not as part of the actual name.

I don't think it is a good idea to start adding random characters to the list of supported characters in variable names, especially if they might be useful as an operator some day (not sure about that for $ though).

@FSMaxB

This comment has been minimized.

Collaborator

FSMaxB commented Sep 22, 2016

Maybe it might make sense to introduce some way to escape special characters in variable names in order to allow access to all kinds of keys in objects. I'm not sure about that though.

@josdejong

This comment has been minimized.

Owner

josdejong commented Sep 25, 2016

I don't think it is a good idea to start adding random characters to the list of supported characters in variable names, especially if they might be useful as an operator some day (not sure about that for $ though).

Agree. The dollar sign $ is sort of a special case here. I think I would like to reserve it for a future operator.

As for allowing all sort of unicode chars (we've already expanded before), we can see whether we can simply follow similar rules as JavaScript or Python here:

https://mathiasbynens.be/notes/javascript-identifiers
http://stackoverflow.com/questions/19482730/allowed-characters-in-python-function-names

@ThomasBrierley

This comment has been minimized.

Contributor

ThomasBrierley commented Sep 28, 2016

I had the same issue for my use case, using the "$" character to give symbols special properties (outside of the scope of mathjs). I ended up pre-parsing without a proper AST to find symbols and remove the characters before feeding it into mathjs. Preferably i'd do it the other way around to get the full benefit of mathjs's parser and then filter out symbol nodes.

One way to support arbitrary characters that may not make sense as default would be to expose the parser's valid symbol unicode block so that it could be extended. I guess that could be tricky if it was not written with that kind of flexibility in mind... e.g dealing with cases where operator and symbol characters overlap.

@josdejong

This comment has been minimized.

Owner

josdejong commented Oct 2, 2016

Thanks for your input Thomas. Exposing the function which checks whether a character is valid makes sense, gives you the freedom to finetune when needed. It's up to you then to make sure this doesn't conflict with characters used for operators (well, we could write a check on load which tests whether the function returns false for each of the operators as a safety measure).

I've been thinking about allowing the $ character or not. I can't really come up with future use cases for which to reserve this character. It's also an allowed character in many programming languages. So it maybe a good idea after all to just allow it in mathjs too.

@shivambarsaley

This comment has been minimized.

shivambarsaley commented Oct 21, 2016

Thanks for the help guys.
We finally resorted to storing the expressions without quotes, and just before parsing them we append quotes to variables having special characters.

Also @josdejong +1 to exposing the checker function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment