mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Add information about node's original location in the parsed string

Open serso opened this issue 2 months ago • 6 comments

Prior to this commit it was not possible to trace back the parsed node location in the original string. This information could be used to highlight the syntax of a math expression displayed in a rich editor (e.g. a spreadsheet or a calculator).

This commit is based on the PR #2796 that has been abandoned for 2 years. I rebased the task branch and fixed the issues reported in the original PR.

In the nutshell, each parsed node stores an array of sources (SourceMapping[]) that are set during parsing (see tokenSource and its usages for details). The node's constructor and clone method are adjusted to take an optional MetaOptions object containing the source mappings. In the future MetaOptions could be extended to store more information.

Benchmarks showed no change in evaluate. parse became slower, from 3.68µs to 5.17µs with the changes from this commit.

Closes #2795

serso avatar Oct 17 '25 13:10 serso

@josdejong any chance you or someone else can look into this PR? Feedback is highly appreciated 🙏

serso avatar Oct 22 '25 13:10 serso

Hi Sergey, thanks a lot for picking this up! I think I don't have enough time today to review this (large) PR, I hope to look into it coming Friday.

josdejong avatar Oct 22 '25 14:10 josdejong

Does this also supersede #2615 ?

gwhitney avatar Oct 28 '25 08:10 gwhitney

Does this also supersede #2615 ?

Ah, yes. I'll close that PR in favor of this one. Thanks for pointing this out.

josdejong avatar Oct 29 '25 10:10 josdejong

@serso I just merged #3547 adding support for optional chaining. This causes a few merge conflicts with this PR. Please let me know if you need help resolving them.

josdejong avatar Oct 29 '25 10:10 josdejong

@serso can you have a look into the merge conflicts? Please let me know if you need help.

josdejong avatar Nov 05 '25 11:11 josdejong