forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindents.rs
More file actions
216 lines (198 loc) · 7.32 KB
/
indents.rs
File metadata and controls
216 lines (198 loc) · 7.32 KB
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
//! The indentation processor.
use std::collections::VecDeque;
use crate::{Location, Context, DMError};
use crate::lexer::{LocatedToken, Token, Punctuation};
/// Eliminates blank lines, parses and validates indentation, braces, and semicolons.
///
/// After processing, no Newline, Tab, or Space tokens remain.
pub struct IndentProcessor<'ctx, I> {
context: &'ctx Context,
inner: I,
last_input_loc: Location,
eol_location: Option<Location>,
output: VecDeque<LocatedToken>,
// If we're indented, the number of spaces per indent and the number of indents.
current: Option<(usize, usize)>,
// The number of spaces/tabs accumulated on the current line. None when not at line head.
current_spaces: Option<usize>,
parentheses: usize,
eof_yielded: bool,
}
impl<'ctx, I> IndentProcessor<'ctx, I> where
I: Iterator<Item=LocatedToken>
{
pub fn new<J: IntoIterator<Item=LocatedToken, IntoIter=I>>(context: &'ctx Context, inner: J) -> Self {
IndentProcessor {
context,
inner: inner.into_iter(),
last_input_loc: Location::default(),
eol_location: None,
output: VecDeque::new(),
current: None,
current_spaces: None,
parentheses: 0,
eof_yielded: false,
}
}
#[inline]
fn inner_next(&mut self) -> Option<LocatedToken> {
self.inner.next()
}
#[inline]
fn push(&mut self, tok: Token) {
self.output.push_back(LocatedToken::new(self.last_input_loc, tok));
}
#[inline]
fn push_eol(&mut self, tok: Token) {
self.output.push_back(LocatedToken::new(self.eol_location.unwrap_or(self.last_input_loc), tok));
}
#[inline]
fn push_semicolon(&mut self) {
self.push_eol(Token::Punct(Punctuation::Semicolon));
}
fn real_next(&mut self, read: Token) {
// handle whitespace
match read {
Token::Punct(Punctuation::Newline) => {
if self.parentheses == 0 {
self.current_spaces = Some(0);
}
// semicolons are placed by the first token on the next line
if self.eol_location.is_none() {
self.eol_location = Some(self.last_input_loc);
}
return;
}
Token::Punct(Punctuation::Tab) |
Token::Punct(Punctuation::Space) => {
if let Some(spaces) = self.current_spaces.as_mut() {
*spaces += 1;
}
return;
}
_ => {}
}
// handle pre-existing braces
match read {
Token::Punct(Punctuation::LBrace) => self.current_spaces = None,
Token::Punct(Punctuation::RBrace) => {
self.current_spaces = None;
}
_ => {}
}
// handle indentation
if let Some(spaces) = self.current_spaces.take() {
let (indents, new_indents);
match self.current {
None => {
indents = 0;
if spaces == 0 {
new_indents = 0;
self.current = None;
} else {
new_indents = 1;
self.current = Some((spaces, 1));
}
}
Some((spaces_per_indent, indents_)) => {
indents = indents_;
if spaces == 0 {
self.current = None;
new_indents = 0;
} else {
if spaces % spaces_per_indent != 0 {
// Register the error, but cross our fingers and
// hope that truncating division will approximate
// a sane situation.
DMError::new(self.last_input_loc, format!(
"inconsistent indentation: {} % {} != 0",
spaces, spaces_per_indent,
)).register(self.context)
}
new_indents = spaces / spaces_per_indent;
self.current = Some((spaces_per_indent, new_indents));
}
}
}
if indents + 1 == new_indents {
// single indent
self.push_eol(Token::Punct(Punctuation::LBrace));
} else if indents < new_indents {
// multiple indent is an error, register it but let it work
DMError::new(self.last_input_loc, format!(
"inconsistent multiple indentation: {} > 1",
new_indents - indents,
)).register(self.context);
for _ in indents..new_indents {
self.push_eol(Token::Punct(Punctuation::LBrace));
}
} else if indents == new_indents + 1 {
// single unindent
self.push(Token::Punct(Punctuation::RBrace));
} else if indents > new_indents {
// multiple unindent
for _ in new_indents..indents {
self.push(Token::Punct(Punctuation::RBrace));
}
} else {
// same indent as before
self.push_semicolon();
}
}
// handle non-whitespace
match read {
Token::Punct(Punctuation::LBrace) => {
self.current = match self.current {
None => Some((1, 1)),
Some((x, y)) => Some((x, y + 1)),
};
}
Token::Punct(Punctuation::RBrace) => {
self.current = match self.current {
None => {
DMError::new(self.last_input_loc, "unmatched right brace").register(self.context);
None
}
Some((_, 1)) => None,
Some((x, y)) => Some((x, y - 1)),
};
}
Token::Punct(Punctuation::LParen) => {
self.parentheses += 1;
}
Token::Punct(Punctuation::RParen) => {
self.parentheses = self.parentheses.saturating_sub(1);
}
_ => {}
}
self.eol_location = None;
self.push(read);
}
}
impl<'ctx, I> Iterator for IndentProcessor<'ctx, I> where
I: Iterator<Item=LocatedToken>
{
type Item = LocatedToken;
fn next(&mut self) -> Option<LocatedToken> {
loop {
if let Some(token) = self.output.pop_front() {
return Some(token);
}
if let Some(tok) = self.inner_next() {
self.last_input_loc = tok.location;
self.real_next(tok.token);
} else if self.eof_yielded {
return None;
} else {
self.push(Token::Punct(Punctuation::Semicolon));
if let Some((_, indents)) = self.current {
for _ in 0..indents {
self.push(Token::Punct(Punctuation::RBrace));
}
}
self.current = None;
self.eof_yielded = true;
}
}
}
}