Function dynparser::peg::rules_from_peg
source · Expand description
Given a peg
set of rules on an string, it will generate
the set of rules to use in the parser
Next, is a full example showing the error messages, if so
extern crate dynparser;
use dynparser::{parse, rules_from_peg};
fn main() {
let rules = rules_from_peg(
r#"
main = 'hello' ' ' 'world' dot
dot = "\0x2E"
"#,
).map_err(|e| {
println!("{}", e);
panic!("FAIL");
})
.unwrap();
println!("{:#?}", rules);
let result = parse("hello world.", &rules);
assert!(result.is_ok());
match result {
Ok(ast) => println!("{:#?}", ast),
Err(e) => println!("Error: {:?}", e),
};
}
Next is an example with some and
literals
and comments on peg grammar
extern crate dynparser;
use dynparser::{parse, rules_from_peg};
let rules = rules_from_peg(
r#"
// classic hello world
main = 'hello' ' ' 'world'
/* with a multiline comment
*/
"#,
).unwrap();
assert!(parse("hello world", &rules).is_ok());
Next is an example with some error info
extern crate dynparser;
use dynparser::{parse, rules_from_peg};
let rules = rules_from_peg(
r#"
main = '(' main ( ')' / error("unbalanced parenthesys") )
/ 'hello'
"#,
).unwrap();
assert!(parse("hello", &rules).is_ok());
println!("{:?}", parse("(hello)", &rules));
assert!(parse("(hello)", &rules).is_ok());
assert!(parse("((hello))", &rules).is_ok());
assert!(parse("(((hello)))", &rules).is_ok());
match parse("(hello", &rules) {
Err(e) => {assert!(e.descr == "unbalanced parenthesys");},
_ => ()
}
match parse("((hello)", &rules) {
Err(e) => {assert!(e.descr == "unbalanced parenthesys");},
_ => ()
}