Skip to content
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

Malformed addition/subtraction don't get parsed as errors #28

Open
bengolds opened this issue Feb 10, 2022 · 3 comments
Open

Malformed addition/subtraction don't get parsed as errors #28

bengolds opened this issue Feb 10, 2022 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@bengolds
Copy link

If you're missing an argument, addition and subtraction parse inconsistently compared to multiplication and division.

parse('x * ') => [ 'Multiply', 'x', 'Missing' ]
parse('x + ') => 'x'                                       // should be [ 'Add', 'x', 'Missing' ]

parse('x / ') => [ 'Divide', 'x', 'Missing' ]
parse('x - ') => 'x'                                       // should be [ 'Subtract', 'x', 'Missing' ]

I'd be curious to know a bit more about the error-handling philosophy for parse. MathLive throws math-error events when it can't parse the latex (including the above four cases). Compute Engine seems to not throw any errors at all in parse. Is that intentional?

@arnog
Copy link
Member

arnog commented Feb 10, 2022

Well, a bit of consistency to start would be nice, wouldn't it :)

There are actually two LaTeX parsers involved. The one in MathLive core, and the other in the Compute Engine.

The MathLive math-error event fires when a parsing error is encountered:

  • unbalanced braces e.g. \frac{ or \frac{}}
  • unknown commands, e.g. \foo
  • unknown environments, e.g. \begin{foo}\end{foo}
  • unexpected delimiters, e.g. \left*\right*
  • a few other conditions

They typically represent a syntax error in the input Latex. Despite those errors being signaled, attempt to recover and continue is made.

When using the Compute Engine parse() function, similar warnings are indicated by activating a onError handler. They are much less common than in MathLive, however (I think the only one that may be triggered is if a command is used inside a string, i.e. \begin{\alpha}. That's invalid in LaTeX, but a bit of a corner case.

However, while the MathLive parser is purely syntactic, the Compute Engine parser applies some semantic knowledge about what is being parsed. For example, \times is an operator that expect a left-hand-side and a right-hand-side, \frac is a command that should be followed by two LaTeX arguments indicating a numerator and denominator, etc...

So, when an "error" is encountered, the Compute Engine parser first tries to "fill in the blanks", in particular adding Missing symbols when expected. It will also return an ["Error"...] function in the place of an argument, typically when there is a syntactic parsing error. This allows the preservation of whatever portion of the LaTeX string that could not be parsed (while the MathLive parser would discard whatever it could not make sense of).

So, in both cases, best attempt is made to interpret the input. In the case of MathLive, the result is biased towards getting something that the editor will be able to display, with the intent that the user could correct whatever problem there may be, and to use a side channel (the 'math-error' event) to notify the client that something not quite right happened. In the case of the Compute Engine, since the idea is that the caller is an API that will do further processing, as much information as possible is provided in the resulting Expression, so that it can be further processed and interpreted by the calling software.

That said, the Add function should behave like Multiply. As to why it doesn't... Well, the + symbol is peculiar. It can be used in a many different contexts. It can be a unary operator, or an infix operator, or part of a postfix operator (x++). So, when parsing the + symbol, I was conservatively failing, forcing the parser to backtrack and try another path. However, I think it's safe to assume that even in the case of the + symbol, if a rhs is missing, no other definition will succeed, and it can succeed with a Missing symbol, just like *.

@arnog arnog self-assigned this Feb 10, 2022
@arnog arnog added the bug Something isn't working label Feb 10, 2022
@arnog
Copy link
Member

arnog commented Feb 10, 2022

I have a pending fix for this.

@strickinato
Copy link

An 0.4.3 update (parsing with compute engine):

> c.parse('x * ').json
[ 'Error', 'x', "'syntax-error'", [ 'LatexForm', "'* '" ] ]

> c.parse('x + ').json
[ 'Error', 'x', "'syntax-error'", [ 'LatexForm', "'+ '" ] ]

> c.parse('x / ').json
[ 'Divide', 'x', 'Missing' ]

> c.parse('x - ').json
[ 'Error', 'x', "'syntax-error'", [ 'LatexForm', "'- '" ] ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

3 participants