mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:42:10 +00:00
Emit LexError
for dedent to incorrect level (#7638)
This commit is contained in:
parent
10e35e38d7
commit
8ce138760a
5 changed files with 126 additions and 13 deletions
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue