Skip to content

Commit 9592e94

Browse files
committed
Isolate determination of indentation into seperate method.
1 parent b8c7881 commit 9592e94

File tree

1 file changed

+59
-55
lines changed

1 file changed

+59
-55
lines changed

parser/src/lexer.rs

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,64 @@ where
649649
self.location.column = 1;
650650
}
651651

652+
/// Given we are at the start of a line, count the number of spaces and/or tabs until the first character.
653+
fn determine_indentation(&mut self) -> Result<IndentationLevel, LexicalError> {
654+
// Determine indentation:
655+
let mut spaces: usize = 0;
656+
let mut tabs: usize = 0;
657+
loop {
658+
match self.chr0 {
659+
Some(' ') => {
660+
/*
661+
if tabs != 0 {
662+
// Don't allow spaces after tabs as part of indentation.
663+
// This is technically stricter than python3 but spaces after
664+
// tabs is even more insane than mixing spaces and tabs.
665+
return Some(Err(LexicalError {
666+
error: LexicalErrorType::OtherError("Spaces not allowed as part of indentation after tabs".to_string()),
667+
location: self.get_pos(),
668+
}));
669+
}
670+
*/
671+
self.next_char();
672+
spaces += 1;
673+
}
674+
Some('\t') => {
675+
if spaces != 0 {
676+
// Don't allow tabs after spaces as part of indentation.
677+
// This is technically stricter than python3 but spaces before
678+
// tabs is even more insane than mixing spaces and tabs.
679+
return Err(LexicalError {
680+
error: LexicalErrorType::OtherError(
681+
"Tabs not allowed as part of indentation after spaces".to_string(),
682+
),
683+
location: self.get_pos(),
684+
});
685+
}
686+
self.next_char();
687+
tabs += 1;
688+
}
689+
Some('#') => {
690+
self.lex_comment();
691+
spaces = 0;
692+
tabs = 0;
693+
}
694+
Some('\n') => {
695+
// Empty line!
696+
self.next_char();
697+
self.new_line();
698+
spaces = 0;
699+
tabs = 0;
700+
}
701+
_ => {
702+
break;
703+
}
704+
}
705+
}
706+
707+
Ok(IndentationLevel { spaces, tabs })
708+
}
709+
652710
fn inner_next(&mut self) -> LexResult {
653711
if !self.pending.is_empty() {
654712
return self.pending.remove(0);
@@ -659,61 +717,7 @@ where
659717
if self.at_begin_of_line {
660718
self.at_begin_of_line = false;
661719

662-
// Determine indentation:
663-
let mut spaces: usize = 0;
664-
let mut tabs: usize = 0;
665-
loop {
666-
match self.chr0 {
667-
Some(' ') => {
668-
/*
669-
if tabs != 0 {
670-
// Don't allow spaces after tabs as part of indentation.
671-
// This is technically stricter than python3 but spaces after
672-
// tabs is even more insane than mixing spaces and tabs.
673-
return Some(Err(LexicalError {
674-
error: LexicalErrorType::OtherError("Spaces not allowed as part of indentation after tabs".to_string()),
675-
location: self.get_pos(),
676-
}));
677-
}
678-
*/
679-
self.next_char();
680-
spaces += 1;
681-
}
682-
Some('\t') => {
683-
if spaces != 0 {
684-
// Don't allow tabs after spaces as part of indentation.
685-
// This is technically stricter than python3 but spaces before
686-
// tabs is even more insane than mixing spaces and tabs.
687-
return Err(LexicalError {
688-
error: LexicalErrorType::OtherError(
689-
"Tabs not allowed as part of indentation after spaces"
690-
.to_string(),
691-
),
692-
location: self.get_pos(),
693-
});
694-
}
695-
self.next_char();
696-
tabs += 1;
697-
}
698-
Some('#') => {
699-
self.lex_comment();
700-
self.at_begin_of_line = true;
701-
continue 'top_loop;
702-
}
703-
Some('\n') => {
704-
// Empty line!
705-
self.next_char();
706-
self.at_begin_of_line = true;
707-
self.new_line();
708-
continue 'top_loop;
709-
}
710-
_ => {
711-
break;
712-
}
713-
}
714-
}
715-
716-
let indentation_level = IndentationLevel { spaces, tabs };
720+
let indentation_level = self.determine_indentation()?;
717721

718722
if self.nesting == 0 {
719723
// Determine indent or dedent:

0 commit comments

Comments
 (0)