Skip to content

Commit

Permalink
Remove keywords map
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Jan 20, 2025
1 parent 2c2986b commit 040dcb0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 65 deletions.
69 changes: 4 additions & 65 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<usize>,
}
Expand Down Expand Up @@ -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');
Expand All @@ -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(),
}
Expand Down Expand Up @@ -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)),
Expand Down
62 changes: 62 additions & 0 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub enum Token {
And,
As,
Block,
Boxed,
Do,
Effect,
Else,
Expand All @@ -104,6 +105,7 @@ pub enum Token {
In,
Loop,
Match,
Methods,
Module,
Not,
Or,
Expand Down Expand Up @@ -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'"),
Expand All @@ -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'"),
Expand Down Expand Up @@ -343,3 +347,61 @@ impl Display for Token {
}
}
}

pub fn lookup_keyword(word: &str) -> Option<Token> {
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,
}
}

0 comments on commit 040dcb0

Please sign in to comment.