pub fn literal<Literal, Input, Error>(
literal: Literal,
) -> impl Parser<Input, <Input as Stream>::Slice, Error>where
Input: StreamIsPartial + Stream + Compare<Literal>,
Literal: SliceLen + Clone + Debug,
Error: ParserError<Input>,
Expand description
Recognizes a literal
The input data will be compared to the literal combinator’s argument and will return the part of the input that matches the argument
It will return Err(ErrMode::Backtrack(InputError::new(_, ErrorKind::Tag)))
if the input doesn’t match the literal
Note: Parser
is implemented for strings and byte strings as a convenience (complete
only)
§Effective Signature
Assuming you are parsing a &str
Stream:
pub fn literal(literal: &str) -> impl Parser<&str, &str, ContextError>
§Example
fn parser(s: &str) -> IResult<&str, &str> {
"Hello".parse_peek(s)
}
assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello")));
assert_eq!(parser("Something"), Err(ErrMode::Backtrack(InputError::new("Something", ErrorKind::Tag))));
assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag))));
fn parser(s: Partial<&str>) -> IResult<Partial<&str>, &str> {
"Hello".parse_peek(s)
}
assert_eq!(parser(Partial::new("Hello, World!")), Ok((Partial::new(", World!"), "Hello")));
assert_eq!(parser(Partial::new("Something")), Err(ErrMode::Backtrack(InputError::new(Partial::new("Something"), ErrorKind::Tag))));
assert_eq!(parser(Partial::new("S")), Err(ErrMode::Backtrack(InputError::new(Partial::new("S"), ErrorKind::Tag))));
assert_eq!(parser(Partial::new("H")), Err(ErrMode::Incomplete(Needed::new(4))));
use winnow::token::literal;
use winnow::ascii::Caseless;
fn parser(s: &str) -> IResult<&str, &str> {
literal(Caseless("hello")).parse_peek(s)
}
assert_eq!(parser("Hello, World!"), Ok((", World!", "Hello")));
assert_eq!(parser("hello, World!"), Ok((", World!", "hello")));
assert_eq!(parser("HeLlO, World!"), Ok((", World!", "HeLlO")));
assert_eq!(parser("Something"), Err(ErrMode::Backtrack(InputError::new("Something", ErrorKind::Tag))));
assert_eq!(parser(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag))));