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

Recover from an error and continue parsing (for language server) #2476

Open
wkillerud opened this issue Jan 4, 2025 · 0 comments
Open

Recover from an error and continue parsing (for language server) #2476

wkillerud opened this issue Jan 4, 2025 · 0 comments

Comments

@wkillerud
Copy link

wkillerud commented Jan 4, 2025

Hello, hope you're all well 👋

Opening this issue as I'm not sure how to proceed with sass/dart-sass-language-server#34

There doesn't seem to be a way to get a Stylesheet from Stylesheet.parseScss() etc. if the parser finds an error. I've searched through the docs of sass_api and skimmed the source code for the StylesheetParser. I couldn't find options to, say, treat errors as warnings or to continue parsing after an error.

Take this SCSS document for example, which I'm using to test completions in the language server:

.a {
  display: /* [1] */ ;
}

/* [1] Here the parser throws (Error: Expected expression).
   Since we don't have a Stylesheet we can't tell what the context/AstNode is at this position.
   We would like to know that we're in a Declaration with the name "display"
   so we can provide completion items for the allowed values for the display property.

   We could probably get _something_ by looking at the raw string, but strings have their own
   limitations and problems (how to handle scope for Sass variables for instance). */

The language server needs the parser to still tracks errors (for diagnostics, to give user feedback on errors), but instead of throwing it should try to proceed to a point where it can reasonably recover (if possible), and continue from there. This way the parser always returns some kind of Stylesheet so the language server can give as much help as possible, even in a "broken" document.

For some prior art, the SCSS/CSS parser that ships with VS Code has this approach of resync/recovery tokens. As part of marking an error the parser lists tokens that the scanner should proceed to before trying to parse more nodes.
https://github.com/microsoft/vscode-css-languageservice/blob/9e3bfe4698a4a2888fc134eb7579f99c449c9d02/src/parser/cssParser.ts#L483

Their parser does not recover from all errors of course, but at least the Stylesheet has enough information to give the completions feature the context it needs for correct suggestions. In the example above the scanner would move to the ; on L2 C22 and continue.

I realise retrofitting such a mode to the parser is quite a bit of work. Unfortunately I don't think the language server can get to a shippable state without it. For completions in particular users expect context-aware suggestions in documents where the parser throws today.

What do you think? Would this be doable at some point? Any workarounds or sass_api features I've missed?

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

No branches or pull requests

1 participant