From 040dcb0f15aa71f1e5f9687d6e92f38e81d8500d Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Mon, 20 Jan 2025 11:49:46 -0600 Subject: [PATCH] Remove keywords map --- src/lexer/mod.rs | 69 +++------------------------------------------- src/lexer/token.rs | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 65 deletions(-) diff --git a/src/lexer/mod.rs b/src/lexer/mod.rs index 7f48ebbe..2a02a5b8 100644 --- a/src/lexer/mod.rs +++ b/src/lexer/mod.rs @@ -39,10 +39,9 @@ pub mod token; use crate::error::location::{EndPosition, Locatable, Location, Position}; -use std::collections::HashMap; use std::path::Path; use std::str::Chars; -use token::{FloatKind, IntegerKind, LexerError, Token}; +use token::{lookup_keyword, FloatKind, IntegerKind, LexerError, Token}; #[derive(Clone)] struct OpenBraces { @@ -64,7 +63,6 @@ pub struct Lexer<'cache, 'contents> { return_newline: bool, // Hack to always return a newline after an Unindent token previous_token_expects_indent: bool, chars: Chars<'contents>, - keywords: HashMap<&'static str, Token>, open_braces: OpenBraces, pending_interpolations: Vec, } @@ -100,64 +98,6 @@ impl<'cache, 'contents> Locatable<'cache> for Lexer<'cache, 'contents> { type IterElem<'a> = Option<(Token, Location<'a>)>; impl<'cache, 'contents> Lexer<'cache, 'contents> { - pub fn get_keywords() -> HashMap<&'static str, Token> { - vec![ - ("I8", Token::IntegerType(IntegerKind::I8)), - ("I16", Token::IntegerType(IntegerKind::I16)), - ("I32", Token::IntegerType(IntegerKind::I32)), - ("I64", Token::IntegerType(IntegerKind::I64)), - ("Isz", Token::IntegerType(IntegerKind::Isz)), - ("U8", Token::IntegerType(IntegerKind::U8)), - ("U16", Token::IntegerType(IntegerKind::U16)), - ("U32", Token::IntegerType(IntegerKind::U32)), - ("U64", Token::IntegerType(IntegerKind::U64)), - ("Usz", Token::IntegerType(IntegerKind::Usz)), - ("F32", Token::FloatType(FloatKind::F32)), - ("F64", Token::FloatType(FloatKind::F64)), - ("Int", Token::PolymorphicIntType), - ("Float", Token::PolymorphicFloatType), - ("Char", Token::CharType), - ("String", Token::StringType), - ("Ptr", Token::PointerType), - ("Bool", Token::BooleanType), - ("Unit", Token::UnitType), - ("mut", Token::Mut), - ("true", Token::BooleanLiteral(true)), - ("false", Token::BooleanLiteral(false)), - ("and", Token::And), - ("as", Token::As), - ("block", Token::Block), - ("do", Token::Do), - ("effect", Token::Effect), - ("else", Token::Else), - ("extern", Token::Extern), - ("fn", Token::Fn), - ("given", Token::Given), - ("handle", Token::Handle), - ("if", Token::If), - ("impl", Token::Impl), - ("import", Token::Import), - ("in", Token::In), - ("loop", Token::Loop), - ("match", Token::Match), - ("module", Token::Module), - ("not", Token::Not), - ("or", Token::Or), - ("owned", Token::Owned), - ("return", Token::Return), - ("ref", Token::Ref), - ("return", Token::Return), - ("shared", Token::Shared), - ("then", Token::Then), - ("trait", Token::Trait), - ("type", Token::Type), - ("while", Token::While), - ("with", Token::With), - ] - .into_iter() - .collect() - } - pub fn new(filename: &'cache Path, file_contents: &'contents str) -> Lexer<'cache, 'contents> { let mut chars = file_contents.chars(); let current = chars.next().unwrap_or('\0'); @@ -174,7 +114,6 @@ impl<'cache, 'contents> Lexer<'cache, 'contents> { return_newline: false, previous_token_expects_indent: false, chars, - keywords: Lexer::get_keywords(), open_braces: OpenBraces { parenthesis: 0, curly: 0, square: 0 }, pending_interpolations: Vec::new(), } @@ -340,10 +279,10 @@ impl<'cache, 'contents> Lexer<'cache, 'contents> { let word = self.advance_while(|current, _| current.is_alphanumeric() || current == '_'); let location = self.locate(); - match self.keywords.get(word) { + match lookup_keyword(word) { Some(keyword) => { - self.previous_token_expects_indent = Lexer::should_expect_indent_after_token(keyword); - Some((keyword.clone(), location)) + self.previous_token_expects_indent = Lexer::should_expect_indent_after_token(&keyword); + Some((keyword, location)) }, None if is_type => Some((Token::TypeName(word.to_owned()), location)), None => Some((Token::Identifier(word.to_owned()), location)), diff --git a/src/lexer/token.rs b/src/lexer/token.rs index 730f24d3..0e7664a8 100644 --- a/src/lexer/token.rs +++ b/src/lexer/token.rs @@ -91,6 +91,7 @@ pub enum Token { And, As, Block, + Boxed, Do, Effect, Else, @@ -104,6 +105,7 @@ pub enum Token { In, Loop, Match, + Methods, Module, Not, Or, @@ -275,6 +277,7 @@ impl Display for Token { Token::And => write!(f, "'and'"), Token::As => write!(f, "'as'"), Token::Block => write!(f, "'block'"), + Token::Boxed => write!(f, "'boxed'"), Token::Do => write!(f, "'do'"), Token::Effect => write!(f, "'effect'"), Token::Else => write!(f, "'else'"), @@ -288,6 +291,7 @@ impl Display for Token { Token::In => write!(f, "'in'"), Token::Loop => write!(f, "'loop'"), Token::Match => write!(f, "'match'"), + Token::Methods => write!(f, "'methods'"), Token::Module => write!(f, "'module'"), Token::Not => write!(f, "'not'"), Token::Or => write!(f, "'or'"), @@ -343,3 +347,61 @@ impl Display for Token { } } } + +pub fn lookup_keyword(word: &str) -> Option { + match word { + "I8" => Some(Token::IntegerType(IntegerKind::I8)), + "I16" => Some(Token::IntegerType(IntegerKind::I16)), + "I32" => Some(Token::IntegerType(IntegerKind::I32)), + "I64" => Some(Token::IntegerType(IntegerKind::I64)), + "Isz" => Some(Token::IntegerType(IntegerKind::Isz)), + "U8" => Some(Token::IntegerType(IntegerKind::U8)), + "U16" => Some(Token::IntegerType(IntegerKind::U16)), + "U32" => Some(Token::IntegerType(IntegerKind::U32)), + "U64" => Some(Token::IntegerType(IntegerKind::U64)), + "Usz" => Some(Token::IntegerType(IntegerKind::Usz)), + "F32" => Some(Token::FloatType(FloatKind::F32)), + "F64" => Some(Token::FloatType(FloatKind::F64)), + "Int" => Some(Token::PolymorphicIntType), + "Float" => Some(Token::PolymorphicFloatType), + "Char" => Some(Token::CharType), + "String" => Some(Token::StringType), + "Ptr" => Some(Token::PointerType), + "Bool" => Some(Token::BooleanType), + "Unit" => Some(Token::UnitType), + "mut" => Some(Token::Mut), + "true" => Some(Token::BooleanLiteral(true)), + "false" => Some(Token::BooleanLiteral(false)), + "and" => Some(Token::And), + "as" => Some(Token::As), + "block" => Some(Token::Block), + "boxed" => Some(Token::Boxed), + "do" => Some(Token::Do), + "effect" => Some(Token::Effect), + "else" => Some(Token::Else), + "extern" => Some(Token::Extern), + "fn" => Some(Token::Fn), + "given" => Some(Token::Given), + "handle" => Some(Token::Handle), + "if" => Some(Token::If), + "impl" => Some(Token::Impl), + "import" => Some(Token::Import), + "in" => Some(Token::In), + "loop" => Some(Token::Loop), + "match" => Some(Token::Match), + "methods" => Some(Token::Methods), + "module" => Some(Token::Module), + "not" => Some(Token::Not), + "or" => Some(Token::Or), + "owned" => Some(Token::Owned), + "ref" => Some(Token::Ref), + "return" => Some(Token::Return), + "shared" => Some(Token::Shared), + "then" => Some(Token::Then), + "trait" => Some(Token::Trait), + "type" => Some(Token::Type), + "while" => Some(Token::While), + "with" => Some(Token::With), + _ => None, + } +}