Use TryFrom<&str> for Tokens

This commit is contained in:
Greg Shuflin 2021-10-14 02:47:19 -07:00
parent 61e2acc338
commit 421a33c42c
4 changed files with 12 additions and 19 deletions

2
Cargo.lock generated
View File

@ -836,8 +836,6 @@ dependencies = [
"ena", "ena",
"failure", "failure",
"itertools", "itertools",
"lazy_static 1.4.0",
"maplit",
"radix_trie", "radix_trie",
"schala-lang-codegen", "schala-lang-codegen",
"schala-repl", "schala-repl",

View File

@ -8,8 +8,6 @@ resolver = "2"
[dependencies] [dependencies]
itertools = "0.10" itertools = "0.10"
take_mut = "0.2.2" take_mut = "0.2.2"
maplit = "1.0.1"
lazy_static = "1.3.0"
failure = "0.1.5" failure = "0.1.5"
ena = "0.11.0" ena = "0.11.0"
stopwatch = "0.0.7" stopwatch = "0.0.7"

View File

@ -6,10 +6,6 @@
//! It defines the `Schala` type, which contains the state for a Schala REPL, and implements //! It defines the `Schala` type, which contains the state for a Schala REPL, and implements
//! `ProgrammingLanguageInterface` and the chain of compiler passes for it. //! `ProgrammingLanguageInterface` and the chain of compiler passes for it.
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate maplit;
extern crate schala_repl; extern crate schala_repl;
#[macro_use] #[macro_use]
extern crate schala_lang_codegen; extern crate schala_lang_codegen;

View File

@ -1,8 +1,5 @@
use itertools::Itertools; use itertools::Itertools;
use std::collections::HashMap; use std::{iter::{Iterator, Peekable}, convert::TryFrom, rc::Rc, fmt};
use std::rc::Rc;
use std::iter::{Iterator, Peekable};
use std::fmt;
use crate::source_map::Location; use crate::source_map::Location;
@ -63,9 +60,11 @@ pub enum Kw {
Module, Import Module, Import
} }
lazy_static! { impl TryFrom<&str> for Kw {
static ref KEYWORDS: HashMap<&'static str, Kw> = type Error = ();
hashmap! {
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
"if" => Kw::If, "if" => Kw::If,
"then" => Kw::Then, "then" => Kw::Then,
"else" => Kw::Else, "else" => Kw::Else,
@ -88,7 +87,9 @@ lazy_static! {
"false" => Kw::False, "false" => Kw::False,
"module" => Kw::Module, "module" => Kw::Module,
"import" => Kw::Import, "import" => Kw::Import,
}; _ => return Err(()),
})
}
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -239,9 +240,9 @@ fn handle_alphabetic(c: char, input: &mut Peekable<impl Iterator<Item=CharData>>
} }
} }
match KEYWORDS.get(buf.as_str()) { match Kw::try_from(buf.as_str()) {
Some(kw) => TokenKind::Keyword(*kw), Ok(kw) => TokenKind::Keyword(kw),
None => TokenKind::Identifier(Rc::new(buf)), Err(()) => TokenKind::Identifier(Rc::new(buf)),
} }
} }