Parser fails on "0x"
- Dominant language
- JavaScript
- Stars
- 15.1k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
I'm attempting to port the google/mathsteps library to the current MathJS release. Along the way I've encountered an error I've seen elsewhere in my own code. It's easy to trigger, just do `math.parse('0x')` and you'll get
```
Uncaught SyntaxError: String "0x" is no valid number
```
It seems the parser sees this as a degenerate form of a hex literal and thus rejects it. This feels unnecessarily restrictive as 0x is a frequent thing to represent in algebra. It's come up in my current project, and also in this mathsteps port. From the behavior of the mathsteps tests, it's also clear that this was previously supported in older versions of MathJS.
Fortunately, "0x" is unambiguously _not_ a hex literal as it's not followed by any numbers. I'd suggest special-casing "0x" to interpret it as `ConstantNode` 0 implicitly multiplied by `SymbolNode` x, or at least allowing such a behavior to be configured in the `math.parser` parser constructor. Or better than special-casing, allowing 0x to fall through to the same parsing that would apply to 1x or 2x, should there be no hex literal.
I'd be willing to implement but would like your thoughts on whether this would be appropriate as the default, or as an opt-in option, or not at all :-)
Contributor guide
Assessment
This issue has not been assessed yet.