-
Notifications
You must be signed in to change notification settings - Fork 260
Add diagnostics for bad semicolons errors, closes #352 #362
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
Add diagnostics for bad semicolons errors, closes #352 #362
Conversation
Thank you. I can confirm that the usefulness of the diagnostics have increased by pinpointing the offending tokens. The |
9f05d96
to
d41fc77
Compare
I have removed |
ab25cd1
to
cacd826
Compare
cacd826
to
d806563
Compare
@hsutter all requested changes applied. |
d806563
to
8674999
Compare
This change introduce diagnostics when there is a semicolon at the end of the function body: ```cpp g: () = {}; ``` Generates: ``` error: a compound function body shall not end with a semicolon (at ';') ``` And ```cpp g: () = 42;; ``` Generates: ``` error: a short function syntax shall end with a single semicolon (at ';') ```
This change introduce diagnostics when there is a semicolon at the end of the for..do loop body: ```cpp for v* | all next log() do :(e) = { foo(); bar*; }; ``` generates: ``` error: for..do loop body shall not end with a semicolon (at ';') ```
This change introduce diagnostics when there is a semicolon at the end of the while loop body: ```cpp i := 0; while i* < container.size() next i++ { std::cout << container[i] << std::endl; }; ``` generates: ``` error: while loop body shall not end with a semicolon (at ';') ```
This change introduce diagnostics when there is a double semicolon at the end of the short function body for unnamed function: ```cpp l2 := :() -> int = 24;; ``` Generates: ``` error: a short function syntax shall end with a single semicolon (at ';') ```
This change introduce diagnostics when there is a semicolon without statement ```cpp ; ``` Generates: ``` error: empty statement is not allowed - remove extra semicolon (at ';') ``` That also handles cases when there is double semicolon after a proper statemnt: ```cpp i := 0;; ``` Generates: ``` error: empty statement is not allowed - remove extra semicolon (at ';') ```
When done() is true last correct token and its position is printed.
8674999
to
6881f5b
Compare
@@ -4707,6 +4722,11 @@ class parser | |||
) | |||
-> std::unique_ptr<statement_node> | |||
{ | |||
if (!done() && curr().type() == lexeme::Semicolon) { | |||
error("empty statement is not allowed - remove extra semicolon"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this works, but just a note: Emitting an error here (not just returning null) could interfere with productions where statement is one alternative but where if it doesn't parse as a statement we would want to try other fallback production(s) instead. I don't think that actually happens today in the grammar though, so this should be okay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, the scenario would be "is there any production where a statement could appear, where if it's not a statement something else that starts with a ;
could appear instead" -- I think there isn't today so it should be safe to reject a ;
if we're in a position where a statement could appear
…r#362) * Add diagnostics for semicolon at the end of function This change introduce diagnostics when there is a semicolon at the end of the function body: ```cpp g: () = {}; ``` Generates: ``` error: a compound function body shall not end with a semicolon (at ';') ``` And ```cpp g: () = 42;; ``` Generates: ``` error: a short function syntax shall end with a single semicolon (at ';') ``` * Add diagnostics for semicolon at the end of for loop This change introduce diagnostics when there is a semicolon at the end of the for..do loop body: ```cpp for v* | all next log() do :(e) = { foo(); bar*; }; ``` generates: ``` error: for..do loop body shall not end with a semicolon (at ';') ``` * Add diagnostics for semicolon at the end of while body This change introduce diagnostics when there is a semicolon at the end of the while loop body: ```cpp i := 0; while i* < container.size() next i++ { std::cout << container[i] << std::endl; }; ``` generates: ``` error: while loop body shall not end with a semicolon (at ';') ``` * Add diagnostics for double semicolon at short lambda This change introduce diagnostics when there is a double semicolon at the end of the short function body for unnamed function: ```cpp l2 := :() -> int = 24;; ``` Generates: ``` error: a short function syntax shall end with a single semicolon (at ';') ``` * Add diagnostics for empty statements (extra semicolon) This change introduce diagnostics when there is a semicolon without statement ```cpp ; ``` Generates: ``` error: empty statement is not allowed - remove extra semicolon (at ';') ``` That also handles cases when there is double semicolon after a proper statemnt: ```cpp i := 0;; ``` Generates: ``` error: empty statement is not allowed - remove extra semicolon (at ';') ``` * Fix error() method that throws when done() is true When done() is true last correct token and its position is printed.
The current cppfront (f83ca9) implementation provides unclear error messages - it is hard to find what causes the error.
This change introduces diagnostics for:
All regression tests pass. Closes #352