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
mod parsers;
mod std_impls;
pub use parsers::*;
pub use sexpy_derive::Sexpy;

// List of all the parsers used by the derive function so that automatically
// deriving things works.
pub use nom::{
    branch::alt,
    bytes::complete::tag,
    character::complete::{
        alpha1, alphanumeric0, digit1, multispace0, multispace1,
    },
    combinator::opt,
    error::{context, convert_error, VerboseError},
    multi::many0,
    sequence::{preceded, tuple},
    Err, IResult,
};

pub trait Sexpy {
    /// Takes a string and tries calling the parser for this trait on it.
    fn parse(input: &str) -> Result<Self, String>
    where
        Self: Sized,
    {
        match Self::sexp_parse(input) {
            Ok((_, x)) => Ok(x),
            Err(Err::Error(e)) => Err(convert_error(input, e)),
            Err(Err::Failure(e)) => Err(convert_error(input, e)),
            Err(Err::Incomplete(_)) => Err("Need more bytes to nom".to_string()),
        }
    }

    /// The core parsing function that should be defined for each trait.
    fn sexp_parse<'a>(
        input: &'a str,
    ) -> IResult<&'a str, Self, VerboseError<&'a str>>
    where
        Self: Sized;
}

#[cfg(test)]
mod tests {
    use crate::*;
    use sexpy_derive::Sexpy;

    #[test]
    fn simple_struct() {
        #[derive(Sexpy, Debug, PartialEq)]
        struct Portdef {
            name: String,
            width: u64,
        }

        let input = "(portdef foo 20)";
        let gold = Portdef {
            name: "foo".to_string(),
            width: 20,
        };
        assert_eq!(Portdef::parse(input), Ok(gold))
    }

    #[test]
    fn simple_struct_one_field() {
        #[derive(Sexpy, Debug, PartialEq)]
        struct Portdef {
            name: String,
        }

        let input = "(portdef foo)";
        let gold = Portdef {
            name: "foo".to_string(),
        };
        assert_eq!(Portdef::parse(input), Ok(gold))
    }

    #[test]
    fn simple_struct_no_fields() {
        #[derive(Sexpy, Debug, PartialEq)]
        struct Portdef {}

        assert_eq!(Portdef::parse("(portdef)"), Ok(Portdef {}));
        assert_eq!(Portdef::parse("(portdef   )"), Ok(Portdef {}));
        assert!(Portdef::parse("(portdef hi)").is_err());
    }

    #[test]
    fn struct_rename_head() {
        #[derive(Sexpy, Debug, PartialEq)]
        #[sexpy(head = "port")]
        struct Portdef {
            name: String,
            width: u64,
        }

        let input = "(port foo 32)";
        let gold = Portdef {
            name: "foo".to_string(),
            width: 32,
        };
        assert_eq!(Portdef::parse(input), Ok(gold))
    }

    #[test]
    fn enum_rename_head() {
        #[derive(Sexpy, Debug, PartialEq)]
        #[sexpy(head = "plt")]
        enum Plant {
            PalmTree(String, u64),
            Cactus,
        }

        assert_eq!(
            Plant::parse("(plt test 4)"),
            Ok(Plant::PalmTree("test".to_string(), 4))
        );
        assert_eq!(Plant::parse("(plt)"), Ok(Plant::Cactus));
    }

    #[test]
    fn unit_enum() {
        #[derive(Sexpy, Debug, PartialEq)]
        enum Plant {
            PalmTree,
            Cactus,
        }

        let input = "(plant)";
        assert_eq!(Plant::parse(input), Ok(Plant::PalmTree))
    }
}