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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::rc::Rc;
use std::cell::{Ref, RefMut, RefCell};
use crate::earley_parse::{ASTNode, Terminal, NonTerminal};
use crate::types::Elem;
use crate::types::Elem::{*};
pub enum Type {
Accept,
Split,
Single,
}
pub struct State {
t: Type,
key: Elem,
out1: Link,
out2: Link,
}
pub type Link = Option<Rc<RefCell<State>>>;
impl State {
pub fn new(key: Elem, t: Type) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(
State {t: t, key: key, out1: None, out2: None}))
}
pub fn add(&mut self, next: Link) {
match self.out1 {
| None => self.out1 = next.clone(),
| Some(_) => {
match self.out2 {
| None => self.out2 = next.clone(),
| Some(_) => panic!("max capacity"),
}
}
}
}
pub fn show(&self, space: &str) {
println!("{}{}", space, self.key.to_str());
match self.t {
| Type::Split =>
{
self.out1.as_ref().map(|out|
{out.borrow_mut().show(&(space.to_owned() + " "))});
self.out2.as_ref().map(|out|
{out.borrow_mut().show(&(space.to_owned() + " "))});
},
| Type::Single => {self.out1.as_ref().map(|out|
{out.borrow_mut().show(&(space.to_owned() + " "))});},
| Type::Accept => (),
}
}
pub fn next(&self){
// match self.t {
// | Type::Single => self.out1.as_ref().map(|out1|
// out1).cloned(),
// | Type::Split => self.out1.as_ref().map(|out1|
// out1).cloned(),
// | Type::Accept => None
// }
match self.t {
| Type::Single => println!("single"),
| Type::Split => println!("split"),
| Type::Accept => println!("this is an accept, no next")
}
}
}
pub struct Fragment {
head: Link,
outlst: Option<Vec<Link>>,
}
impl Fragment {
fn make_outlst(&mut self) {
match self.outlst {
| Some (_) => (),
| None =>
{
let mut newlst = Vec::new();
newlst.push(self.head.clone());
self.outlst = Some(newlst);
}
}
}
fn new_from_type(key: Elem, t: Type) -> Self {
Fragment {head: Some(State::new(key, t)),
outlst: None}
}
pub fn new(key: Elem) -> Self {
let mut new_frag = Self::new_from_type(key, Type::Single);
new_frag.make_outlst();
new_frag
}
pub fn new_split(key: Elem) -> Self {
let mut new_frag = Self::new_from_type(key, Type::Split);
new_frag.make_outlst();
new_frag
}
pub fn new_accept() -> Self {
let mut new_frag = Self::new_from_type(Elem::Accept, Type::Accept);
new_frag.make_outlst();
new_frag
}
pub fn head(&self) -> &Link {
&self.head
}
pub fn next(&self) -> Option<Ref<Link>> {
self.head.as_ref().map(|node| {
Ref::map(node.borrow(), |node| &node.out1)
})
}
fn outlst_to_state(&mut self, state: Link) {
// connect each node in outlst to head of fragment
match self.outlst.take() {
| Some(outlst) =>
{
for mut out in outlst.clone() {
match out.take() {
|Some(old_out) =>
{old_out.borrow_mut().add(state.clone());
out = Some(old_out);},
| None => (),
}
}
self.outlst = Some(outlst);
},
| None => panic!("empty outlist in outlst_to_state")
}
}
pub fn concatenate(&mut self, mut frag: Fragment) {
let outlst2 = frag.outlst.clone();
self.outlst_to_state(frag.head);
self.outlst = frag.outlst;
}
pub fn alternate(&mut self, mut frag1: Fragment, mut frag2: Fragment) {
// append outlsts before fragments go out of scope
let mut outlst_final = frag1.outlst.clone();
match outlst_final.take() {
| Some(mut outlst) =>
{
outlst.append(&mut frag2.outlst.unwrap());
outlst_final = Some(outlst);
},
| None => panic!("empty outlist in alternate")
}
// concatenate current fragment to both fragments
self.outlst_to_state(frag1.head);
self.outlst_to_state(frag2.head);
self.outlst = outlst_final;
}
pub fn qm(&mut self, mut frag: Fragment) {
// question mark
let mut outlst_final = self.outlst.clone();
match outlst_final.take() {
| Some(mut outlst) =>
{
outlst.append(&mut frag.outlst.unwrap());
outlst_final = Some(outlst);
},
| None => panic!("empty outlist in qm")
}
self.outlst_to_state(frag.head);
self.outlst = outlst_final;
}
pub fn star(&mut self, mut frag: Fragment) {
frag.outlst_to_state(self.head.clone());
self.outlst_to_state(frag.head);
}
pub fn plus(&mut self, mut frag: Fragment) {
let outlst_final = frag.outlst.clone();
frag.outlst_to_state(self.head.clone());
self.outlst_to_state(frag.head);
self.outlst = outlst_final;
}
pub fn show(&self) {
match &self.head {
| Some(node) => node.borrow_mut().show(""),
| None => (),
}
}
}
impl ASTNode {
pub fn to_Fragment(&self) -> Fragment {
match self {
| Self::Terminal(c) => {
if *c != '.' {
Fragment::new(Elem::Unique(*c))
}
else {
Fragment::new(Elem::Dot)
}
},
| Self::NonTerminal {sym, children} =>
match *sym {
| "CONCAT" =>
{
let mut frag_A = children[0].to_Fragment();
frag_A.concatenate(children[1].to_Fragment());
frag_A
},
| "UNION" =>
{
let mut new_frag = Fragment::new_split(Elem::Epsilon);
new_frag.alternate(children[0].to_Fragment(),
children[2].to_Fragment());
new_frag
},
| "Q" =>
{
let mut new_frag = Fragment::new_split(Elem::Epsilon);
let frag = children[0].to_Fragment();
new_frag.qm(frag);
new_frag
},
| "STAR" =>
{
let mut new_frag = Fragment::new_split(Elem::Epsilon);
let frag = children[0].to_Fragment();
new_frag.star(frag);
new_frag
},
| "PLUS" =>
{
let frag = Fragment::new_split(Elem::Epsilon);
let mut new_frag = children[0].to_Fragment();
new_frag.plus(frag);
new_frag
},
| "DIGIT" => Fragment::new(Elem::Digit),
| "NON-DIGIT" => Fragment::new(Elem::NonDigit),
| "LETTER" => Fragment::new(Elem::Letter),
| "NON-LETTER" => Fragment::new(Elem::NonLetter),
| "WHITESPACE" => Fragment::new(Elem::Whitespace),
| "NON-WHITESPACE" => Fragment::new(Elem::NonWhitespace),
| "SPECIAL" | "EXPR" => children[1].to_Fragment(),
| "LITERAL-DOT" => Fragment::new(Elem::Unique('.')),
| _ =>
{
unimplemented!();
}
}
}
}
}