rust-crafting-interpreters-.../src/parser.rs

300 lines
9.6 KiB
Rust
Raw Normal View History

2022-12-31 00:50:31 +01:00
use crate::{
ast,
errors::{ErrorHandler, ParserError},
tokens::{Token, TokenType},
};
2022-12-30 23:50:33 +01:00
2022-12-31 10:12:11 +01:00
/// The parser contains the input tokens and the current input position.
2022-12-30 23:50:33 +01:00
#[derive(Debug)]
pub struct Parser {
tokens: Vec<Token>,
2022-12-31 00:50:31 +01:00
current: usize,
2022-12-30 23:50:33 +01:00
}
impl Parser {
2022-12-31 10:12:11 +01:00
/// Initialize the parser.
2022-12-30 23:50:33 +01:00
pub fn new(tokens: Vec<Token>) -> Self {
2022-12-31 00:50:31 +01:00
Self { tokens, current: 0 }
}
2022-12-31 10:12:11 +01:00
/// Parse the tokens into an AST and return it, or return nothing if a
/// parser error occurs.
pub fn parse(mut self, err_hdl: &mut ErrorHandler) -> Option<ast::ProgramNode> {
match self.parse_program() {
2022-12-31 00:50:31 +01:00
Ok(expr) => Some(expr),
Err(e) => {
e.report(err_hdl);
None
}
}
}
2022-12-31 12:27:51 +01:00
/// Synchronize the parser after an error.
fn _synchronize(&mut self) {
self.advance();
while !self.is_at_end() {
if self.previous().token_type == TokenType::Semicolon
|| matches!(
self.peek().token_type,
TokenType::Class
| TokenType::Fun
| TokenType::If
| TokenType::Print
| TokenType::Return
| TokenType::Var
| TokenType::While
)
{
return;
}
}
}
2022-12-31 10:12:11 +01:00
/* ------------------------ *
* RECURSIVE DESCENT PARSER *
* ------------------------ */
/// Parse the following rule:
/// ```
/// program := statement*
/// ```
fn parse_program(&mut self) -> Result<ast::ProgramNode, ParserError> {
let mut stmts: Vec<ast::StmtNode> = Vec::new();
while !self.is_at_end() {
let stmt = self.parse_statement()?;
stmts.push(stmt);
}
Ok(ast::ProgramNode(stmts))
}
/// Parse the following rule:
/// ```
/// statement := expression ";"
/// statement := "print" expression ";"
2022-12-31 15:39:52 +01:00
/// statement := declaration ";"
/// ```
fn parse_statement(&mut self) -> Result<ast::StmtNode, ParserError> {
2022-12-31 15:39:52 +01:00
if self.expect(&[TokenType::Var]).is_some() {
self.parse_declaration()
} else if self.expect(&[TokenType::Print]).is_some() {
let expression = self.parse_expression()?;
2022-12-31 15:19:41 +01:00
self.consume(&TokenType::Semicolon, "expected ';' after value")?;
Ok(ast::StmtNode::Print(expression))
} else {
let expression = self.parse_expression()?;
2022-12-31 15:19:41 +01:00
self.consume(&TokenType::Semicolon, "expected ';' after expression")?;
Ok(ast::StmtNode::Expression(expression))
}
}
2022-12-31 15:39:52 +01:00
/// Parse the following rule:
/// ```
/// declaration := "var" IDENTIFIER ";"
/// declaration := "var" IDENTIFIER "=" expression ";"
/// ```
fn parse_declaration(&mut self) -> Result<ast::StmtNode, ParserError> {
let name = match self.peek().token_type {
TokenType::Identifier(_) => self.advance().clone(),
_ => return Err(ParserError::new(self.peek(), "expected variable name")),
};
let initializer: Option<ast::ExprNode> = match self.expect(&[TokenType::Equal]) {
Some(_) => Some(self.parse_expression()?),
None => None,
};
self.consume(
&TokenType::Semicolon,
"expected ';' after variable declaration",
)?;
Ok(ast::StmtNode::VarDecl(name.lexeme, initializer))
}
2022-12-31 10:12:11 +01:00
/// Parse the following rule:
/// ```
/// expression := equality
/// ```
2022-12-31 00:50:31 +01:00
fn parse_expression(&mut self) -> Result<ast::ExprNode, ParserError> {
self.parse_equality()
}
2022-12-31 10:12:11 +01:00
/// Parse the following rule:
/// ```
/// equality := comparison "==" comparison
/// equality := comparison "!=" comparison
/// ```
2022-12-31 00:50:31 +01:00
fn parse_equality(&mut self) -> Result<ast::ExprNode, ParserError> {
let mut expr = self.parse_comparison()?;
while let Some(operator) = self.expect(&[TokenType::BangEqual, TokenType::EqualEqual]) {
let right = self.parse_comparison()?;
expr = ast::ExprNode::Binary {
left: Box::new(expr),
operator: operator.clone(),
right: Box::new(right),
};
}
Ok(expr)
}
2022-12-31 10:12:11 +01:00
/// Parse the following rule:
/// ```
/// comparison := term comparison_operator term
/// comparison_operator := "<" | "<=" | ">" | ">="
/// ```
2022-12-31 00:50:31 +01:00
fn parse_comparison(&mut self) -> Result<ast::ExprNode, ParserError> {
let mut expr = self.parse_term()?;
while let Some(operator) = self.expect(&[
TokenType::Greater,
TokenType::GreaterEqual,
TokenType::Less,
TokenType::LessEqual,
]) {
let right = self.parse_term()?;
expr = ast::ExprNode::Binary {
left: Box::new(expr),
operator: operator.clone(),
right: Box::new(right),
};
}
Ok(expr)
}
2022-12-31 10:12:11 +01:00
/// Parse the following rule:
/// ```
/// term := factor "+" factor
/// term := factor "-" factor
/// ```
2022-12-31 00:50:31 +01:00
fn parse_term(&mut self) -> Result<ast::ExprNode, ParserError> {
let mut expr = self.parse_factor()?;
while let Some(operator) = self.expect(&[TokenType::Minus, TokenType::Plus]) {
let right = self.parse_factor()?;
expr = ast::ExprNode::Binary {
left: Box::new(expr),
operator: operator.clone(),
right: Box::new(right),
};
}
Ok(expr)
}
2022-12-31 10:12:11 +01:00
/// Parse the following rule:
/// ```
/// factor := unary "*" unary
/// factor := unary "/" unary
/// ```
2022-12-31 00:50:31 +01:00
fn parse_factor(&mut self) -> Result<ast::ExprNode, ParserError> {
let mut expr = self.parse_unary()?;
while let Some(operator) = self.expect(&[TokenType::Slash, TokenType::Star]) {
let right = self.parse_unary()?;
expr = ast::ExprNode::Binary {
left: Box::new(expr),
operator: operator.clone(),
right: Box::new(right),
};
}
Ok(expr)
}
2022-12-31 10:12:11 +01:00
/// Parse the following rule:
/// ```
/// unary := "-" unary
/// unary := "!" unary
/// unary := primary
/// ```
2022-12-31 00:50:31 +01:00
fn parse_unary(&mut self) -> Result<ast::ExprNode, ParserError> {
if let Some(operator) = self.expect(&[TokenType::Bang, TokenType::Minus]) {
Ok(ast::ExprNode::Unary {
2022-12-31 10:14:58 +01:00
operator,
2022-12-31 00:50:31 +01:00
right: Box::new(self.parse_unary()?),
})
} else {
self.parse_primary()
}
}
2022-12-31 10:12:11 +01:00
/// Parse the following rule:
/// ```
/// primary := "(" expression ")"
/// primary := FALSE | TRUE | NIL | STRING | NUMBER
/// ```
2022-12-31 00:50:31 +01:00
fn parse_primary(&mut self) -> Result<ast::ExprNode, ParserError> {
if self.expect(&[TokenType::LeftParen]).is_some() {
let expr = self.parse_expression()?;
self.consume(&TokenType::RightParen, "expected ')' after expression")?;
Ok(ast::ExprNode::Grouping {
expression: Box::new(expr),
})
} else if let Some(token) =
self.expect(&[TokenType::False, TokenType::True, TokenType::Nil])
{
Ok(ast::ExprNode::Litteral { value: token })
} else {
match &self.peek().token_type {
TokenType::Number(_) | &TokenType::String(_) => Ok(ast::ExprNode::Litteral {
value: self.advance().clone(),
}),
_ => Err(ParserError::new(self.peek(), "expected expression")),
}
}
}
2022-12-31 10:12:11 +01:00
/* -------------- *
* HELPER METHODS *
* -------------- */
/// Expect a token of some types. If a matching token is found, the read
/// pointer is moved and a clone of the token is returned.
2022-12-31 00:50:31 +01:00
fn expect(&mut self, accepts: &[TokenType]) -> Option<Token> {
for tt in accepts {
if self.check(tt) {
return Some(self.advance().clone());
}
}
None
}
2022-12-31 10:12:11 +01:00
/// Consume a token of a given type. If no matching token is found, a
/// parse error is returned instead. Otherwise the read pointer is moved.
2022-12-31 00:50:31 +01:00
fn consume(&mut self, token_type: &TokenType, error: &str) -> Result<&Token, ParserError> {
if self.check(token_type) {
Ok(self.advance())
} else {
Err(ParserError::new(self.peek(), error))
}
}
2022-12-31 10:12:11 +01:00
/// Check for a token of some type. Returns `false` if the end of the input
/// has been reached. The read pointer isn't affected.
2022-12-31 00:50:31 +01:00
fn check(&self, token_type: &TokenType) -> bool {
if self.is_at_end() {
false
} else {
&self.peek().token_type == token_type
}
}
2022-12-31 10:12:11 +01:00
/// Move the read pointer forward if the end hasn't been reached. In all
/// cases, return the previous element (so either the element that was
/// current before the pointer moved, or the last, non-`EOF` token).
2022-12-31 00:50:31 +01:00
fn advance(&mut self) -> &Token {
if !self.is_at_end() {
self.current += 1
}
self.previous()
}
2022-12-31 10:12:11 +01:00
/// Check whether the end of token stream has been reached by checking
/// for the `EOF` token.
2022-12-31 00:50:31 +01:00
fn is_at_end(&self) -> bool {
2022-12-31 10:17:25 +01:00
self.peek().token_type == TokenType::Eof
2022-12-31 00:50:31 +01:00
}
2022-12-31 10:12:11 +01:00
/// Return a reference to the current token in the stream.
2022-12-31 00:50:31 +01:00
fn peek(&self) -> &Token {
&self.tokens[self.current]
}
2022-12-31 10:12:11 +01:00
/// Return a reference to the previous token in the stream.
2022-12-31 00:50:31 +01:00
fn previous(&self) -> &Token {
&self.tokens[self.current - 1]
2022-12-30 23:50:33 +01:00
}
}