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
use std::fmt::Debug;
use std::fmt::{Display, Error, Formatter};
use std::hash::Hash;

// ---------------------------------------------------
// Part of the Public API
// *Changes will affect crate's version according to semver*
// ---------------------------------------------------
///
/// Position within a Cfg
/// Immutable struct
///
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Ord, PartialOrd)]
pub struct Pos {
    /// Index of the production
    pr_index: usize,

    /// Index of the symbol within the production
    /// 0: Always the index of the left-hand-side of the production
    /// >0: Index of a symbol on the hight-hand-side of the production
    sy_index: usize,
}

impl Pos {
    /// Creates an immutable Pos instance
    pub fn new(pr_index: usize, sy_index: usize) -> Self {
        Self { pr_index, sy_index }
    }

    /// Returns the production index
    pub fn pr_index(&self) -> usize {
        self.pr_index
    }

    /// Returns the symbol index
    pub fn sy_index(&self) -> usize {
        self.sy_index
    }

    /// Returns the members as a tuple
    pub fn as_tuple(&self) -> (usize, usize) {
        (self.pr_index, self.sy_index)
    }
}

impl From<(usize, usize)> for Pos {
    fn from(p: (usize, usize)) -> Self {
        Self::new(p.0, p.1)
    }
}

impl Display for Pos {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        write!(f, "({},{})", self.pr_index, self.sy_index)
    }
}