PaesslerAG / gval

Expression evaluation in golang
BSD 3-Clause "New" or "Revised" License
713 stars 83 forks source link

Parsing errors are not user-friendly #72

Open preciselytom opened 2 years ago

preciselytom commented 2 years ago

Our app allows end users (not developers) to write mathematical expressions. We would like to present errors in a way that the users will understand. Unfortunately, the parser errors are very low-level and not related to the mathematical syntax.

Examples of actual and desired output:

Input:
Actual error:  unexpected EOF while scanning extensions
Desired error: input is empty

Input:         (
Actual error:  unexpected EOF while scanning extensions
Desired error: missing right parenthesis

Input:         )
Actual error:  unexpected ")" while scanning extensions
Desired error: missing left parenthesis

Input:         ()
Actual error:  unexpected ")" while scanning extensions
Desired error: no expression inside parentheses

Input:         1+
Actual error:  unexpected EOF while scanning extensions
Desired error: missing expression to the right of "+"

Input:         +1
Actual error:  unexpected "+" while scanning extensions
Desired error: missing expression to the left of "+"
generikvault commented 2 years ago

Yes better errors would be great. It's pretty hard to implement them inside a framework as generell as gval.

If you have a solution thats general enough to work with the generic approach, feel free to make a pull request

Maybe it's easier to make a post check if an error occurs. Run a few RegExp and replace the error message if it matchs the expression?