cfg_predict_sets/
sets.rs

1//! Definitions for accessing the result of FIRST, FOLLOW
2//! and LAST set computation.
3
4use std::{
5    collections::BTreeMap,
6    ops::{Deref, DerefMut},
7};
8
9use cfg_symbol::Symbol;
10
11/// A set of symbols implemented as a list.
12/// May contain a "none" value in case a nullable was found.
13#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug)]
14pub struct PerSymbolSetVal {
15    /// Nullable found.
16    pub has_none: bool,
17    /// Set of symbols.
18    pub list: Vec<Symbol>,
19}
20
21/// The representation of FIRST and FOLLOW sets.
22pub type PerSymbolSets = BTreeMap<Symbol, PerSymbolSetVal>;
23
24/// Trait for grabbing the result of FIRST, FOLLOW and LAST
25/// set computation.
26pub trait PredictSets {
27    /// Provides access to the mapping from a symbol
28    /// to a symbol set.
29    fn predict_sets(&self) -> &PerSymbolSets;
30}
31
32impl PerSymbolSetVal {
33    pub(crate) fn new() -> Self {
34        PerSymbolSetVal {
35            has_none: false,
36            list: vec![],
37        }
38    }
39
40    pub(crate) fn clear(&mut self) {
41        self.list.clear();
42        self.has_none = false;
43    }
44
45    /// Returns whether a nullable was found.
46    pub fn has_none(&self) -> bool {
47        self.has_none
48    }
49
50    pub(crate) fn len(&self) -> usize {
51        self.list.len() + self.has_none as usize
52    }
53}
54
55impl Deref for PerSymbolSetVal {
56    type Target = Vec<Symbol>;
57    fn deref(&self) -> &Self::Target {
58        &self.list
59    }
60}
61
62impl DerefMut for PerSymbolSetVal {
63    fn deref_mut(&mut self) -> &mut Self::Target {
64        &mut self.list
65    }
66}