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/scanner.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'users/tazjin/rlox/src/scanner.rs') diff --git a/users/tazjin/rlox/src/scanner.rs b/users/tazjin/rlox/src/scanner.rs index fde43975316f..36c2d6a966ae 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