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
use std::iter::FromIterator;
use std::ops::Index;

use super::SExp::{self, Atom, Null, Pair};

/// An iterator over an S-Expression. Returns list elements until the end of a chain of pairs.
pub struct SExpIterator {
    exp: SExp,
}

impl Iterator for SExpIterator {
    type Item = SExp;

    fn next(&mut self) -> Option<Self::Item> {
        match self.exp.to_owned() {
            Pair { head, tail } => {
                self.exp = *tail;
                Some(*head)
            }
            a @ Atom(_) => {
                self.exp = Null;
                Some(a)
            }
            _ => None,
        }
    }
}

impl IntoIterator for SExp {
    type Item = Self;
    type IntoIter = SExpIterator;

    fn into_iter(self) -> Self::IntoIter {
        SExpIterator { exp: self }
    }
}

pub struct SExpRefIterator<'a> {
    exp: &'a SExp,
}

impl<'a> Iterator for SExpRefIterator<'a> {
    type Item = &'a SExp;

    fn next(&mut self) -> Option<Self::Item> {
        match self.exp {
            Pair { head, tail } => {
                self.exp = &*tail;
                Some(&*head)
            }
            a @ Atom(_) => {
                self.exp = &Null;
                Some(&a)
            }
            Null => None,
        }
    }
}

impl SExp {
    /// Iterate over an S-Expression, by reference.
    ///
    /// # Example
    /// ```
    /// use parsley::prelude::*;
    /// assert_eq!(
    ///     sexp![()].iter().next().unwrap(),
    ///     &SExp::Null
    /// );
    /// ```
    pub fn iter(&self) -> SExpRefIterator {
        SExpRefIterator { exp: &self }
    }

    /// Easy way to check for `Null` if you're planning on iterating
    pub fn is_empty(&self) -> bool {
        if let Null = self {
            true
        } else {
            false
        }
    }

    /// Get the length of an S-Expression (vector or list)
    ///
    /// # Example
    /// ```
    /// use parsley::prelude::*;
    /// assert_eq!(
    ///     sexp!['a', "bee", SExp::sym("sea")].len(),
    ///     3
    /// );
    /// ```
    pub fn len(&self) -> usize {
        self.iter().count()
    }
}

impl Index<usize> for SExp {
    type Output = Self;

    fn index(&self, index: usize) -> &Self::Output {
        self.iter().nth(index).unwrap()
    }
}

impl FromIterator<SExp> for SExp {
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = SExp>,
    {
        let mut exp_out = Null;
        let mut last = &mut exp_out;

        for exp in iter {
            let new_val = Pair {
                head: Box::new(exp),
                tail: Box::new(Null),
            };

            match last {
                Null => {
                    *last = new_val;
                }
                Pair { ref mut tail, .. } => {
                    *tail = Box::new(new_val);
                    last = tail;
                }
                _ => (),
            }
        }

        exp_out
    }
}