From 7b423fe18aca238318814c4c1fed3e3d99dee9d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vili=20Sinerv=C3=A4?= Date: Sat, 18 Jan 2025 19:13:55 +0200 Subject: [PATCH] Add location info to tokens --- src/compiler/token.rs | 15 +++++++++------ src/compiler/tokenizer.rs | 16 +++++++++------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/compiler/token.rs b/src/compiler/token.rs index 53ef9c5..dae89b9 100644 --- a/src/compiler/token.rs +++ b/src/compiler/token.rs @@ -1,21 +1,24 @@ #[derive(Debug, Copy, Clone)] pub struct CodeLocation { - row: i32, - col: i32, + start: usize, + end: usize, } impl CodeLocation { - pub fn new(row: i32, col: i32) -> Self { - Self { row, col } + pub fn new(start: usize, end: usize) -> Self { + Self { start, end } } } impl PartialEq for CodeLocation { fn eq(&self, other: &Self) -> bool { - let true_match = self.row == other.row && self.col == other.col; + let true_match = self.start == other.start && self.end == other.end; // For testing purposes - let simulated_match = self.row < 0 || self.col < 0 || other.row < 0 || other.col < 0; + let simulated_match = self.start == usize::MAX + || self.end == usize::MAX + || other.start == usize::MAX + || other.end == usize::MAX; true_match || simulated_match } diff --git a/src/compiler/tokenizer.rs b/src/compiler/tokenizer.rs index 19ca518..93e2f30 100644 --- a/src/compiler/tokenizer.rs +++ b/src/compiler/tokenizer.rs @@ -31,10 +31,12 @@ pub fn tokenize(code: &str) -> Vec { if let Some(token) = found_match { if !token_type.ignore() { + let start = pos + token.start(); + let end = pos + token.end(); tokens.push(Token::new( - &code[pos + token.start()..pos + token.end()], + &code[start..end], *token_type, - CodeLocation::new(0, 0), + CodeLocation::new(start, end), )); } @@ -57,7 +59,7 @@ mod tests { #[test] fn test_tokenize_basic() { - let loc = CodeLocation::new(-1, -1); + let loc = CodeLocation::new(usize::MAX, usize::MAX); let result = tokenize("if 3 \n\twhile"); use TokenType::*; @@ -73,7 +75,7 @@ mod tests { #[test] fn test_tokenize_comment() { - let loc = CodeLocation::new(-1, -1); + let loc = CodeLocation::new(usize::MAX, usize::MAX); let result = tokenize("if 3 \n\n\\\\Comment\n#Another\n\twhile"); use TokenType::*; @@ -89,7 +91,7 @@ mod tests { #[test] fn test_tokenize_operators_basic() { - let loc = CodeLocation::new(-1, -1); + let loc = CodeLocation::new(usize::MAX, usize::MAX); let result = tokenize("var = 1 + 2"); use TokenType::*; @@ -107,7 +109,7 @@ mod tests { #[test] fn test_tokenize_operators_all() { - let loc = CodeLocation::new(-1, -1); + let loc = CodeLocation::new(usize::MAX, usize::MAX); let result = tokenize("var 1 + - * 1/2 = == != < <= > >= 2"); use TokenType::*; @@ -136,7 +138,7 @@ mod tests { #[test] fn test_tokenize_punctuation_basic() { - let loc = CodeLocation::new(-1, -1); + let loc = CodeLocation::new(usize::MAX, usize::MAX); let result = tokenize("{var = (1 + 2, 3);}"); use TokenType::*;