Skip to content

Commit 8ffd4c5

Browse files
Tom1380cthulahoops
authored andcommitted
Raise syntax error rather than panicking on expected closing bracket. * Fixed RustPython#402.
1 parent 7807dc3 commit 8ffd4c5

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

parser/src/lexer.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct Lexer<T: Iterator<Item = char>> {
5454
#[derive(Debug)]
5555
pub enum LexicalError {
5656
StringError,
57+
NestingError,
5758
}
5859

5960
#[derive(Clone, Debug, Default, PartialEq)]
@@ -428,9 +429,7 @@ where
428429
self.next_char();
429430
loop {
430431
match self.chr0 {
431-
Some('\n') => {
432-
return;
433-
}
432+
Some('\n') => return,
434433
Some(_) => {}
435434
None => return,
436435
}
@@ -904,6 +903,9 @@ where
904903
}
905904
Some(')') => {
906905
let result = self.eat_single_char(Tok::Rpar);
906+
if self.nesting == 0 {
907+
return Some(Err(LexicalError::NestingError));
908+
}
907909
self.nesting -= 1;
908910
return Some(result);
909911
}
@@ -914,6 +916,9 @@ where
914916
}
915917
Some(']') => {
916918
let result = self.eat_single_char(Tok::Rsqb);
919+
if self.nesting == 0 {
920+
return Some(Err(LexicalError::NestingError));
921+
}
917922
self.nesting -= 1;
918923
return Some(result);
919924
}
@@ -924,6 +929,9 @@ where
924929
}
925930
Some('}') => {
926931
let result = self.eat_single_char(Tok::Rbrace);
932+
if self.nesting == 0 {
933+
return Some(Err(LexicalError::NestingError));
934+
}
927935
self.nesting -= 1;
928936
return Some(result);
929937
}

0 commit comments

Comments
 (0)