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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use crate::analysis::k_tuple::TerminalMappings;
use crate::parser::parol_grammar::Factor;
use crate::parser::to_grammar_config::try_from_factor;
use parol_runtime::parser::ScannerIndex;
use std::convert::TryFrom;
use std::fmt::{Debug, Display, Error, Formatter};
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub enum Terminal {
Trm(String, Vec<usize>),
Eps,
End,
}
impl Terminal {
pub fn t(t: &str, s: Vec<usize>) -> Self {
Self::Trm(t.to_owned(), s)
}
pub fn is_trm(&self) -> bool {
matches!(self, Self::Trm(_, _))
}
pub fn is_eps(&self) -> bool {
matches!(self, Self::Eps)
}
pub fn is_end(&self) -> bool {
matches!(self, Self::End)
}
pub fn create(s: &Symbol) -> Self {
match s {
Symbol::T(Terminal::Trm(t, s)) => Terminal::Trm(t.to_string(), s.to_vec()),
Symbol::T(Terminal::End) => Terminal::End,
_ => panic!("Unexpected symbol type: {:?}", s),
}
}
pub fn add_scanner(&mut self, sc: usize) {
match self {
Terminal::Trm(_, s) => {
if !s.contains(&sc) {
s.push(sc);
s.sort_unstable();
}
}
_ => panic!("Unexpected symbol type: {:?}", self),
}
}
pub fn format<R>(&self, scanner_state_resolver: R) -> String
where
R: Fn(&[usize]) -> String,
{
match self {
Self::Trm(t, s) => {
if *s == vec![0] {
format!("\"{}\"", t)
} else {
format!("<{}>\"{}\"", scanner_state_resolver(s), t)
}
}
Self::Eps => "\u{03B5}".to_string(),
Self::End => "$".to_string(),
}
}
}
impl Display for Terminal {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
match self {
Self::Trm(t, _) => write!(f, "\"{}\"", t),
Self::Eps => write!(f, "\u{03B5}"),
Self::End => write!(f, "$"),
}
}
}
impl TerminalMappings<Terminal> for Terminal {
fn eps() -> Terminal {
Self::Eps
}
fn end() -> Terminal {
Self::End
}
fn is_eps(&self) -> bool {
self.is_eps()
}
fn is_end(&self) -> bool {
self.is_end()
}
}
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub enum Symbol {
N(String),
T(Terminal),
S(ScannerIndex),
Push(ScannerIndex),
Pop,
}
impl Symbol {
pub fn t(t: &str, s: Vec<usize>) -> Self {
Self::T(Terminal::Trm(t.to_owned(), s))
}
pub fn n(n: &str) -> Self {
Self::N(n.to_owned())
}
pub fn e() -> Self {
Self::T(Terminal::End)
}
pub fn s(s: usize) -> Self {
Self::S(s)
}
pub fn is_t(&self) -> bool {
matches!(self, Self::T(_))
}
pub fn is_n(&self) -> bool {
matches!(self, Self::N(_))
}
pub fn is_end(&self) -> bool {
matches!(self, Self::T(Terminal::End))
}
pub fn is_switch(&self) -> bool {
matches!(self, Self::S(_)) || matches!(self, Self::Push(_)) || matches!(self, Self::Pop)
}
pub fn get_t(&self) -> Option<Terminal> {
if let Self::T(t) = &self {
Some(t.clone())
} else {
None
}
}
pub fn get_t_ref(&self) -> Option<&Terminal> {
if let Self::T(t) = &self {
Some(t)
} else {
None
}
}
pub fn get_n(&self) -> Option<String> {
if let Self::N(n) = &self {
Some(n.clone())
} else {
None
}
}
pub fn get_n_ref(&self) -> Option<&String> {
if let Self::N(n) = &self {
Some(n)
} else {
None
}
}
pub fn format<R>(&self, scanner_state_resolver: &R) -> String
where
R: Fn(&[usize]) -> String,
{
match self {
Self::N(n) => n.to_string(),
Self::T(t) => t.format(scanner_state_resolver),
Self::S(s) => {
if *s == 0 {
"%sc()".to_string()
} else {
format!("%sc({})", scanner_state_resolver(&[*s]))
}
}
Self::Push(s) => format!("%push({})", scanner_state_resolver(&[*s])),
Self::Pop => "%pop".to_string(),
}
}
}
impl Display for Symbol {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
match self {
Self::N(n) => write!(f, "{}", n),
Self::T(t) => write!(f, "{}", t),
Self::S(s) => write!(f, "S({})", s),
Self::Push(s) => write!(f, "Push({})", s),
Self::Pop => write!(f, "Pop"),
}
}
}
impl TryFrom<Factor> for Symbol {
type Error = crate::errors::Error;
fn try_from(factor: Factor) -> crate::errors::Result<Self> {
try_from_factor(factor)
}
}