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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use super::processors::ToString;
use crate::generator::ast::*;
use crate::generator::processor::*;
use crate::{Error, Operation, STATUS};
use std::fmt;

#[derive(Clone, PartialEq, Serialize)]
pub struct Node {
    pub attrs: Attrs,
    pub meta: Option<Meta>,
    pub children: Vec<Box<Node>>,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Meta {
    pub filename: Option<String>,
    pub lineno: Option<usize>,
}

impl fmt::Display for Node {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl fmt::Debug for Node {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}

impl PartialEq<AST> for Node {
    fn eq(&self, ast: &AST) -> bool {
        *self == ast.to_node()
    }
}

impl Node {
    pub fn new(attrs: Attrs) -> Node {
        Node {
            attrs: attrs,
            children: Vec::new(),
            meta: None,
        }
    }

    pub fn new_with_meta(attrs: Attrs, meta: Option<Meta>) -> Node {
        Node {
            attrs: attrs,
            children: Vec::new(),
            meta: meta,
        }
    }

    pub fn new_with_children(attrs: Attrs, children: Vec<Node>) -> Node {
        Node {
            attrs: attrs,
            children: children.into_iter().map(|n| Box::new(n)).collect(),
            meta: None,
        }
    }

    pub fn unwrap(&mut self) -> Option<Node> {
        match self.children.pop() {
            None => None,
            Some(n) => Some(*n),
        }
    }

    pub fn error(&self, error: Error) -> Result<()> {
        // Messaging may need to be slightly different for patgen
        if STATUS.operation() == Operation::GenerateFlow {
            let help = {
                if self.meta.is_some() && self.meta.as_ref().unwrap().filename.is_some() {
                    let mut m = self
                        .meta
                        .as_ref()
                        .unwrap()
                        .filename
                        .as_ref()
                        .unwrap()
                        .to_string();
                    if let Some(l) = self.meta.as_ref().unwrap().lineno {
                        m += &format!(":{}", l);
                    }
                    m
                } else {
                    if STATUS.is_debug_enabled() {
                        // Don't display children since it's potentially huge
                        let n = self.replace_children(vec![]);
                        format!("Sorry, no flow source information was found, here is the flow node that failed if it helps:\n{}", n)
                    } else {
                        "Run again with the --debug switch to try and trace this back to a flow source file location".to_string()
                    }
                }
            };
            error!("{}\n{}", error, &help)
        } else {
            Err(error)
        }
    }

    /// Returns a new node which is the output of the node processed by the given processor.
    /// Returning None means that the processor has decided that the node should be removed
    /// from the next stage AST.
    pub fn process(&self, processor: &mut dyn Processor) -> Result<Option<Node>> {
        let r = processor.on_node(&self)?;
        self.process_return_code(r, processor)
    }

    fn inline(nodes: Vec<Box<Node>>) -> Node {
        Node {
            attrs: Attrs::_Inline,
            meta: None,
            children: nodes,
        }
    }

    pub fn to_string(&self) -> String {
        ToString::run(self)
    }

    /// Serializes the AST for import into Python
    pub fn to_pickle(&self) -> Vec<u8> {
        serde_pickle::to_vec(self, true).unwrap()
    }

    pub fn add_child(&mut self, node: Node) {
        self.children.push(Box::new(node));
    }

    pub fn add_children(&mut self, nodes: Vec<Node>) -> &Self {
        for n in nodes {
            self.add_child(n);
        }
        self
    }

    pub fn insert_child(&mut self, node: Node, offset: usize) -> Result<()> {
        let len = self.children.len();
        if offset > len {
            return Err(Error::new(&format!(
                "An offset of {} was given to insert a child into a node with only {} children",
                offset, len
            )));
        }
        let index = self.children.len() - offset;
        self.children.insert(index, Box::new(node));
        Ok(())
    }

    /// Replace the child n - offset with the given node, use offset = 0 to
    /// replace the last child that was pushed.
    /// Fails if the node has no children or if the given offset is
    /// otherwise out of range.
    pub fn replace_child(&mut self, node: Node, offset: usize) -> Result<()> {
        let len = self.children.len();
        if len == 0 {
            return Err(Error::new(
                "Attempted to replace a child in a node with no children",
            ));
        } else if offset > len - 1 {
            return Err(Error::new(&format!(
                "An offset of {} was given to replace a child in a node with only {} children",
                offset, len
            )));
        }
        let index = self.children.len() - 1 - offset;
        self.children.remove(index);
        self.children.insert(index, Box::new(node));
        Ok(())
    }

    /// Returns a copy of child n - offset, an offset of 0 means
    /// the last child that was pushed.
    /// Fails if the node has no children or if the given offset is
    /// otherwise out of range.
    pub fn get_child(&self, offset: usize) -> Result<Node> {
        let len = self.children.len();
        if len == 0 {
            return Err(Error::new(
                "Attempted to get a child in a node with no children",
            ));
        } else if offset > len - 1 {
            return Err(Error::new(&format!(
                "An offset of {} was given to get a child in a node with only {} children",
                offset, len
            )));
        }
        let index = self.children.len() - 1 - offset;
        Ok(*self.children[index].clone())
    }

