use alloc::sync::Arc;
use alloc::vec::Vec;
use core::hash::{Hash, Hasher};
use hashbrown::Equivalent;
use crate::state::{
FeaturePresence, Lang, LangFeatures, ParsingState, ParsingStateDelta, TokenRulesOverrides,
};
use crate::token::GroupRule;
pub(super) type StateMemo<L> = hashbrown::HashMap<StateMemoKey<L>, Arc<ParsingState<L>>>;
pub(super) type GroupInteriorMemo<L> =
hashbrown::HashMap<GroupInteriorKey<L>, GroupInteriorEntry<L>>;
pub(super) struct GroupInteriorEntry<L: Lang> {
pub(super) state: Arc<ParsingState<L>>,
pub(super) delta: Arc<ParsingStateDelta<L>>,
}
pub(super) struct GroupInteriorKey<L: Lang> {
pub(super) base: Arc<ParsingState<L>>,
pub(super) rule: Arc<GroupRule<L>>,
}
pub(super) struct GroupInteriorProbe<'a, L: Lang> {
pub(super) base: &'a Arc<ParsingState<L>>,
pub(super) rule: &'a Arc<GroupRule<L>>,
}
impl<L: Lang> PartialEq for GroupInteriorKey<L> {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.base, &other.base) && Arc::ptr_eq(&self.rule, &other.rule)
}
}
impl<L: Lang> Eq for GroupInteriorKey<L> {}
impl<L: Lang> Hash for GroupInteriorKey<L> {
fn hash<H: Hasher>(&self, state: &mut H) {
arc_addr(&self.base).hash(state);
arc_addr(&self.rule).hash(state);
}
}
impl<L: Lang> Hash for GroupInteriorProbe<'_, L> {
fn hash<H: Hasher>(&self, state: &mut H) {
arc_addr(self.base).hash(state);
arc_addr(self.rule).hash(state);
}
}
impl<L: Lang> Equivalent<GroupInteriorKey<L>> for GroupInteriorProbe<'_, L> {
fn equivalent(&self, key: &GroupInteriorKey<L>) -> bool {
Arc::ptr_eq(self.base, &key.base) && Arc::ptr_eq(self.rule, &key.rule)
}
}
pub(super) struct StateMemoKey<L: Lang> {
pub(super) base: Arc<ParsingState<L>>,
pub(super) mode: Option<L::ModeId>,
pub(super) rules: TokenRulesOverrides<L>,
}
pub(super) struct StateMemoProbe<'a, L: Lang> {
pub(super) base: &'a Arc<ParsingState<L>>,
pub(super) mode: Option<L::ModeId>,
pub(super) rules: &'a TokenRulesOverrides<L>,
}
impl<L: Lang> PartialEq for StateMemoKey<L> {
fn eq(&self, other: &Self) -> bool {
keys_eq(&self.base, self.mode, &self.rules, &other.base, other.mode, &other.rules)
}
}
impl<L: Lang> Eq for StateMemoKey<L> {}
impl<L: Lang> Hash for StateMemoKey<L> {
fn hash<H: Hasher>(&self, state: &mut H) {
hash_key(&self.base, self.mode, &self.rules, state)
}
}
impl<L: Lang> Hash for StateMemoProbe<'_, L> {
fn hash<H: Hasher>(&self, state: &mut H) {
hash_key(self.base, self.mode, self.rules, state)
}
}
impl<L: Lang> Equivalent<StateMemoKey<L>> for StateMemoProbe<'_, L> {
fn equivalent(&self, key: &StateMemoKey<L>) -> bool {
keys_eq(self.base, self.mode, self.rules, &key.base, key.mode, &key.rules)
}
}
fn arc_addr<T>(arc: &Arc<T>) -> usize {
Arc::as_ptr(arc) as usize
}
fn str_addr(arc: &Arc<str>) -> usize {
Arc::as_ptr(arc) as *const u8 as usize
}
fn hash_key<L: Lang, H: Hasher>(
base: &Arc<ParsingState<L>>,
mode: Option<L::ModeId>,
rules: &TokenRulesOverrides<L>,
h: &mut H,
) {
arc_addr(base).hash(h);
mode.hash(h);
if let Some(whitespace) = <L::Features as LangFeatures>::Whitespace::store_get(&rules.whitespace)
{
whitespace.enabled.hash(h);
match &whitespace.chars {
None => h.write_u8(0),
Some(chars) => {
h.write_u8(1);
str_addr(chars).hash(h);
}
}
}
if let Some(paragraphs) = <L::Features as LangFeatures>::Paragraphs::store_get(&rules.paragraphs)
{
paragraphs.enabled.hash(h);
}
if let Some(groups) = <L::Features as LangFeatures>::Groups::store_get(&rules.groups) {
groups.enabled.hash(h);
hash_arc_slice(&groups.rules, h);
hash_arc_slice(&groups.temporary, h);
}
if let Some(commands) = <L::Features as LangFeatures>::Commands::store_get(&rules.commands) {
commands.enabled.hash(h);
hash_arc_slice(&commands.rules, h);
}
if let Some(comments) = <L::Features as LangFeatures>::Comments::store_get(&rules.comments) {
comments.enabled.hash(h);
hash_arc_slice(&comments.rules, h);
}
if let Some(specials) = <L::Features as LangFeatures>::Specials::store_get(&rules.specials) {
specials.enabled.hash(h);
}
if let Some(forbidden_chars) =
<L::Features as LangFeatures>::ForbiddenChars::store_get(&rules.forbidden_chars)
{
match &forbidden_chars.chars {
None => h.write_u8(0),
Some(chars) => {
h.write_u8(1);
str_addr(chars).hash(h);
}
}
}
if let Some(groups) = <L::Features as LangFeatures>::Groups::store_get(&rules.groups) {
match &groups.expecting_close {
None => h.write_u8(0),
Some(None) => h.write_u8(1),
Some(Some(rule)) => {
h.write_u8(2);
arc_addr(rule).hash(h);
}
}
}
}
fn hash_arc_slice<T, H: Hasher>(field: &Option<Vec<Arc<T>>>, h: &mut H) {
match field {
None => h.write_u8(0),
Some(items) => {
h.write_u8(1);
items.len().hash(h);
for item in items {
arc_addr(item).hash(h);
}
}
}
}
fn keys_eq<L: Lang>(
a_base: &Arc<ParsingState<L>>,
a_mode: Option<L::ModeId>,
a: &TokenRulesOverrides<L>,
b_base: &Arc<ParsingState<L>>,
b_mode: Option<L::ModeId>,
b: &TokenRulesOverrides<L>,
) -> bool {
Arc::ptr_eq(a_base, b_base)
&& a_mode == b_mode
&& stores_eq::<<L::Features as LangFeatures>::Whitespace, _>(
&a.whitespace,
&b.whitespace,
|x, y| {
x.enabled == y.enabled && opt_eq_by(&x.chars, &y.chars, Arc::ptr_eq)
},
)
&& stores_eq::<<L::Features as LangFeatures>::Paragraphs, _>(
&a.paragraphs,
&b.paragraphs,
|x, y| x.enabled == y.enabled,
)
&& stores_eq::<<L::Features as LangFeatures>::Groups, _>(&a.groups, &b.groups, |x, y| {
x.enabled == y.enabled
&& opt_eq_by(&x.rules, &y.rules, |x, y| arc_slices_eq(x, y))
&& opt_eq_by(&x.temporary, &y.temporary, |x, y| arc_slices_eq(x, y))
})
&& stores_eq::<<L::Features as LangFeatures>::Commands, _>(
&a.commands,
&b.commands,
|x, y| {
x.enabled == y.enabled
&& opt_eq_by(&x.rules, &y.rules, |x, y| arc_slices_eq(x, y))
},
)
&& stores_eq::<<L::Features as LangFeatures>::Comments, _>(
&a.comments,
&b.comments,
|x, y| {
x.enabled == y.enabled
&& opt_eq_by(&x.rules, &y.rules, |x, y| arc_slices_eq(x, y))
},
)
&& stores_eq::<<L::Features as LangFeatures>::Specials, _>(
&a.specials,
&b.specials,
|x, y| x.enabled == y.enabled,
)
&& stores_eq::<<L::Features as LangFeatures>::ForbiddenChars, _>(
&a.forbidden_chars,
&b.forbidden_chars,
|x, y| opt_eq_by(&x.chars, &y.chars, Arc::ptr_eq),
)
&& stores_eq::<<L::Features as LangFeatures>::Groups, _>(&a.groups, &b.groups, |x, y| {
opt_eq_by(&x.expecting_close, &y.expecting_close, |x, y| {
opt_eq_by(x, y, Arc::ptr_eq)
})
})
}
fn stores_eq<P: FeaturePresence, T: Clone + core::fmt::Debug + Send + Sync>(
a: &P::Store<T>,
b: &P::Store<T>,
eq: impl FnOnce(&T, &T) -> bool,
) -> bool {
match (P::store_get(a), P::store_get(b)) {
(Some(x), Some(y)) => eq(x, y),
(None, None) => true,
_ => false,
}
}
fn opt_eq_by<T>(a: &Option<T>, b: &Option<T>, eq: impl Fn(&T, &T) -> bool) -> bool {
match (a, b) {
(None, None) => true,
(Some(x), Some(y)) => eq(x, y),
_ => false,
}
}
fn arc_slices_eq<T>(a: &[Arc<T>], b: &[Arc<T>]) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| Arc::ptr_eq(x, y))
}