mirror of https://codeberg.org/topola/topola.git
fix(specctra): less (possible) panic!s in read_string
This commit is contained in:
parent
0cac186aa4
commit
1d0c2cf953
|
|
@ -13,6 +13,8 @@ pub enum ParseError {
|
||||||
Expected(&'static str),
|
Expected(&'static str),
|
||||||
#[error("expected ({0}")]
|
#[error("expected ({0}")]
|
||||||
ExpectedStartOfList(&'static str),
|
ExpectedStartOfList(&'static str),
|
||||||
|
#[error("found a space inside a quoted string, but file didn't declare this possibility")]
|
||||||
|
UnexpectedSpaceInQuotedStr,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParseError {
|
impl ParseError {
|
||||||
|
|
@ -241,9 +243,24 @@ impl<R: std::io::BufRead> ListTokenizer<R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_string(&mut self) -> Result<String, ParseErrorContext> {
|
fn read_string(&mut self) -> Result<String, ParseErrorContext> {
|
||||||
if let Some(chr) = self.quote_char {
|
if let Some(quote_chr) = self.quote_char {
|
||||||
if chr == self.peek_char()? {
|
if quote_chr == self.peek_char()? {
|
||||||
return self.read_quoted();
|
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()
|
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
|
// the following two methods effectively allow 1 token of lookahead
|
||||||
|
|
||||||
// returns next token, either a cached one returned earlier or a newly read one
|
// returns next token, either a cached one returned earlier or a newly read one
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue