Enum 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§

Source§

impl Node

Source

pub fn flatten(&self) -> Vec<Node>

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)
Source§

impl Node

Source

pub fn prune(&self, nodes2prune: &[&str]) -> Self

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)
Source

pub fn passthrow_except(&self, nodes2keep: &[&str]) -> Self

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)
Source

pub fn compact(&self) -> Self

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§

Source§

impl Debug for Node

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Node

Source§

fn eq(&self, other: &Node) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Node

Auto Trait Implementations§

§

impl Freeze for Node

§

impl RefUnwindSafe for Node

§

impl Send for Node

§

impl Sync for Node

§

impl Unpin for Node

§

impl UnwindSafe for Node

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.