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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use std::fmt::*;

/// The path followed when walking an AST.
///
/// Designed to be used both to quickly find out how to contextually handle
/// a specific node and for error-reporting.
///
/// ```
/// use binjs_shared::ast::Path;
///
/// let mut path = Path::new();
/// assert!(path.get(0).is_none());
///
/// // Once we have entered both an interface and a field, `path.get(0)` will be `Some`.
/// path.enter_interface("Interface 1");
/// assert!(path.get(0).is_none());
///
/// path.enter_field("Field 1");
///
/// {
///   let item = path.get(0).unwrap();
///   assert_eq!(item.field, "Field 1");
///   assert_eq!(item.interface, "Interface 1");
/// }
///
/// path.enter_interface("Interface 2");
/// path.enter_field("Field 2");
///
/// {
///   let item = path.get(0).unwrap();
///   assert_eq!(item.field, "Field 2");
///   assert_eq!(item.interface, "Interface 2");
/// }
/// {
///   let item = path.get(1).unwrap();
///   assert_eq!(item.field, "Field 1");
///   assert_eq!(item.interface, "Interface 1");
/// }
///
/// // We need to exit the field before exiting the interface.
/// path.exit_field("Field 2"); // Exiting the wrong field would panic.
/// path.exit_interface("Interface 2"); // Exiting the wrong interface would panic.
/// path.exit_field("Field 1"); // Exiting the wrong field would panic.
/// path.exit_interface("Interface 1"); // Exiting the wrong interface would panic.
/// ```

#[derive(Clone, Default, Deserialize, Serialize)]
pub struct Path<I, F>
where
    I: Debug,
    F: Debug,
{
    /// Some(foo) if we have entered interface foo but no field yet.
    /// Otherwise, None.
    interface: Option<I>,
    items: Vec<PathItem<I, F>>,
}
impl<I, F> From<Vec<PathItem<I, F>>> for Path<I, F>
where
    I: Debug,
    F: Debug,
{
    fn from(items: Vec<PathItem<I, F>>) -> Self {
        Path {
            interface: None,
            items,
        }
    }
}
impl<I, F> std::hash::Hash for Path<I, F>
where
    I: Debug + std::hash::Hash,
    F: Debug + std::hash::Hash,
{
    /// As we implement Borrow<[PathItem<...>] for Path, we must ensure that `Hash`
    /// gives the same result for a `Path` and its `[PathItem]` representation.
    fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
        self.items.as_slice().hash(hasher)
    }
}
impl<I, F> std::cmp::PartialEq for Path<I, F>
where
    I: Debug + PartialEq,
    F: Debug + PartialEq,
{
    /// As we implement Borrow<[PathItem<...>] for Path, we must ensure that `Eq`
    /// gives the same result for a `Eq` and its `[PathItem]` representation.
    fn eq(&self, other: &Self) -> bool {
        self.items == other.items
    }
}
impl<I, F> std::cmp::Eq for Path<I, F>
where
    I: Debug + Eq,
    F: Debug + Eq,
{
    // Nothing to do.
}
impl<I, F> std::fmt::Display for Path<I, F>
where
    I: Debug + Display,
    F: Debug + Display,
{
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result {
        self.items.fmt(formatter)
    }
}

#[derive(Debug, PartialEq, Eq, Hash, Clone, Deserialize, Serialize)]
pub struct PathItem<I, F>
where
    I: Debug,
    F: Debug,
{
    pub interface: I,
    pub field: F,
}
impl<I, F> PathItem<I, F>
where
    I: Debug,
    F: Debug,
{
    pub fn interface(&self) -> &I {
        &self.interface
    }
    pub fn field(&self) -> &F {
        &self.field
    }
}

impl<I, F> Debug for Path<I, F>
where
    I: Debug,
    F: Debug,
{
    fn fmt(&self, f: &mut Formatter) -> Result {
        use itertools::Itertools;
        write!(
            f,
            "[{items}{more}]",
            items = self
                .items
                .iter()
                .map(|item| format!("{:?}.{:?}", item.interface, item.field))
                .format(" > "),
            more = if let Some(ref interface) = self.interface {
                format!(" > {:?}", interface)
            } else {
                "".to_string()
            }
        )
    }
}
impl<I, F> Path<I, F>
where
    I: Debug + PartialEq,
    F: Debug + PartialEq,
{
    /// Create an empty `Path`.
    pub fn new() -> Self {
        Self {
            interface: None,
            items: vec![],
        }
    }

    pub fn extend_from_slice(&mut self, slice: &[PathItem<I, F>])
    where
        I: Clone,
        F: Clone,
    {
        self.items.extend_from_slice(slice)
    }

    /// Create an empty `Path`, initialized to hold up
    /// to `capacity` elements without resize.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            interface: None,
            items: Vec::with_capacity(capacity),
        }
    }

    /// Enter an interface.
    ///
    /// All calls to `enter_interface` MUST be balanced with calls
    /// to `exit_interface`.
    pub fn enter_interface(&mut self, node: I) {
        debug!(target: "path", "enter_interface: {:?}", node);
        debug_assert!(
            self.interface.is_none(),
            "We shouldn't have a pending interface, got {:?}",
            self.interface
        );
        self.interface = Some(node);
    }
    pub fn exit_interface(&mut self, node: I) {
        debug!(target: "path", "exit_interface: {:?}", node);
        let interface = self
            .interface
            .take()
            .expect("Could not exit_interface if we're not in an interface");
        debug_assert!(node == interface);
    }
    pub fn enter_field(&mut self, field: F) {
        debug!(target: "path", "enter_field: {:?} at {:?}", field, self.interface);
        let interface = self.interface.take().unwrap();
        self.items.push(PathItem { interface, field });
    }
    pub fn exit_field(&mut self, field: F) {
        debug!(target: "path", "exit_field: {:?}", field);
        debug_assert!(
            self.interface.is_none(),
            "We shouldn't have a pending interface, got {:?}",
            self.interface
        );
        let PathItem {
            interface,
            field: prev,
        } = self
            .items
            .pop()
            .expect("Could not exit_field from an empty ASTath");
        debug_assert!(prev == field);
        self.interface = Some(interface);
    }
    pub fn len(&self) -> usize {
        self.items.len()
    }

    pub fn get(&self, index: usize) -> Option<&PathItem<I, F>> {
        if index >= self.len() {
            return None;
        }
        Some(&self.items[self.len() - index - 1])
    }

    /// Return the last `len` elements of the path, in
    /// the order in which they appear in the path
    /// (current element is last).
    ///
    /// If there are fewer than `len` elements, return
    /// as many elements as possible.
    pub fn tail(&self, len: usize) -> &[PathItem<I, F>] {
        if len < self.len() {
            &self.items[self.len() - len..]
        } else {
            &self.items
        }
    }

    /// Iter through the path, from the root to the current position.
    pub fn iter(&self) -> impl Iterator<Item = &PathItem<I, F>> {
        self.items.iter()
    }
}

impl<I, F> std::borrow::Borrow<[PathItem<I, F>]> for Path<I, F>
where
    I: Debug + PartialEq,
    F: Debug + PartialEq,
{
    fn borrow(&self) -> &[PathItem<I, F>] {
        &self.items
    }
}

/// The root type for nodes in the AST.
pub trait Node: downcast_rs::Downcast {
    /// Return the name of the current node.
    fn name(&self) -> &'static str;

    /// If this node should cause a dictionary change, return the name of the new dictionary
    /// to use. Otherwise, None.
    fn scoped_dictionary(&self) -> Option<&::SharedString> {
        None
    }
}
impl_downcast!(Node);