| 1 |
//! Various nom parser helpers. |
| 2 |
|
| 3 |
use nom::branch::alt; |
| 4 |
use nom::bytes::complete::tag; |
| 5 |
use nom::character::complete::{anychar, line_ending, multispace1}; |
| 6 |
use nom::combinator::{map, recognize, value}; |
| 7 |
use nom::error::{ErrorKind, VerboseError, VerboseErrorKind}; |
| 8 |
use nom::multi::fold_many0; |
| 9 |
use nom::{Err as NomErr, IResult}; |
| 10 |
|
| 11 |
pub type ParserResult<'a, O> = IResult<&'a str, O, VerboseError<&'a str>>; |
| 12 |
|
| 13 |
// A constant parser that just forwards the value it’s parametered with without reading anything |
| 14 |
// from the input. Especially useful as “fallback” in an alternative parser. |
| 15 |
pub fn cnst<'a, T, E>(t: T) -> impl FnMut(&'a str) -> Result<(&'a str, T), E> |
| 16 |
where |
| 17 |
T: 'a + Clone, |
| 18 |
{ |
| 19 |
move |i| Ok((i, t.clone())) |
| 20 |
} |
| 21 |
|
| 22 |
// End-of-input parser. |
| 23 |
// |
| 24 |
// Yields `()` if the parser is at the end of the input; an error otherwise. |
| 25 |
pub fn eoi(i: &str) -> ParserResult<()> { |
| 26 |
if i.is_empty() { |
| 27 |
Ok((i, ())) |
| 28 |
} else { |
| 29 |
Err(NomErr::Error(VerboseError { |
| 30 |
errors: vec![(i, VerboseErrorKind::Nom(ErrorKind::Eof))], |
| 31 |
})) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// A newline parser that accepts: |
| 36 |
// |
| 37 |
// - A newline. |
| 38 |
// - The end of input. |
| 39 |
pub fn eol(i: &str) -> ParserResult<()> { |
| 40 |
alt(( |
| 41 |
eoi, // this one goes first because it’s very cheap |
| 42 |
value((), line_ending), |
| 43 |
))(i) |
| 44 |
} |
| 45 |
|
| 46 |
// Apply the `f` parser until `g` succeeds. Both parsers consume the input. |
| 47 |
pub fn till<'a, A, B, F, G>(mut f: F, mut g: G) -> impl FnMut(&'a str) -> ParserResult<'a, ()> |
| 48 |
where |
| 49 |
F: FnMut(&'a str) -> ParserResult<'a, A>, |
| 50 |
G: FnMut(&'a str) -> ParserResult<'a, B>, |
| 51 |
{ |
| 52 |
move |mut i| loop { |
| 53 |
if let Ok((i2, _)) = g(i) { |
| 54 |
break Ok((i2, ())); |
| 55 |
} |
| 56 |
|
| 57 |
let (i2, _) = f(i)?; |
| 58 |
i = i2; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// A version of many0 that discards the result of the parser, preventing allocating. |
| 63 |
pub fn many0_<'a, A, F>(mut f: F) -> impl FnMut(&'a str) -> ParserResult<'a, ()> |
| 64 |
where |
| 65 |
F: FnMut(&'a str) -> ParserResult<'a, A>, |
| 66 |
{ |
| 67 |
move |i| fold_many0(&mut f, || (), |_, _| ())(i) |
| 68 |
} |
| 69 |
|
| 70 |
/// Parse a string until the end of line. |
| 71 |
/// |
| 72 |
/// This parser accepts the multiline annotation (\) to break the string on several lines. |
| 73 |
/// |
| 74 |
/// Discard any leading newline. |
| 75 |
pub fn str_till_eol(i: &str) -> ParserResult<&str> { |
| 76 |
map( |
| 77 |
recognize(till(alt((value((), tag("\\\n")), value((), anychar))), eol)), |
| 78 |
|i| { |
| 79 |
if i.as_bytes().last() == Some(&b'\n') { |
| 80 |
&i[0..i.len() - 1] |
| 81 |
} else { |
| 82 |
i |
| 83 |
} |
| 84 |
}, |
| 85 |
)(i) |
| 86 |
} |
| 87 |
|
| 88 |
// Blank base parser. |
| 89 |
// |
| 90 |
// This parser succeeds with multispaces and multiline annotation. |
| 91 |
// |
| 92 |
// Taylor Swift loves it. |
| 93 |
pub fn blank_space(i: &str) -> ParserResult<&str> { |
| 94 |
recognize(many0_(alt((multispace1, tag("\\\n")))))(i) |
| 95 |
} |