1 use ena::unify::{UnifyKey, UnifyValue};
2 use lr1::lane_table::table::context_set::{ContextSet, OverlappingLookahead};
3 
4 /// The unification key for a set of states in the lane table
5 /// algorithm.  Each set of states is associated with a
6 /// `ContextSet`. When two sets of states are merged, their conflict
7 /// sets are merged as well; this will fail if that would produce an
8 /// overlapping conflict set.
9 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
10 pub struct StateSet {
11     index: u32,
12 }
13 
14 impl UnifyKey for StateSet {
15     type Value = ContextSet;
16 
index(&self) -> u3217     fn index(&self) -> u32 {
18         self.index
19     }
20 
from_index(u: u32) -> Self21     fn from_index(u: u32) -> Self {
22         StateSet { index: u }
23     }
24 
tag() -> &'static str25     fn tag() -> &'static str {
26         "StateSet"
27     }
28 }
29 
30 // FIXME: The `ena` interface is really designed around `UnifyValue`
31 // being cheaply cloneable; we should either refactor `ena` a bit or
32 // find some other way to associate a `ContextSet` with a state set
33 // (for example, we could have each state set be associated with an
34 // index that maps to a `ContextSet`), and do the merging ourselves.
35 // But this is easier for now, and cloning a `ContextSet` isn't THAT
36 // expensive, right? :)
37 impl UnifyValue for ContextSet {
38     type Error = (Self, Self);
39 
unify_values(value1: &Self, value2: &Self) -> Result<Self, (Self, Self)>40     fn unify_values(value1: &Self, value2: &Self) -> Result<Self, (Self, Self)> {
41         match ContextSet::union(value1, value2) {
42             Ok(v) => Ok(v),
43             Err(OverlappingLookahead) => Err((value1.clone(), value2.clone())),
44         }
45     }
46 }
47