SHARE
TWEET

Untitled

a guest Jun 25th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #[derive(PartialEq, Eq)]
  2. pub enum Match<T, E> {
  3.     Nothing,
  4.     Next(T),
  5.     Error(E),
  6. }
  7.  
  8. pub trait ParseStream<T> {
  9.     type Item;
  10.     type Error;
  11.    
  12.     fn feed(&mut self, _: T) -> Match<Self::Item, Self::Error>;
  13.    
  14.     fn followed_by<P>(self, p: P) -> FollowedBy<Self, P>
  15.     where Self: Sized, P: ParseStream<T, Error=Self::Error> {
  16.         FollowedBy{ phase: FollowingPhase::A, pa: self, pb: p }
  17.     }
  18. }
  19.  
  20. enum FollowingPhase { A, B, Done }
  21.  
  22. pub struct FollowedBy<A, B> {
  23.     phase: FollowingPhase,
  24.     pa: A,
  25.     pb: B,
  26. }
  27.  
  28. impl<A, B, T> ParseStream<T> for FollowedBy<A, B>
  29. where A: ParseStream<T>, B: ParseStream<T, Error=A::Error> {
  30.     type Item = B::Item;
  31.     type Error = B::Error;
  32.    
  33.     fn feed(&mut self, x: T) -> Match<Self::Item, Self::Error> {
  34.         use FollowingPhase::*;
  35.         match self.phase {
  36.             A => {
  37.                 match self.pa.feed(x) {
  38.                     Match::Nothing => Match::Nothing,
  39.                     Match::Next(_) => {
  40.                         self.phase = B;
  41.                         Match::Nothing
  42.                     },
  43.                     Match::Error(e) => {
  44.                         self.phase = Done;
  45.                         Match::Error(e)
  46.                     }
  47.                 }
  48.             },
  49.             B => {
  50.                 match self.pb.feed(x) {
  51.                     Match::Nothing => Match::Nothing,
  52.                     Match::Next(y) => {
  53.                         self.phase = Done;
  54.                         Match::Next(y)
  55.                     },
  56.                     Match::Error(e) => {
  57.                         self.phase = Done;
  58.                         Match::Error(e)
  59.                     }
  60.                 }
  61.                
  62.             },
  63.             Done => {
  64.                 Match::Nothing
  65.             },
  66.         }
  67.     }
  68. }
  69.  
  70.  
  71. //----------------------------------------------------------------------
  72. pub struct LiteralByte(pub u8);
  73.  
  74. impl ParseStream<u8> for LiteralByte {
  75.     type Item = ();
  76.     type Error = String;
  77.    
  78.     fn feed(&mut self, b: u8) -> Match<Self::Item, Self::Error> {
  79.         if b == self.0 { Match::Next(()) }
  80.         else           { Match::Error("Not the right byte".to_string()) }
  81.     }
  82. }
  83.  
  84. pub struct AnyByte;
  85.  
  86. impl ParseStream<u8> for AnyByte {
  87.     type Item = u8;
  88.     type Error = String;
  89.    
  90.     fn feed(&mut self, b: u8) -> Match<Self::Item, Self::Error> {
  91.         Match::Next(b)
  92.     }
  93. }
  94.  
  95. fn main() {
  96.     let mut p = LiteralByte(0xf5_u8).followed_by(AnyByte);
  97.    
  98.     assert!(p.feed(0xf5_u8) == Match::Nothing);
  99.     assert!(p.feed(0x01_u8) == Match::Next(0x01_u8));
  100.     assert!(p.feed(0x00_u8) == Match::Nothing);
  101.    
  102.  
  103.     println!("hello");
  104. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top