dora_parser/lexer/
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::fmt::{Display, Error, Formatter};
use std::result::Result;

#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub struct Position {
    pub line: u32,
    pub column: u32,
}

impl Position {
    pub fn new(l: u32, c: u32) -> Position {
        assert!(l >= 1);
        assert!(c >= 1);

        Position { line: l, column: c }
    }
}

impl Display for Position {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}:{}", self.line, self.column)
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Span {
    start: Loc,
    count: u32,
}

impl Span {
    pub fn new(start: u32, count: u32) -> Span {
        Span {
            start: Loc::new(start),
            count,
        }
    }

    pub fn at(start: u32) -> Span {
        Span {
            start: Loc::new(start),
            count: 0,
        }
    }

    pub fn invalid() -> Span {
        Span {
            start: Loc::invalid(),
            count: 0,
        }
    }

    pub fn is_valid(&self) -> bool {
        self.start.is_valid()
    }

    pub fn start(&self) -> u32 {
        self.start.idx()
    }

    pub fn count(&self) -> u32 {
        self.count
    }

    pub fn end(&self) -> u32 {
        self.start.idx() + self.count
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Loc(u32);

impl Loc {
    fn new(pos: u32) -> Loc {
        assert!(pos < u32::max_value());
        Loc(pos)
    }

    fn invalid() -> Loc {
        Loc(u32::max_value())
    }

    fn is_valid(&self) -> bool {
        self.0 != u32::max_value()
    }

    fn idx(&self) -> u32 {
        assert!(self.is_valid());
        self.0
    }
}

#[test]
fn test_new() {
    let pos = Position::new(3, 1);

    assert_eq!(pos.line, 3);
    assert_eq!(pos.column, 1);

    assert_eq!(&format!("{}", pos)[..], "3:1");
}