Skip to main content

bynk_check/
recovery.rs

1//! #1663: diagnosing a file the strict parser rejects, for the CLI's
2//! single-file check. The editor's `bynk_ide::diagnose` follows the same rules
3//! (Decisions A and B) on its own recovering parse.
4
5use bynk_syntax::CompileError;
6use bynk_syntax::ast::SourceUnit;
7use bynk_syntax::lexer::Token;
8use bynk_syntax::parser;
9
10use crate::{checker, resolver};
11
12/// #1663: the diagnostics for a file the strict parser rejects with `strict` —
13/// that error, every other syntax error from a recovering parse, then the
14/// resolver's and checker's findings in the declarations that did parse. References to a declaration recovery had to
15/// skip are known names, not unknown ones (Decision B), and the checker's
16/// diagnostics in a declaration the resolver rejected are its echoes (Decision
17/// A); both are left out.
18pub fn diagnose_unparsable(
19    tokens: &[Token],
20    source: &str,
21    strict: Vec<CompileError>,
22) -> Vec<CompileError> {
23    diagnose_unlexable(tokens, source, Vec::new(), strict)
24}
25
26/// [`diagnose_unparsable`] for a file the lexer recovered from (#2807): `tokens`
27/// are the recovering lex's, `lex_errors` its errors. They come first in the
28/// result, and the syntax errors they caused are left out.
29pub fn diagnose_unlexable(
30    tokens: &[Token],
31    source: &str,
32    lex_errors: Vec<CompileError>,
33    strict: Vec<CompileError>,
34) -> Vec<CompileError> {
35    let parser::Recovered {
36        units,
37        errors,
38        broken_decl_names,
39    } = parser::parse_units_recovering(tokens, source);
40    let (recovered, _warnings) = bynk_syntax::partition_by_severity(errors);
41    // #2807: lexical errors left gaps the parser tripped over.
42    let recovered = bynk_syntax::lexer::without_lex_echoes(&lex_errors, tokens, recovered);
43    let mut strict = strict;
44    strict.splice(0..0, lex_errors);
45    let mut out = parser::merge_syntax_errors(strict, recovered);
46    if let Some(SourceUnit::Commons(commons)) = units.into_iter().next() {
47        let item_spans: Vec<_> = commons.items.iter().map(|i| i.span()).collect();
48        let (resolved, resolve_errors) = resolver::resolve_recovering(commons);
49        let (shown, _hidden) =
50            resolver::split_broken_decl_echoes(resolve_errors.clone(), &broken_decl_names);
51        out.extend(shown);
52        if let Err(checked) = checker::check(resolved) {
53            let (checked, _warnings) = bynk_syntax::partition_by_severity(checked);
54            // #1710: the checker reports some echoes of a skipped declaration
55            // under its own codes (a method, a capability, an actor).
56            let checked = resolver::without_resolve_echoes(checked, &resolve_errors, &item_spans);
57            out.extend(resolver::split_broken_decl_echoes(checked, &broken_decl_names).0);
58        }
59    }
60    out
61}