Emit LexError for dedent to incorrect level (#7638)

This commit is contained in:
Micha Reiser 2023-09-25 12:45:44 +02:00 committed by GitHub
parent 10e35e38d7
commit 8ce138760a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 126 additions and 13 deletions

View file

@ -90,13 +90,33 @@ pub(super) struct Indentations {
}
impl Indentations {
pub(super) fn push(&mut self, indent: Indentation) {
pub(super) fn indent(&mut self, indent: Indentation) {
debug_assert_eq!(self.current().try_compare(indent), Ok(Ordering::Less));
self.stack.push(indent);
}
pub(super) fn pop(&mut self) -> Option<Indentation> {
/// Dedent one level to eventually reach `new_indentation`.
///
/// Returns `Err` if the `new_indentation` is greater than the new current indentation level.
pub(super) fn dedent_one(
&mut self,
new_indentation: Indentation,
) -> Result<Option<Indentation>, UnexpectedIndentation> {
let previous = self.dedent();
match new_indentation.try_compare(*self.current())? {
Ordering::Less | Ordering::Equal => Ok(previous),
// ```python
// if True:
// pass
// pass <- The indentation is greater than the expected indent of 0.
// ```
Ordering::Greater => Err(UnexpectedIndentation),
}
}
pub(super) fn dedent(&mut self) -> Option<Indentation> {
self.stack.pop()
}