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
use crate::chars::*; use crate::iter::LCChars; use crate::ptrait::{ParseRes, Parser}; pub fn do_skip_c<'a, CB: CharBool>( it: &LCChars<'a>, cb: &CB, min: usize, exact: bool, ) -> ParseRes<'a, ()> { let mut it = it.clone(); let mut done = 0; loop { let it2 = it.clone(); match it.next() { Some(c) if cb.char_bool(c) => done += 1, Some(_) | None => { if done >= min { let eo = it2.err_oc(cb); return Ok((it2, (), eo)); } else { return it2.err_r(cb.expected()); } } } if done == min && exact { return Ok((it, (), None)); } } } pub fn do_skip_p<'a, P: Parser>( it: &LCChars<'a>, p: &P, min: usize, exact: bool, ) -> ParseRes<'a, ()> { let mut it = it.clone(); let mut done = 0; loop { let it2 = it.clone(); it = match p.parse(&it) { Ok((nit, _, _)) => { done += 1; nit } Err(e) => { if done >= min { return Ok((it2, (), Some(e))); } else { return Err(e); } } }; if done == min && exact { return Ok((it, (), None)); } } } pub struct CharSkip<CB: CharBool> { pub cb: CB, } impl<CB: CharBool> Parser for CharSkip<CB> { type Out = (); fn parse<'a>(&self, it: &LCChars<'a>) -> ParseRes<'a, ()> { do_skip_c(it, &self.cb, 0, false) } } #[derive(Clone)] pub struct CharSkipPlus<CB: CharBool> { pub cb: CB, } impl<CB: CharBool> Parser for CharSkipPlus<CB> { type Out = (); fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, ()> { do_skip_c(i, &self.cb, 1, false) } } #[derive(Clone)] pub struct CharSkipExact<CB: CharBool> { pub cb: CB, pub n: usize, } impl<CB: CharBool> Parser for CharSkipExact<CB> { type Out = (); fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, ()> { do_skip_c(i, &self.cb, self.n, true) } } pub fn ig_star<A: Parser>(a: A) -> PSkipStar<A> { PSkipStar { a } } pub fn ig_plus<A: Parser>(a: A) -> PSkipPlus<A> { PSkipPlus { a } } pub fn ig_exact<A: Parser>(a: A, n: usize) -> PSkipExact<A> { PSkipExact { a, n } } #[derive(Clone)] pub struct PSkipStar<A: Parser> { pub a: A, } impl<A: Parser> Parser for PSkipStar<A> { type Out = (); fn parse<'a>(&self, it: &LCChars<'a>) -> ParseRes<'a, ()> { do_skip_p(it, &self.a, 0, false) } } #[derive(Clone)] pub struct PSkipPlus<A: Parser> { pub a: A, } impl<A: Parser> Parser for PSkipPlus<A> { type Out = (); fn parse<'a>(&self, it: &LCChars<'a>) -> ParseRes<'a, ()> { do_skip_p(it, &self.a, 1, false) } } #[derive(Clone)] pub struct PSkipExact<A: Parser> { pub a: A, pub n: usize, } impl<A: Parser> Parser for PSkipExact<A> { type Out = (); fn parse<'a>(&self, it: &LCChars<'a>) -> ParseRes<'a, ()> { do_skip_p(it, &self.a, self.n, true) } } pub fn skip_2_star<A: Parser, B: Parser>(a: A, b: B) -> Skip2Star<A, B> { Skip2Star { a, b } } pub struct Skip2Star<A: Parser, B: Parser> { a: A, b: B, } impl<A: Parser, B: Parser> Parser for Skip2Star<A, B> { type Out = (); fn parse<'a>(&self, it: &LCChars<'a>) -> ParseRes<'a, ()> { let mut it = it.clone(); loop { if let Ok((nit, _, _)) = self.a.parse(&it) { it = nit; } else if let Ok((nit, _, _)) = self.b.parse(&it) { it = nit; } else { return Ok((it, (), None)); } } } }