Trait ParseIt

Source
pub trait ParseIt {
    type Lexer<'a>: Lexer<'a>;
    type Output;

    // Required method
    fn parse_stream(
        &self,
        state: &ParserState<Self::Lexer<'_>>,
    ) -> Result<Self::Output, Error>;

    // Provided method
    fn parse(&self, input: &str) -> Result<Self::Output, Error> { ... }
}
Expand description

A parser.

Required Associated Types§

Source

type Lexer<'a>: Lexer<'a>

The lexer type.

Source

type Output

The parser output type.

Required Methods§

Source

fn parse_stream( &self, state: &ParserState<Self::Lexer<'_>>, ) -> Result<Self::Output, Error>

Parse from a ParserState.

Provided Methods§

Source

fn parse(&self, input: &str) -> Result<Self::Output, Error>

Parse from a string.

Examples found in repository?
examples/brainfuck.rs (line 40)
36fn main() {
37    let parser = parse::Brainfuck::default();
38    let src = "--[>--->->->++>-<<<<<-------]>--.>---------.>--..+++.>----.>+++++++++.<<.+++.------.<-.>>+.";
39
40    match parser.parse(src) {
41        Ok(ast) => execute(&ast, &mut 0, &mut [0; TAPE_LEN]),
42        Err(err) => println!("{:?}", err),
43    };
44}
More examples
Hide additional examples
examples/calc.rs (line 50)
45fn main() {
46    let parser = parse::Expr::default();
47
48    let input = "11+(6-1-1)*(4/2/2)";
49
50    let result = match parser.parse(input) {
51        Ok(value) => value,
52        Err(err) => {
53            println!("span: {}..{}", err.span.start, err.span.end);
54            return;
55        }
56    };
57
58    println!("parser: {}", result);
59    assert_eq!(result, 15);
60}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§