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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::{vec::IntoIter, fmt};
use rand::random;
use super::ParseError;
#[derive(Clone, Debug, PartialEq)]
pub struct Position {
pub column: usize,
pub row: usize,
pub index: usize,
pub file: Option<String>,
pub file_id: u32
}
impl Position {
pub fn end(value: &str, file: Option<String>, file_id: u32) -> Position {
let mut column = 0;
let mut row = 0;
for c in value.chars() {
match c {
'\n' => {
column = 0;
row += 1;
}
_ => column += 1,
}
}
Self { column, row, index: value.len(), file, file_id }
}
}
impl PartialOrd for Position {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match self.file.partial_cmp(&other.file) {
Some(core::cmp::Ordering::Equal) => {}
_ => return None,
}
match self.file_id.partial_cmp(&other.file_id) {
Some(core::cmp::Ordering::Equal) => {}
_ => return None,
}
match self.row.partial_cmp(&other.row) {
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
self.index.partial_cmp(&other.index)
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.file {
Some(file) => write!(f,"{}:{}:{}", file, self.row, self.column),
None => write!(f, "{}:{}", self.row, self.column)
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Span {
pub start: Position,
pub end: Position
}
impl Span {
pub fn new(start: Position, end: Position) -> Self {
Self { start, end }
}
}
impl PartialOrd for Span {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match self.start.partial_cmp(&other.start) {
Some(core::cmp::Ordering::Equal) => {}
ord => return ord,
}
self.end.partial_cmp(&other.end)
}
}
impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} - {}:{}", self.start, self.end.row, self.end.column)
}
}
#[derive(Debug, Clone)]
pub enum WhitespaceType {
Ignore,
KeepAll,
Indent
}
pub struct CharStreamBuilder {
buffer: String,
file: Option<String>,
file_id: u32,
indent_size: u8
}
impl CharStreamBuilder {
pub fn new(buffer: String) -> Self {
Self { buffer, file: None, file_id: random(), indent_size: 4 }
}
pub fn build(&mut self) -> CharStream {
let buffer = self.buffer.clone();
let chars = buffer.chars().collect::<Vec<_>>().into_iter();
let file = self.file.clone();
let eof = Position::end(&buffer, file.clone(), self.file_id);
CharStream {
chars,
file,
file_id: self.file_id,
column: 0,
row: 0,
index: 0,
eof,
whitespace: WhitespaceType::Ignore,
indent: 0,
indent_size: self.indent_size,
in_indent: true
}
}
}
#[derive(Debug, Clone)]
pub struct CharStream {
chars: IntoIter<char>,
file: Option<String>,
file_id: u32,
column: usize,
row: usize,
index: usize,
eof: Position,
whitespace: WhitespaceType,
indent: u8,
indent_size: u8,
in_indent: bool
}
impl CharStream {
pub fn new(value: String) -> CharStreamBuilder {
CharStreamBuilder::new(value)
}
pub fn position(&self) -> Position {
Position { column: self.column, row: self.row, index: self.index, file: self.file.clone(), file_id: self.file_id }
}
pub fn next(&mut self) -> Option<char> {
let chr = match self.chars.next() {
Some('\n') => {
self.index += 1;
self.column = 0;
self.row += 1;
Some('\n')
}
Some(value) => {
self.index += 1;
self.column += 1;
Some(value)
}
None => None
};
match self.whitespace {
WhitespaceType::Ignore => {
match chr {
Some(c) if c.is_whitespace() => {
self.next()
}
c => c
}
}
WhitespaceType::KeepAll => chr,
WhitespaceType::Indent => {
match chr {
Some(c) if c.is_whitespace() => {
match c {
'\t' => {
if self.in_indent {
self.indent += self.indent_size;
}
}
' ' => {
if self.in_indent {
self.indent += 1;
}
}
'\n' => {
self.in_indent = true;
self.indent = 0;
}
_ => {}
}
self.next()
}
c => {
self.in_indent = false;
c
}
}
}
}
}
pub fn goto(&mut self, position: Position) -> Result<(), ParseError> {
if self.file_id != position.file_id {
return Err(ParseError::error("Could not go to position in different buffer.", position));
}
if position < self.position() {
return Err(ParseError::error("Charstream does not support going back.", position));
}
if position > self.eof {
return Err(ParseError::error("Charstream can not go to position after end of buffer.", self.eof.clone()));
}
while self.position() < position {
self.next();
}
Ok(())
}
pub fn set_whitespace(&mut self, whitespace: WhitespaceType) {
self.whitespace = whitespace;
}
pub fn indent(&self) -> u8 {
self.indent
}
}