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
use {Input, Length, ParseResult, Expected}; use result::error; use ParseResult::*; #[inline(always)] fn advance_and<I: Input, T>(input: &mut I, num: usize, out: T) -> ParseResult<I, T> { input.advance(num); Done(out) } #[inline] pub fn eat<I: Input>(input: &mut I, token: I::Token) -> ParseResult<I, I::Token> { match input.peek() { Some(peeked) if peeked == token => advance_and(input, 1, token), t@Some(_) | t@None => error("eat", Expected::Token(Some(token), t)), } } #[inline] pub fn eat_if<I: Input, F>(input: &mut I, cond: F) -> ParseResult<I, I::Token> where F: Fn(I::Token) -> bool { match input.peek() { Some(peeked) if cond(peeked) => advance_and(input, 1, peeked), t@Some(_) | t@None => error("eat_if", Expected::Token(None, t)), } } #[inline] pub fn eat_slice<I: Input>(input: &mut I, slice: I::Slice) -> ParseResult<I, I::Slice> { let len = slice.len(); match input.peek_slice(slice) { Some(peeked) if peeked == slice => advance_and(input, len, slice), t@Some(_) | t@None => error("eat_slice", Expected::Slice(Some(slice), t)), } } #[inline] pub fn eat_any<I: Input>(input: &mut I) -> ParseResult<I, I::Token> { match input.peek() { Some(peeked) => advance_and(input, 1, peeked), None => error("eat_any", Expected::Token(None, None)), } } #[inline] pub fn peek<I: Input>(input: &mut I, token: I::Token) -> ParseResult<I, I::Token> { match input.peek() { Some(peeked) if peeked == token => Done(token), t@Some(_) | t@None => error("eat", Expected::Token(Some(token), t)), } } #[inline] pub fn peek_if<I: Input, F>(input: &mut I, cond: F) -> ParseResult<I, I::Token> where F: Fn(I::Token) -> bool { match input.peek() { Some(peeked) if cond(peeked) => Done(peeked), t@Some(_) | t@None => error("peek_id", Expected::Token(None, t)), } } #[inline] pub fn peek_slice<I: Input>(input: &mut I, slice: I::Slice) -> ParseResult<I, I::Slice> { match input.peek_slice(slice) { Some(peeked) if peeked == slice => Done(slice), t@Some(_) | t@None => error("peek_slice", Expected::Slice(Some(slice), t)), } } #[inline] pub fn skip_while<I: Input, F>(input: &mut I, condition: F) -> ParseResult<I, ()> where F: FnMut(I::Token) -> bool { input.skip_many(condition); Done(()) } #[inline] pub fn take_some_while<I: Input, F>(input: &mut I, condition: F) -> ParseResult<I, I::Many> where F: FnMut(I::Token) -> bool { let value = input.take_many(condition); if value.len() == 0 { return error("take_some_while", Expected::Token(None, None)); } Done(value) } #[inline(always)] pub fn take_while<I: Input, F>(input: &mut I, condition: F) -> ParseResult<I, I::Many> where F: FnMut(I::Token) -> bool { Done(input.take_many(condition)) } #[inline] pub fn delimited<I: Input, F>(input: &mut I, start: I::Token, mut cond: F, end: I::Token) -> ParseResult<I, I::Many> where F: FnMut(I::Token) -> bool { if let Error(mut e) = eat(input, start) { e.parser = "delimited"; return Error(e); } let output = match take_some_while(input, |c| c != end && cond(c)) { Done(output) => output, Error(mut e) => { e.parser = "delimited"; return Error(e); } }; if let Error(mut e) = eat(input, end) { e.parser = "delimited"; return Error(e); } Done(output) } #[inline(always)] pub fn eof<I: Input>(input: &mut I) -> ParseResult<I, ()> { if input.is_empty() { Done(()) } else { error("eof", Expected::EOF) } }