text_parsing/paragraph/
parser.rs

1
2use crate::{
3    ParserResult,
4    Source,
5    Parser, Runtime, Breaker,
6    SourceEvent,ParserEvent,
7    PipeParser, SourceResult,
8};
9
10use super::{
11    state::{ParaState,Paragraph},
12};
13
14/*
15
16    Breaker::Line {Breaker::_}*X Breaker::Line = Breaker::Paragraph
17
18*/
19
20
21#[derive(Debug,Clone)]
22pub struct Builder {
23
24}
25impl Builder {
26    pub fn new() -> Builder {
27        Builder{
28          
29        }
30    }
31    pub fn create(self) -> Paragraphs {
32        Paragraphs(Runtime::new(()))
33    }
34}
35
36
37pub struct Paragraphs(Runtime<ParaState,Paragraph,()>);
38
39impl Parser for Paragraphs {
40    type Data = Paragraph;
41    
42    fn next_event<S: Source>(&mut self, src: &mut S) -> ParserResult<Paragraph> {
43        self.0.next_event(src)
44    }
45}
46
47impl PipeParser for Paragraphs {
48    fn next_char<S: Source>(&mut self, src: &mut S) -> SourceResult {
49        Ok(match self.next_event(src)? {
50            Some(local_pe) => {
51                let (local,pe) = local_pe.into_inner();
52                Some(local.local(match pe {
53                    ParserEvent::Char(c) => SourceEvent::Char(c),
54                    ParserEvent::Breaker(b) => SourceEvent::Breaker(b),
55                    ParserEvent::Parsed(Paragraph) => SourceEvent::Breaker(Breaker::Paragraph),
56                }))
57            },
58            None => None,
59        })
60    }
61}
62
63
64
65#[cfg(test)]
66mod tests {
67    use crate::*;
68    use super::*;
69    
70    #[test]
71    fn basic() {
72        let mut src = "Hello, world!\n\nПривет, мир!".into_source();
73        let mut parser = Builder::new().create();
74
75        let mut res_iter = [
76            ParserEvent::Char('H').localize(Snip { offset: 0, length: 1 },Snip { offset: 0, length: 1 }),
77            ParserEvent::Char('e').localize(Snip { offset: 1, length: 1 },Snip { offset: 1, length: 1 }),
78            ParserEvent::Char('l').localize(Snip { offset: 2, length: 1 },Snip { offset: 2, length: 1 }),
79            ParserEvent::Char('l').localize(Snip { offset: 3, length: 1 },Snip { offset: 3, length: 1 }),
80            ParserEvent::Char('o').localize(Snip { offset: 4, length: 1 },Snip { offset: 4, length: 1 }),
81            ParserEvent::Char(',').localize(Snip { offset: 5, length: 1 },Snip { offset: 5, length: 1 }),
82            ParserEvent::Char(' ').localize(Snip { offset: 6, length: 1 },Snip { offset: 6, length: 1 }),
83            ParserEvent::Char('w').localize(Snip { offset: 7, length: 1 },Snip { offset: 7, length: 1 }),
84            ParserEvent::Char('o').localize(Snip { offset: 8, length: 1 },Snip { offset: 8, length: 1 }),
85            ParserEvent::Char('r').localize(Snip { offset: 9, length: 1 },Snip { offset: 9, length: 1 }),
86            ParserEvent::Char('l').localize(Snip { offset: 10, length: 1 },Snip { offset: 10, length: 1 }),
87            ParserEvent::Char('d').localize(Snip { offset: 11, length: 1 },Snip { offset: 11, length: 1 }),
88            ParserEvent::Char('!').localize(Snip { offset: 12, length: 1 },Snip { offset: 12, length: 1 }),
89            ParserEvent::Parsed(Paragraph).localize(Snip { offset: 13, length: 2 },Snip { offset: 13, length: 2 }),
90            ParserEvent::Char('П').localize(Snip { offset: 15, length: 1 },Snip { offset: 15, length: 2 }),
91            ParserEvent::Char('р').localize(Snip { offset: 16, length: 1 },Snip { offset: 17, length: 2 }),
92            ParserEvent::Char('и').localize(Snip { offset: 17, length: 1 },Snip { offset: 19, length: 2 }),
93            ParserEvent::Char('в').localize(Snip { offset: 18, length: 1 },Snip { offset: 21, length: 2 }),
94            ParserEvent::Char('е').localize(Snip { offset: 19, length: 1 },Snip { offset: 23, length: 2 }),
95            ParserEvent::Char('т').localize(Snip { offset: 20, length: 1 },Snip { offset: 25, length: 2 }),
96            ParserEvent::Char(',').localize(Snip { offset: 21, length: 1 },Snip { offset: 27, length: 1 }),
97            ParserEvent::Char(' ').localize(Snip { offset: 22, length: 1 },Snip { offset: 28, length: 1 }),
98            ParserEvent::Char('м').localize(Snip { offset: 23, length: 1 },Snip { offset: 29, length: 2 }),
99            ParserEvent::Char('и').localize(Snip { offset: 24, length: 1 },Snip { offset: 31, length: 2 }),
100            ParserEvent::Char('р').localize(Snip { offset: 25, length: 1 },Snip { offset: 33, length: 2 }),
101            ParserEvent::Char('!').localize(Snip { offset: 26, length: 1 },Snip { offset: 35, length: 1 }),
102        ].into_iter();
103
104        while let Some(local_event) = parser.next_event(&mut src).unwrap() {
105            //let (local,event) = local_event.into_inner();
106            //println!("ParserEvent::{:?}.localize({:?},{:?}),",event,local.chars(),local.bytes());
107            match res_iter.next() {
108                Some(ev) => {
109                    println!("Parser: {:?}",local_event);
110                    println!("Result: {:?}",ev);
111                    assert_eq!(local_event,ev);
112                },
113                None => {
114                    panic!("parser has more events then test result");
115                },
116            }
117        }
118    }
119
120    #[test]
121    fn basic_2() {
122        let mut src = "Hello, world!  \n   \t  \n  Привет, мир!".into_source();
123        let mut parser = Builder::new().create();
124
125        let mut res_iter = [
126            ParserEvent::Char('H').localize(Snip { offset: 0, length: 1 },Snip { offset: 0, length: 1 }),
127            ParserEvent::Char('e').localize(Snip { offset: 1, length: 1 },Snip { offset: 1, length: 1 }),
128            ParserEvent::Char('l').localize(Snip { offset: 2, length: 1 },Snip { offset: 2, length: 1 }),
129            ParserEvent::Char('l').localize(Snip { offset: 3, length: 1 },Snip { offset: 3, length: 1 }),
130            ParserEvent::Char('o').localize(Snip { offset: 4, length: 1 },Snip { offset: 4, length: 1 }),
131            ParserEvent::Char(',').localize(Snip { offset: 5, length: 1 },Snip { offset: 5, length: 1 }),
132            ParserEvent::Char(' ').localize(Snip { offset: 6, length: 1 },Snip { offset: 6, length: 1 }),
133            ParserEvent::Char('w').localize(Snip { offset: 7, length: 1 },Snip { offset: 7, length: 1 }),
134            ParserEvent::Char('o').localize(Snip { offset: 8, length: 1 },Snip { offset: 8, length: 1 }),
135            ParserEvent::Char('r').localize(Snip { offset: 9, length: 1 },Snip { offset: 9, length: 1 }),
136            ParserEvent::Char('l').localize(Snip { offset: 10, length: 1 },Snip { offset: 10, length: 1 }),
137            ParserEvent::Char('d').localize(Snip { offset: 11, length: 1 },Snip { offset: 11, length: 1 }),
138            ParserEvent::Char('!').localize(Snip { offset: 12, length: 1 },Snip { offset: 12, length: 1 }),
139            ParserEvent::Char(' ').localize(Snip { offset: 13, length: 1 },Snip { offset: 13, length: 1 }),
140            ParserEvent::Char(' ').localize(Snip { offset: 14, length: 1 },Snip { offset: 14, length: 1 }),
141            ParserEvent::Parsed(Paragraph).localize(Snip { offset: 15, length: 8 },Snip { offset: 15, length: 8 }),
142            ParserEvent::Char(' ').localize(Snip { offset: 23, length: 1 },Snip { offset: 23, length: 1 }),
143            ParserEvent::Char(' ').localize(Snip { offset: 24, length: 1 },Snip { offset: 24, length: 1 }),
144            ParserEvent::Char('П').localize(Snip { offset: 25, length: 1 },Snip { offset: 25, length: 2 }),
145            ParserEvent::Char('р').localize(Snip { offset: 26, length: 1 },Snip { offset: 27, length: 2 }),
146            ParserEvent::Char('и').localize(Snip { offset: 27, length: 1 },Snip { offset: 29, length: 2 }),
147            ParserEvent::Char('в').localize(Snip { offset: 28, length: 1 },Snip { offset: 31, length: 2 }),
148            ParserEvent::Char('е').localize(Snip { offset: 29, length: 1 },Snip { offset: 33, length: 2 }),
149            ParserEvent::Char('т').localize(Snip { offset: 30, length: 1 },Snip { offset: 35, length: 2 }),
150            ParserEvent::Char(',').localize(Snip { offset: 31, length: 1 },Snip { offset: 37, length: 1 }),
151            ParserEvent::Char(' ').localize(Snip { offset: 32, length: 1 },Snip { offset: 38, length: 1 }),
152            ParserEvent::Char('м').localize(Snip { offset: 33, length: 1 },Snip { offset: 39, length: 2 }),
153            ParserEvent::Char('и').localize(Snip { offset: 34, length: 1 },Snip { offset: 41, length: 2 }),
154            ParserEvent::Char('р').localize(Snip { offset: 35, length: 1 },Snip { offset: 43, length: 2 }),
155            ParserEvent::Char('!').localize(Snip { offset: 36, length: 1 },Snip { offset: 45, length: 1 }),
156        ].into_iter();
157
158        while let Some(local_event) = parser.next_event(&mut src).unwrap() {
159            //let (local,event) = local_event.into_inner();
160            //println!("ParserEvent::{:?}.localize({:?},{:?}),",event,local.chars(),local.bytes());
161            match res_iter.next() {
162                Some(ev) => {
163                    println!("Parser: {:?}",local_event);
164                    println!("Result: {:?}",ev);
165                    assert_eq!(local_event,ev);
166                },
167                None => {
168                    panic!("parser has more events then test result");
169                },
170            }
171        }
172    }   
173}