Trait Parser

Source
pub trait Parser<'i> {
Show 19 methods // Required methods fn input(&mut self) -> &'i str; fn index(&mut self) -> &mut usize; // Provided methods fn expected<T>(&mut self, exp: &str) -> Result<T, ParseError> { ... } fn expected_and<T>(&mut self, exp: &str, msg: &str) -> Result<T, ParseError> { ... } fn peek_one(&mut self) -> Option<char> { ... } fn peek_many(&mut self, count: usize) -> Option<&'i str> { ... } fn advance_one(&mut self) -> Option<char> { ... } fn advance_many(&mut self, count: usize) -> Option<&'i str> { ... } fn skip_spaces(&mut self) { ... } fn skip_trivia(&mut self) { ... } fn is_eof(&mut self) -> bool { ... } fn consume(&mut self, text: &str) -> Result<(), ParseError> { ... } fn starts_with(&mut self, text: &str) -> bool { ... } fn take_while(&mut self, f: impl FnMut(char) -> bool) -> &'i str { ... } fn parse_name(&mut self) -> Result<String, ParseError> { ... } fn parse_u64(&mut self) -> Result<u64, ParseError> { ... } fn parse_char(&mut self) -> Result<char, ParseError> { ... } fn parse_quoted_char(&mut self) -> Result<char, ParseError> { ... } fn parse_quoted_string(&mut self) -> Result<String, ParseError> { ... }
}

Required Methods§

Source

fn input(&mut self) -> &'i str

Source

fn index(&mut self) -> &mut usize

Provided Methods§

Source

fn expected<T>(&mut self, exp: &str) -> Result<T, ParseError>

Generates an error message for parsing failures, including the highlighted context.

Source

fn expected_and<T>(&mut self, exp: &str, msg: &str) -> Result<T, ParseError>

Generates an error message with an additional custom message.

Source

fn peek_one(&mut self) -> Option<char>

Inspects the next character in the text without consuming it.

Examples found in repository?
examples/lambda_term.rs (line 15)
13  fn parse(&mut self) -> Result<Term, String> {
14    self.skip_trivia();
15    match self.peek_one() {
16      Some('λ') => {
17        self.consume("λ")?;
18        let name = self.parse_name()?;
19        let body = Box::new(self.parse()?);
20        Ok(Term::Lam { name, body })
21      }
22      Some('(') => {
23        self.consume("(")?;
24        let func = Box::new(self.parse()?);
25        let argm = Box::new(self.parse()?);
26        self.consume(")")?;
27        Ok(Term::App { func, argm })
28      }
29      _ => {
30        let name = self.parse_name()?;
31        Ok(Term::Var { name })
32      }
33    }
34  }
Source

fn peek_many(&mut self, count: usize) -> Option<&'i str>

Inspects the next count characters in the text without consuming them.

Source

fn advance_one(&mut self) -> Option<char>

Consumes the next character in the text.

Source

fn advance_many(&mut self, count: usize) -> Option<&'i str>

Advances the parser by count characters, consuming them.

Source

fn skip_spaces(&mut self)

Skips spaces in the text.

Source

fn skip_trivia(&mut self)

Skips whitespace & comments in the text.

Examples found in repository?
examples/lambda_term.rs (line 14)
13  fn parse(&mut self) -> Result<Term, String> {
14    self.skip_trivia();
15    match self.peek_one() {
16      Some('λ') => {
17        self.consume("λ")?;
18        let name = self.parse_name()?;
19        let body = Box::new(self.parse()?);
20        Ok(Term::Lam { name, body })
21      }
22      Some('(') => {
23        self.consume("(")?;
24        let func = Box::new(self.parse()?);
25        let argm = Box::new(self.parse()?);
26        self.consume(")")?;
27        Ok(Term::App { func, argm })
28      }
29      _ => {
30        let name = self.parse_name()?;
31        Ok(Term::Var { name })
32      }
33    }
34  }
Source

fn is_eof(&mut self) -> bool

Checks if the parser has reached the end of the input.

Source

fn consume(&mut self, text: &str) -> Result<(), ParseError>

Consumes an instance of the given string, erroring if it is not found.

Examples found in repository?
examples/lambda_term.rs (line 17)
13  fn parse(&mut self) -> Result<Term, String> {
14    self.skip_trivia();
15    match self.peek_one() {
16      Some('λ') => {
17        self.consume("λ")?;
18        let name = self.parse_name()?;
19        let body = Box::new(self.parse()?);
20        Ok(Term::Lam { name, body })
21      }
22      Some('(') => {
23        self.consume("(")?;
24        let func = Box::new(self.parse()?);
25        let argm = Box::new(self.parse()?);
26        self.consume(")")?;
27        Ok(Term::App { func, argm })
28      }
29      _ => {
30        let name = self.parse_name()?;
31        Ok(Term::Var { name })
32      }
33    }
34  }
Source

fn starts_with(&mut self, text: &str) -> bool

Checks if the next characters in the input start with the given string.

Source

fn take_while(&mut self, f: impl FnMut(char) -> bool) -> &'i str

Consumes all contiguous characters matching a given predicate.

Source

fn parse_name(&mut self) -> Result<String, ParseError>

Parses a name from the input, supporting alphanumeric characters, underscores, periods, and hyphens.

Examples found in repository?
examples/lambda_term.rs (line 18)
13  fn parse(&mut self) -> Result<Term, String> {
14    self.skip_trivia();
15    match self.peek_one() {
16      Some('λ') => {
17        self.consume("λ")?;
18        let name = self.parse_name()?;
19        let body = Box::new(self.parse()?);
20        Ok(Term::Lam { name, body })
21      }
22      Some('(') => {
23        self.consume("(")?;
24        let func = Box::new(self.parse()?);
25        let argm = Box::new(self.parse()?);
26        self.consume(")")?;
27        Ok(Term::App { func, argm })
28      }
29      _ => {
30        let name = self.parse_name()?;
31        Ok(Term::Var { name })
32      }
33    }
34  }
Source

fn parse_u64(&mut self) -> Result<u64, ParseError>

Parses a u64 from the input, supporting dec, hex (0xNUM), and bin (0bNUM).

Source

fn parse_char(&mut self) -> Result<char, ParseError>

Parses a single unicode character, supporting scape sequences.

Source

fn parse_quoted_char(&mut self) -> Result<char, ParseError>

Parses a quoted character, like ‘x’.

Source

fn parse_quoted_string(&mut self) -> Result<String, ParseError>

Parses a quoted string, like “foobar”.

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§