diff --git a/src/main.rs b/src/main.rs index bac9a74..9a230a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,14 @@ use nom::{ IResult, Parser, - branch::permutation, - bytes::take_until, - character::{char, complete::multispace1}, - error::{FromExternalError, ParseError}, - multi::{fold, many_m_n}, + branch::alt, + bytes::complete::{tag, take_until}, + character::complete::{line_ending, not_line_ending}, + combinator::{map, success}, + error::Error, + multi::fold, sequence::{delimited, preceded}, }; +use std::vec::Vec; enum Fragment<'a> { Literal(&'a str), @@ -15,42 +17,54 @@ enum Fragment<'a> { fn main() { let input = concat!( - "Hallo daar!\n", - "Hoe gaat het?\n", + // "Hallo daar!\n", + // "Hoe gaat het?\n", "# Met mij gaat het goed\n", "Met jou\n", "## Ja ook goed\n", - "Echt?\n", - "### Wow (not matched because we only accept 1 or 2 '#')\n", - "#StartOfFileHeaderNoSpace <-- not matched because we require a space after '#'\n", - " ## Indented header\n" + // "Echt?\n", + // "### Wow (not matched because we only accept 1 or 2 '#')\n", + // "#StartOfFileHeaderNoSpace <-- not matched because we require a space after '#'\n", + // " ## Indented header\n" ); - let x = parse_string::<()>(input).unwrap(); - println!("{:?}", x); + let vec = Vec::::new(); + use nom::bytes::complete::tag; + use nom::sequence::preceded; + + println!("{:?}", parse_fragment(input)) } -fn parse_string<'a, E>(input: &'a str) -> IResult<&'a str, String, E> -where - E: ParseError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, -{ - fold(0.., parse_header, String::new, |mut string, newstr| { - string.push_str(newstr); - string - }) +fn parse_header<'a>(input: &'a str) -> IResult<&'a str, &str, ()> { + delimited(tag::<&str, &str, ()>("# "), not_line_ending, line_ending).parse(input) +} +fn parse_fragment<'a>(input: &'a str) -> IResult<&str, Fragment, ()> { + alt(( + map(parse_header, Fragment::Header), + map(not_line_ending, Fragment::Literal), + )) .parse(input) } -fn parse_fragment<'a, E>(input: &'a str) -> IResult<&'a str, StringFragment<'a>, E> -where - E: ParseError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, -{ - alt((map(parse_header, Fragment::Header))) -} - -fn parse_header<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str, E> { - preceded( - permutation((char('\n'), many_m_n(1, 2, char('#')))), - take_until("\n"), +fn parse_string<'a>(input: &'a str) -> IResult<&'a str, Vec>, ()> { + fold( + 0.., + parse_fragment, + Vec::::new(), + |mut vector, fragment| vector, ) - .parse(input) } + +// fn parse_fragment<'a, E>(input: &'a str) -> IResult<&'a str, StringFragment<'a>, E> +// where +// E: ParseError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>, +// { +// alt((map(parse_header, Fragment::Header))) +// } +// +// fn parse_header<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str, E> { +// preceded( +// permutation((char('\n'), many_m_n(1, 2, char('#')))), +// take_until("\n"), +// ) +// .parse(input) +// }