stackr_rs/interpreter/
location.rs

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
use std::{fmt::Display, path::PathBuf};

/// Location of a token/word in the source code.
#[derive(Debug, PartialEq, Clone)]
pub struct Location {
    /// Line number.
    pub line: usize,
    /// Column number.
    pub column: usize,
    /// File name.
    pub path: Option<PathBuf>,
}
impl Location {
    pub fn new(path: Option<PathBuf>) -> Self {
        Location {
            line: 1,
            column: 1,
            path,
        }
    }

    pub fn next(&mut self) {
        self.column += 1;
    }

    pub fn new_line(&mut self) {
        self.column = 0;
        self.line += 1;
    }
}
impl Default for Location {
    fn default() -> Self {
        Location {
            line: 1,
            column: 1,
            path: None,
        }
    }
}
impl Display for Location {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.path {
            Some(path) => write!(f, "{}:{}:{}", path.display(), self.line, self.column),
            None => write!(f, "{}:{}:{}", "stdin", self.line, self.column),
        }
    }
}