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

Better parsing errors: indicate position with a caret #33735

Open
timholy opened this issue Nov 1, 2019 · 1 comment
Open

Better parsing errors: indicate position with a caret #33735

timholy opened this issue Nov 1, 2019 · 1 comment
Labels
domain:error messages Better, more actionable error messages parser Language parsing and surface syntax

Comments

@timholy
Copy link
Sponsor Member

timholy commented Nov 1, 2019

I have some new or newish programmers and am trying to pay attention to their roadblocks in learning Julia, so I may occasionally file issues which don't look like my usual fare.

Suppose you try to define a variable using a name that starts with a number:

julia> 2x = 22
ERROR: syntax: "2" is not a valid function argument name

A new programmer might wonder, why does this talk about a "function argument name"? I haven't called any functions (or so I thought).

Python is not great at this, but the differences are interesting:

>>> 2x = 22
  File "<stdin>", line 1
    2x = 22
     ^
SyntaxError: invalid syntax
>>>

"invalid syntax" isn't very helpful, but python partly makes up for this with the caret syntax showing the specific place in the line that causes the trouble. (In this case, disambiguating which 2 is the problematic one.)

C (gcc) is much better, though still lacks the key hint that variable names should not start with a number:

#include <stdio.h>

int main(void) {
    int 2x = 22;
    return 0;
}
$ gcc c.c
c.c: In function ‘main’:
c.c:4:9: error: invalid suffix "x" on integer constant
     int 2x = 22;
         ^~
c.c:4:9: error: expected identifier or ‘(’ before numeric constant

Matlab isn't perfect either, but it goes out of its way to be helpful:

>> 2x = 22
 2x = 22Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or
other syntax error. To construct matrices, use brackets instead of parentheses.
 
Did you mean:
>> x = 22

(To clarify, that x = 22 is queued up on the REPL.)

To me, the biggest takeaway is that most other languages have decided that a caret mark is a crucial component of the parser's error-reporting.

@timholy timholy added parser Language parsing and surface syntax domain:error messages Better, more actionable error messages labels Nov 1, 2019
@Keno
Copy link
Member

Keno commented Nov 1, 2019

Completely agreeed. I think it's the biggest usability problem we have with the language right now. https://github.com/JuliaLang/FancyDiagnostics.jl fixes this but has bitrotted. There is an ongoing effort to bring it into base, but it has stalled out a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:error messages Better, more actionable error messages parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

2 participants