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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#[macro_use]
use corollary_support::*;
use std::sync::Arc;
use data::r_list::Reversed;
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub enum Position {
Position {
offset: isize,
file: Arc<String>,
row: isize,
column: isize,
},
NoPosition,
BuiltinPosition,
InternalPosition,
}
pub use self::Position::{NoPosition, BuiltinPosition, InternalPosition};
impl ::std::fmt::Display for Position {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl Position {
pub fn new(offset: isize, file: Arc<String>, row: isize, column: isize) -> Position {
Position::Position { offset, file: file, row, column }
}
pub fn from_file(file: FilePath) -> Position {
Position::Position { offset: 0, file: Arc::new(file.into()), row: 1, column: 1 }
}
pub fn none() -> Position {
NoPosition
}
pub fn builtin() -> Position {
BuiltinPosition
}
pub fn internal() -> Position {
InternalPosition
}
pub fn offset(&self) -> isize {
if let Position::Position { offset, .. } = *self {
offset
} else {
panic!("Non Position::Position passed to posOffset")
}
}
pub fn file(&self) -> Arc<String> {
if let Position::Position { ref file, .. } = *self {
file.clone()
} else {
panic!("Non Position::Position passed to posFile")
}
}
pub fn row(&self) -> isize {
if let Position::Position { row, .. } = *self {
row
} else {
panic!("Non Position::Position passed to posRow")
}
}
pub fn column(&self) -> isize {
if let Position::Position { column, .. } = *self {
column
} else {
panic!("Non Position::Position passed to posColumn")
}
}
pub fn isSource(&self) -> bool {
match *self {
Position::Position { .. } => true,
_ => false,
}
}
pub fn isNone(&self) -> bool {
self == &NoPosition
}
pub fn isBuiltin(&self) -> bool {
self == &BuiltinPosition
}
pub fn isInternal(&self) -> bool {
self == &InternalPosition
}
pub fn inc(self, by: isize) -> Position {
match self {
Position::Position { offset, file, row, column } =>
Self::new(offset + by, file, row, column + by),
p => p,
}
}
pub fn retPos(self) -> Position {
match self {
Position::Position { offset, file, row, .. } =>
Self::new(offset + 1, file, row + 1, 1),
p => p,
}
}
pub fn adjust(self, new_file: FilePath, row: isize) -> Position {
match self {
Position::Position { offset, .. } =>
Self::new(offset, Arc::new(new_file.into()), row, 1),
p => p,
}
}
pub fn incOffset(self, by: isize) -> Position {
match self {
Position::Position { offset, file, row, column } =>
Self::new(offset + by, file, row, column),
p => p,
}
}
}
pub type PosLength = (Position, isize);
pub trait Pos {
fn pos(&self) -> &Position;
fn into_pos(self) -> Position;
}
impl<A: Pos> Pos for Vec<A> {
fn pos(&self) -> &Position {
self[0].pos()
}
fn into_pos(mut self) -> Position {
self.remove(0).into_pos()
}
}
impl<A: Pos> Pos for Reversed<A> {
fn pos(&self) -> &Position {
(self.0).pos()
}
fn into_pos(self) -> Position {
(self.0).into_pos()
}
}
#[derive(Clone)]
pub struct Located<T>(T, Position);
impl<T> Pos for Located<T> {
fn pos(&self) -> &Position {
&self.1
}
fn into_pos(self) -> Position {
self.1
}
}
impl<T> Located<T> {
pub fn new<P: Pos>(t: T, pos: P) -> Located<T> {
Located(t, pos.into_pos())
}
pub fn into_inner(self) -> T {
self.0
}
}