| 1 |
#![allow(dead_code)] |
| 2 |
// #![allow(unused_variables)] |
| 3 |
|
| 4 |
use std::str; |
| 5 |
|
| 6 |
use nom::bytes::complete::is_not; |
| 7 |
use nom::character::complete::char; |
| 8 |
use nom::combinator::{map, map_res}; |
| 9 |
use nom::multi::fold; |
| 10 |
use nom::sequence::delimited; |
| 11 |
use nom::{IResult, Parser}; |
| 12 |
|
| 13 |
fn atom(_tomb: &mut ()) -> impl for<'a> FnMut(&'a [u8]) -> IResult<&'a [u8], String> { |
| 14 |
move |input| { |
| 15 |
map( |
| 16 |
map_res(is_not(" \t\r\n"), str::from_utf8), |
| 17 |
ToString::to_string, |
| 18 |
) |
| 19 |
.parse(input) |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
// FIXME: should we support the use case of borrowing data mutably in a parser? |
| 24 |
fn list<'a>(i: &'a [u8], tomb: &mut ()) -> IResult<&'a [u8], String> { |
| 25 |
delimited( |
| 26 |
char('('), |
| 27 |
fold(0.., atom(tomb), String::new, |acc: String, next: String| { |
| 28 |
acc + next.as_str() |
| 29 |
}), |
| 30 |
char(')'), |
| 31 |
) |
| 32 |
.parse(i) |
| 33 |
} |