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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//! The parsing context
//!
//! # TODO
//!
//! * Consider delegating skip_while() to iter::skip_while(), and replacing the
//! "skip_sequence" methods with some useful predicate functions.
use crate::{tokenizer::Tokenizer, util};
/// A struct that holds the parsing context: the iterator over the input string, and
/// any relevant flags.
pub struct EvalPtr<'a> {
// The input iterator
tok: Tokenizer<'a>,
// Whether we're looking for a bracket or not.
bracket_term: bool,
// The term_char: the character that ends the script, None or Some<']'>
term_char: Option<char>,
// Whether we're evaluating commands or just checking for completeness.
no_eval: bool,
}
impl<'a> EvalPtr<'a> {
pub fn new(input: &'a str) -> Self {
Self {
tok: Tokenizer::new(input),
bracket_term: false,
term_char: None,
no_eval: false,
}
}
/// Returns a mutable reference to the inner tokenizer.
pub fn tok(&mut self) -> &mut Tokenizer<'a> {
&mut self.tok
}
// TEMPORARY; used by `expr`. remove in favor of tok()
pub fn from_tokenizer(ptr: &Tokenizer<'a>) -> Self {
Self {
tok: ptr.clone(),
bracket_term: false,
term_char: None,
no_eval: false,
}
}
// TEMPORARY; used by `expr`. remove in favor of tok()
pub fn to_tokenizer(&self) -> Tokenizer<'a> {
self.tok.clone()
}
//-----------------------------------------------------------------------
// Configuration
/// If true, the script ends with a right-bracket, ']'; otherwise it ends
/// at the end of the input.
pub fn set_bracket_term(&mut self, flag: bool) {
self.bracket_term = flag;
self.term_char = if flag { Some(']') } else { None };
}
// Returns whether or not the input ends with ']', i.e., at the end of the
// an interpolated script.
pub fn is_bracket_term(&self) -> bool {
self.bracket_term
}
// Sets/clears "no eval" mode. In "no eval" mode we scan the input for
// validity, e.g., no unmatched braces, brackets, or quotes.
pub fn set_no_eval(&mut self, flag: bool) {
self.no_eval = flag;
}
// Returns whether or not we are scanning the input for completeness.
pub fn is_no_eval(&self) -> bool {
self.no_eval
}
//-----------------------------------------------------------------------
// Tokenizer methods
/// Get the next character.
pub fn next(&mut self) -> Option<char> {
self.tok.next()
}
/// Sees if the next character is the given character.
// TODO: Change to "is".
pub fn next_is(&mut self, ch: char) -> bool {
self.tok.is(ch)
}
/// We are at the end of the input when there are no more characters left.
pub fn at_end(&mut self) -> bool {
self.tok.at_end()
}
/// Skip the next character
pub fn skip(&mut self) {
self.tok.skip();
}
/// Skip a specific character
pub fn skip_char(&mut self, ch: char) {
self.tok.skip_char(ch);
}
/// Skips over characters while the predicate is true. Updates the index.
pub fn skip_while<P>(&mut self, predicate: P)
where
P: Fn(char) -> bool,
{
self.tok.skip_while(predicate);
}
// Returns the current index as a mark, for later use.
pub fn mark(&mut self) -> usize {
self.tok.mark()
}
/// Get the token between the mark and the index. Returns "" if we're at the
/// end or mark == index.
pub fn token(&self, mark: usize) -> &str {
self.tok.token(mark)
}
/// Parses a backslash-escape and returns its value. If the escape is valid,
/// the value will be the substituted character. If the escape is not valid,
/// it will be the single character following the backslash. Either way, the
/// the index will point at what's next. If there's nothing following the backslash,
/// return the backslash.
pub fn backslash_subst(&mut self) -> char {
self.tok.backslash_subst()
}
//-----------------------------------------------------------------------
// Parsing Helpers
/// We are at the end of the script when we've reached the end-of-script marker
/// or we are at the end of the input.
pub fn at_end_of_script(&mut self) -> bool {
self.tok.at_end() || self.tok.peek() == self.term_char
}
/// We are at the end of the command if we've reached a semi-colon or new-line, or
/// we are at the end of the script.
pub fn at_end_of_command(&mut self) -> bool {
self.next_is('\n') || self.next_is(';') || self.at_end_of_script()
}
/// Is the current character is a valid whitespace character, including newlines?
pub fn next_is_block_white(&mut self) -> bool {
match self.tok.peek() {
Some(c) => util::is_whitespace(c),
None => false,
}
}
/// Is the current character is a valid whitespace character, excluding newlines?
pub fn next_is_line_white(&mut self) -> bool {
match self.tok.peek() {
Some(c) => util::is_whitespace(c) && c != '\n',
None => false,
}
}
/// Is the current character a valid variable name character?
pub fn next_is_varname_char(&mut self) -> bool {
match self.tok.peek() {
Some(c) => c.is_ascii_alphanumeric() || c == '_',
None => false,
}
}
/// Skips past any whitespace at the current point, *including* newlines.
/// When this is complete we will be at the end of the script or on a non-white-space
/// character.
pub fn skip_block_white(&mut self) {
while !self.at_end() && self.next_is_block_white() {
self.tok.next();
}
}
/// Skips past any whitespace on the current line, thus *excluding* newlines.
/// When this is complete we will be at the end of the script, at the end of the
/// current command, or on a non-white-space character.
pub fn skip_line_white(&mut self) {
while !self.at_end() && self.next_is_line_white() {
self.tok.next();
}
}
/// Skips past a comment if there is one, including any terminating newline.
/// Returns true if it skipped a comment, and false otherwise.
pub fn skip_comment(&mut self) -> bool {
if self.next_is('#') {
while !self.at_end() {
let c = self.tok.next();
if c == Some('\n') {
break;
} else if c == Some('\\') {
// Skip the following character. The intent is to skip
// backslashed newlines, but in
// this context it doesn't matter.
self.tok.next();
}
}
true
} else {
false
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bracket_term() {
let mut ctx = EvalPtr::new("123");
assert!(!ctx.is_bracket_term());
ctx.set_bracket_term(true);
assert!(ctx.is_bracket_term());
}
#[test]
fn test_next_is() {
let mut ctx = EvalPtr::new("123");
assert!(ctx.next_is('1'));
assert!(!ctx.next_is('2'));
let mut ctx = EvalPtr::new("");
assert!(!ctx.next_is('1'));
}
#[test]
fn test_at_end() {
let mut ctx = EvalPtr::new("123");
assert!(!ctx.at_end());
let mut ctx = EvalPtr::new("");
assert!(ctx.at_end());
}
#[test]
fn test_at_end_of_script() {
let mut ctx = EvalPtr::new("123");
assert!(!ctx.at_end_of_script());
let mut ctx = EvalPtr::new("");
assert!(ctx.at_end_of_script());
let mut ctx = EvalPtr::new("]");
assert!(!ctx.at_end_of_script());
ctx.set_bracket_term(true);
assert!(ctx.at_end_of_script());
}
#[test]
fn test_at_end_of_command() {
let mut ctx = EvalPtr::new("123");
assert!(!ctx.at_end_of_command());
let mut ctx = EvalPtr::new(";123");
assert!(ctx.at_end_of_command());
let mut ctx = EvalPtr::new("\n123");
assert!(ctx.at_end_of_command());
let mut ctx = EvalPtr::new("]123");
assert!(!ctx.at_end_of_command());
let mut ctx = EvalPtr::new("]123");
ctx.set_bracket_term(true);
assert!(ctx.at_end_of_command());
}
#[test]
fn test_next_is_block_white() {
let mut ctx = EvalPtr::new("123");
assert!(!ctx.next_is_block_white());
let mut ctx = EvalPtr::new(" 123");
assert!(ctx.next_is_block_white());
let mut ctx = EvalPtr::new("\n123");
assert!(ctx.next_is_block_white());
}
#[test]
fn test_skip_block_white() {
let mut ctx = EvalPtr::new("123");
ctx.skip_block_white();
assert!(ctx.next_is('1'));
let mut ctx = EvalPtr::new(" 123");
ctx.skip_block_white();
assert!(ctx.next_is('1'));
let mut ctx = EvalPtr::new(" \n 123");
ctx.skip_block_white();
assert!(ctx.next_is('1'));
}
#[test]
fn test_next_is_line_white() {
let mut ctx = EvalPtr::new("123");
assert!(!ctx.next_is_line_white());
let mut ctx = EvalPtr::new(" 123");
assert!(ctx.next_is_line_white());
let mut ctx = EvalPtr::new("\n123");
assert!(!ctx.next_is_line_white());
}
#[test]
fn test_skip_line_white() {
let mut ctx = EvalPtr::new("123");
ctx.skip_line_white();
assert!(ctx.next_is('1'));
let mut ctx = EvalPtr::new(" 123");
ctx.skip_line_white();
assert!(ctx.next_is('1'));
let mut ctx = EvalPtr::new(" \n 123");
ctx.skip_line_white();
assert!(ctx.next_is('\n'));
}
#[test]
fn test_skip_comment() {
let mut ctx = EvalPtr::new("123");
assert!(!ctx.skip_comment());
assert!(ctx.next_is('1'));
let mut ctx = EvalPtr::new(" #123");
assert!(!ctx.skip_comment());
assert!(ctx.next_is(' '));
let mut ctx = EvalPtr::new("#123");
assert!(ctx.skip_comment());
assert!(ctx.at_end());
let mut ctx = EvalPtr::new("#1 2 3 \na");
assert!(ctx.skip_comment());
assert!(ctx.next_is('a'));
let mut ctx = EvalPtr::new("#1 \\na\nb");
assert!(ctx.skip_comment());
assert!(ctx.next_is('b'));
let mut ctx = EvalPtr::new("#1 2] 3 \na");
ctx.set_bracket_term(true);
assert!(ctx.skip_comment());
assert!(ctx.next_is('a'));
}
}