From 20a6cfeee233bde9ba1f482fa4545f5e395d6235 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 14 Jan 2021 18:36:06 +0300 Subject: refactor(tazjin/rlox): Let scanner tokens own their lexeme This removes the runtime dependency on a borrow into the program source code. It's not yet ideal because there are a lot of tokens where we really don't care about the lexeme, but this is what the book does and I am not going to change that. Change-Id: I888e18f98597766d6f725cbf9241e8eb2bd839e2 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2394 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/interpreter.rs | 82 ++++++------- users/tazjin/rlox/src/interpreter/tests.rs | 29 ++--- users/tazjin/rlox/src/main.rs | 9 +- users/tazjin/rlox/src/parser.rs | 178 ++++++++++++++--------------- users/tazjin/rlox/src/scanner.rs | 10 +- 5 files changed, 150 insertions(+), 158 deletions(-) (limited to 'users/tazjin') diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index f04d767dc5..5fb5283bc7 100644 --- a/users/tazjin/rlox/src/interpreter.rs +++ b/users/tazjin/rlox/src/interpreter.rs @@ -16,12 +16,12 @@ mod tests; // Representation of all callables, including builtins & user-defined // functions. #[derive(Clone, Debug)] -pub enum Callable<'a> { +pub enum Callable { Builtin(&'static dyn builtins::Builtin), - Function(Rc>), + Function(Rc), } -impl<'a> Callable<'a> { +impl Callable { fn arity(&self) -> usize { match self { Callable::Builtin(builtin) => builtin.arity(), @@ -29,12 +29,12 @@ impl<'a> Callable<'a> { } } - fn call(&self, lox: &mut Interpreter<'a>, args: Vec>) -> Result, Error> { + fn call(&self, lox: &mut Interpreter, args: Vec) -> Result { match self { Callable::Builtin(builtin) => builtin.call(args), Callable::Function(func) => { - let mut fn_env: Environment<'a> = Default::default(); + let mut fn_env: Environment = Default::default(); for (param, value) in func.params.iter().zip(args.into_iter()) { fn_env.define(param, value)?; @@ -48,12 +48,12 @@ impl<'a> Callable<'a> { // Representation of an in-language value. #[derive(Clone, Debug)] -pub enum Value<'a> { +pub enum Value { Literal(Literal), - Callable(Callable<'a>), + Callable(Callable), } -impl<'a> PartialEq for Value<'a> { +impl PartialEq for Value { fn eq(&self, other: &Self) -> bool { match (self, other) { (Value::Literal(lhs), Value::Literal(rhs)) => lhs == rhs, @@ -63,13 +63,13 @@ impl<'a> PartialEq for Value<'a> { } } -impl<'a> From for Value<'a> { - fn from(lit: Literal) -> Value<'a> { +impl From for Value { + fn from(lit: Literal) -> Value { Value::Literal(lit) } } -impl<'a> Value<'a> { +impl Value { fn expect_literal(self) -> Result { match self { Value::Literal(lit) => Ok(lit), @@ -79,19 +79,19 @@ impl<'a> Value<'a> { } #[derive(Debug, Default)] -struct Environment<'a> { - enclosing: Option>>>, - values: HashMap>, +struct Environment { + enclosing: Option>>, + values: HashMap, } -impl<'a> Environment<'a> { - fn define(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { +impl Environment { + fn define(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { let ident = identifier_str(name)?; self.values.insert(ident.into(), value); Ok(()) } - fn get(&self, name: &parser::Variable) -> Result, Error> { + fn get(&self, name: &parser::Variable) -> Result { let ident = identifier_str(&name.0)?; self.values @@ -110,7 +110,7 @@ impl<'a> Environment<'a> { }) } - fn assign(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { + fn assign(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { let ident = identifier_str(name)?; match self.values.get_mut(ident) { @@ -132,7 +132,7 @@ impl<'a> Environment<'a> { } } -fn identifier_str<'a>(name: &'a scanner::Token) -> Result<&'a str, Error> { +fn identifier_str(name: &scanner::Token) -> Result<&str, Error> { if let TokenKind::Identifier(ident) = &name.kind { Ok(ident) } else { @@ -144,11 +144,11 @@ fn identifier_str<'a>(name: &'a scanner::Token) -> Result<&'a str, Error> { } #[derive(Debug)] -pub struct Interpreter<'a> { - env: Rc>>, +pub struct Interpreter { + env: Rc>, } -impl<'a> Interpreter<'a> { +impl Interpreter { /// Create a new interpreter and configure the initial global /// variable set. pub fn create() -> Self { @@ -168,28 +168,28 @@ impl<'a> Interpreter<'a> { } // Environment modification helpers - fn define_var(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { + fn define_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { self.env .write() .expect("environment lock is poisoned") .define(name, value) } - fn assign_var(&mut self, name: &scanner::Token, value: Value<'a>) -> Result<(), Error> { + fn assign_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> { self.env .write() .expect("environment lock is poisoned") .assign(name, value) } - fn get_var(&mut self, var: &parser::Variable) -> Result, Error> { + fn get_var(&mut self, var: &parser::Variable) -> Result { self.env .read() .expect("environment lock is poisoned") .get(var) } - fn set_enclosing(&mut self, parent: Rc>>) { + fn set_enclosing(&mut self, parent: Rc>) { self.env .write() .expect("environment lock is poisoned") @@ -197,7 +197,7 @@ impl<'a> Interpreter<'a> { } // Interpreter itself - pub fn interpret(&mut self, program: &Block<'a>) -> Result, Error> { + pub fn interpret(&mut self, program: &Block) -> Result { let mut value = Value::Literal(Literal::Nil); for stmt in program { @@ -207,7 +207,7 @@ impl<'a> Interpreter<'a> { Ok(value) } - fn interpret_stmt(&mut self, stmt: &Statement<'a>) -> Result, Error> { + fn interpret_stmt(&mut self, stmt: &Statement) -> Result { let value = match stmt { Statement::Expr(expr) => self.eval(expr)?, Statement::Print(expr) => { @@ -226,7 +226,7 @@ impl<'a> Interpreter<'a> { Ok(value) } - fn interpret_var(&mut self, var: &parser::Var<'a>) -> Result, Error> { + fn interpret_var(&mut self, var: &parser::Var) -> Result { let init = var.initialiser.as_ref().ok_or_else(|| Error { line: var.name.line, kind: ErrorKind::InternalError("missing variable initialiser".into()), @@ -238,9 +238,9 @@ impl<'a> Interpreter<'a> { fn interpret_block( &mut self, - env: Rc>>, - block: &parser::Block<'a>, - ) -> Result, Error> { + env: Rc>, + block: &parser::Block, + ) -> Result { // Initialise a new environment and point it at the parent // (this is a bit tedious because we need to wrap it in and // out of the Rc). @@ -257,7 +257,7 @@ impl<'a> Interpreter<'a> { return result; } - fn interpret_if(&mut self, if_stmt: &parser::If<'a>) -> Result, Error> { + fn interpret_if(&mut self, if_stmt: &parser::If) -> Result { let condition = self.eval(&if_stmt.condition)?; if eval_truthy(&condition) { @@ -269,7 +269,7 @@ impl<'a> Interpreter<'a> { } } - fn interpret_while(&mut self, stmt: &parser::While<'a>) -> Result, Error> { + fn interpret_while(&mut self, stmt: &parser::While) -> Result { let mut value = Value::Literal(Literal::Nil); while eval_truthy(&self.eval(&stmt.condition)?) { value = self.interpret_stmt(&stmt.body)?; @@ -278,14 +278,14 @@ impl<'a> Interpreter<'a> { Ok(value) } - fn interpret_function(&mut self, stmt: Rc>) -> Result, Error> { + fn interpret_function(&mut self, stmt: Rc) -> Result { let name = stmt.name.clone(); let value = Value::Callable(Callable::Function(stmt)); self.define_var(&name, value.clone())?; Ok(value) } - fn eval(&mut self, expr: &Expr<'a>) -> Result, Error> { + fn eval(&mut self, expr: &Expr) -> Result { match expr { Expr::Assign(assign) => self.eval_assign(assign), Expr::Literal(lit) => Ok(lit.clone().into()), @@ -298,7 +298,7 @@ impl<'a> Interpreter<'a> { } } - fn eval_unary(&mut self, expr: &parser::Unary<'a>) -> Result, Error> { + fn eval_unary(&mut self, expr: &parser::Unary) -> Result { let right = self.eval(&*expr.right)?; match (&expr.operator.kind, right) { @@ -317,7 +317,7 @@ impl<'a> Interpreter<'a> { } } - fn eval_binary(&mut self, expr: &parser::Binary<'a>) -> Result, Error> { + fn eval_binary(&mut self, expr: &parser::Binary) -> Result { let left = self.eval(&*expr.left)?.expect_literal()?; let right = self.eval(&*expr.right)?.expect_literal()?; @@ -361,13 +361,13 @@ impl<'a> Interpreter<'a> { Ok(result.into()) } - fn eval_assign(&mut self, assign: &parser::Assign<'a>) -> Result, Error> { + fn eval_assign(&mut self, assign: &parser::Assign) -> Result { let value = self.eval(&assign.value)?; self.assign_var(&assign.name, value.clone())?; Ok(value) } - fn eval_logical(&mut self, logical: &parser::Logical<'a>) -> Result, Error> { + fn eval_logical(&mut self, logical: &parser::Logical) -> Result { let left = eval_truthy(&self.eval(&logical.left)?); let right = eval_truthy(&self.eval(&logical.right)?); @@ -381,7 +381,7 @@ impl<'a> Interpreter<'a> { } } - fn eval_call(&mut self, call: &parser::Call<'a>) -> Result, Error> { + fn eval_call(&mut self, call: &parser::Call) -> Result { let callable = match self.eval(&call.callee)? { Value::Callable(c) => c, Value::Literal(v) => { diff --git a/users/tazjin/rlox/src/interpreter/tests.rs b/users/tazjin/rlox/src/interpreter/tests.rs index bf2cf61b0a..5bc9f0a0a4 100644 --- a/users/tazjin/rlox/src/interpreter/tests.rs +++ b/users/tazjin/rlox/src/interpreter/tests.rs @@ -1,11 +1,8 @@ use super::*; -fn code(code: &str) -> Vec { - code.chars().collect() -} - /// Evaluate a code snippet, returning a value. -fn parse_eval<'a>(chars: &'a [char]) -> Value<'a> { +fn parse_eval(code: &str) -> Value { + let chars: Vec = code.chars().collect(); let tokens = scanner::scan(&chars).expect("could not scan code"); let program = parser::parse(tokens).expect("could not parse code"); Interpreter::create() @@ -15,7 +12,7 @@ fn parse_eval<'a>(chars: &'a [char]) -> Value<'a> { #[test] fn test_if() { - let code = code( + let result = parse_eval( r#" if (42 > 23) "pass"; @@ -24,15 +21,12 @@ else "#, ); - assert_eq!( - Value::Literal(Literal::String("pass".into())), - parse_eval(&code) - ); + assert_eq!(Value::Literal(Literal::String("pass".into())), result,); } #[test] fn test_scope() { - let code = code( + let result = parse_eval( r#" var result = ""; @@ -54,26 +48,23 @@ var c = "global c"; assert_eq!( Value::Literal(Literal::String("inner a, outer b, global c".into())), - parse_eval(&code), + result, ); } #[test] fn test_binary_operators() { - assert_eq!( - Value::Literal(Literal::Number(42.0)), - parse_eval(&code("40 + 2;")) - ); + assert_eq!(Value::Literal(Literal::Number(42.0)), parse_eval("40 + 2;")); assert_eq!( Value::Literal(Literal::String("foobar".into())), - parse_eval(&code("\"foo\" + \"bar\";")) + parse_eval("\"foo\" + \"bar\";") ); } #[test] fn test_functions() { - let code = code( + let result = parse_eval( r#" fun add(a, b, c) { a + b + c; @@ -83,5 +74,5 @@ add(1, 2, 3); "#, ); - assert_eq!(Value::Literal(Literal::Number(6.0)), parse_eval(&code)); + assert_eq!(Value::Literal(Literal::Number(6.0)), result); } diff --git a/users/tazjin/rlox/src/main.rs b/users/tazjin/rlox/src/main.rs index 222eb4cd53..8970349bfa 100644 --- a/users/tazjin/rlox/src/main.rs +++ b/users/tazjin/rlox/src/main.rs @@ -25,12 +25,14 @@ fn main() { // Run Lox code from a file and print results to stdout fn run_file(file: &str) { let contents = fs::read_to_string(file).expect("failed to read the input file"); - run(&contents); + let mut lox = interpreter::Interpreter::create(); + run(&mut lox, &contents); } // Evaluate Lox code interactively in a shitty REPL. fn run_prompt() { let mut line = String::new(); + let mut lox = interpreter::Interpreter::create(); loop { print!("> "); @@ -38,14 +40,13 @@ fn run_prompt() { io::stdin() .read_line(&mut line) .expect("failed to read user input"); - run(&line); + run(&mut lox, &line); line.clear(); } } -fn run(code: &str) { +fn run(lox: &mut interpreter::Interpreter, code: &str) { let chars: Vec = code.chars().collect(); - let mut lox = interpreter::Interpreter::create(); match scanner::scan(&chars) { Ok(tokens) => match parser::parse(tokens) { diff --git a/users/tazjin/rlox/src/parser.rs b/users/tazjin/rlox/src/parser.rs index 78aaec105e..ed5e670ecb 100644 --- a/users/tazjin/rlox/src/parser.rs +++ b/users/tazjin/rlox/src/parser.rs @@ -12,27 +12,27 @@ use std::rc::Rc; // AST #[derive(Debug)] -pub struct Assign<'a> { - pub name: Token<'a>, - pub value: Box>, +pub struct Assign { + pub name: Token, + pub value: Box, } #[derive(Debug)] -pub struct Binary<'a> { - pub left: Box>, - pub operator: Token<'a>, - pub right: Box>, +pub struct Binary { + pub left: Box, + pub operator: Token, + pub right: Box, } #[derive(Debug)] -pub struct Logical<'a> { - pub left: Box>, - pub operator: Token<'a>, - pub right: Box>, +pub struct Logical { + pub left: Box, + pub operator: Token, + pub right: Box, } #[derive(Debug)] -pub struct Grouping<'a>(pub Box>); +pub struct Grouping(pub Box); #[derive(Debug, Clone, PartialEq)] pub enum Literal { @@ -43,73 +43,73 @@ pub enum Literal { } #[derive(Debug)] -pub struct Unary<'a> { - pub operator: Token<'a>, - pub right: Box>, +pub struct Unary { + pub operator: Token, + pub right: Box, } #[derive(Debug)] -pub struct Call<'a> { - pub callee: Box>, - pub paren: Token<'a>, - pub args: Vec>, +pub struct Call { + pub callee: Box, + pub paren: Token, + pub args: Vec, } // Not to be confused with `Var`, which is for assignment. #[derive(Debug)] -pub struct Variable<'a>(pub Token<'a>); +pub struct Variable(pub Token); #[derive(Debug)] -pub enum Expr<'a> { - Assign(Assign<'a>), - Binary(Binary<'a>), - Grouping(Grouping<'a>), +pub enum Expr { + Assign(Assign), + Binary(Binary), + Grouping(Grouping), Literal(Literal), - Unary(Unary<'a>), - Call(Call<'a>), - Variable(Variable<'a>), - Logical(Logical<'a>), + Unary(Unary), + Call(Call), + Variable(Variable), + Logical(Logical), } // Variable assignment. Not to be confused with `Variable`, which is // for access. #[derive(Debug)] -pub struct Var<'a> { - pub name: Token<'a>, - pub initialiser: Option>, +pub struct Var { + pub name: Token, + pub initialiser: Option, } #[derive(Debug)] -pub struct If<'a> { - pub condition: Expr<'a>, - pub then_branch: Box>, - pub else_branch: Option>>, +pub struct If { + pub condition: Expr, + pub then_branch: Box, + pub else_branch: Option>, } #[derive(Debug)] -pub struct While<'a> { - pub condition: Expr<'a>, - pub body: Box>, +pub struct While { + pub condition: Expr, + pub body: Box, } -pub type Block<'a> = Vec>; +pub type Block = Vec; #[derive(Debug)] -pub struct Function<'a> { - pub name: Token<'a>, - pub params: Vec>, - pub body: Block<'a>, +pub struct Function { + pub name: Token, + pub params: Vec, + pub body: Block, } #[derive(Debug)] -pub enum Statement<'a> { - Expr(Expr<'a>), - Print(Expr<'a>), - Var(Var<'a>), - Block(Block<'a>), - If(If<'a>), - While(While<'a>), - Function(Rc>), +pub enum Statement { + Expr(Expr), + Print(Expr), + Var(Var), + Block(Block), + If(If), + While(While), + Function(Rc), } // Parser @@ -162,18 +162,18 @@ primary → NUMBER | STRING | "true" | "false" | "nil" | "(" expression ")" ; */ -struct Parser<'a> { - tokens: Vec>, +struct Parser { + tokens: Vec, current: usize, } -type ExprResult<'a> = Result, Error>; -type StmtResult<'a> = Result, Error>; +type ExprResult = Result; +type StmtResult = Result; -impl<'a> Parser<'a> { +impl Parser { // recursive-descent parser functions - fn declaration(&mut self) -> StmtResult<'a> { + fn declaration(&mut self) -> StmtResult { if self.match_token(&TokenKind::Fun) { return self.function(); } @@ -185,7 +185,7 @@ impl<'a> Parser<'a> { self.statement() } - fn function(&mut self) -> StmtResult<'a> { + fn function(&mut self) -> StmtResult { let name = self.identifier("Expected function name.")?; self.consume( @@ -229,7 +229,7 @@ impl<'a> Parser<'a> { }))) } - fn var_declaration(&mut self) -> StmtResult<'a> { + fn var_declaration(&mut self) -> StmtResult { // Since `TokenKind::Identifier` carries data, we can't use // `consume`. let mut var = Var { @@ -245,7 +245,7 @@ impl<'a> Parser<'a> { Ok(Statement::Var(var)) } - fn statement(&mut self) -> StmtResult<'a> { + fn statement(&mut self) -> StmtResult { if self.match_token(&TokenKind::Print) { self.print_statement() } else if self.match_token(&TokenKind::LeftBrace) { @@ -261,14 +261,14 @@ impl<'a> Parser<'a> { } } - fn print_statement(&mut self) -> StmtResult<'a> { + fn print_statement(&mut self) -> StmtResult { let expr = self.expression()?; self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?; Ok(Statement::Print(expr)) } - fn block_statement(&mut self) -> Result, Error> { - let mut block: Block<'a> = vec![]; + fn block_statement(&mut self) -> Result { + let mut block: Block = vec![]; while !self.check_token(&TokenKind::RightBrace) && !self.is_at_end() { block.push(self.declaration()?); @@ -279,7 +279,7 @@ impl<'a> Parser<'a> { Ok(block) } - fn if_statement(&mut self) -> StmtResult<'a> { + fn if_statement(&mut self) -> StmtResult { self.consume( &TokenKind::LeftParen, ErrorKind::ExpectedToken("Expected '(' after 'if'"), @@ -305,7 +305,7 @@ impl<'a> Parser<'a> { Ok(Statement::If(stmt)) } - fn while_statement(&mut self) -> StmtResult<'a> { + fn while_statement(&mut self) -> StmtResult { self.consume( &TokenKind::LeftParen, ErrorKind::ExpectedToken("Expected '(' after 'while'"), @@ -324,7 +324,7 @@ impl<'a> Parser<'a> { })) } - fn for_statement(&mut self) -> StmtResult<'a> { + fn for_statement(&mut self) -> StmtResult { // Parsing of clauses ... self.consume( &TokenKind::LeftParen, @@ -379,17 +379,17 @@ impl<'a> Parser<'a> { Ok(body) } - fn expr_statement(&mut self) -> StmtResult<'a> { + fn expr_statement(&mut self) -> StmtResult { let expr = self.expression()?; self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?; Ok(Statement::Expr(expr)) } - fn expression(&mut self) -> ExprResult<'a> { + fn expression(&mut self) -> ExprResult { self.assignment() } - fn assignment(&mut self) -> ExprResult<'a> { + fn assignment(&mut self) -> ExprResult { let expr = self.logic_or()?; if self.match_token(&TokenKind::Equal) { @@ -412,7 +412,7 @@ impl<'a> Parser<'a> { Ok(expr) } - fn logic_or(&mut self) -> ExprResult<'a> { + fn logic_or(&mut self) -> ExprResult { let mut expr = self.logic_and()?; while self.match_token(&TokenKind::Or) { @@ -426,7 +426,7 @@ impl<'a> Parser<'a> { Ok(expr) } - fn logic_and(&mut self) -> ExprResult<'a> { + fn logic_and(&mut self) -> ExprResult { let mut expr = self.equality()?; while self.match_token(&TokenKind::And) { @@ -440,14 +440,14 @@ impl<'a> Parser<'a> { Ok(expr) } - fn equality(&mut self) -> ExprResult<'a> { + fn equality(&mut self) -> ExprResult { self.binary_operator( &[TokenKind::BangEqual, TokenKind::EqualEqual], Self::comparison, ) } - fn comparison(&mut self) -> ExprResult<'a> { + fn comparison(&mut self) -> ExprResult { self.binary_operator( &[ TokenKind::Greater, @@ -459,15 +459,15 @@ impl<'a> Parser<'a> { ) } - fn term(&mut self) -> ExprResult<'a> { + fn term(&mut self) -> ExprResult { self.binary_operator(&[TokenKind::Minus, TokenKind::Plus], Self::factor) } - fn factor(&mut self) -> ExprResult<'a> { + fn factor(&mut self) -> ExprResult { self.binary_operator(&[TokenKind::Slash, TokenKind::Star], Self::unary) } - fn unary(&mut self) -> ExprResult<'a> { + fn unary(&mut self) -> ExprResult { if self.match_token(&TokenKind::Bang) || self.match_token(&TokenKind::Minus) { return Ok(Expr::Unary(Unary { operator: self.previous().clone(), @@ -478,7 +478,7 @@ impl<'a> Parser<'a> { return self.call(); } - fn call(&mut self) -> ExprResult<'a> { + fn call(&mut self) -> ExprResult { let mut expr = self.primary()?; loop { @@ -492,7 +492,7 @@ impl<'a> Parser<'a> { Ok(expr) } - fn finish_call(&mut self, callee: Expr<'a>) -> ExprResult<'a> { + fn finish_call(&mut self, callee: Expr) -> ExprResult { let mut args = vec![]; if !self.check_token(&TokenKind::RightParen) { @@ -517,7 +517,7 @@ impl<'a> Parser<'a> { })) } - fn primary(&mut self) -> ExprResult<'a> { + fn primary(&mut self) -> ExprResult { let next = self.advance(); let literal = match next.kind { TokenKind::True => Literal::Boolean(true), @@ -538,7 +538,7 @@ impl<'a> Parser<'a> { eprintln!("encountered {:?}", unexpected); return Err(Error { line: next.line, - kind: ErrorKind::ExpectedExpression(next.lexeme.into_iter().collect()), + kind: ErrorKind::ExpectedExpression(next.lexeme), }); } }; @@ -548,7 +548,7 @@ impl<'a> Parser<'a> { // internal helpers - fn identifier(&mut self, err: &'static str) -> Result, Error> { + fn identifier(&mut self, err: &'static str) -> Result { if let TokenKind::Identifier(_) = self.peek().kind { Ok(self.advance()) } else { @@ -570,7 +570,7 @@ impl<'a> Parser<'a> { } /// Return the next token and advance parser state. - fn advance(&mut self) -> Token<'a> { + fn advance(&mut self) -> Token { if !self.is_at_end() { self.current += 1; } @@ -587,15 +587,15 @@ impl<'a> Parser<'a> { self.peek().kind == *token } - fn peek(&self) -> &Token<'a> { + fn peek(&self) -> &Token { &self.tokens[self.current] } - fn previous(&self) -> &Token<'a> { + fn previous(&self) -> &Token { &self.tokens[self.current - 1] } - fn consume(&mut self, kind: &TokenKind, err: ErrorKind) -> Result, Error> { + fn consume(&mut self, kind: &TokenKind, err: ErrorKind) -> Result { if self.check_token(kind) { return Ok(self.advance()); } @@ -634,8 +634,8 @@ impl<'a> Parser<'a> { fn binary_operator( &mut self, oneof: &[TokenKind], - each: fn(&mut Parser<'a>) -> ExprResult<'a>, - ) -> ExprResult<'a> { + each: fn(&mut Parser) -> ExprResult, + ) -> ExprResult { let mut expr = each(self)?; while oneof.iter().any(|t| self.match_token(t)) { @@ -650,9 +650,9 @@ impl<'a> Parser<'a> { } } -pub fn parse<'a>(tokens: Vec>) -> Result, Vec> { +pub fn parse(tokens: Vec) -> Result> { let mut parser = Parser { tokens, current: 0 }; - let mut program: Block<'a> = vec![]; + let mut program: Block = vec![]; let mut errors: Vec = vec![]; while !parser.is_at_end() { diff --git a/users/tazjin/rlox/src/scanner.rs b/users/tazjin/rlox/src/scanner.rs index fde4397531..36c2d6a966 100644 --- a/users/tazjin/rlox/src/scanner.rs +++ b/users/tazjin/rlox/src/scanner.rs @@ -53,15 +53,15 @@ pub enum TokenKind { } #[derive(Clone, Debug)] -pub struct Token<'a> { +pub struct Token { pub kind: TokenKind, - pub lexeme: &'a [char], + pub lexeme: String, pub line: usize, } struct Scanner<'a> { source: &'a [char], - tokens: Vec>, + tokens: Vec, errors: Vec, start: usize, // offset of first character in current lexeme current: usize, // current offset into source @@ -82,7 +82,7 @@ impl<'a> Scanner<'a> { let lexeme = &self.source[self.start..self.current]; self.tokens.push(Token { kind, - lexeme, + lexeme: lexeme.into_iter().collect(), line: self.line, }) } @@ -263,7 +263,7 @@ impl<'a> Scanner<'a> { } } -pub fn scan<'a>(input: &'a [char]) -> Result>, Vec> { +pub fn scan<'a>(input: &'a [char]) -> Result, Vec> { let mut scanner = Scanner { source: &input, tokens: vec![], -- cgit 1.4.1