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

[Bug] Wrong line number reporting of (red) Syntax error in code editor #354

Open
hoppy53 opened this issue May 29, 2021 · 4 comments
Open
Labels
bug Something isn't working software: pybricks-code Issues with https://code.pybricks.com application software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: MicroPython Issues related to the MicroPython programming language

Comments

@hoppy53
Copy link

hoppy53 commented May 29, 2021

Symptom:
if there is a syntax error caused in the middle of a line, the editor reports the correct line number where the error occurred. see attachment: code_editor_syntax_error_within_line.png
Here, as an example in line 10, the left parenthesis in Motor Port.A) is missing
Line number is correctly shown.

if there is a syntax error caused by the last character of a line, the editor reports instead the error being in the next line (line+1). see attachment: code_editor_syntax_error_end_of_line.png
Here, as an example in line 10, the right parenthesis in Motor (Port.A is missing.
Line number is not correct. It shows 11 instead of 10

It goes for all types of instructions as long as it is the last character of the line.
Also, it applies to both the webpage editor and the chrome app install.

code_editor_syntax_error_end_of_line
code_editor_syntax_error_within_line

Environment:
Linux Mint 20.1 Ulyssa
Chrome Version 91.0.4472.77

@hoppy53 hoppy53 added the triage Issues that have not been triaged yet label May 29, 2021
@dlech
Copy link
Member

dlech commented May 29, 2021

In Python and MicroPython, it is allowable to continue on the next line after an ( in the code. So the compiler doesn't realize that ) is missing until something else unexpected is reached in the code.

As a side note, I actually read somewhere recently that CPython got a compiler upgrade that will actually report the error on the line with the ( as expected. But since we are using MicroPython, I don't expect it to get the same fix. In MicroPython, often keeping the firmware size to a minimum is more important than niceties like this.

@dlech dlech added blocked: upstream This issue is with a 3rd-party project and is out of scope of Pybricks bug Something isn't working software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: MicroPython Issues related to the MicroPython programming language software: pybricks-code Issues with https://code.pybricks.com application and removed triage Issues that have not been triaged yet blocked: upstream This issue is with a 3rd-party project and is out of scope of Pybricks labels May 29, 2021
@dlech
Copy link
Member

dlech commented May 29, 2021

Although, we may be able to add other checkers to Pybricks code in the future that perform additional analysis. The trick is being able to run Python in the web browser or find JavaScript/TypeScript tools for analyzing Python code.

@davecparker
Copy link

Although, we may be able to add other checkers to Pybricks code in the future that perform additional analysis.

Yes, and here are two more ideas for you to kick around:

  1. As an alternative to finding web-ready analysis code, you might also look for a free server-based solution you could use. I wouldn't be surprised if some university project did one, perhaps even using AI. Or maybe you could even put a Python 3.10 parser (has improved syntax errors) on your own server and call it yourself.

  2. Consider analyzing some simple situations yourself. I have done similar things with Java. For example, if the parser tells you the syntax error is on line n, try re-parsing with line n replaced by "pass" and again with "(True)". If both of these also fail at line n at the first column (hopefully the parser tells you the column internally), then pretty high confidence that the error is actually on line n-1. So... if the first token of line n-1 is "if", "elif", "else", "while", "for", or "def" and the last token of line n-1 is not ":" then guess what :) Not 100% confidence, so word the error as "missing colon?" , but it almost always will be, and if you're wrong the user is not in much worse of a situation than they were before.

@davecparker
Copy link

  1. Consider analyzing some simple situations yourself.

I should add that in the case of missing : you can test the hypothesis by adding a : and re-parsing and see if it passes. I have done this to great success with missing ; against a weak Java parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working software: pybricks-code Issues with https://code.pybricks.com application software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: MicroPython Issues related to the MicroPython programming language
Projects
None yet
Development

No branches or pull requests

3 participants