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
use std::convert::From;
use std::iter::Iterator;
use std::path::PathBuf;
use abortable_parser::iter::{SliceIter, StrIter};
use abortable_parser::{InputIter, Offsetable, Peekable, Positioned, Seekable, Span, SpanRange};
use crate::ast::{Position, Token};
#[derive(Debug)]
pub struct OffsetStrIter<'a> {
source_file: Option<PathBuf>,
contained: StrIter<'a>,
line_offset: usize,
col_offset: usize,
}
impl<'a> OffsetStrIter<'a> {
pub fn new(input: &'a str) -> Self {
Self::new_with_offsets(input, 0, 0)
}
pub fn new_with_offsets(input: &'a str, line_offset: usize, col_offset: usize) -> Self {
OffsetStrIter {
source_file: None,
contained: StrIter::new(input),
line_offset: line_offset,
col_offset: col_offset,
}
}
pub fn with_src_file(mut self, file: PathBuf) -> Self {
self.source_file = Some(file);
self
}
}
impl<'a> Iterator for OffsetStrIter<'a> {
type Item = &'a u8;
fn next(&mut self) -> Option<Self::Item> {
self.contained.next()
}
}
impl<'a> Offsetable for OffsetStrIter<'a> {
fn get_offset(&self) -> usize {
self.contained.get_offset()
}
}
impl<'a> Clone for OffsetStrIter<'a> {
fn clone(&self) -> Self {
OffsetStrIter {
source_file: self.source_file.clone(),
contained: self.contained.clone(),
line_offset: self.line_offset,
col_offset: self.col_offset,
}
}
}
impl<'a> From<&'a str> for OffsetStrIter<'a> {
fn from(source: &'a str) -> Self {
OffsetStrIter {
source_file: None,
contained: StrIter::new(source),
line_offset: 0,
col_offset: 0,
}
}
}
impl<'a> Seekable for OffsetStrIter<'a> {
fn seek(&mut self, to: usize) -> usize {
self.contained.seek(to)
}
}
impl<'a> Span<&'a str> for OffsetStrIter<'a> {
fn span(&self, idx: SpanRange) -> &'a str {
self.contained.span(idx)
}
}
impl<'a> Peekable<&'a u8> for OffsetStrIter<'a> {
fn peek_next(&self) -> Option<&'a u8> {
self.contained.peek_next()
}
}
impl<'a> Positioned for OffsetStrIter<'a> {
fn line(&self) -> usize {
self.contained.line() + self.line_offset
}
fn column(&self) -> usize {
self.contained.column() + self.col_offset
}
}
impl<'a> InputIter for OffsetStrIter<'a> {
fn curr(&self) -> Self::Item {
self.clone().peek_next().unwrap()
}
}
impl<'a> From<&'a SliceIter<'a, Token>> for Position {
fn from(source: &'a SliceIter<'a, Token>) -> Self {
match source.peek_next() {
Some(t) => t.pos.clone(),
None => source.curr().pos.clone(),
}
}
}
impl<'a> From<&'a OffsetStrIter<'a>> for Position {
fn from(s: &'a OffsetStrIter<'a>) -> Position {
Position {
file: s.source_file.clone(),
line: s.line(),
column: s.column(),
offset: s.get_offset(),
}
}
}
pub trait FilePositioned: Positioned {
fn file(&self) -> Option<&PathBuf>;
}
impl<'a> FilePositioned for OffsetStrIter<'a> {
fn file(&self) -> Option<&PathBuf> {
self.source_file.as_ref()
}
}
impl<'a> FilePositioned for SliceIter<'a, Token> {
fn file(&self) -> Option<&PathBuf> {
match self.peek_next() {
Some(t) => t.pos.file.as_ref(),
None => None,
}
}
}