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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2024 Hemashushu <hippospark@gmail.com>, All rights reserved.
//
// This Source Code Form is subject to the terms of
// the Mozilla Public License version 2.0 and additional exceptions,
// more details in file LICENSE, LICENSE.additional and CONTRIBUTING.
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Location {
// pub unit: usize, // the index of source file
pub index: usize, // character index
pub line: usize, // line index
pub column: usize, // column index
pub length: usize, // text length, 0 for position
}
impl Location {
pub fn new_position(/*unit: usize,*/ index: usize, line: usize, column: usize) -> Self {
Self {
// unit,
index,
line,
column,
length: 0,
}
}
pub fn new_range(
/*unit: usize,*/ index: usize,
line: usize,
column: usize,
length: usize,
) -> Self {
Self {
// unit,
index,
line,
column,
length,
}
}
/// Build Range with Position and length
pub fn from_position_and_length(position: &Location, length: usize) -> Self {
Self::new_range(
// position.unit,
position.index,
position.line,
position.column,
length,
)
}
/// Convert two Positions to Range
pub fn from_position_pair(position_start: &Location, position_end: &Location) -> Self {
Self::new_range(
// position_start.unit,
position_start.index,
position_start.line,
position_start.column,
position_end.index - position_start.index,
)
}
/// Convert two Positions to Range
pub fn from_position_pair_with_end_included(
position_start: &Location,
position_end_included: &Location,
) -> Self {
Self::new_range(
// position_start.unit,
position_start.index,
position_start.line,
position_start.column,
position_end_included.index - position_start.index + 1,
)
}
/// Combine two ranges into a new range
pub fn from_range_pair(range_start: &Location, range_end: &Location) -> Self {
Self::new_range(
// range_start.unit,
range_start.index,
range_start.line,
range_start.column,
range_end.index - range_start.index + range_end.length,
)
}
/// Convert Range to Position
pub fn get_position_by_range_start(&self) -> Self {
Self::new_position(/*self.unit,*/ self.index, self.line, self.column)
}
// Convert Range to Position
// pub fn get_position_by_range_end(&self) -> Self {
// let index = self.index + self.length;
// let column = self.column + self.length;
// Self::new_position(self.unit, index, self.line, column)
// }
pub fn move_position_forward(&self) -> Self {
Self {
index: self.index + 1,
column: self.column + 1,
..*self
}
}
pub fn move_position_backward(&self) -> Self {
Self {
index: self.index - 1,
column: self.column - 1,
..*self
}
}
}