Struct dynparser::parser::expression::SetOfRules
source · pub struct SetOfRules(_);
Expand description
The set of rules to be parsed Any rule has a name A rule can be registered just once The starting rule is main
Implementations§
source§impl SetOfRules
impl SetOfRules
sourcepub fn new(mrules: HashMap<String, Expression>) -> Self
pub fn new(mrules: HashMap<String, Expression>) -> Self
Initialize a set of rules with a hashmap of <String, Expression>
In general, is better to use the rules!
macro
sourcepub fn add(self, name: &str, expr: Expression) -> Self
pub fn add(self, name: &str, expr: Expression) -> Self
As this is a dynamic parser, it is necessary to add rules on runtime.
This method, will take the owner ship, and will return itself
In this way, you don’t need to declare mutable vars. You could need recursion in some cases
To add several rules at once, look for merge
#[macro_use] extern crate dynparser;
use dynparser::parse;
fn main() {
let rules = rules!{
"main" => and!{
rep!(lit!("a"), 1, 5),
ref_rule!("rule2")
}
};
let rules = rules.add("rule2", lit!("bcd"));
assert!(parse("aabcd", &rules).is_ok())
}
sourcepub fn merge(self, rules2merge: Self) -> Self
pub fn merge(self, rules2merge: Self) -> Self
As this is a dynamic parser, it is necessary to add rules on runtime.
This method, will take the owner ship, and will return itself
In this way, you don’t need to declare mutable vars. You could need recursion in some cases
It will add the rules from the parameter
#[macro_use] extern crate dynparser;
use dynparser::parse;
fn main() {
let rules = rules!{
"main" => and!{
rep!(lit!("a"), 1, 5),
ref_rule!("rule2")
}
};
let rules = rules.merge(rules!{"rule2" => lit!("bcd")});
assert!(parse("aabcd", &rules).is_ok())
}