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

parser recovery for let statements #4925

Merged
merged 10 commits into from
Aug 15, 2023
Merged

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Aug 8, 2023

Description

This PR closes #4911.

It primarily does two things:

1 - Extend a lot of functions that return CompileError to return Vec<CompileError> allowing multiple errors to return;
2 - Implements the concept of parser recovery.

This happens when using the function guarded_parse_with_recovery, exemplified below. When the guard fails, this function return Ok(None); when it succeeds it returns Ok(item). The exciting part is when the parsing fails.

It returns an instance of Recoverer which contains a reference to the original parser before any tentative parser was done and the forked as left by the parsing function.

The idea is that it is the caller's responsibility to put the forked parser in a "good position". For that, it offers some helpers function and access to the forked parser.

To avoid boilerplate code one recovery strategy is already implemented recover_at_next_line_with_fallback_error which consumes everything at the forked parser's current line and emits an error if none were generated by the tentative parser.

 match parser.guarded_parse_with_recovery::<LetToken, StatementLet>() {
        Ok(None) => {}
        Ok(Some(item)) => return stmt(Statement::Let(item)),
        Err(r) => {
            let (spans, error) =
                r.recover_at_next_line_with_fallback_error(ParseErrorKind::InvalidStatement);
            return stmt(Statement::Error(spans, error));
        }
    }

With this, we have the LSP not "dying" when a strange error happens. The errors themselves are not brilliant, but they will be improved in other PRs.

image

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@xunilrj xunilrj force-pushed the xunilrj/parser-recovery-let branch from 4f2829c to cae82cc Compare August 11, 2023 10:04
@xunilrj xunilrj marked this pull request as ready for review August 11, 2023 14:11
@xunilrj xunilrj requested a review from a team August 11, 2023 14:12
@xunilrj xunilrj self-assigned this Aug 14, 2023
Copy link
Contributor

@IGI-111 IGI-111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the base structure is sound but the way the error handling is propagated needs to be rewritten, you shouldn't ever need to clear errors and warnings.

sway-core/src/lib.rs Outdated Show resolved Hide resolved
sway-error/src/handler.rs Outdated Show resolved Hide resolved
sway-error/src/handler.rs Outdated Show resolved Hide resolved
sway-parse/src/parser.rs Outdated Show resolved Hide resolved
@xunilrj xunilrj requested review from IGI-111 and a team August 15, 2023 08:27
@IGI-111 IGI-111 requested a review from a team August 15, 2023 13:27
@xunilrj xunilrj enabled auto-merge (squash) August 15, 2023 13:36
@xunilrj xunilrj disabled auto-merge August 15, 2023 15:25
@xunilrj xunilrj enabled auto-merge (squash) August 15, 2023 15:26
@xunilrj xunilrj disabled auto-merge August 15, 2023 15:32
@xunilrj xunilrj force-pushed the xunilrj/parser-recovery-let branch from be892bb to 247aa43 Compare August 15, 2023 19:14
@xunilrj xunilrj merged commit be08640 into master Aug 15, 2023
30 checks passed
@xunilrj xunilrj deleted the xunilrj/parser-recovery-let branch August 15, 2023 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

parser recovery: let statement without an = sign fails to return an AST
3 participants