fix(specctra): less (possible) panic!s in read_string

This commit is contained in:
Alain Emilia Anna Zscheile 2024-11-29 17:31:36 +01:00
parent 0cac186aa4
commit 1d0c2cf953
1 changed files with 20 additions and 25 deletions

View File

@ -13,6 +13,8 @@ pub enum ParseError {
Expected(&'static str),
#[error("expected ({0}")]
ExpectedStartOfList(&'static str),
#[error("found a space inside a quoted string, but file didn't declare this possibility")]
UnexpectedSpaceInQuotedStr,
}
impl ParseError {
@ -241,9 +243,24 @@ impl<R: std::io::BufRead> ListTokenizer<R> {
}
fn read_string(&mut self) -> Result<String, ParseErrorContext> {
if let Some(chr) = self.quote_char {
if chr == self.peek_char()? {
return self.read_quoted();
if let Some(quote_chr) = self.quote_char {
if quote_chr == self.peek_char()? {
let mut string = String::new();
assert_eq!(self.next_char().unwrap(), quote_chr);
loop {
let ctx = self.context();
let chr = self.next_char()?;
if chr == ' ' && !self.space_in_quoted {
return Err(ParseError::UnexpectedSpaceInQuotedStr.add_context(ctx));
} else if chr == quote_chr {
break;
} else {
string.push(chr);
}
}
return Ok(string);
}
}
self.read_unquoted()
@ -267,28 +284,6 @@ impl<R: std::io::BufRead> ListTokenizer<R> {
}
}
fn read_quoted(&mut self) -> Result<String, ParseErrorContext> {
let mut string = String::new();
if self.next_char().unwrap() != self.quote_char.unwrap() {
panic!();
}
loop {
let chr = self.peek_char()?;
if !self.space_in_quoted && chr == ' ' {
panic!("found a space inside a quoted string, but file didn't declare this possibility");
}
if chr == self.quote_char.unwrap() {
self.next_char().unwrap();
break;
}
string.push(self.next_char().unwrap());
}
Ok(string)
}
// the following two methods effectively allow 1 token of lookahead
// returns next token, either a cached one returned earlier or a newly read one