Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Consider adding fail-fast ERROR nodes to better guide Tree-sitter error recovery #43

Open
novusnota opened this issue Nov 8, 2024 · 0 comments
Labels
tree-sitter Updates related to upstream Tree-sitter, tree-sitter-cli or bindings

Comments

@novusnota
Copy link
Member

novusnota commented Nov 8, 2024

See: tree-sitter-grammars/tree-sitter-readline@e436eae#diff-919ac210accac9ecc55a76d10a7590e3d85ca3f0e165b52d30f08faee486d0cb

The code at the link above adds the following node alias:

const ERROR_ALIAS = ($) =>
  alias(token(prec(-1, ANYTHING_STARTING_NON_WHITESPACE)), $.ERROR);

And then it's used in appropriate choice() rules (aka | in EBNF notation) to fail fast and introduce error nodes earlier, thus improving the resulting CST. That said, this approach doesn't (yet) create actual ERROR nodes that Tree-sitter produces on errors, but merely aliases the nodes to $.ERROR. In practice, that's close enough.

P.S.:

// Some other useful regexes
const WHITE_SPACE = /[ \t\f\v]/;
const ANYTHING = /[^\r\n]+/;
const ANYTHING_STARTING_NON_WHITESPACE = /\S[^\r\n]*/;
@novusnota novusnota added the tree-sitter Updates related to upstream Tree-sitter, tree-sitter-cli or bindings label Nov 8, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
tree-sitter Updates related to upstream Tree-sitter, tree-sitter-cli or bindings
Projects
None yet
Development

No branches or pull requests

1 participant