pub mod macros;

#[cfg(test)]
mod tests {
    use core::panic;

    use crate::combinators::common::characters::Char;
    use crate::parser::Parser;

    use crate::{parser};

    #[test]
    fn test_parser() -> Result<(), String> {        
        let p = Char {c: 'h'};
        match parser!((p)).parse("he") {
            Ok(c) => assert_eq!(c, 'h'),
            Err(e) => panic!("{}", e)
        }

        match parser!(p).parse("he") {
            Ok(c) => assert_eq!(c, 'h'),
            Err(e) => panic!("{}", e)
        }

        match parser!(((p))).parse("he") {
            Ok(c) => assert_eq!(c, 'h'),
            Err(e) => panic!("{}", e)
        }

        Ok(())
    }


    #[test]
    fn test_pure() -> Result<(), String>{

        match parser!(pure 1).parse("c") {
            Ok(_c) => (),
            Err(e) => panic!("{}", e) 
        }

        // match parser!(pure (1 + 1)).parse("c") {
        //     Ok(_c) => (),
        //     Err(e) => panic!("{}", e) 
        // }

        match parser!((pure (false))).parse("c") {
            Ok(_c) => (),
            Err(e) => panic!("{}", e) 
        }

        match parser!((pure 'l')).parse("c") {
            Ok(_c) => (),
            Err(e) => panic!("{}", e) 
        }

        match parser!(pure 1).parse("c") {
            Ok(_c) => Ok(()),
            Err(e) => panic!("{}", e) 
        }
    }
    
    #[test]
    fn test_empty() -> Result<(), String>{

        match parser!(empty).parse("c") {
            Ok(e) => panic!("{}", e),
            Err(_s) => ()
        }

        match parser!(empty <* empty).parse("c") {
            Ok(e) => panic!("{}", e),
            Err(_s) => Ok(())
        }
    }

    // #[test]
    // fn test_sat() -> Result<(), String>{

    //     let f = |c:char| c == 'k';
    //     match parser!((sat (f))).parse("kb") {
    //         Ok(c) => assert_eq!(c, 'k'),
    //         Err(e) => panic!("{}", e)
    //     }

    //     match parser!((sat f)).parse("kb") {
    //         Ok(c) => assert_eq!(c, 'k'),
    //         Err(e) => panic!("{}", e)
    //     }

    //     match parser!(sat (f)).parse("kb") {
    //         Ok(c) => assert_eq!(c, 'k'),
    //         Err(e) => panic!("{}", e)
    //     }

    //     let f = |c:char| c == 'k';
    //     match parser!(sat f).parse("kb") {
    //         Ok(c) => {
    //             assert_eq!(c, 'k');
    //             Ok(())
    //         },
    //         Err(e) => panic!("{}", e)
    //     }
    // }


    #[test]
    fn test_char() -> Result<(), String> {
        match parser!('h').parse("he") {
            Ok(c) => assert_eq!(c, 'h'),
            Err(e) => panic!("{}", e)
        }

        match parser!(('h')).parse("he") {
            Ok(c) => assert_eq!(c, 'h'),
            Err(e) => panic!("{}", e)
        }
        
        let p = Char {c: 'h'};
        match parser!((p)).parse("he") {
            Ok(c) => assert_eq!(c, 'h'),
            Err(e) => panic!("{}", e)
        }
        
        Ok(())
    }

