pub fn parse_input<'a>(
grammar: Grammar,
rule: String,
input: String,
) -> Result<(), String>Expand description
Parse input with the provided grammar and start rule returns Ok if the parse is sucessfull, Err otherwise
It’s used for validate the examples generated with the original grammar
use mo::config::GeneratorConfig;
use mo::{compile_grammar, parse_input, parallel_generate_examples};
// Default configuration for the generator
let mut config: GeneratorConfig = Default::default();
// Grammar string
let mut grammar = r#"
language = {"Rust" | "Python" | "Go" | "Java" | "PHP" | "Haskell"}
one = {"1"}
daysNumber = {one ~ " day" | !one ~ ASCII_NONZERO_DIGIT ~ " days"}
sentence = {"I have been programming in " ~ language ~ " for " ~ daysNumber ~ "."}
"#;
// Generate the ast
let grammar_ast = compile_grammar(grammar.to_string());
// Generate the examples
let results = parallel_generate_examples(
grammar.to_string(), // The grammar
1, // Quantity of examples
"sentences".to_string(), // Start rule
&config, // Config of the generator
false, // Print progress
false, // Print in stdout, false return a vector with the examples
);
let one_example = results[0].as_ref().unwrap();
// Validate the generated example with the grammar
let validate = parse_input(grammar_ast.unwrap(), "sentence".to_string(), one_example.to_string());
println!("{:?}", validate);