use std::collections::BTreeSet;
use derivative::Derivative;
use crate::lex::TokenSrc;
use crate::syntax::{FirstSet, TerminalSet};
use crate::Lexicon;
#[derive(Debug, Derivative, PartialEq, Clone)]
#[derivative(Default(new = "true", bound = ""))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FollowSet<L: Lexicon>(TerminalSet<L>);
impl<L: Lexicon> std::fmt::Display for FollowSet<L> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<L: Lexicon> From<TerminalSet<L>> for FollowSet<L> {
fn from(set: TerminalSet<L>) -> Self {
Self(set)
}
}
impl<L: Lexicon> FollowSet<L> {
#[inline]
pub fn insert_eof(&mut self) {
self.0.insert_e();
}
#[inline]
pub fn contains_eof(&self) -> bool {
self.0.contains_e()
}
#[inline]
pub fn insert(&mut self, ty: L, lit: Option<&'static str>) -> bool {
self.0.insert(ty, lit)
}
#[inline]
pub fn contains(&self, token: Option<TokenSrc<'_, L>>) -> bool {
self.0.contains(token)
}
#[inline]
pub fn union_first(&mut self, other: &FirstSet<L>) -> bool {
self.0.union(other.as_terminal_set(), false)
}
#[inline]
pub fn union_follow(&mut self, other: &Self) -> bool {
self.0.union(&other.0, true)
}
#[inline]
pub fn intersects_first(&self, other: &FirstSet<L>) -> bool {
self.0.intersects(other.as_terminal_set(), false)
}
#[inline]
pub fn intersection_repr_first(&self, other: &FirstSet<L>) -> BTreeSet<String> {
self.0.intersection_repr(other.as_terminal_set(), false)
}
#[inline]
pub(crate) fn as_terminal_set(&self) -> &TerminalSet<L> {
&self.0
}
}