    #[test]
    fn test_zip_base() -> Result<(), String>{

        // Only literals
        match parser!('c'<~>'x').parse("cx") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!(('c'<~>'x')).parse("cx") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        // Only idents
        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!(p <~> p2).parse("cx") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!((p) <~> p2).parse("cx") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!(p <~> (p2)).parse("cx") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!((p) <~> (p2)).parse("cx") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
            },
            Err(e) => panic!("{}", e)
        }


        // Combining idents and literals

        let p = Char {c: 'x'};

        match parser!('c' <~> p).parse("cx") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p <~>'x').parse("cx") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
            },
            Err(e) => panic!("{}", e)
        }


        let p = Char {c: 'c'};

        match parser!((p)<~>'x').parse("cx") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'x'};

        match parser!('c'<~>(p)).parse("cx") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                Ok(())
            },
            Err(e) => panic!("{}", e)
        }
        
    }

    #[test]
    fn test_zip_recursive() -> Result<(), String>{

        match parser!(('c'<~>'x') <~> 'k').parse("cxk") {
            Ok(((c1, c2), c3)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'k'};

        match parser!(('c'<~>'x') <~> p).parse("cxk") {
            Ok(((c1, c2), c3)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!('c'<~> ('x' <~> 'k')).parse("cxk") {
            Ok((c1, (c2, c3))) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p <~> ('x' <~> 'k')).parse("cxk") {
            Ok((c1, (c2, c3))) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p <~> ('x' <~> ('k' <~> 'p'))).parse("cxkp") {
            Ok((c1, (c2, (c3, c4)))) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
                assert_eq!(c4, 'p');
            },
            Err(e) => panic!("{}", e)
        }

        // Right associativity

        match parser!('c' <~> 'x' <~> ('k')).parse("cxk") {
            Ok(((c1, c2), c3)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!('c' <~> 'x' <~> 'k').parse("cxk") {
            Ok(((c1, c2), c3)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p <~> 'x' <~> 'k').parse("cxk") {
            Ok(((c1, c2), c3)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'x'};

        match parser!('c' <~> p <~> 'k').parse("cxk") {
            Ok(((c1, c2), c3)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!(p <~> p2 <~> 'k').parse("cxk") {
            Ok(((c1, c2), c3)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!('c' <~> ('x' <~> 'k') <~> 'r').parse("cxkr") {
            Ok(((c1, (c2, c3)), c4)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
                assert_eq!(c4, 'r');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!(('c' <~> 'x') <~> 'k' <~> 'r').parse("cxkr") {
            Ok((((c1, c2),  c3), c4)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
                assert_eq!(c4, 'r');
            },
            Err(e) => panic!("{}", e)
        }


        match parser!(('c' <~> 'x') <~> ('k' <~> 'r') <~> 'm').parse("cxkrm") {
            Ok((((c1, c2),  (c3, c4)), c5)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
                assert_eq!(c4, 'r');
                assert_eq!(c5, 'm');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!(('c' <~> 'x') <~> ('k' <~> 'r') <~> 'm').parse("cxkrm") {
            Ok((((c1, c2),  (c3, c4)), c5)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
                assert_eq!(c4, 'r');
                assert_eq!(c5, 'm');
            },
            Err(e) => panic!("{}", e)
        }

        // Not fully covered, but essentially right associativity with idents

        let p = Char {c:'k'};
        match parser!(('c' <~> 'x') <~> p <~> 'm').parse("cxkm") {
            Ok((((c1, c2),  c3), c4)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'x');
                assert_eq!(c3, 'k');
                assert_eq!(c4, 'm');
            },
            Err(e) => panic!("{}", e)
        }

        Ok(())

    }


    #[test]
    fn test_alternative_base() -> Result<(), String>{

        // Only literals
        match parser!('c'<|>'x').parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!(('c'<|>'x')).parse("x") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        // Only idents
        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!(p <|> p2).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!((p) <|> p2).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!(p <|> (p2)).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!(p <|> (p2)).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!((p) <|> (p2)).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'c');
            },
            Err(e) => panic!("{}", e)
        }



        // Combining idents and literals

        let p = Char {c: 'x'};

        match parser!('k' <|> p).parse("x") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p <|>'x').parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'c');
            },
            Err(e) => panic!("{}", e)
        }


        let p = Char {c: 'c'};

        match parser!((p)<|>'x').parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'x'};

        match parser!('c'<|>(p)).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'c');
                Ok(())
            },
            Err(e) => panic!("{}", e)
        }
        
    }

    #[test]
    fn test_alternative_recursive() -> Result<(), String>{

        match parser!(('c'<|>'x') <|> 'k').parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'k'};

        match parser!(('c'<|>'x') <|> p).parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!('c'<|> ('x' <|> 'k')).parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p <|> ('x' <|> 'k')).parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p <|> ('x' <|> ('k' <|> 'p'))).parse("cxkp") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        // Right associativity

        match parser!('c' <|> 'x' <|> ('k')).parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!('c' <|> 'x' <|> 'k').parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p <|> 'x' <|> 'k').parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'x'};

        match parser!('c' <|> p <|> 'k').parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!(p <|> p2 <|> 'k').parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!('c' <|> ('x' <|> 'k') <|> 'r').parse("cxkr") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!(('c' <|> 'x') <|> 'k' <|> 'r').parse("cxkr") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }


        match parser!(('c' <|> 'x') <|> ('k' <|> 'r') <|> 'm').parse("cxkrm") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!(('c' <|> 'x') <|> ('k' <|> 'r') <|> 'm').parse("cxkrm") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        // Not fully covered, but essentially right associativity with idents

        let p = Char {c:'k'};
        match parser!(('c' <|> 'x') <|> p <|> 'm').parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        Ok(())

    }

    #[test]
    fn test_alternative_zip() -> Result<(), String> {

        let p = parser!('a' <~> 'c');

        match parser!(p <|> 'b' <~> 'c').parse("ac") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'a');
                assert_eq!(c2, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = parser!('a' <~> 'c');
        let p2 = Char {c: 'b'};

        match parser!(p <|> p2 <~> 'c').parse("bc") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'b');
                assert_eq!(c2, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = parser!('a' <~> 'c');

        match parser!(p <|> ('a' <|> 'b') <~> 'c').parse("bc") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'b');
                assert_eq!(c2, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'b'};

        match parser!(('a' <~> 'b') <|> p <~> 'c').parse("bc") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'b');
                assert_eq!(c2, 'c');
            },
            Err(e) => panic!("{}", e)
        }


        match parser!(('a' <~> 'b') <|> 'b' <~> 'c').parse("bc") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'b');
                assert_eq!(c2, 'c');
            },
            Err(e) => panic!("{}", e)
        }



        match parser!(('a' <~> 'b') <|> ('b' <|> 'k') <~> 'c').parse("bc") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'b');
                assert_eq!(c2, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        Ok(())
    }
    
    #[test]
    fn test_zip_alternative() -> Result<(), String> {

        let p = parser!('c' <~> 'd');
        match parser!('a' <~> 'b' <|> p).parse("ab") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'a');
                assert_eq!(c2, 'b');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'a'};
        let p2 = parser!('c' <~> 'd');
        match parser!(p <~> 'b' <|> p2).parse("cd") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'd');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'b'};
        let p2 = parser!('c' <~> 'd');
        match parser!('a' <~> p <|> p2).parse("ab") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'a');
                assert_eq!(c2, 'b');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'a'};
        let p2 = Char {c: 'b'};
        let p3 = parser!('c' <~> 'd');
        match parser!(p <~> p2 <|> p3).parse("ab") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'a');
                assert_eq!(c2, 'b');
            },
            Err(e) => panic!("{}", e)
        }

        let p = parser!('c' <~> 'd');
        match parser!('a' <~> ('a' <|> 'b') <|> p).parse("ab") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'a');
                assert_eq!(c2, 'b');
            },
            Err(e) => panic!("{}", e)
        }

        let p = parser!('c' <~> 'd');
        match parser!(('a' <|> 'b') <~> 'a' <|> p).parse("ba") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'b');
                assert_eq!(c2, 'a');
            },
            Err(e) => panic!("{}", e)
        }

        let p1 = parser!('a');
        let p2 = parser!('c' <~> 'd');
        match parser!(p1 <~> ('a' <|> 'b') <|> p2).parse("aa") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'a');
                assert_eq!(c2, 'a');
            },
            Err(e) => panic!("{}", e)
        }

        let p1 = parser!('a');
        let p2 = parser!('c' <~> 'd');
        match parser!(('a' <|> 'b') <~> p1 <|> p2).parse("ba") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'b');
                assert_eq!(c2, 'a');
            },
            Err(e) => panic!("{}", e)
        }


        let p3 = parser!('c' <~> 'd');
        match parser!(('a' <|> 'b') <~> ('b' <|> 'a') <|> p3).parse("ab") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'a');
                assert_eq!(c2, 'b');
            },
            Err(e) => panic!("{}", e)
        }

        Ok(())
    }
    
    #[test]
    fn test_thern_r_base() -> Result<(), String> {

        // Only literals
        match parser!('c'*>'x').parse("cx") {
            Ok(c1) => assert_eq!(c1, 'x'),
            Err(e) => panic!("{}", e)
        }

        match parser!(('c'*>'x')).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        // Only idents
        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!(p *> p2).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!((p) *> p2).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!(p *> (p2)).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!((p) *> (p2)).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }


        // Combining idents and literals

        let p = Char {c: 'x'};

        match parser!('k' *> p).parse("kx") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p *>'x').parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }


        let p = Char {c: 'c'};

        match parser!((p)*>'x').parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'x'};

        match parser!('c'*>(p)).parse("cx") {
            Ok(c1) => {
                assert_eq!(c1, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = parser!('c' <|> 'a');
        let p2 = parser!('c' <|> 'a');

        match parser!((p)*> p2).parse("ca") {
            Ok(c1) => {
                assert_eq!(c1, 'a');
                Ok(())
            },
            Err(e) => panic!("{}", e)
        }
        
    }

    #[test]
    fn test_then_r_recursive() -> Result<(), String>{

        match parser!(('c'*>'x') *> 'k').parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'k'};

        match parser!(('c'*>'x') *> p).parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!('c'*> ('x' *> 'k')).parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p *> ('x' *> 'k')).parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p *> ('x' *> ('k' *> 'p'))).parse("cxkp") {
            Ok(c) => {
                assert_eq!(c, 'p');
            },
            Err(e) => panic!("{}", e)
        }

        // Right associativity

        match parser!('c' *> 'x' *> ('k')).parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }
        
        

        match parser!('c' *> 'x' *> 'k').parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};

        match parser!(p *> 'x' *> 'k').parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'x'};

        match parser!('c' *> p *> 'k').parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};

        match parser!(p *> p2 *> 'k').parse("cxk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!('c' *> ('x' *> 'k') *> 'r').parse("cxkr") {
            Ok(c) => {
                assert_eq!(c, 'r');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!(('c' *> 'x') *> 'k' *> 'r').parse("cxkr") {
            Ok(c) => {
                assert_eq!(c, 'r');
            },
            Err(e) => panic!("{}", e)
        }


        match parser!(('c' *> 'x') *> ('k' *> 'r') *> 'm').parse("cxkrm") {
            Ok(c) => {
                assert_eq!(c, 'm');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!(('c' *> 'x') *> ('k' *> 'r') *> 'm').parse("cxkrm") {
            Ok(c) => {
                assert_eq!(c, 'm');
            },
            Err(e) => panic!("{}", e)
        }

        // Not fully covered, but essentially right associativity with idents

        let p = Char {c:'k'};
        match parser!(('c' *> 'x') *> p *> 'm').parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'm');
            },
            Err(e) => panic!("{}", e)
        }

        Ok(())
    }

    #[test]
    fn test_right_r_combine() -> Result<(), String> {

        let p = Char {c: 'k'};
        match parser!('c' *> 'x' <|> p).parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        match parser!(p *> 'x' <|> 'k').parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'x'};
        match parser!('c' *> p <|> 'k').parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'x');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};
        match parser!(p *> p2 <|> 'k').parse("km") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }


        match parser!(('c' *> 'k') *> ('x' *> 'b') <|> 'k').parse("k") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'k'};
        match parser!('c' *> 'x' <~> p).parse("cxkm") {
            Ok((c, c2)) => {
                assert_eq!(c, 'x');
                assert_eq!(c2, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        match parser!(p *> 'x' <~> 'k').parse("cxkm") {
            Ok((c, c2)) => {
                assert_eq!(c, 'x');
                assert_eq!(c2, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'x'};
        match parser!('c' *> p <~> 'k').parse("cxkm") {
            Ok((c, c2)) => {
                assert_eq!(c, 'x');
                assert_eq!(c2, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};
        match parser!(p *> p2 <~> 'k').parse("cxkm") {
            Ok((c, c2)) => {
                assert_eq!(c, 'x');
                assert_eq!(c2, 'k');
            },
            Err(e) => panic!("{}", e)
        }


        match parser!(('c' *> 'k') *> ('x' *> 'b') <~> 'k').parse("ckxbk") {
            Ok((c, c2)) => {
                assert_eq!(c, 'b');
                assert_eq!(c2, 'k');
            },
            Err(e) => panic!("{}", e)
        }
        Ok(())
    }

    #[test]
    fn test_combine_right_r() -> Result<(), String> {

        let p = Char {c: 'k'};
        match parser!('c' <|> 'x' *> p).parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        match parser!(p <|> 'x' *> 'k').parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'x'};
        match parser!('c' <|> p *> 'k').parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};
        match parser!(p <|> p2 *> 'k').parse("xk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!(('c' *> 'k') <|> ('x' *> 'b') *> 'k').parse("xbk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'k'};
        match parser!('c' <~> 'x' *> p).parse("cxkm") {
            Ok(c ) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        match parser!(p <~> 'x' *> 'k').parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'x'};
        match parser!('c' <~> p *> 'k').parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        let p2 = Char {c: 'x'};
        match parser!(p <~> p2 *> 'k').parse("cxkm") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }


        match parser!(('c' *> 'k') <~> ('x' *> 'b') *> 'k').parse("ckxbk") {
            Ok(c) => {
                assert_eq!(c, 'k');
            },
            Err(e) => panic!("{}", e)
        }
        Ok(())
    }


    #[test]
    fn test_left() -> Result<(), String>{
        let p = Char {c: 'c'};
        let p2 = Char {c: 'k'};
        let p3 = Char {c: 'l'};

        match parser!('c'<*'x').parse("cx") {
            Ok(c) => assert_eq!(c, 'c'),
            Err(e) => panic!("{}", e)
        }

        match parser!(p<*p2).parse("ck") {
            Ok(c) => assert_eq!(c, 'c'),
            Err(e) => panic!("{}", e)
        }

        match parser!('c' <* 'm').parse("cl") {
            Ok(_) => panic!("Didnt fail at m"),
            Err(_e) => ()
        }

        match parser!(('c' <* ('a' <* 'r'))).parse("car") {
            Ok(c) => assert_eq!(c, 'c'),
            Err(e) => panic!("{}", e)
        }

        match parser!(p3<*'x').parse("lx") {
            Ok(c) => {
                assert_eq!(c, 'l');
                Ok(())
            },
            Err(e) => panic!("{}", e)
        }        
    }


    #[test]
    fn test_right() -> Result<(), String>{
        let p = Char {c: 'c'};
        let p2 = Char {c: 'k'};
        let p3 = Char {c: 'l'};

        match parser!('c'*>'x').parse("cx") {
            Ok(c) => assert_eq!(c, 'x'),
            Err(e) => panic!("{}", e)
        }

        match parser!(p*>p2).parse("ck") {
            Ok(c) => assert_eq!(c, 'k'),
            Err(e) => panic!("{}", e)
        }

        match parser!('c' *> 'm').parse("cl") {
            Ok(_) => panic!("Didnt fail at m"),
            Err(_e) => ()
        }

        let p = Char {c: 'a'};
        match parser!('c' *> p *> 'r').parse("car") {
            Ok(c) => assert_eq!(c, 'r'),
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        match parser!((p *> 'a' *> 'r')).parse("car") {
            Ok(c) => assert_eq!(c, 'r'),
            Err(e) => panic!("{}", e)
        }

        match parser!(p3*>'x').parse("lx") {
            Ok(c) => {
                assert_eq!(c, 'x');
                Ok(())
            },
            Err(e) => panic!("{}", e)
        }
        
    }

    #[test]
    fn test_recursive_extra() -> Result<(), String> {

        match parser!(('a' *> 'b') <|> 'c').parse("ab") {
            Ok(c) => assert_eq!(c, 'b'),
            Err(e) => panic!("{}", e)
        }

        match parser!(('a' *> 'b') <|> 'c').parse("cb") {
            Ok(c) => assert_eq!(c, 'c'),
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        match parser!(('a' *> 'b') <|> p).parse("ab") {
            Ok(c) => assert_eq!(c, 'b'),
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'c'};
        match parser!(p *> 'b' <|> 'k').parse("cb") {
            Ok(c) => assert_eq!(c, 'b'),
            Err(e) => panic!("{}", e)
        }

        match parser!(('a' <|> 'b') <|> 'c').parse("b") {
            Ok(c) => assert_eq!(c, 'b'),
            Err(e) => panic!("{}", e)
        }

        match parser!(('a' <|> 'b') <|> 'c').parse("b") {
            Ok(c) => assert_eq!(c, 'b'),
            Err(e) => panic!("{}", e)
        }

        match parser!('a' <|> 'b' <|> 'c').parse("b") {
            Ok(c) => assert_eq!(c, 'b'),
            Err(e) => panic!("{}", e)
        }

        match parser!('a' <|> 'b' <|> ('c' <|> 'c')).parse("b") {
            Ok(c) => assert_eq!(c, 'b'),
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'a'};
        match parser!(p <|> 'b' <|> ('c' <|> 'c') <|> 'l').parse("b") {
            Ok(c) => assert_eq!(c, 'b'),
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'b'};
        match parser!('a' <|> p <|> ('c' <|> 'c')).parse("b") {
            Ok(c) => assert_eq!(c, 'b'),
            Err(e) => panic!("{}", e)
        }

        let p1 = Char {c: 'a'};
        let p2 = Char {c: 'b'};
        match parser!(p1 <|> p2 <|> ('c' <|> 'c')).parse("b") {
            Ok(c) => assert_eq!(c, 'b'),
            Err(e) => panic!("{}", e)
        }

        match parser!(('a' <~> 'b') <|> ('c' <~> 'c')).parse("cc") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!('a' <~> 'b' <|> ('c' <~> 'c')).parse("cc") {
            Ok((c1, c2)) => {
                assert_eq!(c1, 'c');
                assert_eq!(c2, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!(('a' *> 'b') *> 'c').parse("abc") {
            Ok(c) => assert_eq!(c, 'c'),
            Err(e) => panic!("{}", e)
        }

        let p = Char {c: 'a'};
        match parser!(p *> ('b' *> 'c')).parse("abc") {
            Ok(c) => assert_eq!(c, 'c'),
            Err(e) => panic!("{}", e)
        }

        match parser!(('a' <~> 'b') <~> 'c').parse("abc") {
            Ok(((a, b), c)) => {
                assert_eq!(a, 'a');
                assert_eq!(b, 'b');
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }

        match parser!('a' <~> ('b' <~> 'c')).parse("abc") {
            Ok((a, (b, c))) => {
                assert_eq!(a, 'a');
                assert_eq!(b, 'b');
                assert_eq!(c, 'c');
            },
            Err(e) => panic!("{}", e)
        }
        Ok(())
    }


    #[test]
    fn test_hello_world_brackets() -> Result<(), String> {
        let p = parser!(('h' *> (((('e' *> 'l') *> 'l') *> 'o') <|> 'i')) *> ((((' '*> ('w' *> 'o')) *> 'r') *> 'l') *>'d'));
        match p.parse("hello world") {
            Ok(_c) => (),
            Err(e) => panic!("{}", e) 
        }

        match p.parse("hi world") {
            Ok(_c) => (),
            Err(e) => panic!("{}", e) 
        }
        
        Ok(())
    }

    #[test]
    fn test_hello_world_non_brackets() -> Result<(), String> {

        let p = parser!('h' *> ('e' *> 'l' *> 'l' *> 'o' <|> 'i') *> ' ' *>'w' *> 'o' *> 'r' *> 'l' *> 'd');
        
        match p.parse("hello world") {
            Ok(_c) => (),
            Err(e) => panic!("{}", e) 
        }

        match p.parse("hi world") {
            Ok(_c) => (),
            Err(e) => panic!("{}", e) 
        }

        Ok(())
    }

    #[test]
    fn test_try() -> Result<(), String>{
        let p = Char {c: 'c'};
        match parser!(try(p)).parse("cb") {
            Ok(c) => assert_eq!(c, 'c'),
            Err(e) => panic!("{}", e)
        }

        match parser!(try 'c' *> try ('d' <|> 'c')).parse("cd") {
            Ok(c) => assert_eq!(c, 'd'),
            Err(e) => panic!("{}", e)
        }

        match parser!(try 'c').parse("cb") {
            Ok(c) => {
                assert_eq!(c, 'c');
                Ok(())
            },
            Err(e) => panic!("{}", e)
        }
    }

    // #[test]
    // fn test_map() -> Result<(), String>{
    //     let f = |_c: char| 'b';
    //     match parser!(map f 'c').parse("c"){
    //         Ok(c) => assert_eq!(c, 'b'),
    //         Err(e) => panic!("{}", e)
    //     }
    //     let p = Char {c: 'c'};
    //     match parser!((map f p)).parse("c"){
    //         Ok(c) => {
    //             assert_eq!(c, 'b');
    //             Ok(())
    //         },
    //         Err(e) => panic!("{}", e)
    //     }
    // }

}