use crate::parser::{code, predicate, prelude::*, Input, ParseResult};
use super::{escaped_char, LiteralError};
pub fn quoted_char(input: Input<'_>) -> ParseResult<'_, char> {
delimited(
char('\''),
any((escaped_char, satisfy(predicate::is_char_quotable))),
char('\''),
)(input)
.map_err(|mut err| {
err.push(err.span(), code::ERR_QUOTED_CHAR, LiteralError::QuotedChar);
err
})
}
#[cfg(test)]
mod tests {
use crate::{
parser::{ParseError, Span},
Position,
};
use super::*;
#[test]
fn quoted_char_success() {
let (output, input) = quoted_char(Input::new("'a'")).expect("valid output");
assert_eq!(output, 'a');
assert_eq!(input, Input::from(""));
assert_eq!(input.current(), None);
assert_eq!(input.position(), Position::new(3, 1, 4));
}
#[test]
fn quoted_char_failure() {
let error = quoted_char(Input::new("'a")).expect_err("invalid output");
assert!(error.is_eof());
assert_eq!(
error,
ParseError::eof(Position::new(2, 1, 3)).and(
Span::new(Position::new(0, 1, 1), Position::new(2, 1, 3)),
code::ERR_QUOTED_CHAR,
LiteralError::QuotedChar
)
);
}
}