    pub fn depth(&self) -> usize {
        let mut depth = 0;
        for n in self.children.iter() {
            depth += n.depth();
        }
        depth
    }

    pub fn get_descendant(&self, offset: usize, depth: &mut usize) -> Option<Node> {
        for n in self.children.iter().rev() {
            if let Some(node) = n.get_descendant(offset, depth) {
                return Some(node);
            }
        }
        if offset == *depth {
            Some(self.clone())
        } else {
            *depth += 1;
            None
        }
    }

    pub fn process_return_code(
        &self,
        code: Return,
        processor: &mut dyn Processor,
    ) -> Result<Option<Node>> {
        match code {
            Return::None => Ok(None),
            Return::ProcessChildren => Ok(Some(self.process_and_update_children(processor)?)),
            Return::Unmodified => Ok(Some(self.clone())),
            Return::Replace(node) => Ok(Some(node)),
            // We can't return multiple nodes from this function, so we return them
            // wrapped in a meta-node and the process_children method will identify
            // this and remove the wrapper to inline the contained nodes.
            Return::Unwrap => Ok(Some(Node::inline(self.children.clone()))),
            Return::Inline(nodes) => Ok(Some(Node::inline(
                nodes.into_iter().map(|n| Box::new(n)).collect(),
            ))),
            Return::InlineBoxed(nodes) => Ok(Some(Node::inline(nodes))),
            Return::UnwrapWithProcessedChildren => {
                let nodes = self.process_children(processor)?;
                Ok(Some(Node::inline(
                    nodes.into_iter().map(|n| Box::new(n)).collect(),
                )))
            }
            Return::InlineWithProcessedChildren(mut nodes) => {
                nodes.append(&mut self.process_children(processor)?);
                Ok(Some(Node::inline(
                    nodes.into_iter().map(|n| Box::new(n)).collect(),
                )))
            }
            Return::ReplaceChildren(nodes) => {
                let new_node = self.replace_unboxed_children(nodes);
                Ok(Some(new_node))
            }
        }
    }

    /// Returns a new node which is a copy of self with its children replaced
    /// by their processed counterparts.
    pub fn process_and_update_children(&self, processor: &mut dyn Processor) -> Result<Node> {
        if self.children.len() == 0 {
            return Ok(self.clone());
        }
        Ok(self.replace_children(self.process_and_box_children(processor)?))
    }

    /// Returns processed versions of the node's children, each wrapped in a Box
    pub fn process_and_box_children(
        &self,
        processor: &mut dyn Processor,
    ) -> Result<Vec<Box<Node>>> {
        let mut nodes: Vec<Box<Node>> = Vec::new();
        for child in &self.children {
            if let Some(node) = child.process(processor)? {
                if let Attrs::_Inline = node.attrs {
                    for c in node.children {
                        nodes.push(c);
                    }
                } else {
                    nodes.push(Box::new(node));
                }
            }
        }
        // Call the end of block handler, giving the processor a chance to do any
        // internal clean up or inject some more nodes at the end
        let r = processor.on_end_of_block(&self)?;
        if let Some(node) = self.process_return_code(r, processor)? {
            if let Attrs::_Inline = node.attrs {
                for c in node.children {
                    nodes.push(c);
                }
            } else {
                nodes.push(Box::new(node));
            }
        }
        Ok(nodes)
    }

    /// Returns processed versions of the node's children
    pub fn process_children(&self, processor: &mut dyn Processor) -> Result<Vec<Node>> {
        let mut nodes: Vec<Node> = Vec::new();
        for child in &self.children {
            if let Some(node) = child.process(processor)? {
                if let Attrs::_Inline = node.attrs {
                    for c in node.children {
                        nodes.push(*c);
                    }
                } else {
                    nodes.push(node);
                }
            }
        }
        // Call the end of block handler, giving the processor a chance to do any
        // internal clean up or inject some more nodes at the end
        let r = processor.on_end_of_block(&self)?;
        if let Some(node) = self.process_return_code(r, processor)? {
            if let Attrs::_Inline = node.attrs {
                for c in node.children {
                    nodes.push(*c);
                }
            } else {
                nodes.push(node);
            }
        }
        Ok(nodes)
    }

    /// Returns a new node which is a copy of self with its children replaced
    /// by the given collection of nodes.
    pub fn replace_children(&self, nodes: Vec<Box<Node>>) -> Node {
        let new_node = Node {
            attrs: self.attrs.clone(),
            meta: self.meta.clone(),
            children: nodes,
        };
        new_node
    }

    /// Returns a new node which is a copy of self with its children replaced
    /// by the given collection of nodes.
    pub fn replace_unboxed_children(&self, nodes: Vec<Node>) -> Node {
        let new_node = Node {
            attrs: self.attrs.clone(),
            meta: self.meta.clone(),
            children: nodes.into_iter().map(|n| Box::new(n)).collect(),
        };
        new_node
    }

    /// Returns a new node which is a copy of self with its attrs replaced
    /// by the given attrs.
    pub fn replace_attrs(&self, attrs: Attrs) -> Node {
        let new_node = Node {
            attrs: attrs,
            meta: self.meta.clone(),
            children: self.children.clone(),
        };
        new_node
    }
}