From 421a33c42c74f72b7d7834b090972d066de24f7d Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 14 Oct 2021 02:47:19 -0700 Subject: [PATCH] Use TryFrom<&str> for Tokens --- Cargo.lock | 2 -- schala-lang/language/Cargo.toml | 2 -- schala-lang/language/src/lib.rs | 4 ---- schala-lang/language/src/tokenizing.rs | 23 ++++++++++++----------- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1071a61..8ae5c95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -836,8 +836,6 @@ dependencies = [ "ena", "failure", "itertools", - "lazy_static 1.4.0", - "maplit", "radix_trie", "schala-lang-codegen", "schala-repl", diff --git a/schala-lang/language/Cargo.toml b/schala-lang/language/Cargo.toml index 39660be..a7fcefa 100644 --- a/schala-lang/language/Cargo.toml +++ b/schala-lang/language/Cargo.toml @@ -8,8 +8,6 @@ resolver = "2" [dependencies] itertools = "0.10" take_mut = "0.2.2" -maplit = "1.0.1" -lazy_static = "1.3.0" failure = "0.1.5" ena = "0.11.0" stopwatch = "0.0.7" diff --git a/schala-lang/language/src/lib.rs b/schala-lang/language/src/lib.rs index 39736f2..6c7f100 100644 --- a/schala-lang/language/src/lib.rs +++ b/schala-lang/language/src/lib.rs @@ -6,10 +6,6 @@ //! It defines the `Schala` type, which contains the state for a Schala REPL, and implements //! `ProgrammingLanguageInterface` and the chain of compiler passes for it. -#[macro_use] -extern crate lazy_static; -#[macro_use] -extern crate maplit; extern crate schala_repl; #[macro_use] extern crate schala_lang_codegen; diff --git a/schala-lang/language/src/tokenizing.rs b/schala-lang/language/src/tokenizing.rs index 28f4a8e..b83a5d8 100644 --- a/schala-lang/language/src/tokenizing.rs +++ b/schala-lang/language/src/tokenizing.rs @@ -1,8 +1,5 @@ use itertools::Itertools; -use std::collections::HashMap; -use std::rc::Rc; -use std::iter::{Iterator, Peekable}; -use std::fmt; +use std::{iter::{Iterator, Peekable}, convert::TryFrom, rc::Rc, fmt}; use crate::source_map::Location; @@ -63,9 +60,11 @@ pub enum Kw { Module, Import } -lazy_static! { - static ref KEYWORDS: HashMap<&'static str, Kw> = - hashmap! { +impl TryFrom<&str> for Kw { + type Error = (); + + fn try_from(value: &str) -> Result { + Ok(match value { "if" => Kw::If, "then" => Kw::Then, "else" => Kw::Else, @@ -88,7 +87,9 @@ lazy_static! { "false" => Kw::False, "module" => Kw::Module, "import" => Kw::Import, - }; + _ => return Err(()), + }) + } } #[derive(Debug, Clone, PartialEq)] @@ -239,9 +240,9 @@ fn handle_alphabetic(c: char, input: &mut Peekable> } } - match KEYWORDS.get(buf.as_str()) { - Some(kw) => TokenKind::Keyword(*kw), - None => TokenKind::Identifier(Rc::new(buf)), + match Kw::try_from(buf.as_str()) { + Ok(kw) => TokenKind::Keyword(kw), + Err(()) => TokenKind::Identifier(Rc::new(buf)), } }