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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::collections::HashSet;
use std::str::FromStr;

use crate::ast::IdentType;
use crate::error::SyntaxError;
use crate::syntax::pest::iterators::Pair;
use crate::syntax::Rule;

/// A string cache to recycle memory for shared values.
#[derive(Debug, Default)]
pub struct Cache {
    #[cfg(feature = "threading")]
    cache: std::sync::RwLock<HashSet<IdentType>>,
    #[cfg(not(feature = "threading"))]
    cache: std::cell::RefCell<HashSet<IdentType>>,
}

impl Cache {
    /// Create a new string cache.
    pub fn new() -> Self {
        Self::default()
    }

    /// Obtain a new `IndentType` for this string, or return the cached one.
    pub fn intern(&self, s: &str) -> IdentType {
        #[cfg(feature = "threading")]
        {
            // read-only if the string was already interned
            let readable = self.cache.read().expect("failed to acquire lock");
            if let Some(interned) = readable.get(s) {
                return interned.clone();
            }
            drop(readable);
            // write access if the string was not interned
            let new = IdentType::from(s);
            let mut writable = self.cache.write().expect("failed to acquire lock");
            writable.insert(new.clone());
            new
        }
        #[cfg(not(feature = "threading"))]
        {
            // read-only if the string was already interned
            let readable = self.cache.borrow();
            if let Some(interned) = readable.get(s) {
                return interned.clone();
            }
            drop(readable);
            // write access if the string was not interned
            let new = IdentType::from(s);
            let mut writable = self.cache.borrow_mut();
            writable.insert(new.clone());
            new
        }
    }
}

impl Clone for Cache {
    #[cfg(feature = "threading")]
    fn clone(&self) -> Self {
        let set = self.cache.read().expect("failed to acquire lock").clone();
        Cache {
            cache: std::sync::RwLock::new(set),
        }
    }
    #[cfg(not(feature = "threading"))]
    fn clone(&self) -> Self {
        Cache {
            cache: self.cache.clone(),
        }
    }
}

/// A trait for structures that can be parsed from a [`pest::Pair`].
///
/// [`pest::Pair`]: https://docs.rs/pest/latest/pest/iterators/struct.Pair.html
pub trait FromPair<'i>: Sized {
    /// The production rule the pair is expected to be obtained from.
    const RULE: Rule;

    /// Create a new instance from a `Pair` without checking the rule.
    ///
    /// # Safety
    /// May panic if the pair was not produced by the right rule, i.e.
    /// `pair.as_rule() != <Self as FromPair>::RULE`.
    unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>, cache: &Cache)
        -> Result<Self, SyntaxError>;

    /// Create a new instance from a `Pair`.
    #[inline]
    fn from_pair(pair: Pair<'i, Rule>, cache: &Cache) -> Result<Self, SyntaxError> {
        if pair.as_rule() != Self::RULE {
            Err(SyntaxError::UnexpectedRule {
                actual: pair.as_rule(),
                expected: Self::RULE,
            })
        } else {
            unsafe { Self::from_pair_unchecked(pair, cache) }
        }
    }
}

impl<'i> FromPair<'i> for bool {
    const RULE: Rule = Rule::Boolean;
    unsafe fn from_pair_unchecked(
        pair: Pair<'i, Rule>,
        _cache: &Cache,
    ) -> Result<Self, SyntaxError> {
        Ok(bool::from_str(pair.as_str()).expect("cannot fail."))
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use crate::ast::*;
    use crate::syntax::Lexer;

    mod boolean {

        use super::*;

        #[test]
        fn from_pair() {
            let cache = Cache::default();

            let pairs = Lexer::tokenize(Rule::Boolean, "true");
            let pair = pairs.unwrap().next().unwrap();
            assert_eq!(bool::from_pair(pair, &cache).unwrap(), true);

            let pairs = Lexer::tokenize(Rule::Boolean, "false");
            let pair = pairs.unwrap().next().unwrap();
            assert_eq!(bool::from_pair(pair, &cache).unwrap(), false);
        }
    }

    #[test]
    fn unexpected_rule() {
        let cache = Cache::default();

        let pairs = Lexer::tokenize(Rule::Boolean, "true");
        let pair = pairs.unwrap().next().unwrap();

        let err = Ident::from_pair(pair, &cache).unwrap_err();
        match err {
            SyntaxError::UnexpectedRule {
                ref actual,
                ref expected,
            } => {
                assert_eq!(actual, &Rule::Boolean);
                assert_eq!(expected, &Rule::Id);
            }
            e => panic!("unexpected error: {:?}", e),
        }
    }
}