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
use stream::*;
use str_stream::*;
use std::rc::*;
use std::cmp::Ordering;
pub use std::ops::Range;
pub use syntex_pos::{Span, FileMap};
use syntex_pos::{BytePos, mk_sp};
impl<'a> Stream for &'a Rc<FileMap>
{
type Output = FileMapStream<'a>;
fn stream(self) -> Self::Output {
FileMapStream::new(self)
}
}
#[derive(Clone)]
pub struct FileMapStream<'a>
{
filemap: Rc<FileMap>,
str_stream: StrStream<'a>,
}
impl<'a> FileMapStream<'a>
{
fn new(filemap: &'a Rc<FileMap>) -> Self {
let src = filemap.src.as_ref().unwrap();
FileMapStream::register_lines(filemap, src);
FileMapStream {
filemap: filemap.clone(),
str_stream: (*src).stream()
}
}
fn register_lines(filemap: &Rc<FileMap>, src: &String) {
if filemap.count_lines() == 0 {
let mut byte_pos: u32 = filemap.start_pos.0;
for line in src.lines() {
filemap.next_line(BytePos(byte_pos));
byte_pos += line.len() as u32 + 1;
}
}
}
fn abs_pos(&self) -> BytePos {
self.filemap.start_pos + BytePos(self.str_stream.bytes_offset() as u32)
}
}
impl<'a> Iterator for FileMapStream<'a>
{
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
self.str_stream.next()
}
}
impl<'a> PartialEq for FileMapStream<'a>
{
fn eq(&self, other: &Self) -> bool {
(&self.str_stream).eq(&other.str_stream)
}
}
impl<'a> Eq for FileMapStream<'a> {}
impl<'a> PartialOrd for FileMapStream<'a>
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
(&self.str_stream).partial_cmp(&other.str_stream)
}
}
impl<'a> Ord for FileMapStream<'a>
{
fn cmp(&self, other: &Self) -> Ordering {
(&self.str_stream).cmp(&other.str_stream)
}
}
impl<'a> Location for FileMapStream<'a>
{
fn location(&self) -> String {
self.str_stream.location()
}
}
impl<'a> CodeSnippet for FileMapStream<'a>
{
fn code_snippet(&self, len_hint: usize) -> String {
self.str_stream.code_snippet(len_hint)
}
}
impl<'a> ConsumePrefix<&'static str> for FileMapStream<'a>
{
fn consume_prefix(&mut self, prefix: &'static str) -> bool {
self.str_stream.consume_prefix(prefix)
}
}
impl<'a> HasNext for FileMapStream<'a>
{
fn has_next(&self) -> bool {
self.str_stream.has_next()
}
}
impl<'a> StreamSpan for Range<FileMapStream<'a>>
{
type Output = Span;
fn stream_span(&self) -> Self::Output {
mk_sp(
self.start.abs_pos(),
self.end.abs_pos()
)
}
}
#[cfg(test)]
mod test {
extern crate syntex_syntax;
use self::syntex_syntax::codemap::CodeMap;
use super::*;
#[test]
fn test_filemap() {
let codemap = CodeMap::new();
let filemap = codemap.new_filemap(format!("fake"), None, format!("A\n\nT\n"));
let mut stream = filemap.stream();
assert_eq!(filemap.count_lines(), 3);
assert!(stream.next() == Some('A'));
assert!(stream.next() == Some('\n'));
assert!(stream.next() == Some('\n'));
assert!(stream.next() == Some('T'));
let mut stream2 = stream.clone();
assert!(stream.next() == Some('\n'));
assert!(stream2.next() == Some('\n'));
assert!(stream.next() == None);
assert!(stream2.next() == None);
}
}