Skip to content

Commit

Permalink
Add error for unmatched double quotes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ISibboI committed Jul 26, 2023
1 parent dc4bce6 commit 2cfdb9d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/error/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl fmt::Display for EvalexprError {
),
UnmatchedLBrace => write!(f, "Found an unmatched opening parenthesis '('."),
UnmatchedRBrace => write!(f, "Found an unmatched closing parenthesis ')'."),
UnmatchedDoubleQuote => write!(f, "Found an unmatched double quote '\"'"),
MissingOperatorOutsideOfBrace { .. } => write!(
f,
"Found an opening parenthesis that is preceded by something that does not take \
Expand Down
3 changes: 3 additions & 0 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ pub enum EvalexprError {
/// A closing brace without a matching opening brace was found.
UnmatchedRBrace,

/// A double quote without a matching second double quote was found.
UnmatchedDoubleQuote,

/// Left of an opening brace or right of a closing brace is a token that does not expect the brace next to it.
/// For example, writing `4(5)` would yield this error, as the `4` does not have any operands.
MissingOperatorOutsideOfBrace,
Expand Down
4 changes: 2 additions & 2 deletions src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,13 @@ fn parse_string_literal<Iter: Iterator<Item = char>>(

while let Some(c) = iter.next() {
match c {
'"' => break,
'"' => return Ok(PartialToken::Token(Token::String(result))),
'\\' => result.push(parse_escape_sequence(&mut iter)?),
c => result.push(c),
}
}

Ok(PartialToken::Token(Token::String(result)))
Err(EvalexprError::UnmatchedDoubleQuote)
}

/// Converts a string to a vector of partial tokens.
Expand Down
5 changes: 4 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2307,5 +2307,8 @@ fn test_hex() {

#[test]
fn test_broken_string() {
assert!(eval(r#""abc" == "broken string"#).is_err());
assert_eq!(
eval(r#""abc" == "broken string"#),
Err(EvalexprError::UnmatchedDoubleQuote)
);
}

0 comments on commit 2cfdb9d

Please sign in to comment.