josdejong/mathjs

Parser fails on "0x"

Open

#2 374 ouverte le 5 janv. 2022

Voir sur GitHub
 (4 commentaires) (1 réaction) (0 assignés)JavaScript (1 298 forks)batch import
featurehelp wanted

Métriques du dépôt

Stars
 (13 832 stars)
Métriques de merge PR
 (Aucune PR mergée en 30 j)

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 :-)

Guide contributeur