//! Delimiters for the lexer. use std::collections::HashSet; use std::iter::FromIterator; pub trait Inner { fn new_with_inner(v: HashSet) -> Self; fn inner(&self) -> &HashSet; } /// The interface of [DiscardDelimiters] and /// [KeepDelimiters]. pub trait Delimiters: Inner + Sized { /// Construct a new set of delimiters from /// a list of characters. fn new(vec: Vec) -> Self { Self::new_with_inner( HashSet::from_iter(vec.iter().cloned()) ) } /// Check if the set of delimiters contains a /// particular delimiter character. fn contains(&self, ch: char) -> bool { self.inner().contains(&ch) } } /// Set of all delimiter characters that will not /// become tokens through [Tokens::new](crate::lexer::Tokens::new). pub struct DiscardDelimiters(HashSet); impl Inner for DiscardDelimiters { fn new_with_inner(v: HashSet) -> Self { Self(v) } fn inner(&self) -> &HashSet { &self.0 } } impl Delimiters for DiscardDelimiters {} /// Set of all delimiter characters that will /// also become tokens through [Tokens::new](crate::lexer::Tokens::new). pub struct KeepDelimiters(HashSet); impl Delimiters for KeepDelimiters {} impl Inner for KeepDelimiters { fn new_with_inner(v: HashSet) -> Self { Self(v) } fn inner(&self) -> &HashSet { &self.0 } } /// Group the two delimiter sets. pub struct DelimitersParam { /// The set of delimiters to keep. pub keep: KeepDelimiters, /// The set of delimiters to discard. pub discard: DiscardDelimiters }