Skip to main content

parse

Function parse 

Source
pub fn parse<S: Substitutor, I: Iterator<Item = char>>(
    p: &mut I,
) -> Result<Syntax<S>, CompileError>
Expand description

Parse a phrase syntax to create the instance of the Syntax.

§Parameter

  • p: The iterator of the source text.

§Return

The human readable error message when Err.

§Eample

let syntax = tphrase::parse(&mut r#"main = Hello, World!"#.chars())?;
let mut ph: tphrase::Generator = tphrase::Generator::new();
let _ = ph.add(syntax);
assert_eq!(ph.generate(), "Hello, World!");

Err in the result holds some human readable error messages.

let syntax_result: Result<tphrase::Syntax, _> = tphrase::parse(&mut r#"
    main = "Hello, " {WORLD}
    WORLD
      = world
"#.chars());
match syntax_result {
    Ok(_) => assert!(syntax_result.is_err()),
    Err(err) => {
        assert_eq!(err.error_messages().len(), 3);
        assert_eq!(
            err.to_string(),
            format!("{}{}{}{}",
                "compile error:\n",
                "Line#2, Column#22: The end of the text or \"\\n\" is expected.\n",
                "Line#3, Column#10: \"=\" or \":=\" is expected.\n",
                "Line#4, Column#7: A nonterminal \"[A-Za-z0-9_.]+\" is expected."));
    },
}