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§
Provided Methods§
sourcefn expected<T>(&mut self, exp: &str) -> Result<T, String>
fn expected<T>(&mut self, exp: &str) -> Result<T, String>
Generates an error message for parsing failures, including the highlighted context.
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 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 })
}
}
}
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 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 })
}
}
}
sourcefn consume(&mut self, text: &str) -> Result<(), String>
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?
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 })
}
}
}
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, String>
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?
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 })
}
}
}
sourcefn parse_u64(&mut self) -> Result<u64, String>
fn parse_u64(&mut self) -> Result<u64, String>
Parses a u64 from the input, supporting dec, hex (0xNUM), and bin (0bNUM).
sourcefn parse_char(&mut self) -> Result<char, String>
fn parse_char(&mut self) -> Result<char, String>
Parses a single unicode character, supporting scape sequences.
sourcefn parse_quoted_char(&mut self) -> Result<char, String>
fn parse_quoted_char(&mut self) -> Result<char, String>
Parses a quoted character, like ‘x’.
sourcefn parse_quoted_string(&mut self) -> Result<String, String>
fn parse_quoted_string(&mut self) -> Result<String, String>
Parses a quoted string, like “foobar”.