pub enum TrieNode {
    Decision {
        edges: Vec<TrieEdge>,
    },
    Leaf {
        prio: Prio,
        output: ExprSequence,
    },
    Empty,
}
Expand description

A node in the term trie.

Variants§

§

Decision

Fields

§edges: Vec<TrieEdge>

The child sub-tries that we can match from this point on.

One or more patterns could match.

Maybe one pattern already has matched, but there are more (higher priority and/or same priority but more specific) patterns that could still match.

§

Leaf

Fields

§prio: Prio

The priority of this rule.

§output: ExprSequence

The RHS expression to evaluate upon a successful LHS pattern match.

The successful match of an LHS pattern, and here is its RHS expression.

§

Empty

No LHS pattern matches.

Implementations§

Sort edges by priority.

Examples found in repository?
src/trie.rs (line 245)
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
    pub fn sort(&mut self) {
        match self {
            TrieNode::Decision { edges } => {
                // Sort by priority, highest integer value first; then
                // by trie symbol.
                edges.sort_by_cached_key(|edge| (-edge.prio, edge.symbol.clone()));
                for child in edges {
                    child.node.sort();
                }
            }
            _ => {}
        }
    }

    /// Get a pretty-printed version of this trie, for debugging.
    pub fn pretty(&self) -> String {
        let mut s = String::new();
        pretty_rec(&mut s, self, "");
        return s;

        fn pretty_rec(s: &mut String, node: &TrieNode, indent: &str) {
            match node {
                TrieNode::Decision { edges } => {
                    s.push_str(indent);
                    s.push_str("TrieNode::Decision:\n");

                    let new_indent = indent.to_owned() + "    ";
                    for edge in edges {
                        s.push_str(indent);
                        s.push_str(&format!(
                            "  edge: prio = {:?}, symbol: {:?}\n",
                            edge.prio, edge.symbol
                        ));
                        pretty_rec(s, &edge.node, &new_indent);
                    }
                }
                TrieNode::Empty | TrieNode::Leaf { .. } => {
                    s.push_str(indent);
                    s.push_str(&format!("{:?}\n", node));
                }
            }
        }
    }
}

#[derive(Debug, Default)]
struct TermFunctionsBuilder {
    builders_by_term: BTreeMap<TermId, TrieNode>,
}

impl TermFunctionsBuilder {
    fn build(&mut self, termenv: &TermEnv) {
        log!("termenv: {:?}", termenv);
        for rule in termenv.rules.iter() {
            let (pattern, expr) = lower_rule(termenv, rule.id);

            log!(
                "build:\n- rule {:?}\n- pattern {:?}\n- expr {:?}",
                rule,
                pattern,
                expr
            );

            let symbols = pattern
                .insts
                .into_iter()
                .map(|op| TrieSymbol::Match { op })
                .chain(std::iter::once(TrieSymbol::EndOfMatch));

            self.builders_by_term
                .entry(rule.root_term)
                .or_insert(TrieNode::Empty)
                .insert(rule.prio, symbols, expr);
        }

        for builder in self.builders_by_term.values_mut() {
            builder.sort();
        }
    }

Get a pretty-printed version of this trie, for debugging.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.