diff options
author | Greg Brown <gmb60@cam.ac.uk> | 2021-01-08 18:00:11 +0000 |
---|---|---|
committer | Greg Brown <gmb60@cam.ac.uk> | 2021-01-08 18:00:11 +0000 |
commit | e1452227b8bd9ad3805480f8a5a66a75fb8370dd (patch) | |
tree | b02c9dfdc157d753e3f1c8a09bbd2ffb0bbfcc36 /src/chomp/mod.rs | |
parent | fe2eac31d9dbec772796c3ea75be32e9cd01b810 (diff) |
Do more restructuring.
Diffstat (limited to 'src/chomp/mod.rs')
-rw-r--r-- | src/chomp/mod.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/chomp/mod.rs b/src/chomp/mod.rs index bb31b6f..1e30738 100644 --- a/src/chomp/mod.rs +++ b/src/chomp/mod.rs @@ -1,3 +1,8 @@ +use std::{fmt, hash}; + +use proc_macro2::{Ident, Span}; +use syn::ext::IdentExt; + pub mod ast; pub mod check; pub mod context; @@ -5,3 +10,77 @@ pub mod error; pub mod set; pub mod typed; pub mod visit; + +#[derive(Clone, Debug)] +pub enum Name { + Spanned(Ident), + Spanless(String), +} + +impl Name { + pub fn span(&self) -> Option<Span> { + match self { + Self::Spanned(i) => Some(i.span()), + Self::Spanless(_) => None, + } + } + + pub fn as_ident(self, span: Span) -> Ident { + match self { + Self::Spanned(i) => i, + Self::Spanless(s) => Ident::new(&s, span), + } + } +} + +impl PartialEq for Name { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Self::Spanned(me), Self::Spanned(them)) => me == them, + (Self::Spanned(me), Self::Spanless(them)) => me.unraw() == them, + (Self::Spanless(me), Self::Spanned(them)) => them.unraw() == me, + (Self::Spanless(me), Self::Spanless(them)) => me == them, + } + } +} + +impl<T: AsRef<str>> PartialEq<T> for Name { + fn eq(&self, other: &T) -> bool { + match self { + Name::Spanned(me) => me.unraw() == other, + Name::Spanless(me) => me == other.as_ref(), + } + } +} + +impl Eq for Name {} + +impl hash::Hash for Name { + fn hash<H: hash::Hasher>(&self, state: &mut H) { + match self { + Self::Spanned(i) => i.unraw().to_string().hash(state), + Self::Spanless(s) => s.hash(state), + } + } +} + +impl fmt::Display for Name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Name::Spanned(i) => i.fmt(f), + Name::Spanless(s) => s.fmt(f), + } + } +} + +impl From<Ident> for Name { + fn from(ident: Ident) -> Self { + Self::Spanned(ident) + } +} + +impl From<String> for Name { + fn from(string: String) -> Self { + Self::Spanless(string) + } +} |