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
impl Node
sourcepub fn flatten(&self) -> Vec<Node>
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
impl Node
sourcepub fn prune(&self, nodes2prune: &[&str]) -> Self
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)
sourcepub fn passthrow_except(&self, nodes2keep: &[&str]) -> Self
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)
sourcepub fn compact(&self) -> Self
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)