use std::fmt::{Display, Formatter, Result};
use std::sync::Arc;
use crate::{Span, Spanned};
use super::{Lex, TokenStream, TokenTree};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Spanned)]
pub struct Punct {
span: Span,
}
impl Punct {
pub fn from_span(span: Span) -> Self {
assert!(
span.text().is_some(),
"punctuation spans must name source text"
);
Self { span }
}
pub fn new(text: impl Into<Arc<str>>, span: Span) -> Self {
Self {
span: span.with_text(text),
}
}
pub fn text(&self) -> &str {
self.span
.text()
.expect("punctuation tokens must have source-backed spans")
}
}
impl Display for Punct {
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
formatter.write_str(self.text())
}
}
impl Lex for Punct {
fn to_tokens(&self, output: &mut TokenStream) {
output.0.push(TokenTree::Punct(self.clone()));
}
}