Skip to content

Commit

Permalink
dedupe expected tokens in ParseError
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik committed Aug 10, 2024
1 parent 34e8c4b commit ed1cd9e
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ impl ParserResult {

let skipped = input.content(skipped_range.utf8());

input.emit(ParseError {
text_range: skipped_range,
terminals_that_would_have_allowed_more_progress: expected_terminals.clone(),
});
input.emit(ParseError::new(skipped_range, expected_terminals.clone()));

ParserResult::SkippedUntil(SkippedUntil {
nodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ impl SeparatedHelper {
kind,
input.content(skipped_range.utf8()),
)));
input.emit(ParseError {
text_range: skipped_range,
terminals_that_would_have_allowed_more_progress: incomplete
.expected_terminals,
});
input.emit(ParseError::new(
skipped_range,
incomplete.expected_terminals,
));

match lexer.parse_terminal_with_trivia::<LexCtx>(input, separator) {
ParserResult::Match(r#match) => {
Expand Down
35 changes: 18 additions & 17 deletions crates/codegen/runtime/cargo/src/runtime/parse_error/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::BTreeSet;
use std::fmt;

use crate::diagnostic::{self, Diagnostic};
Expand All @@ -10,8 +9,8 @@ use crate::text_index::TextRange;
/// This could have been caused by a syntax error, or by reaching the end of the file when more tokens were expected.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ParseError {
pub(crate) text_range: TextRange,
pub(crate) terminals_that_would_have_allowed_more_progress: Vec<TerminalKind>,
text_range: TextRange,
terminals_that_would_have_allowed_more_progress: Vec<TerminalKind>,
}

impl ParseError {
Expand All @@ -29,8 +28,11 @@ impl ParseError {
impl ParseError {
pub(crate) fn new(
text_range: TextRange,
terminals_that_would_have_allowed_more_progress: Vec<TerminalKind>,
mut terminals_that_would_have_allowed_more_progress: Vec<TerminalKind>,
) -> Self {
terminals_that_would_have_allowed_more_progress.sort_unstable();
terminals_that_would_have_allowed_more_progress.dedup();

Self {
text_range,
terminals_that_would_have_allowed_more_progress,
Expand All @@ -44,23 +46,22 @@ impl fmt::Display for ParseError {
.terminals_that_would_have_allowed_more_progress
.is_empty()
{
write!(f, "Expected end of file.")
} else {
let deduped = self
.terminals_that_would_have_allowed_more_progress
.iter()
.collect::<BTreeSet<_>>();
return write!(f, "Expected end of file.");
}

write!(f, "Expected ")?;
let mut expected = self.terminals_that_would_have_allowed_more_progress.iter();

for kind in deduped.iter().take(deduped.len() - 1) {
write!(f, "{kind} or ")?;
}
let last = deduped.last().expect("we just checked that it's not empty");
write!(f, "{last}.")?;
let first = expected
.next()
.expect("we just checked that it's not empty");

Ok(())
write!(f, "Expected {first}")?;

for kind in expected {
write!(f, " or {kind}")?;
}

write!(f, ".")
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

Loading

0 comments on commit ed1cd9e

Please sign in to comment.