rustemo_compiler/
index.rs

1use std::{
2    fmt::{self, Display},
3    ops::{Index, IndexMut},
4    slice::{Iter, IterMut},
5};
6
7#[macro_export]
8macro_rules! create_index {
9    ($index:ident, $collection:ident) => {
10        #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
11        pub struct $index(pub usize);
12
13        impl Display for $index {
14            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15                write!(f, "{}", self.0)
16            }
17        }
18
19        impl fmt::Debug for $index {
20            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21                write!(f, "{}", self.0)
22            }
23        }
24
25        impl From<usize> for $index {
26            fn from(a: usize) -> Self {
27                Self(a)
28            }
29        }
30
31        impl From<$index> for usize {
32            fn from(i: $index) -> Self {
33                i.0
34            }
35        }
36
37        impl Default for $index {
38            fn default() -> Self {
39                Self(0)
40            }
41        }
42
43        #[derive(Debug, Clone)]
44        pub struct $collection<T>(pub Vec<T>);
45
46        impl<T> Default for $collection<T> {
47            fn default() -> Self {
48                Self(vec![])
49            }
50        }
51
52        impl<T> $collection<T> {
53            pub const fn new() -> Self {
54                Self(Vec::new())
55            }
56
57            pub fn get(&self, index: $index) -> Option<&T> {
58                self.0.get(index.0)
59            }
60
61            pub fn len(&self) -> usize {
62                self.0.len()
63            }
64
65            pub fn is_empty(&self) -> bool {
66                self.0.is_empty()
67            }
68
69            pub fn contains(&self, x: &T) -> bool
70            where
71                T: PartialEq<T>,
72            {
73                self.0.contains(x)
74            }
75
76            pub fn last(&self) -> Option<&T> {
77                self.0.last()
78            }
79
80            pub fn push(&mut self, value: T) {
81                self.0.push(value);
82            }
83
84            pub fn extend<I: IntoIterator<Item = T>>(&mut self, value: I) {
85                self.0.extend(value);
86            }
87
88            pub fn iter(&self) -> Iter<'_, T> {
89                self.0.iter()
90            }
91
92            pub fn iter_mut(&mut self) -> IterMut<'_, T> {
93                self.0.iter_mut()
94            }
95
96            pub fn sort(&mut self)
97            where
98                T: Ord,
99            {
100                self.0.sort()
101            }
102        }
103
104        impl<T> IntoIterator for $collection<T> {
105            type Item = T;
106            type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
107
108            #[inline]
109            fn into_iter(self) -> Self::IntoIter {
110                self.0.into_iter()
111            }
112        }
113
114        impl<'a, T> IntoIterator for &'a $collection<T> {
115            type Item = &'a T;
116            type IntoIter = Iter<'a, T>;
117
118            #[inline]
119            fn into_iter(self) -> Iter<'a, T> {
120                self.0.iter()
121            }
122        }
123
124        impl<'a, T> IntoIterator for &'a mut $collection<T> {
125            type Item = &'a mut T;
126            type IntoIter = IterMut<'a, T>;
127
128            #[inline]
129            fn into_iter(self) -> IterMut<'a, T> {
130                self.0.iter_mut()
131            }
132        }
133
134        impl<T> FromIterator<T> for $collection<T> {
135            #[inline]
136            fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> $collection<T> {
137                $collection(Vec::from_iter(iter))
138            }
139        }
140
141        impl<T> Index<$index> for $collection<T> {
142            type Output = T;
143
144            fn index(&self, index: $index) -> &Self::Output {
145                self.0.index(index.0)
146            }
147        }
148
149        impl<T> Index<std::ops::Range<usize>> for $collection<T> {
150            type Output = [T];
151            fn index(&self, index: std::ops::Range<usize>) -> &Self::Output {
152                &self.0[index.start..index.end]
153            }
154        }
155
156        impl<T> Index<std::ops::RangeFrom<usize>> for $collection<T> {
157            type Output = [T];
158            fn index(&self, index: std::ops::RangeFrom<usize>) -> &Self::Output {
159                &self.0[index.start..]
160            }
161        }
162
163        impl<T> Index<std::ops::RangeTo<usize>> for $collection<T> {
164            type Output = [T];
165            fn index(&self, index: std::ops::RangeTo<usize>) -> &Self::Output {
166                &self.0[..index.end]
167            }
168        }
169
170        impl<T> IndexMut<$index> for $collection<T> {
171            fn index_mut(&mut self, index: $index) -> &mut Self::Output {
172                self.0.index_mut(index.0)
173            }
174        }
175    };
176}
177
178create_index!(StateIndex, StateVec);
179create_index!(ProdIndex, ProdVec);
180create_index!(TermIndex, TermVec);
181create_index!(NonTermIndex, NonTermVec);
182create_index!(SymbolIndex, SymbolVec);
183
184impl TermIndex {
185    pub fn symbol_index(&self) -> SymbolIndex {
186        SymbolIndex(self.0)
187    }
188}
189
190impl NonTermIndex {
191    pub fn symbol_index(&self, term_len: usize) -> SymbolIndex {
192        SymbolIndex(self.0 + term_len)
193    }
194}