Trait TSPL::Parser

source ·
pub trait Parser<'i> {
Show 18 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, String> { ... } 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<(), String> { ... } 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, String> { ... } fn parse_u64(&mut self) -> Result<u64, String> { ... } fn parse_char(&mut self) -> Result<char, String> { ... } fn parse_quoted_char(&mut self) -> Result<char, String> { ... } fn parse_quoted_string(&mut self) -> Result<String, String> { ... }
}

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, String>

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

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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  fn parse(&mut self) -> Result<Term, String> {
    self.skip_trivia();
    match self.peek_one() {
      Some('λ') => {
        self.consume("λ")?;
        let name = self.parse_name()?;
        let body = Box::new(self.parse()?);
        Ok(Term::Lam { name, body })
      }
      Some('(') => {
        self.consume("(")?;
        let func = Box::new(self.parse()?);
        let argm = Box::new(self.parse()?);
        self.consume(")")?;
        Ok(Term::App { func, argm })
      }
      _ => {
        let name = self.parse_name()?;
        Ok(Term::Var { name })
      }
    }
  }
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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  fn parse(&mut self) -> Result<Term, String> {
    self.skip_trivia();
    match self.peek_one() {
      Some('λ') => {
        self.consume("λ")?;
        let name = self.parse_name()?;
        let body = Box::new(self.parse()?);
        Ok(Term::Lam { name, body })
      }
      Some('(') => {
        self.consume("(")?;
        let func = Box::new(self.parse()?);
        let argm = Box::new(self.parse()?);
        self.consume(")")?;
        Ok(Term::App { func, argm })
      }
      _ => {
        let name = self.parse_name()?;
        Ok(Term::Var { name })
      }
    }
  }
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<(), String>

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

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

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

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

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

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

source

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

Parses a single unicode character, supporting scape sequences.

source

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

Parses a quoted character, like ‘x’.

source

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

Parses a quoted string, like “foobar”.

Object Safety§

This trait is not object safe.

Implementors§