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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Data information to build the AST
//! And some functions to work with AST
//!

use idata::cont::IVec;
use std::result::Result;

pub mod flat;

// -------------------------------------------------------------------------------------
//  T Y P E S

/// Context information about an error manipulanting the ast
/// You will have an String with the description, and the node
/// wich produced the error
/// It will have the error description and the string of node
/// info
#[derive(Debug, PartialEq)]
pub struct Error(pub String, pub Option<String>);

/// Helper to create an ast::Error
/// ```
///    use dynparser::ast;
///
///    let error_a = ast::error("testing", None);
///    let error_b = ast::Error("testing".to_string(), None);
///
///    assert!(error_a == error_b)
/// ```

pub fn error(desc: &str, ast_context: Option<&str>) -> Error {
    Error(
        desc.to_string(),
        ast_context.and_then(|a| Some(a.to_string())),
    )
}

/// Information of a node
#[derive(Debug, PartialEq)]
pub enum Node {
    /// The node is terminal (atom) with a name
    Val(String),
    /// The node is not terminal (rule)
    /// with a name and a vec of nodes
    Rule((String, Vec<Node>)),
    /// Reached end of file
    EOF,
}

impl Node {
    /// Remove nodes with one of the names in the list.
    /// It will remove the childs
    /// ```
    ///    use dynparser::ast;
    ///
    ///    let ast_before_prune: ast::Node = ast::Node::Rule((
    ///        "root".to_string(),
    ///        vec![ast::Node::Rule((
    ///            "a".to_string(),
    ///            vec![
    ///                ast::Node::Rule(("_1".to_string(), vec![])),
    ///                ast::Node::Rule(("_2".to_string(), vec![])),
    ///            ],
    ///        ))],
    ///    ));
    ///
    ///    let ast_after_prune = ast::Node::Rule((
    ///        "root".to_string(),
    ///        vec![ast::Node::Rule(("a".to_string(), vec![]))],
    ///    ));
    ///
    ///    assert!(ast_before_prune.prune(&vec!["_1", "_2"]) == ast_after_prune)
    /// ```

    pub fn prune(&self, nodes2prune: &[&str]) -> Self {
        let nname2prune = |nname: &str| nodes2prune.iter().find(|n| *n == &nname);
        let node2prune = |node: &Node| match node {
            Node::Rule((nname, _)) => nname2prune(nname).is_some(),
            _ => false,
        };
        let prune_vn = |vnodes: &[Node]| {
            vnodes.iter().fold(vec![], |acc, n| {
                if !node2prune(n) {
                    acc.ipush(n.prune(nodes2prune))
                } else {
                    acc
                }
            })
        };
        match self {
            Node::EOF => Node::EOF,
            Node::Val(v) => Node::Val(v.clone()),
            Node::Rule((n, vn)) => Node::Rule((n.clone(), prune_vn(vn))),
        }
    }

    /// Remove nodes excepting names in the list.
    /// Childs will be connected to the parent node removed
    /// ```
    ///
    /// use dynparser::ast;
    ///
    /// let ast_before_passthrow: ast::Node = ast::Node::Rule((
    ///     "root".to_string(),
    ///     vec![ast::Node::Rule((
    ///         "a".to_string(),
    ///         vec![ast::Node::Rule((
    ///             "_1".to_string(),
    ///             vec![ast::Node::Rule(("_2".to_string(), vec![]))],
    ///         ))],
    ///     ))],
    /// ));
    ///
    /// let ast_after_passthrow: ast::Node = ast::Node::Rule((
    ///     "root".to_string(),
    ///     vec![ast::Node::Rule((
    ///         "a".to_string(),
    ///         vec![ast::Node::Rule(("_2".to_string(), vec![]))],
    ///     ))],
    /// ));
    ///
    /// assert!(ast_before_passthrow.passthrow_except(&vec!["root", "a", "_2"]) == ast_after_passthrow)
    /// ```

    pub fn passthrow_except(&self, nodes2keep: &[&str]) -> Self {
        fn pthr_vn(vnodes: &[Node], nodes2keep: &[&str]) -> Vec<Node> {
            let nname2keep = |nname: &str| nodes2keep.iter().find(|n| *n == &nname);
            let node2keep = |node: &Node| match node {
                Node::Rule((nname, _)) => nname2keep(nname).is_some(),
                _ => true,
            };
            vnodes.iter().fold(vec![], |acc, n| {
                if node2keep(n) {
                    acc.ipush(n.passthrow_except(nodes2keep))
                } else {
                    match n {
                        Node::Rule((_, new_nodes)) => acc.iappend(pthr_vn(new_nodes, nodes2keep)),
                        _ => acc.ipush(n.passthrow_except(nodes2keep)),
                    }
                }
            })
        };
        match self {
            Node::EOF => Node::EOF,
            Node::Val(v) => Node::Val(v.clone()),
            Node::Rule((n, vn)) => Node::Rule((n.clone(), pthr_vn(vn, nodes2keep))),
        }
    }

