Enum dynparser::ast::Node

source ·
pub enum Node {
    Val(String),
    Rule((String, Vec<Node>)),
    EOF,
}
Expand description

Information of a node

Variants§

§

Val(String)

The node is terminal (atom) with a name

§

Rule((String, Vec<Node>))

The node is not terminal (rule) with a name and a vec of nodes

§

EOF

Reached end of file

Implementations§

Flattening tree

It’s very commont to visit nodes in order The grammar checked the consistency of input Flattening the AST, could be interesting in order to process the tree

   use dynparser::ast::{self, flat};
   
   let ast_before_flatten = ast::Node::Rule((
       "first".to_string(),
       vec![
           ast::Node::Rule((
               "node1".to_string(),
               vec![
                   ast::Node::Val("hello".to_string()),
                   ast::Node::Rule(("node1.1".to_string(), vec![ast::Node::Val(" ".to_string())])),
               ],
           )),
           ast::Node::Rule((
               "node2".to_string(),
               vec![ast::Node::Val("world".to_string())],
           )),
       ],
   ));

   let vec_after_flatten =
       vec![
           flat::Node::BeginRule("first".to_string()),
           flat::Node::BeginRule("node1".to_string()),
           flat::Node::Val("hello".to_string()),
           flat::Node::BeginRule("node1.1".to_string()),
           flat::Node::Val(" ".to_string()),
           flat::Node::EndRule("node1.1".to_string()),
           flat::Node::EndRule("node1".to_string()),
           flat::Node::BeginRule("node2".to_string()),
           flat::Node::Val("world".to_string()),
           flat::Node::EndRule("node2".to_string()),
           flat::Node::EndRule("first".to_string()),
       ];

   assert!(ast_before_flatten.flatten() == vec_after_flatten)

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)

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)

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)

Trait Implementations§

Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.