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
// 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.
use crate::location::Location;
#[derive(Debug, PartialEq)]
pub struct CharWithPosition {
pub character: char,
pub position: Location,
}
impl CharWithPosition {
pub fn new(character: char, position: Location) -> Self {
Self {
character,
position,
}
}
}
pub struct CharsWithPositionIter<'a> {
upstream: &'a mut dyn Iterator<Item = char>,
current_position: Location,
}
impl<'a> CharsWithPositionIter<'a> {
pub fn new(/*unit: usize,*/ upstream: &'a mut dyn Iterator<Item = char>) -> Self {
Self {
upstream,
current_position: Location::new_position(/*unit,*/ 0, 0, 0),
}
}
}
impl Iterator for CharsWithPositionIter<'_> {
type Item = CharWithPosition;
fn next(&mut self) -> Option<Self::Item> {
match self.upstream.next() {
Some(c) => {
// copy
let last_position = self.current_position;
// increase positions
self.current_position.index += 1;
if c == '\n' {
self.current_position.line += 1;
self.current_position.column = 0;
} else {
self.current_position.column += 1;
}
Some(CharWithPosition::new(c, last_position))
}
None => None,
}
}
}
#[cfg(test)]
mod tests {
use crate::{
charwithposition::{CharWithPosition, CharsWithPositionIter},
location::Location,
};
#[test]
fn test_chars_with_position_iter() {
{
let mut chars = "a\nmn\nxyz".chars();
let mut char_position_iter = CharsWithPositionIter::new(&mut chars);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'a',
Location::new_position(/*0,*/ 0, 0, 0)
))
);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'\n',
Location::new_position(/*0,*/ 1, 0, 1)
))
);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'm',
Location::new_position(/*0,*/ 2, 1, 0)
))
);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'n',
Location::new_position(/*0,*/ 3, 1, 1)
))
);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'\n',
Location::new_position(/*0,*/ 4, 1, 2)
))
);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'x',
Location::new_position(/*0,*/ 5, 2, 0)
))
);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'y',
Location::new_position(/*0,*/ 6, 2, 1)
))
);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'z',
Location::new_position(/*0,*/ 7, 2, 2)
))
);
assert!(char_position_iter.next().is_none());
}
{
let mut chars = "\n\r\n\n".chars();
let mut char_position_iter = CharsWithPositionIter::new(&mut chars);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'\n',
Location::new_position(/*1,*/ 0, 0, 0)
))
);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'\r',
Location::new_position(/*1,*/ 1, 1, 0)
))
);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'\n',
Location::new_position(/*1,*/ 2, 1, 1)
))
);
assert_eq!(
char_position_iter.next(),
Some(CharWithPosition::new(
'\n',
Location::new_position(/*1,*/ 3, 2, 0)
))
);
assert!(char_position_iter.next().is_none());
}
}
}