use alloc::string::String;
use alloc::sync::Arc;
use core::fmt;
use crate::source::Span;
use crate::spec::CallableSpec;
use crate::state::Lang;
use super::error::TokenErrorKind;
pub struct SpecialsMatch<L: Lang> {
pub end: usize,
pub callable_type: L::CallableTypeId,
pub spec: Arc<dyn CallableSpec<L>>,
}
impl<L: Lang> fmt::Debug for SpecialsMatch<L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SpecialsMatch")
.field("end", &self.end)
.field("callable_type", &self.callable_type)
.field("spec", &self.spec)
.finish()
}
}
#[derive(Debug, Clone)]
pub struct SpecialsScanError {
pub kind: TokenErrorKind,
pub span: Span,
}
impl fmt::Display for SpecialsScanError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} (at bytes {}..{})", self.kind, self.span.start(), self.span.end())
}
}
impl core::error::Error for SpecialsScanError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TriggerChars {
Only(String),
Any,
}
impl TriggerChars {
pub fn may_start(&self, c: char) -> bool {
match self {
TriggerChars::Only(chars) => chars.contains(c),
TriggerChars::Any => true,
}
}
#[must_use]
pub fn union(&self, other: &TriggerChars) -> TriggerChars {
match (self, other) {
(TriggerChars::Any, _) | (_, TriggerChars::Any) => TriggerChars::Any,
(TriggerChars::Only(a), TriggerChars::Only(b)) => {
let mut chars = a.clone();
chars.extend(b.chars().filter(|&c| !a.contains(c)));
TriggerChars::Only(chars)
}
}
}
}
impl Default for TriggerChars {
fn default() -> Self {
TriggerChars::Only(String::new())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn trigger_chars_membership() {
let none = TriggerChars::default();
assert!(!none.may_start('~'));
let some = TriggerChars::Only("~&".into());
assert!(some.may_start('~'));
assert!(some.may_start('&'));
assert!(!some.may_start('x'));
assert!(TriggerChars::Any.may_start('x'));
}
}