1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! Parsers that parse lines or groups of lines: `line(p)`, `lines(p)`.

use std::marker::PhantomData;

use crate::{
    error::Result,
    parsers::{star, EmptyParser, RepeatParser},
    types::ParserOutput,
    ParseError, ParseIter, Parser,
};

/// This is implemented for `Line` and `Section`, the two region types.
pub trait Region {
    /// True if `start` is an offset within `source` that's the start of this
    /// type of region. Caller promises that `start` is at least in bounds and
    /// a character boundary in `source`.
    fn is_at_start(source: &str, start: usize) -> bool;

    /// If a suitable end is found for this region (`'\n'` for a line, `"\n\n"`
    /// or `/\n\Z/` for a section) then return a pair of
    ///
    /// -   the end of the interior of the region, for the purpose of parsing the
    ///     interior; and
    /// -   the end of the delimiter, for the purpose of reporting how much data
    ///     we consumed on a successful parse.
    fn find_end(source: &str, start: usize) -> Option<(usize, usize)>;

    fn not_at_start_err(source: &str, start: usize) -> ParseError;

    fn extra_err(source: &str, end: usize) -> ParseError;
}

/// A line is a sequence of zero or more non-newline characters, starting
/// either at the beginning of the input or immediately after a newline;
/// followed by a single newline.
pub struct Line;

impl Region for Line {
    fn is_at_start(source: &str, start: usize) -> bool {
        start == 0 || source[..start].ends_with('\n')
    }

    fn find_end(source: &str, start: usize) -> Option<(usize, usize)> {
        source[start..]
            .find('\n')
            .map(|offset| (start + offset, start + offset + 1))
    }

    fn not_at_start_err(source: &str, start: usize) -> ParseError {
        ParseError::new_bad_line_start(source, start)
    }

    fn extra_err(source: &str, end: usize) -> ParseError {
        ParseError::new_line_extra(source, end)
    }
}

/// A "section" is a sequence of zero or more nonblank lines, starting either
/// at the beginning of the input or immediately after a newline; followed by
/// either a blank line or the end of input.
pub struct Section;

impl Region for Section {
    fn is_at_start(source: &str, start: usize) -> bool {
        start == 0 || &source[..start] == "\n" || source[..start].ends_with("\n\n")
    }

    fn find_end(source: &str, start: usize) -> Option<(usize, usize)> {
        // FIXME BUG: unclear what this should do when looking at an empty
        // section at end of input. presumably not repeat forever.
        match source[start..].find("\n\n") {
            // ending at a blank line
            Some(index) => Some((start + index + 1, start + index + 2)),
            // ending at the end of `source`
            None if start < source.len() && source.ends_with('\n') => {
                Some((source.len(), source.len()))
            }
            // no end-of-section delimiter found
            None => None,
        }
    }

    fn not_at_start_err(source: &str, start: usize) -> ParseError {
        ParseError::new_bad_section_start(source, start)
    }

    fn extra_err(source: &str, end: usize) -> ParseError {
        ParseError::new_section_extra(source, end)
    }
}

#[derive(Copy, Clone)]
pub struct RegionParser<R: Region, P> {
    parser: P,
    phantom: PhantomData<fn() -> R>,
}

impl<'parse, 'source, R, P> Parser<'parse, 'source> for RegionParser<R, P>
where
    R: Region + 'parse,
    P: Parser<'parse, 'source> + 'parse,
{
    type RawOutput = (P::Output,);
    type Output = P::Output;
    type Iter = RegionParseIter<'parse, 'source, R, P>;

    fn parse_iter(&'parse self, source: &'source str, start: usize) -> Self::Iter {
        RegionParseIter::Init {
            parser: self,
            source,
            start,
        }
    }
}

pub enum RegionParseIter<'parse, 'source, R, P>
where
    R: Region,
    P: Parser<'parse, 'source>,
{
    Init {
        parser: &'parse RegionParser<R, P>,
        source: &'source str,
        start: usize,
    },
    Matched {
        iter: P::Iter,
    },
    Done,
}

impl<'parse, 'source, R, P> ParseIter for RegionParseIter<'parse, 'source, R, P>
where
    R: Region,
    P: Parser<'parse, 'source>,
{
    type RawOutput = (P::Output,);

    fn next_parse(&mut self) -> Option<Result<usize>> {
        match *self {
            RegionParseIter::Init {
                parser,
                source,
                start,
            } => {
                if !R::is_at_start(source, start) {
                    *self = RegionParseIter::Done;
                    return Some(Err(R::not_at_start_err(source, start)));
                }
                let (inner_end, outer_end) = match R::find_end(source, start) {
                    Some(pair) => pair,
                    None => {
                        *self = RegionParseIter::Done;
                        return Some(Err(ParseError::new_expected(source, source.len(), "\n")));
                    }
                };
                let mut iter = parser.parser.parse_iter(&source[start..inner_end], 0);
                let mut farthest = 0;
                loop {
                    match iter.next_parse() {
                        None => {
                            *self = RegionParseIter::Done;
                            return Some(Err(R::extra_err(source, start + farthest)));
                        }
                        Some(Err(mut err)) => {
                            *self = RegionParseIter::Done;
                            err.adjust_location(start);
                            return Some(Err(err));
                        }
                        Some(Ok(len)) if start + len == inner_end => {
                            *self = RegionParseIter::Matched { iter };
                            return Some(Ok(outer_end));
                        }
                        Some(Ok(len)) => farthest = farthest.max(len),
                    }
                }
            }
            _ => {
                *self = RegionParseIter::Done;
                None
            }
        }
    }

    fn take_data(&mut self) -> Self::RawOutput {
        match self {
            RegionParseIter::Matched { iter } => {
                let v = iter.take_data().into_user_type();
                *self = RegionParseIter::Done;
                (v,)
            }
            _ => panic!("internal error: take_data called in invalid state"),
        }
    }
}

pub type LineParser<P> = RegionParser<Line, P>;
pub type SectionParser<P> = RegionParser<Section, P>;

pub fn line<P>(parser: P) -> LineParser<P> {
    LineParser {
        parser,
        phantom: PhantomData,
    }
}

pub fn lines<P>(parser: P) -> RepeatParser<LineParser<P>, EmptyParser> {
    star(line(parser))
}

pub fn section<P>(parser: P) -> SectionParser<P> {
    SectionParser {
        parser,
        phantom: PhantomData,
    }
}

pub fn sections<P>(parser: P) -> RepeatParser<SectionParser<P>, EmptyParser> {
    star(section(parser))
}