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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::{
    collections::{BTreeMap, BTreeSet},
    ops::{Index, Range},
};

pub mod grammar;
pub use grammar::*;

pub mod parser;
pub use parser::*;

pub mod clr;
pub use clr::Clr;

pub mod lalr;
pub use lalr::Lalr;

pub mod slr;
pub use slr::Slr;

pub type Map<K, V> = BTreeMap<K, V>;
pub type Set<T> = BTreeSet<T>;

pub type ActTable<T> = Vec<Map<T, Action<T>>>;

pub type Rule = &'static str;
pub type State<T> = Set<Position<T>>;

/// Terms table.
/// in FIRST:
/// A = a . . . . -> {A: a}
/// A = a . | b . -> {A: a, b}
/// A = B . . . . -> {A: FIRST(B)}
///
/// in FOLLOW:
/// A = . . . T a -> {T: a}
/// A = . . . T B -> {T: FIRST(B)}
/// A = . . . . T -> {T: FOLLOW(A)}
pub type Table<T> = Map<T, Set<T>>;

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum Sym<T, U> {
    Term(T),
    NoTerm(U),
}

/// For a given f(x), processes `x` until f(x) = f(f(x)) -> f(f(f(f....f(x)))) = f(x)
pub fn transitive<T>(seed: T, map: impl Fn(T) -> T) -> T
where
    T: Clone + PartialEq,
{
    let mut val = seed;
    loop {
        let new = map(val.clone());
        if new == val {
            return val;
        }
        val = new;
    }
}

pub mod dfa;
pub use dfa::*;

pub mod tabler;
pub use tabler::*;

pub mod pos;
pub use pos::*;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct Meta<T> {
    pub item: T,
    pub span: Span,
}

impl<T> Meta<T> {
    pub const fn new(item: T, span: Span) -> Self {
        Self { item, span }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Token<T, M> {
    pub item: T,
    pub ty: M,
}

impl<T, M> Token<T, M> {
    pub const fn new(item: T, ty: M) -> Self {
        Self { item, ty }
    }
}

impl<M> Token<(), M> {
    pub const fn empty(ty: M) -> Self {
        Self::new((), ty)
    }
}

pub fn to_tokens<T: Clone>(it: impl IntoIterator<Item = T>) -> impl Iterator<Item = Token<(), T>> {
    it.into_iter().map(|i| Token::new((), i))
}

/// A exclusive Span struct, for indexing metadata on Tokens.
/// start is where it BEGINS, end is UNTIL collect.
/// ```
/// use lrp::Span;
/// let i: Span = Span::new(0, 2);
/// let src = [0, 1, 2, 3, 4];
/// assert_eq!(i.from_source(&src), &[0, 1]);
/// ```
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Hash)]
pub struct Span {
    pub start: usize,
    pub end: usize,
}

impl Span {
    #[must_use]
    pub const fn new(start: usize, end: usize) -> Self {
        Self { start, end }
    }

    pub fn from_source<'a, T: Index<Range<usize>> + ?Sized>(
        &'a self,
        slice: &'a T,
    ) -> &'a T::Output {
        slice.index(self.start..self.end)
    }
}

impl From<(usize, usize)> for Span {
    fn from(value: (usize, usize)) -> Self {
        Self {
            start: value.0,
            end: value.1,
        }
    }
}

impl From<Range<usize>> for Span {
    fn from(value: Range<usize>) -> Self {
        Self {
            start: value.start,
            end: value.end,
        }
    }
}

#[macro_export]
macro_rules! grammar_map {
    ($($rule:literal -> $($($terms:literal)*)|*),*) => {{
        let mut hmp = $crate::Map::new();
        $($crate::grammar_map!(hmp, $rule -> $($($terms)*)|*);)*
        hmp
    }};
    ($grammar:tt, $rule:literal -> $($($terms:literal)*)|*) => {{
        let rule = $crate::grammar::Rule::new($rule, vec![$(vec![$($terms),*]),*]);
        $grammar.insert($rule, rule);
    }};
    ($($rule:ident -> $($($terms:ident)*)|*),*) => {{
        let mut hmp = $crate::Map::new();
        $($crate::grammar_map!(hmp, $rule -> $($($terms)*)|*);)*
        hmp
    }};
    ($grammar:tt, $rule:ident -> $($($terms:ident)*)|*) => {{
        let rule = $crate::grammar::Rule::new($rule, vec![$(vec![$($terms),*]),*]);
        $grammar.insert($rule, rule);
    }}
}

#[cfg(test)]
pub mod grammars_tests {
    use crate::Grammar;
    include!("../grammars_tests.rs");
}