pub trait ParseIt {
type Lexer: LexIt + Clone;
type Output;
// Required method
fn parse_stream<'a>(
&self,
state: &mut ParserState<'a, Self::Lexer>,
) -> Result<Self::Output, Error>;
// Provided method
fn parse(&self, input: &str) -> Result<Self::Output, Error> { ... }
}Expand description
A parser.
Required Associated Types§
Required Methods§
Sourcefn parse_stream<'a>(
&self,
state: &mut ParserState<'a, Self::Lexer>,
) -> Result<Self::Output, Error>
fn parse_stream<'a>( &self, state: &mut ParserState<'a, Self::Lexer>, ) -> Result<Self::Output, Error>
Parse from a ParserState.
Provided Methods§
Sourcefn parse(&self, input: &str) -> Result<Self::Output, Error>
fn parse(&self, input: &str) -> Result<Self::Output, Error>
Parse from a string.
Examples found in repository?
More examples
examples/calc.rs (line 52)
47fn main() {
48 let parser = parse::Expr::default();
49
50 let input = "11+(6-1-1)*(4/2/2)";
51
52 let result = match parser.parse(input) {
53 Ok(value) => value,
54 Err(err) => {
55 println!("span: {}..{}", err.span.start, err.span.end);
56 return;
57 }
58 };
59
60 println!("parser: {result}");
61 assert_eq!(result, 15);
62}examples/brainfuck.rs (line 43)
39fn main() {
40 let parser = parse::Brainfuck::default();
41 let src = "--[>--->->->++>-<<<<<-------]>--.>---------.>--..+++.>----.>+++++++++.<<.+++.------.<-.>>+.";
42
43 match parser.parse(src) {
44 Ok(ast) => {
45 let mut stdin = BufReader::new(std::io::stdin().lock()).bytes();
46 execute(&ast, &mut 0, &mut [0; TAPE_LEN], &mut stdin)
47 }
48 Err(err) => println!("{err:?}"),
49 };
50}