leap_lang/parser/
position.rs

1use std::cmp::max;
2
3// todo: path only in struct/enum level
4#[derive(PartialEq, Clone, Copy, Debug)]
5pub struct Position {
6    pub start: usize,  // index of start utf8 character
7    pub length: usize, // length in utf8 characters
8}
9
10impl Position {
11    pub fn new(start: usize, length: usize) -> Self {
12        Position { start, length }
13    }
14
15    pub fn end(&self) -> usize {
16        self.start + self.length
17    }
18
19    pub fn extend(&self, pos: &Self) -> Self {
20        let length = max(self.length, pos.start + pos.length - self.start);
21        Self {
22            start: self.start,
23            length,
24        }
25    }
26}