1use crate::{
2 Local,
3 SourceEvent,
4 SourceResult, Source,
5 Breaker, Error,
6 source::ParserSource,
7};
8
9pub trait Parser {
10 type Data;
11 fn next_event<S: Source>(&mut self, src: &mut S) -> ParserResult<Self::Data>;
12}
13
14pub trait PipeParser {
15 fn next_char<S: Source>(&mut self, src: &mut S) -> SourceResult;
16}
17
18pub trait IntoPipeParser {
19 type Piped: PipeParser;
20 fn into_piped(self) -> Self::Piped;
21}
22
23impl<T: Parser> ParserExt for T {}
24
25pub trait ParserExt: Parser + Sized {
26 fn map<F,U>(self, func: F) -> Map<Self,F,U>
27 where F: FnMut(<Self as Parser>::Data) -> U
28 {
29 Map {
30 parser: self,
31 func,
32 }
33 }
34 fn map_eof<I,F>(self, func: F) -> MapEof<Self,I,F>
35 where I: IntoIterator<Item = Local<ParserEvent<<Self as Parser>::Data>>>,
36 F: FnMut(Vec<Local<SourceEvent>>) -> Result<I,Error>
37 {
38 MapEof {
39 parser: self,
40 func,
41 iter: None,
42 }
43 }
44 fn pipe_with<I,F>(self, func: F) -> PipedWith<Self,I,F>
45 where I: IntoIterator<Item = SourceEvent>,
46 F: FnMut(<Self as Parser>::Data) -> I
47 {
48 PipedWith {
49 parser: self,
50 func,
51 current_iter: None,
52 }
53 }
54 fn partial_pipe_with<I,F>(self, func: F) -> PartialPipedWith<Self,I,F>
55 where I: IntoIterator<Item = Local<SourceEvent>>,
56 F: FnMut(<Self as Parser>::Data) -> Result<I,<Self as Parser>::Data>
57 {
58 PartialPipedWith {
59 parser: self,
60 func,
61 current_iter: None,
62 }
63 }
64 fn filter<F: Filter<Self::Data>>(self, filter: F) -> Filtered<Self,F> {
65 Filtered {
66 parser: self,
67 filter,
68 }
69 }
70 fn into_breaker(self) -> PipeBreaker<Self> {
85 PipeBreaker {
86 parser: self,
87 }
88 }
89}
90
91pub type ParserResult<D> = Result<Option<Local<ParserEvent<D>>>,Error>;
92
93#[derive(Debug,Eq,PartialEq)]
94pub enum ParserEvent<D> {
95 Char(char),
96 Breaker(Breaker),
97 Parsed(D),
98}
99impl<D> From<SourceEvent> for ParserEvent<D> {
100 fn from(se: SourceEvent) -> ParserEvent<D> {
101 match se {
102 SourceEvent::Char(c) => ParserEvent::Char(c),
103 SourceEvent::Breaker(b) => ParserEvent::Breaker(b),
104 }
105 }
106}
107
108pub struct Map<P,F,U>
109where P: Parser,
110 F: FnMut(<P as Parser>::Data) -> U
111{
112 parser: P,
113 func: F,
114}
115impl<P,F,U> Parser for Map<P,F,U>
116where P: Parser,
117 F: FnMut(<P as Parser>::Data) -> U,
118{
119 type Data = U;
120
121 fn next_event<S: Source>(&mut self, src: &mut S) -> ParserResult<Self::Data> {
122 self.parser.next_event(src)
123 .map(|opt_lpe| {
124 opt_lpe.map(|local_pe| {
125 local_pe.map(|pe| match pe {
126 ParserEvent::Char(c) => ParserEvent::Char(c),
127 ParserEvent::Breaker(b) => ParserEvent::Breaker(b),
128 ParserEvent::Parsed(d) => ParserEvent::Parsed((self.func)(d)),
129 })
130 })
131 })
132 }
133}
134
135pub struct MapEof<P,I,F>
136where P: Parser,
137 I: IntoIterator<Item = Local<ParserEvent<<P as Parser>::Data>>>,
138 F: FnMut(Vec<Local<SourceEvent>>) -> Result<I,Error>
139{
140 parser: P,
141 func: F,
142 iter: Option<I::IntoIter>,
143}
144impl<P,I,F> Parser for MapEof<P,I,F>
145where P: Parser,
146 I: IntoIterator<Item = Local<ParserEvent<<P as Parser>::Data>>>,
147 F: FnMut(Vec<Local<SourceEvent>>) -> Result<I,Error>
148{
149 type Data = <P as Parser>::Data;
150
151 fn next_event<S: Source>(&mut self, src: &mut S) -> ParserResult<Self::Data> {
152 if let Some(iter) = &mut self.iter {
153 match iter.next() {
154 Some(lpe) => return Ok(Some(lpe)),
155 None => self.iter = None,
156 }
157 }
158
159 match self.parser.next_event(src) {
160 Ok(olpe) => Ok(olpe),
161 Err(Error::EofInTag(raw)) => {
162 let mut iter = (self.func)(raw)?.into_iter();
163 match iter.next() {
164 Some(lpe) => {
165 self.iter = Some(iter);
166 Ok(Some(lpe))
167 },
168 None => Ok(None),
169 }
170 },
171 Err(e) => Err(e),
172 }
173 }
174}
175
176
177pub trait Filter<D> {
178 fn filter(&mut self, ev: ParserEvent<D>) -> Option<ParserEvent<D>>;
179}
180
181pub struct Filtered<P,F> {
182 parser: P,
183 filter: F,
184}
185impl<P,F> Parser for Filtered<P,F>
186where P: Parser,
187 F: Filter<<P as Parser>::Data>
188{
189 type Data = <P as Parser>::Data;
190 fn next_event<S: Source>(&mut self, src: &mut S) -> ParserResult<Self::Data> {
191 while let Some(local_pe) = self.parser.next_event(src)? {
192 let (local,pe) = local_pe.into_inner();
193 if let Some(pe) = self.filter.filter(pe) {
194 return Ok(Some(local.local(pe)));
195 }
196 }
197 Ok(None)
198 }
199}
200
201pub struct PipeBreaker<P> {
260 parser: P,
261}
262impl<P> PipeParser for PipeBreaker<P>
263where P: Parser,
264 P::Data: Into<Breaker>
265{
266 fn next_char<S: Source>(&mut self, src: &mut S) -> SourceResult {
267 Ok(match self.parser.next_event(src)? {
268 Some(local_pe) => {
269 let (local,pe) = local_pe.into_inner();
270 let se = match pe {
271 ParserEvent::Char(c) => SourceEvent::Char(c),
272 ParserEvent::Breaker(b) => SourceEvent::Breaker(b),
273 ParserEvent::Parsed(d) => SourceEvent::Breaker(d.into()),
274 };
275 Some(local.local(se))
276 },
277 None => None,
278 })
279 }
280}
281
282
283pub struct PipedWith<P,I,F>
284where P: Parser,
285 I: IntoIterator<Item = SourceEvent>,
286 F: FnMut(<P as Parser>::Data) -> I
287{
288 parser: P,
289 func: F,
290 current_iter: Option<(Local<()>,<I as IntoIterator>::IntoIter)>,
291}
292impl<P,I,F> PipeParser for PipedWith<P,I,F>
293where P: Parser,
294 I: IntoIterator<Item = SourceEvent>,
295 F: FnMut(<P as Parser>::Data) -> I
296{
297 fn next_char<S: Source>(&mut self, src: &mut S) -> SourceResult {
298 if let Some((local,iter)) = &mut self.current_iter {
299 match iter.next() {
300 Some(se) => return Ok(Some(local.local(se))),
301 None => self.current_iter = None,
302 }
303 }
304 while let Some(local_pe) = self.parser.next_event(src)? {
305 let (local,pe) = local_pe.into_inner();
306 match pe {
307 ParserEvent::Char(c) => return Ok(Some(local.local(SourceEvent::Char(c)))),
308 ParserEvent::Breaker(b) => return Ok(Some(local.local(SourceEvent::Breaker(b)))),
309 ParserEvent::Parsed(d) => {
310 let mut iter = (&mut self.func)(d).into_iter();
311 if let Some(se) = iter.next() {
312 self.current_iter = Some((local,iter));
313 return Ok(Some(local.local(se)));
314 }
315 },
316 }
317 }
318 Ok(None)
319 }
320}
321
322
323pub struct PartialPipedWith<P,I,F>
324where P: Parser,
325 I: IntoIterator<Item = Local<SourceEvent>>,
326 F: FnMut(<P as Parser>::Data) -> Result<I,<P as Parser>::Data>
327{
328 parser: P,
329 func: F,
330 current_iter: Option<<I as IntoIterator>::IntoIter>,
331}
332impl<P,I,F> Parser for PartialPipedWith<P,I,F>
333where P: Parser,
334 I: IntoIterator<Item = Local<SourceEvent>>,
335 F: FnMut(<P as Parser>::Data) -> Result<I,<P as Parser>::Data>
336{
337 type Data = <P as Parser>::Data;
338 fn next_event<S: Source>(&mut self, src: &mut S) -> ParserResult<Self::Data> {
339 if let Some(iter) = &mut self.current_iter {
340 match iter.next() {
341 Some(local_se) => return Ok(Some(local_se.map(|se| se.into()))),
342 None => self.current_iter = None,
343 }
344 }
345 while let Some(local_pe) = self.parser.next_event(src)? {
346 let (local,pe) = local_pe.into_inner();
347 match pe {
348 p @ ParserEvent::Char(..) |
349 p @ ParserEvent::Breaker(..) => return Ok(Some(local.local(p))),
350 ParserEvent::Parsed(d) => match (&mut self.func)(d) {
351 Ok(into_iter) => {
352 let mut iter = into_iter.into_iter();
353 if let Some(local_se) = iter.next() {
354 self.current_iter = Some(iter);
355 return Ok(Some(local_se.map(|se| match se {
356 SourceEvent::Char(c) => ParserEvent::Char(c),
357 SourceEvent::Breaker(b) => ParserEvent::Breaker(b),
358 })));
359 }
360 },
361 Err(d) => return Ok(Some(local.local(ParserEvent::Parsed(d)))),
362 },
363 }
364 }
365 Ok(None)
366 }
367}
368
369
370impl<T: PipeParser> PipeParserExt for T {}
371
372pub trait PipeParserExt: PipeParser + Sized {
373 fn pipe<P>(self, pipe: P) -> Pipe<Self,P>
374 where P: PipeParser
375 {
376 Pipe {
377 parser: self,
378 pipe,
379 }
380 }
381 fn option(self, use_it: bool) -> Option<Self> {
382 match use_it {
383 true => Some(self),
384 false => None,
385 }
386 }
387
388 fn as_source<'p,'s,S: Source>(&'p mut self, src: &'s mut S) -> ParserSource<'p,'s,Self,S> {
389 ParserSource::new(self,src)
390 }
391}
392
393
394pub struct Pipe<P1,P2> {
395 parser: P1,
396 pipe: P2,
397}
398impl<P1,P2> PipeParser for Pipe<P1,P2>
399where P1: PipeParser,
400 P2: PipeParser
401{
402 fn next_char<S: Source>(&mut self, src: &mut S) -> SourceResult {
403 let mut src = self.parser.as_source(src);
404 self.pipe.next_char(&mut src)
405 }
406}
407
408
409
410
411impl<P> Parser for Option<P>
412where P: Parser
413{
414 type Data = <P as Parser>::Data;
415 fn next_event<S: Source>(&mut self, src: &mut S) -> ParserResult<Self::Data> {
416 match self {
417 Some(parser) => parser.next_event(src),
418 None => Ok(src.next_char()?.map(|local_se| local_se.map(|se| match se {
419 SourceEvent::Char(c) => ParserEvent::Char(c),
420 SourceEvent::Breaker(b) => ParserEvent::Breaker(b),
421 }))),
422 }
423 }
424}
425
426impl<P> PipeParser for Option<P>
427where P: PipeParser
428{
429 fn next_char<S: Source>(&mut self, src: &mut S) -> SourceResult {
430 match self {
431 Some(parser) => parser.next_char(src),
432 None => src.next_char(),
433 }
434 }
435}