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",
"failure",
"itertools",
"lazy_static 1.4.0",
"maplit",
"radix_trie",
"schala-lang-codegen",
"schala-repl",

View File

@ -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"

View File

@ -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;

View File

@ -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<Self, Self::Error> {
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<impl Iterator<Item=CharData>>
}
}
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)),
}
}