Skip to content

Commit

Permalink
Chapter 6: Parsing Expressions (#3)
Browse files Browse the repository at this point in the history
This implements the [parsing](http://www.craftinginterpreters.com/parsing-expressions.htmll).

A few differences:
* `matches!` is a macro to allow varargs. It stands in for `match` from the Java version.
* The literals are typed via `LiteralValue`.
* `primary` does not use `matches!` but the Rust pattern matching to extract values.
* We use `Result` instead of exceptions.
  • Loading branch information
jeschkies authored Nov 7, 2019
1 parent 0156a95 commit 9508c9d
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ A [Lox](http://craftinginterpreters.com/the-lox-language.html) Interpreter in Ru
Each commit corresponds to one chapter in the book:

* [Chapter 4](https://github.com/jeschkies/lox-rs/commit/9fef15e73fdf57a3e428bb074059c7e144e257f7)
* [Chapter 5](https://github.com/jeschkies/lox-rs/commit/0156a95b4bf448dbff9cb4341a2339b741a163ca)
25 changes: 25 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::fmt;

use crate::token::{Token, TokenType};

pub fn error(line: i32, message: &str) {
report(line, "", message);
}
Expand All @@ -6,3 +10,24 @@ pub fn report(line: i32, where_: &str, message: &str) {
eprintln!("[line {}] Error{}: {}", line, where_, message);
// had_error = true; TODO: Use custom Error type
}

pub fn parser_error(token: &Token, message: &str) {
if token.tpe == TokenType::EOF {
report(token.line, " at end", message);
} else {
report(token.line, &format!(" at '{}'", token.lexeme), message);
}
}

#[derive(Debug)]
pub enum Error {
Parse,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Parse => write!(f, "ParseError"),
}
}
}
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod error;
mod parser;
mod scanner;
mod syntax;
mod token;
Expand All @@ -7,7 +8,9 @@ use std::io::{self, BufRead};
use std::process::exit;
use std::{env, fs};

use parser::Parser;
use scanner::Scanner;
use syntax::AstPrinter;

fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
let args: Vec<String> = env::args().collect();
Expand Down Expand Up @@ -40,8 +43,10 @@ fn run(source: String) -> io::Result<()> {
let mut scanner = Scanner::new(source);
let tokens = scanner.scan_tokens();

for token in tokens {
println!("{}", token);
let mut parser = Parser::new(tokens);
if let Some(expression) = parser.parse() {
let printer = AstPrinter;
println!("{}", printer.print(expression));
}
Ok(())
}
237 changes: 237 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
use crate::error::{parser_error, Error};
use crate::syntax::{Expr, LiteralValue};
use crate::token::{Token, TokenType};

pub struct Parser<'t> {
tokens: &'t Vec<Token>,
current: usize,
}

/// AKA match in Chapter 6.
macro_rules! matches {
( $sel:ident, $( $x:expr ),* ) => {
{
if $( $sel.check($x) )||* {
$sel.advance();
true
} else {
false
}
}
};
}

impl<'t> Parser<'t> {
pub fn new(tokens: &'t Vec<Token>) -> Self {
Parser { tokens, current: 0 }
}

pub fn parse(&mut self) -> Option<Expr> {
self.expression().ok()
}

fn expression(&mut self) -> Result<Expr, Error> {
self.equality()
}

fn equality(&mut self) -> Result<Expr, Error> {
let mut expr = self.comparison()?;

while matches!(self, TokenType::BangEqual, TokenType::EqualEqual) {
let operator: Token = (*self.previous()).clone();
let right: Expr = self.comparison()?;
expr = Expr::Binary {
left: Box::new(expr),
operator,
right: Box::new(right),
};
}

Ok(expr)
}

fn check(&self, token_type: TokenType) -> bool {
if self.is_at_end() {
return false;
}

token_type == self.peek().tpe
}

fn consume(&mut self, tpe: TokenType, message: &str) -> Result<Token, Error> {
if self.check(tpe) {
Ok(self.advance().clone())
} else {
Err(self.error(self.peek(), message))
}
}

fn advance(&mut self) -> &Token {
if !self.is_at_end() {
self.current += 1;
}
self.previous()
}

fn previous(&self) -> &Token {
self.tokens
.get(self.current - 1)
.expect("Previous was empty.")
}

fn error(&self, token: &Token, message: &str) -> Error {
parser_error(token, message);
Error::Parse
}

fn synchronize(&mut self) {
self.advance();

while !self.is_at_end() {
if self.previous().tpe == TokenType::Semicolon {
return;
}

match self.peek().tpe {
TokenType::Class
| TokenType::Fun
| TokenType::Var
| TokenType::For
| TokenType::If
| TokenType::While
| TokenType::Print
| TokenType::Return => return,
_ => self.advance(),
};
}
}

fn peek(&self) -> &Token {
self.tokens
.get(self.current)
.expect("Peek into end of token stream.")
}

fn is_at_end(&self) -> bool {
self.peek().tpe == TokenType::EOF
}

fn comparison(&mut self) -> Result<Expr, Error> {
let mut expr = self.addition()?;

while matches!(
self,
TokenType::Greater,
TokenType::GreaterEqual,
TokenType::Less,
TokenType::LessEqual
) {
let operator: Token = self.previous().clone();
let right = self.addition()?;
expr = Expr::Binary {
left: Box::new(expr),
operator,
right: Box::new(right),
}
}

Ok(expr)
}

fn addition(&mut self) -> Result<Expr, Error> {
let mut expr = self.multiplication()?;

while matches!(self, TokenType::Minus, TokenType::Plus) {
let operator: Token = self.previous().clone();
let right = self.multiplication()?;
expr = Expr::Binary {
left: Box::new(expr),
operator,
right: Box::new(right),
}
}

Ok(expr)
}

fn multiplication(&mut self) -> Result<Expr, Error> {
let mut expr = self.unary()?;

while matches!(self, TokenType::Slash, TokenType::Star) {
let operator: Token = self.previous().clone();
let right = self.unary()?;
expr = Expr::Binary {
left: Box::new(expr),
operator,
right: Box::new(right),
}
}

Ok(expr)
}

fn unary(&mut self) -> Result<Expr, Error> {
if matches!(self, TokenType::Bang, TokenType::Minus) {
let operator: Token = self.previous().clone();
let right = self.unary()?;
Ok(Expr::Unary {
operator,
right: Box::new(right),
})
} else {
self.primary()
}
}

fn primary(&mut self) -> Result<Expr, Error> {
// We don't use matches!() here since we want to extract the literals.
let expr = match &self.peek().tpe {
TokenType::False => Expr::Literal {
value: LiteralValue::Boolean(false),
},
TokenType::True => Expr::Literal {
value: LiteralValue::Boolean(true),
},
TokenType::Nil => Expr::Literal {
value: LiteralValue::Null,
},
TokenType::String { literal } => Expr::Literal {
value: LiteralValue::String(literal.clone()),
},
TokenType::Number { literal } => Expr::Literal {
value: LiteralValue::Number(literal.clone()),
},
TokenType::LeftParen => {
let expr = self.expression()?;
self.consume(TokenType::RightParen, "Expected ')' after expression.")?;
Expr::Grouping {
expression: Box::new(expr),
}
}
_ => return Err(self.error(self.peek(), "Expect expression.")),
};

self.advance();

Ok(expr)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::scanner::Scanner;
use crate::syntax::AstPrinter;

#[test]
fn test_parser() {
let mut scanner = Scanner::new("-123 * 45.67".to_string());
let tokens = scanner.scan_tokens();

let mut parser = Parser::new(tokens);
let expression = parser.parse().expect("Could not parse sample code.");
let printer = AstPrinter;

assert_eq!(printer.print(expression), "(* (- 123) 45.67)");
}
}
38 changes: 30 additions & 8 deletions src/syntax.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::token::Token;
use std::fmt;

pub enum Expr {
Binary {
Expand All @@ -10,14 +11,32 @@ pub enum Expr {
expression: Box<Expr>,
},
Literal {
value: String,
}, // Object in chapter 5
value: LiteralValue,
},
Unary {
operator: Token,
right: Box<Expr>,
},
}

pub enum LiteralValue {
Boolean(bool),
Null,
Number(f64),
String(String),
}

impl fmt::Display for LiteralValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LiteralValue::Boolean(b) => write!(f, "{}", b),
LiteralValue::Null => write!(f, "null"),
LiteralValue::Number(n) => write!(f, "{}", n),
LiteralValue::String(s) => write!(f, "{}", s),
}
}
}

pub trait Visitor<R> {
fn visit_binary_expr(&self, left: &Expr, operator: &Token, right: &Expr) -> R;
fn visit_grouping_expr(&self, expression: &Expr) -> R;
Expand All @@ -28,10 +47,14 @@ pub trait Visitor<R> {
impl Expr {
pub fn accept<R>(&self, visitor: &Visitor<R>) -> R {
match self {
Expr::Binary {left, operator, right} => visitor.visit_binary_expr(left, operator, right),
Expr::Binary {
left,
operator,
right,
} => visitor.visit_binary_expr(left, operator, right),
Expr::Grouping { expression } => visitor.visit_grouping_expr(expression),
Expr::Literal { value } => visitor.visit_literal_expr(value.to_string()),
Expr::Unary {operator, right } => visitor.visit_unary_expr(operator, right),
Expr::Unary { operator, right } => visitor.visit_unary_expr(operator, right),
}
}
}
Expand Down Expand Up @@ -74,7 +97,6 @@ impl Visitor<String> for AstPrinter {
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -84,15 +106,15 @@ mod tests {
fn test_printer() {
let expression = Expr::Binary {
left: Box::new(Expr::Unary {
operator: Token::new(TokenType::Minus,"-", 1),
operator: Token::new(TokenType::Minus, "-", 1),
right: Box::new(Expr::Literal {
value: "123".to_string(),
value: LiteralValue::Number(123f64),
}),
}),
operator: Token::new(TokenType::Star, "*", 1),
right: Box::new(Expr::Grouping {
expression: Box::new(Expr::Literal {
value: "45.67".to_string(),
value: LiteralValue::Number(45.67f64),
}),
}),
};
Expand Down
Loading

0 comments on commit 9508c9d

Please sign in to comment.