    /// Concat consecutive Val nodes
    /// ```
    ///    use dynparser::ast;
    ///    
    ///    let ast_before_compact: ast::Node = ast::Node::Rule((
    ///        "root".to_string(),
    ///        vec![ast::Node::Rule((
    ///            "node".to_string(),
    ///            vec![
    ///                ast::Node::Val("hello".to_string()),
    ///                ast::Node::Val(" ".to_string()),
    ///                ast::Node::Val("world".to_string()),
    ///            ],
    ///        ))],
    ///    ));
    ///
    ///    let ast_after_compact = ast::Node::Rule((
    ///        "root".to_string(),
    ///        vec![ast::Node::Rule((
    ///            "node".to_string(),
    ///            vec![ast::Node::Val("hello world".to_string())],
    ///        ))],
    ///    ));
    ///
    ///    assert!(ast_before_compact.compact() == ast_after_compact)
    ///```
    pub fn compact(&self) -> Self {
        fn concat_nodes(nodes: Vec<Node>, n: &Node) -> Vec<Node> {
            let get_val = |nodes: &Vec<Node>| match nodes.last() {
                Some(Node::Val(ref v)) => Some(v.to_string()),
                _ => None,
            };
            let concat_v = |v: &String, prev_v: &Option<String>| match (v, prev_v) {
                (v, Some(pv)) => Some(format!("{}{}", pv, v)),
                _ => None,
            };

            match (n, get_val(&nodes)) {
                (Node::EOF, _) => nodes.ipush(Node::EOF),
                (Node::Val(ref v), ref prev_v) => match concat_v(v, prev_v) {
                    Some(c) => {
                        let (_, nodes) = nodes.ipop();
                        nodes.ipush(Node::Val(c.clone()))
                    }
                    _ => nodes.ipush(Node::Val(v.clone())),
                },
                (Node::Rule((ref n, ref vn)), _) => {
                    nodes.ipush(Node::Rule((n.clone(), compact_nodes(vn))))
                }
            }
        };
        fn compact_nodes(nodes: &[Node]) -> Vec<Node> {
            nodes
                .iter()
                .fold(vec![], |acc: Vec<Node>, n| (concat_nodes(acc, n)))
        };
        match self {
            Node::EOF => Node::EOF,
            Node::Val(v) => Node::Val(v.clone()),
            Node::Rule((n, vn)) => Node::Rule((n.clone(), compact_nodes(vn))),
        }
    }
}

/// It will get the node name and a slice to the nodes contained by the node
/// ```
///    use dynparser::ast::{self, get_nodename_and_nodes, Node};
///
///    let ast: Node = Node::Rule((
///        "root".to_string(),
///        vec![Node::Val("hello".to_string())],
///    ));
///
///    let (node_name, nodes) = get_nodename_and_nodes(&ast).unwrap();
///
///    assert!(node_name == "root");
///    assert!(nodes[0] == ast::Node::Val("hello".to_string()),)
/// ```
pub fn get_nodename_and_nodes(node: &Node) -> Result<(&str, &[Node]), Error> {
    match node {
        Node::Rule((nname, nodes)) => Ok((nname, nodes)),
        _ => Err(error("expected node::Rule", None)),
    }
}

/// Get the value of the Node
/// If node is not a Node::Val, it will return an error
///```
///     use dynparser::ast::{self, get_node_val};
///     let ast = ast::Node::Val("hello".to_string());
///     
///     let val = get_node_val(&ast).unwrap();
///     
///     assert!(val == "hello");
///```
pub fn get_node_val(node: &Node) -> Result<&str, Error> {
    match node {
        Node::Val(v) => Ok(v),
        _ => Err(error("expected node::Val", None)),
    }
}

/// Sometimes, processing the ast, you will exptect to have an unique
/// child, and it will have to be a simple Node::Val
/// This function will return the val, or error in other case
///
///```
///     use dynparser::ast;
///     let nodes = vec![ast::Node::Val("hello".to_string())];
///     
///     let val = ast::get_nodes_unique_val(&nodes).unwrap();
///     
///     assert!(val == "hello");
///```
///
/// If you pass an slice with more than one element, it will return
/// an error
///
///```
///     use dynparser::ast;
///     let nodes = vec![ast::Node::Val("hello".to_string()),
///                      ast::Node::Val("world".to_string())];
///     
///     assert!(ast::get_nodes_unique_val(&nodes).is_err());
///```
pub fn get_nodes_unique_val(nodes: &[Node]) -> Result<&str, Error> {
    match (nodes.first(), nodes.len()) {
        (Some(n), 1) => get_node_val(n),
        _ => Err(error("expected only one value in nodes", None)),
    }
}

