lang_interpreter/lexer/
code_position.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct CodePosition {
    line_number_from: isize,
    line_number_to: isize,
    column_from: isize,
    column_to: isize,
}

impl CodePosition {
    pub const EMPTY: CodePosition = CodePosition {
        line_number_from: -1, line_number_to: -1, column_from: -1, column_to: -1,
    };

    pub fn new(line_number_from: isize, line_number_to: isize, column_from: isize, column_to: isize) -> Self {
        Self { line_number_from, line_number_to, column_from, column_to }
    }

    #[must_use]
    pub fn combine(&self, code_position: &CodePosition) -> CodePosition {
        if *self == Self::EMPTY || *code_position == Self::EMPTY {
            return Self::EMPTY;
        }

        let column_from = match self.line_number_from.cmp(&code_position.line_number_from) {
            Ordering::Equal => self.column_from.min(code_position.column_from),
            Ordering::Less => self.column_from,
            Ordering::Greater => code_position.column_from,
        };

        let column_to = match self.line_number_to.cmp(&code_position.line_number_to) {
            Ordering::Equal => self.column_to.max(code_position.column_to),
            Ordering::Greater => self.column_to,
            Ordering::Less => code_position.column_to,
        };

        Self::new(
            self.line_number_from.min(code_position.line_number_from),
            self.line_number_to.min(code_position.line_number_to),
            column_from,
            column_to,
        )
    }

    pub fn to_compact_string(&self) -> String {
        format!(
            "{}:{}-{}:{}",
            self.line_number_from, self.column_from,

            self.line_number_to, self.column_to,
        )
    }

    pub fn line_number_from(&self) -> isize {
        self.line_number_from
    }

    pub fn line_number_to(&self) -> isize {
        self.line_number_to
    }

    pub fn column_from(&self) -> isize {
        self.column_from
    }

    pub fn column_to(&self) -> isize {
        self.column_to
    }
}

impl Display for CodePosition {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f, "{:5}:{:3} - {:5}:{:3}",
            self.line_number_from, self.column_from,

            self.line_number_to, self.column_to,
        )
    }
}