Skip to content

Commit

Permalink
fix: remove empty error blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Jul 10, 2024
1 parent 0396aec commit 1dc2cc6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
37 changes: 34 additions & 3 deletions parser-ng/src/parser/cst/syntax_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,27 @@ impl SyntaxStream {
}
}

/// Similar to [`SyntaxStream::end`], but the kind of the closed block is
/// changed to [`SyntaxKind::ERROR`].
///
/// Also, if the block being closed is empty, (i.e: the `Begin` event is
/// immediately followed by the `End` event) the block is completely
/// removed from the stream.
///
/// # Panics
///
/// * If no matching `Begin` exists for this `End`.
pub(crate) fn end_with_error(&mut self) {
match self.last_open_begin() {
Some((pos, _)) => {
let node = self.events.get_mut(pos).unwrap();
*node = Event::Begin(SyntaxKind::ERROR);
if pos + 1 == self.events.len() {
self.events.pop_back();
} else {
let node = self.events.get_mut(pos).unwrap();
*node = Event::Begin(SyntaxKind::ERROR);
self.events.push_back(Event::End(SyntaxKind::ERROR));
}
self.open_begins.pop_back().unwrap();
self.events.push_back(Event::End(SyntaxKind::ERROR));
}
None => {
panic!("`End` without a corresponding `Begin`")
Expand Down Expand Up @@ -194,6 +208,7 @@ pub(crate) struct Bookmark(usize);
mod tests {
use super::SyntaxKind;
use super::SyntaxStream;
use crate::cst::Event;
use crate::Span;

#[test]
Expand Down Expand Up @@ -231,6 +246,22 @@ mod tests {
s.push_token(SyntaxKind::COLON, Span(0..1));
s.end_with_error();
assert_eq!(s.last_open_begin(), Some((0, SyntaxKind::RULE_DECL)));

let mut s = SyntaxStream::new();
s.begin(SyntaxKind::ERROR);
s.push_token(SyntaxKind::COLON, Span(0..1));
s.end_with_error();
assert_eq!(s.pop(), Some(Event::Begin(SyntaxKind::ERROR)));
assert_eq!(
s.pop(),
Some(Event::Token { kind: SyntaxKind::COLON, span: Span(0..1) })
);
assert_eq!(s.pop(), Some(Event::End(SyntaxKind::ERROR)));

let mut s = SyntaxStream::new();
s.begin(SyntaxKind::RULE_DECL);
s.end_with_error();
assert_eq!(s.pop(), None);
}

#[test]
Expand Down
2 changes: 0 additions & 2 deletions parser-ng/src/parser/tests/testdata/for-error-1.out
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ [email protected]
[email protected] ":"
[email protected] "\n"
[email protected] "\t\t"
[email protected]
[email protected]
[email protected]
[email protected] "for"
[email protected] " "
Expand Down
2 changes: 0 additions & 2 deletions parser-ng/src/parser/tests/testdata/of-error-1.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ [email protected]
[email protected] ":"
[email protected] "\n"
[email protected] "\t\t"
[email protected]
[email protected]
[email protected]
[email protected] "any"
[email protected] " "
Expand Down

0 comments on commit 1dc2cc6

Please sign in to comment.