ninja_build_syntax/
statements.rs1use nom::{branch::alt, character::complete::line_ending, combinator::map};
2
3use crate::{error::Error, parsers::Statement};
4
5struct Statements<'a> {
6 data: &'a [u8],
7}
8
9pub fn parse(data: &[u8]) -> impl Iterator<Item = Result<Statement, Error>> {
10 Statements { data }.filter_map(|item| item.transpose())
11}
12
13impl<'a> Iterator for Statements<'a> {
14 type Item = Result<Option<Statement<'a>>, Error>;
15
16 fn next(&mut self) -> Option<Self::Item> {
17 if self.data.is_empty() {
18 return None;
19 };
20
21 match alt((map(Statement::parse, Some), map(line_ending, |_| None)))(self.data) {
22 Ok((rest, item)) => {
23 self.data = rest;
24 Some(Ok(item))
25 }
26 Err(_) => {
27 self.data = &b""[..];
28 Some(Err(Error::new()))
29 }
30 }
31 }
32}