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
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 {
FileMapStream {
filemap: filemap.clone(),
str_stream: (*filemap.src.as_ref().unwrap()).stream()
}
}
fn abs_pos(&self) -> BytePos {
BytePos(self.str_stream.bytes_offset() as u32) + self.filemap.start_pos
}
}
impl<'a> Iterator for FileMapStream<'a>
{
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
let old_pos = self.abs_pos();
let cur_ch = self.str_stream.current_char();
self.str_stream.next().map(|c| {
let pos = self.abs_pos();
if cur_ch == '\n' {
self.filemap.next_line(pos);
}
let byte_offset_diff = (pos.0 - old_pos.0) as usize;
if byte_offset_diff > 1 {
self.filemap.record_multibyte_char(old_pos, byte_offset_diff);
}
c
})
}
}
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()
)
}
}