summaryrefslogtreecommitdiff
path: root/grammar/src/left_closure.rs
blob: ddee28dd2f7788a5a5a57d86a6dabfef93e867c5 (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//! This file implements some functions to compute the regular
//! language of left-linear closure of a grammar.

use super::*;

use nfa::LabelType;

impl Grammar {
    /// Return the regular language of the left-linear closures of
    /// non-terminals in the grammar.
    ///
    /// The resulting vector is guaranteed to be of the same length as
    /// the number of non-terminals.
    ///
    /// The resulting regular language is not "self-contained".  That
    /// is to say, its terminals indices are packed indices and are
    /// meaningless without the interpretation of the grammar.  They
    /// should be converted to a nondeterministic finite automaton and
    /// then to its null closure later on.
    pub fn left_closure(&mut self) -> Result<Vec<DefaultRegex<TNT>>, Error> {
        match self.state {
            GrammarState::Initial => {
                return Err(Error::WrongState(
                    self.state,
                    GrammarState::AfterComputeFirst,
                ))
            }
            GrammarState::AfterComputeFirst => {
                self.state = GrammarState::AfterLeftClosure;
            }

            _ => {}
        }

        let non_len = self.non_num();

        let mut result = Vec::with_capacity(non_len);

        for (n, rule) in self.rules.iter().enumerate() {
            let regex = &rule.regex;

            let regex_root = if let Some(root) = regex.root() {
                root
            } else {
                result.push(Default::default());

                continue;
            };

            let regex_len = regex.len();

            /// A convenient macro to retrieve the children from the
            /// original regular expression with error propagation.
            macro_rules! children {
                ($n:expr) => {
                    regex
                        .children_of($n)
                        .map_err(|_| Error::IndexOutOfBounds($n, regex_len))?
                };
            }

            /// A convenient macro to retrieve the label from the
            /// original regular expression with error propagation.
            macro_rules! label {
                ($n:expr) => {
                    regex
                        .vertex_label($n)
                        .map_err(|_| Error::IndexOutOfBounds($n, regex_len))?
                };
            }

            let parents = regex.parents_array().map_err(|e| match e {
                nfa::error::Error::UnknownNode(n) => Error::IndexOutOfBounds(n, regex_len),
                nfa::error::Error::Cycle => Error::BuildFail(n, ParseError::Cycle),
                _ => unreachable!(),
            })?;

            use RegexType::*;
            use TNT::*;

            let mut local_result: Vec<RegexType<TNT>> = Vec::with_capacity(regex_len * 2);
            let mut builder = ALGBuilder::default();

            /// Perform a depth-first copy
            macro_rules! df_copy {
                ($parent:expr, $index:expr) => {
                    match label!($index) {
                        Kleene | Plus | Optional | Or | Paren | Empty => {
                            let mut stack = vec![($parent, $index)];

                            while let Some((top_parent, top_index)) = stack.pop() {
                                let label = label!(top_index);
                                let label = match label {
                                    Lit(top_tnt) => Lit(Ter(self.pack_tnt(top_tnt).unwrap())),
                                    _ => label,
                                };

                                local_result.push(label);

                                let new = builder.add_vertex();

                                builder.add_edge(top_parent, new, ()).unwrap();

                                stack.extend(children!(top_index).map(|child| (new, child)));
                            }
                        }
                        Lit(remain_tnt) => {
                            local_result.push(Lit(Ter(self.pack_tnt(remain_tnt).unwrap())));
                            let new = builder.add_vertex();
                            builder.add_edge($parent, new, ()).unwrap();
                        }
                    }
                };
            }

            local_result.push(Or);
            builder.add_vertex();

            // local_result.push(Lit(Ter(self.pack_tnt(Non(n)).unwrap())));
            // let non_lit_index = builder.add_vertex();

            // builder.add_edge(0, non_lit_index, ()).unwrap();

            // If this non-terminal is nullable, add an empty variant.
            if self.is_nullable(n)? {
                local_result.push(Empty);
                let empty_index = builder.add_vertex();
                builder.add_edge(0, empty_index, ()).unwrap();
            }

            for first_node in self.first_nodes_of(n)?.copied() {
                assert!(first_node < parents.len());

                let tnt = match label!(first_node) {
                    Lit(tnt) => Lit(tnt),
                    _ => continue,
                };

                let mut parents_chain = {
                    let mut result = Vec::new();
                    let mut stack = Vec::with_capacity(parents.len());

                    stack.push(first_node);

                    while let Some(top) = stack.pop() {
                        assert!(top < parents.len());
                        if let Some(parent) = parents.get(top).copied().unwrap() {
                            result.push(parent);
                            stack.push(parent.0);
                        }
                    }

                    result.reverse();

                    result
                };

                if let Some((first, _)) = parents_chain.first() {
                    assert_eq!(*first, regex_root);
                } else {
                    local_result.push(tnt);
                    let lit_index = builder.add_vertex();
                    builder.add_edge(0, lit_index, ()).unwrap();

                    continue;
                }

                // A different, "more local", root.
                let mut local_root: usize;

                // Handle the direct parent
                let (parent_node, parent_edge_index) = parents_chain.pop().unwrap();

                match label!(parent_node) {
                    Kleene | Plus => {
                        // TODO: If parent_edge_index is 0, make a
                        // Plus node instead.
                        local_result.extend([Empty, tnt]);

                        local_root = builder.add_vertex();
                        let lit_index = builder.add_vertex();
                        builder.add_edge(local_root, lit_index, ()).unwrap();

                        let iterator = children!(parent_node);

                        for index in iterator.clone().skip(parent_edge_index + 1) {
                            df_copy!(local_root, index);
                        }

                        local_result.push(Kleene);
                        let new_parent = builder.add_vertex();
                        builder.add_edge(local_root, new_parent, ()).unwrap();

                        for index in iterator {
                            df_copy!(new_parent, index);
                        }
                    }

                    Or => {
                        local_result.push(tnt);
                        local_root = builder.add_vertex();
                    }
                    Optional | Empty => {
                        // If this path is taken, it should not be
                        // optional.
                        local_result.extend([Empty, tnt]);
                        local_root = builder.add_vertex();
                        let lit_index = builder.add_vertex();
                        builder.add_edge(local_root, lit_index, ()).unwrap();

                        for index in children!(parent_node).skip(parent_edge_index + 1) {
                            df_copy!(local_root, index);
                        }
                    }
                    Paren | Lit(_) => unreachable!(),
                }

                // Handle successive parents

                for (node, edge_index) in parents_chain.into_iter() {
                    let node_type = label!(node);

                    match node_type {
                        Kleene | Plus => {
                            // TODO: If edge_index is 0, then just
                            // make this a Plus node.

                            local_result.push(Empty);
                            let new_index = builder.add_vertex();
                            builder.add_edge(new_index, local_root, ()).unwrap();

                            local_root = new_index;

                            let iterator = children!(node);

                            for index in iterator.clone().skip(edge_index + 1) {
                                df_copy!(local_root, index);
                            }

                            local_result.push(Kleene);
                            let new_parent = builder.add_vertex();
                            builder.add_edge(local_root, new_parent, ()).unwrap();

                            for index in iterator {
                                df_copy!(new_parent, index);
                            }
                        }
                        RegexType::Or => {}
                        RegexType::Optional | RegexType::Empty => {
                            local_result.push(Empty);
                            let new_index = builder.add_vertex();
                            builder.add_edge(new_index, local_root, ()).unwrap();
                            local_root = new_index;

                            for index in children!(node).skip(edge_index + 1) {
                                df_copy!(local_root, index);
                            }
                        }
                        RegexType::Paren | RegexType::Lit(_) => unreachable!(),
                    }
                }

                builder.add_edge(0, local_root, ()).unwrap();
            }

            local_result.shrink_to_fit();

            let graph = builder.build();

            assert_eq!(graph.nodes_len(), local_result.len());

            result.push(
                DefaultRegex::new(graph, local_result)
                    .map_err(|_| Error::BuildFail(n, ParseError::Cycle))?,
            );
        }

        assert_eq!(result.len(), non_len);

        self.accumulators = {
            let mut acc_result = Vec::with_capacity(result.len() + 1);
            acc_result.push(0);

            for rule in result.iter() {
                acc_result.push(rule.len() + *acc_result.last().unwrap());
            }

            acc_result
        };

        Ok(result)
    }

    /// Convert the regular language of left-linear closures to its
    /// equivalent nondeterministic finite automaton.
    ///
    /// In the generation of the left-linear closure, the terminals
    /// and non-terminals are packed into an unsigned integer.  We
    /// unpack them in converting to nondeterministic finite
    /// automaton.
    ///
    /// The resulting nondeterministic finite automaton should be
    /// transformed to its null-closure for use in our algorithm.
    pub fn left_closure_to_nfa(
        &self,
        closure: &[DefaultRegex<TNT>],
    ) -> Result<DefaultNFA<LabelType<TNT>>, Error> {
        let label_transform = |tnt| match tnt {
            TNT::Ter(t) => {
                let new_tnt = self.unpack_tnt(t).map_err(|e| match e {
                    Error::IndexOutOfBounds(index, bound) => {
                        graph::error::Error::IndexOutOfBounds(index, bound)
                    }
                    _ => unreachable!(),
                })?;

                Ok(SoC::Carry(new_tnt))
            }
            TNT::Non(n) => Ok(SoC::Sub(n)),
        };

        let nfa = DefaultNFA::to_nfa(closure, label_transform, Some(TNT::Non(0)))?;

        Ok(nfa)
    }
}