From 34525ec77a5945f874d4d27069614c005e09b1b6 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 20 Oct 2023 15:34:54 -0500 Subject: [PATCH] lexer: Only allow lines up to 4GB in size. Saves 8 bytes per token --- libconlang/src/lexer.rs | 8 ++++---- libconlang/src/token.rs | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/libconlang/src/lexer.rs b/libconlang/src/lexer.rs index 7bd72d0..0bdf198 100644 --- a/libconlang/src/lexer.rs +++ b/libconlang/src/lexer.rs @@ -23,8 +23,8 @@ impl<'t> IntoIterator for Lexer<'t> { pub struct Lexer<'t> { text: &'t str, cursor: usize, - line: usize, - col: usize, + line: u32, + col: u32, } /// Implements the non-terminals of a language impl<'t> Lexer<'t> { @@ -41,12 +41,12 @@ impl<'t> Lexer<'t> { #[inline] fn count_len(&mut self, len: usize) -> &mut Self { self.cursor += len; - self.col += len; + self.col += len as u32; self } /// Counts a line #[inline] - fn count_line(&mut self, lines: usize) -> &mut Self { + fn count_line(&mut self, lines: u32) -> &mut Self { self.line += lines; self.col = 1; self diff --git a/libconlang/src/token.rs b/libconlang/src/token.rs index e92808a..19db027 100644 --- a/libconlang/src/token.rs +++ b/libconlang/src/token.rs @@ -2,7 +2,7 @@ use std::ops::Range; mod token_type; -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Type { // Invalid syntax Invalid, @@ -72,7 +72,7 @@ pub enum Type { } /// Represents a reserved word. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Keyword { Break, Continue, @@ -88,16 +88,16 @@ pub enum Keyword { While, } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct Token { ty: Type, pub head: usize, pub tail: usize, - line: usize, - col: usize, + line: u32, + col: u32, } impl Token { - pub fn new(ty: Type, head: usize, tail: usize, line: usize, col: usize) -> Self { + pub fn new(ty: Type, head: usize, tail: usize, line: u32, col: u32) -> Self { Self { ty, head, tail, line, col } } /// Cast this [Token] to a new [Type] @@ -109,11 +109,11 @@ impl Token { Self { head, tail, ..self } } /// Gets the line from this token - pub fn line(&self) -> usize { + pub fn line(&self) -> u32 { self.line } /// Gets the column from this token - pub fn col(&self) -> usize { + pub fn col(&self) -> u32 { self.col } pub fn is_empty(&self) -> bool {