1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::type_complexity)]

use std::{hash::Hash, mem::size_of};

use num_traits::{AsPrimitive, PrimInt, Unsigned};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use static_assertions::const_assert;

mod itemset;
mod pager;
mod stategraph;
pub mod statetable;

pub use crate::{
    stategraph::StateGraph,
    statetable::{Action, StateTable, StateTableError, StateTableErrorKind},
};
use cfgrammar::yacc::YaccGrammar;

/// The type of the inner value of an StIdx.
pub type StIdxStorageT = u16;

/// StIdx is a wrapper for a state index. Its internal type is `StIdxStorageT`. The only guarantee
/// we make about `StIdx' is that it can be infallibly converted to usize.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

pub struct StIdx(StIdxStorageT);

impl StIdx {
    fn max_value() -> StIdx {
        StIdx(StIdxStorageT::max_value())
    }
}

impl From<StIdxStorageT> for StIdx {
    fn from(v: StIdxStorageT) -> Self {
        StIdx(v)
    }
}

impl From<StIdx> for usize {
    fn from(st: StIdx) -> Self {
        const_assert!(size_of::<usize>() >= size_of::<StIdxStorageT>());
        st.0 as usize
    }
}

impl From<StIdx> for StIdxStorageT {
    fn from(st: StIdx) -> Self {
        st.0 as StIdxStorageT
    }
}

#[derive(Clone, Copy)]
pub enum Minimiser {
    Pager,
}

pub fn from_yacc<StorageT: 'static + Hash + PrimInt + Unsigned>(
    grm: &YaccGrammar<StorageT>,
    m: Minimiser,
) -> Result<(StateGraph<StorageT>, StateTable<StorageT>), StateTableError<StorageT>>
where
    usize: AsPrimitive<StorageT>,
    u32: AsPrimitive<StorageT>,
{
    match m {
        Minimiser::Pager => {
            let sg = pager::pager_stategraph(grm);
            let st = StateTable::new(grm, &sg)?;
            Ok((sg, st))
        }
    }
}