use std::collections::HashMap;
use std::fmt;
use std::sync::{LazyLock, Mutex, MutexGuard};
use crate::Span;
static INTERNER: LazyLock<Mutex<Interner>> = LazyLock::new(Default::default);
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Symbol(u32);
impl Symbol {
pub fn intern(string: &str) -> Self {
interner().intern(string)
}
pub fn as_str(self) -> &'static str {
interner().strings[self.0 as usize]
}
}
impl fmt::Debug for Symbol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), f)
}
}
impl fmt::Display for Symbol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Ident {
pub name: Symbol,
pub span: Span,
}
impl Ident {
pub fn new(name: Symbol, span: Span) -> Self {
Self { name, span }
}
}
impl fmt::Debug for Ident {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}@{:?}", self.name, self.span)
}
}
impl fmt::Display for Ident {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.name, f)
}
}
#[derive(Default)]
struct Interner {
symbols: HashMap<&'static str, Symbol>,
strings: Vec<&'static str>,
}
impl Interner {
fn intern(&mut self, string: &str) -> Symbol {
if let Some(&symbol) = self.symbols.get(string) {
return symbol;
}
let index = u32::try_from(self.strings.len()).expect("too many interned symbols");
let symbol = Symbol(index);
let string: &'static str = Box::leak(string.into());
self.strings.push(string);
self.symbols.insert(string, symbol);
symbol
}
}
fn interner() -> MutexGuard<'static, Interner> {
INTERNER
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn interning_is_idempotent() {
assert_eq!(Symbol::intern("foo"), Symbol::intern("foo"));
assert_ne!(Symbol::intern("foo"), Symbol::intern("bar"));
}
#[test]
fn round_trips() {
assert_eq!(Symbol::intern("α_beta").as_str(), "α_beta");
}
#[test]
fn formatting() {
let ident = Ident::new(Symbol::intern("main"), Span::new(3, 7));
assert_eq!(ident.to_string(), "main");
assert_eq!(format!("{ident:?}"), r#""main"@3..7"#);
}
}