/// Given a slice of nodes, return the value (&str) of first
/// node if it is a Node::Rule and return the rest of nodes
///
/// If it's not possible, return an error
///
///```
///     use dynparser::ast;
///     let nodes = vec![
///                 ast::Node::Val("hello".to_string()),
///                 ast::Node::Val("world".to_string()),
///     ];
///     
///     let (val, nodes) = ast::consume_val(&nodes).unwrap();
///     assert!(val == "hello");
///     assert!(nodes.len() == 1);
///
///     let (val, nodes) = ast::consume_val(&nodes).unwrap();
///     assert!(val == "world");
///     assert!(nodes.len() == 0);
///```
///
pub fn consume_val(nodes: &[Node]) -> Result<(&str, &[Node]), Error> {
    let (node, nodes) = split_first_nodes(nodes)?;
    match node {
        Node::Val(v) => Ok((&v, nodes)),
        _ => Err(error("expected Val node", None)),
    }
}

/// Given a list of nodes, return the first and the rest on a tuple
///
///```
///     use dynparser::ast;
///     let nodes = vec![
///                 ast::Node::Val("hello".to_string()),
///                 ast::Node::Val("world".to_string()),
///                 ast::Node::Val(".".to_string()),
///     ];
///     
///     let (node, nodes) = ast::split_first_nodes(&nodes).unwrap();
///     assert!(ast::get_node_val(node).unwrap() == "hello");
///     assert!(nodes.len() == 2);
///
///     let (node, nodes) = ast::split_first_nodes(&nodes).unwrap();
///     assert!(ast::get_node_val(node).unwrap() == "world");
///     assert!(nodes.len() == 1);

///     let (node, nodes) = ast::split_first_nodes(&nodes).unwrap();
///     assert!(ast::get_node_val(node).unwrap() == ".");
///     assert!(nodes.len() == 0);
///```
///
pub fn split_first_nodes(nodes: &[Node]) -> Result<(&Node, &[Node]), Error> {
    nodes
        .split_first()
        .ok_or_else(|| error("trying get first element from nodes on empty slice", None))
}

/// Consume a node if it's a Val kind and the vaule is
/// equal to the provider one
///
///```
///     use dynparser::ast;
///     let nodes = vec![
///                 ast::Node::Val("hello".to_string()),
///                 ast::Node::Val("world".to_string()),
///                 ast::Node::Val(".".to_string()),
///     ];
///     
///     let nodes = ast::consume_this_value("hello", &nodes).unwrap();
///     let nodes = ast::consume_this_value("world", &nodes).unwrap();
///```
///
pub fn consume_this_value<'a>(v: &str, nodes: &'a [Node]) -> Result<&'a [Node], Error> {
    let (node, nodes) = split_first_nodes(nodes)?;

    let nv = get_node_val(node)?;
    if nv == v {
        Ok(nodes)
    } else {
        Err(error(
            "trying get first element from nodes on empty slice",
            None,
        ))
    }
}

/// Consume a node if it's a Rule kind with a specific value
/// and return the rest of nodes and the sub_nodes for the consumed node
///
///```
///     use dynparser::ast;
///     let nodes = vec![
///                 ast::Node::Rule(("hello".to_string(), vec![ast::Node::Val("world".to_string())])),
///     ];
///     
///     let (nodes, sub_nodes) = ast::consume_node_get_subnodes_for_rule_name_is("hello", &nodes).unwrap();
///     assert!(nodes.len() == 0);
///     let nodes = ast::consume_this_value("world", &sub_nodes).unwrap();
///```
///
pub fn consume_node_get_subnodes_for_rule_name_is<'a>(
    name: &str,
    nodes: &'a [Node],
) -> Result<(&'a [Node], &'a [Node]), Error> {
    let (node, nodes) = split_first_nodes(nodes)?;
    match node {
        Node::Rule((n, sub_nodes)) => if n == name {
            Ok((nodes, sub_nodes))
        } else {
            Err(error(
                &format!("expected {} node, received {}", name, n),
                None,
            ))
        },
        unknown => Err(error(
            &format!("expected {} Node::Rule, received {:?}", name, unknown),
            None,
        )),
    }
}

/// Consume a node if it's a Rule kind with a specific value
/// and return the rest of nodes and the sub_nodes for the consumed node
///
///```
///     use dynparser::ast;
///     let nodes = vec![];
///     
///     assert!(ast::check_empty_nodes(&nodes).is_ok());
///```
///
pub fn check_empty_nodes(nodes: &[Node]) -> Result<(), Error> {
    if nodes.is_empty() {
        Ok(())
    } else {
        Err(error("not consumed full nodes", None))
    }
}

/// Return a reference to first node
///
///```
///     use dynparser::ast;
///     let nodes = vec![
///                 ast::Node::Rule(("hello".to_string(), vec![])),
///                 ast::Node::Val("world".to_string())];
///     
///     let first = ast::peek_first_node(&nodes).unwrap();
///     assert!(first == &ast::Node::Rule(("hello".to_string(), vec![])));
///```
///
pub fn peek_first_node(nodes: &[Node]) -> Result<&Node, Error> {
    if nodes.is_empty() {
        Err(error("exptected node on peek_first_node", None))
    } else {
        Ok(&nodes[0])
    }
}