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§
Provided Methods§
Sourcefn expected<T>(&mut self, exp: &str) -> Result<T, ParseError>
fn expected<T>(&mut self, exp: &str) -> Result<T, ParseError>
Generates an error message for parsing failures, including the highlighted context.
Sourcefn expected_and<T>(&mut self, exp: &str, msg: &str) -> Result<T, ParseError>
fn expected_and<T>(&mut self, exp: &str, msg: &str) -> Result<T, ParseError>
Generates an error message with an additional custom message.
Sourcefn peek_one(&mut self) -> Option<char>
fn peek_one(&mut self) -> Option<char>
Inspects the next character in the text without consuming it.
Examples found in repository?
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 }
Sourcefn peek_many(&mut self, count: usize) -> Option<&'i str>
fn peek_many(&mut self, count: usize) -> Option<&'i str>
Inspects the next count
characters in the text without consuming them.
Sourcefn advance_one(&mut self) -> Option<char>
fn advance_one(&mut self) -> Option<char>
Consumes the next character in the text.
Sourcefn advance_many(&mut self, count: usize) -> Option<&'i str>
fn advance_many(&mut self, count: usize) -> Option<&'i str>
Advances the parser by count
characters, consuming them.
Sourcefn skip_spaces(&mut self)
fn skip_spaces(&mut self)
Skips spaces in the text.
Sourcefn skip_trivia(&mut self)
fn skip_trivia(&mut self)
Skips whitespace & comments in the text.
Examples found in repository?
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 }
Sourcefn consume(&mut self, text: &str) -> Result<(), ParseError>
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?
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 }
Sourcefn starts_with(&mut self, text: &str) -> bool
fn starts_with(&mut self, text: &str) -> bool
Checks if the next characters in the input start with the given string.
Sourcefn take_while(&mut self, f: impl FnMut(char) -> bool) -> &'i str
fn take_while(&mut self, f: impl FnMut(char) -> bool) -> &'i str
Consumes all contiguous characters matching a given predicate.
Sourcefn parse_name(&mut self) -> Result<String, ParseError>
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?
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 }
Sourcefn parse_u64(&mut self) -> Result<u64, ParseError>
fn parse_u64(&mut self) -> Result<u64, ParseError>
Parses a u64 from the input, supporting dec, hex (0xNUM), and bin (0bNUM).
Sourcefn parse_char(&mut self) -> Result<char, ParseError>
fn parse_char(&mut self) -> Result<char, ParseError>
Parses a single unicode character, supporting scape sequences.
Sourcefn parse_quoted_char(&mut self) -> Result<char, ParseError>
fn parse_quoted_char(&mut self) -> Result<char, ParseError>
Parses a quoted character, like ‘x’.
Sourcefn parse_quoted_string(&mut self) -> Result<String, ParseError>
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.