I'm new to Rust and Nom and I'm trying to parse a (single) quoted string which may contain escaped quotes, e.g. 'foo\' bar'
or 'λx → x'
, ''
or ' '
.
I found the escaped!
macro, whose documentation says:
The first argument matches the normal characters (it must not accept the control character), the second argument is the control character (like \ in most languages), the third argument matches the escaped characters
Since I want to match anything but a backslash in the matcher for “normal characters”, I tried using take_till!
:
named!(till_backslash<&str, &str>, take_till!(|ch| ch == '\\'));
named!(esc<&str, &str>, escaped!(call!(till_backslash), '\\', one_of!("'n\\")));
let (input, _) = nom::character::complete::char('\'')(input)?;
let (input, value) = esc(input)?;
let (input, _) = nom::character::complete::char('\'')(input)?;
// … use `value`
However, when trying to parse 'x'
, this returns Err(Incomplete(Size(1)))
. When searching for this, people generally recommend using CompleteStr
, but that's not in Nom 5. What's the correct way to approach this problem?