josdejong/mathjs

Parser fails on "0x"

Open

#2,374 建立於 2022年1月5日

在 GitHub 查看
 (4 留言) (1 反應) (0 負責人)JavaScript (1,298 fork)batch import
featurehelp wanted

倉庫指標

Star
 (13,832 star)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

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

貢獻者指南