compute-engine
compute-engine copied to clipboard
Parsing certain expressions causes uncaught exceptions
It seems that parsing certain expressions causes the uncaught exception the name "..." cannot be used as a symbol name. The two examples I've found are:
\\frac{x}{} \\text{ cm}
-- Fails with "The name " cm" cannot be used as a symbol name.
\\frac{x}{ } \\text{ cm}
-- Succeeds
\\frac{x}{} \\text{cm}
-- Succeeds
and
\\sqrt(x)
-- Fails with "The name "(" cannot be used as a symbol name"
-- This should probably be \\sqrt{x}, but I'm not sure that crashing is ideal behavior here.
As a broader question, it seems like there are two mechanisms for throwing errors from parse:
- Throwing an exception
- Returning MathJson representing the error
["Error", ....]
Is there a meaningful difference between these?
Hmmm... that's an interesting case.
Yes, there is a logic behind the two mechanisms, but the current behavior doesn't seem right.
You get an Error expression when the parsing could not succeed, i.e. there was some syntax error in the LaTeX expression.
You get an exception if the result of the parsing could not be turned into a valid MathJSON expression. This should happen rarely, if at all.
In your examples, the parser is successfully (although erroneously) parsing the LaTeX and thinks it has recognized a symbol " cm", which, it turns out, is not a valid symbol name in MathJSON (symbols can't start with a space).
Now, that parsing is incorrect, since " cm" is not a symbol in this context, it should be interpreted as a string. Furthermore, the parser should probably handle "invalid" symbol names provided in the LaTeX, either by converting them to something legal, or issuing an Error expression.
The error reporting mechanism has been revamped in 0.7.0.
No errors are thrown during parsing, or when boxing or putting an expression in canonical form an expression.
["Error"] expressions are returned in the expression to indicate where a problem occurred.
While parsing only detect LaTeX syntax errors, using expr.canonical will validate that the number and domain of the arguments is correct.
To check if an expression is valid use expr.isValid. To get the list of errors in an expression, use expr.errors.
\frac{x}{} \text{ cm}
-> ["Sequence", ["Divide", "x", ["Error", "'missing'"]], "' cm'"]
\sqrt(x)
-> [
"Sequence",
[
"Sqrt",
["Error", "'expected-closing-delimiter'", ["Latex", "''"]]
],
[
"Error",
[
"ErrorCode",
"'unexpected-token'",
[
"Error",
[
"ErrorCode",
"'incompatible-domain'",
["Domain", ["Maybe", ["Sequence", "Anything"]]]
],
"'('"
]
],
["Latex", "'(}x)'"]
]
]