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
|
use std::fmt::{self, Debug, Formatter};
use std::slice::SliceIndex;
/// A featureful char-based scanner.
#[derive(Copy, Clone)]
pub struct Scanner<'s> {
src: &'s str,
index: usize,
}
impl<'s> Scanner<'s> {
/// Create a new char scanner.
pub fn new(src: &'s str) -> Self {
Self { src, index: 0 }
}
/// Consume the next char.
pub fn eat(&mut self) -> Option<char> {
let next = self.peek();
if let Some(c) = next {
self.index += c.len_utf8();
}
next
}
/// Consume the next char if it is the given one.
///
/// Returns whether the char was consumed.
pub fn eat_if(&mut self, c: char) -> bool {
// Don't decode the char twice through peek() and eat().
if self.peek() == Some(c) {
self.index += c.len_utf8();
true
} else {
false
}
}
/// Consume the next char, debug-asserting that it is the given one.
pub fn eat_assert(&mut self, c: char) {
let next = self.eat();
debug_assert_eq!(next, Some(c));
}
/// Consume the next char, coalescing `\r\n` to just `\n`.
pub fn eat_merging_crlf(&mut self) -> Option<char> {
let c = self.eat();
if c == Some('\r') && self.eat_if('\n') {
Some('\n')
} else {
c
}
}
/// Eat chars while the condition is true.
pub fn eat_while<F>(&mut self, mut f: F) -> &'s str
where
F: FnMut(char) -> bool,
{
self.eat_until(|c| !f(c))
}
/// Eat chars until the condition is true.
pub fn eat_until<F>(&mut self, mut f: F) -> &'s str
where
F: FnMut(char) -> bool,
{
let start = self.index;
while let Some(c) = self.peek() {
if f(c) {
break;
}
self.index += c.len_utf8();
}
&self.src[start .. self.index]
}
/// Uneat the last eaten char.
pub fn uneat(&mut self) {
self.index = self.last_index();
}
/// Peek at the next char without consuming it.
pub fn peek(&self) -> Option<char> {
self.src[self.index ..].chars().next()
}
/// Peek at the nth-next char without consuming anything.
pub fn peek_nth(&self, n: usize) -> Option<char> {
self.src[self.index ..].chars().nth(n)
}
/// Checks whether the next char fulfills a condition.
///
/// Returns `false` if there is no next char.
pub fn check<F>(&self, f: F) -> bool
where
F: FnOnce(char) -> bool,
{
self.peek().map(f).unwrap_or(false)
}
/// The previous index in the source string.
pub fn last_index(&self) -> usize {
self.src[.. self.index]
.chars()
.next_back()
.map(|c| self.index - c.len_utf8())
.unwrap_or(0)
}
/// The current index in the source string.
pub fn index(&self) -> usize {
self.index
}
/// Jump to an index in the source string.
pub fn jump(&mut self, index: usize) {
self.index = index;
}
/// Slice a part out of the source string.
pub fn get<I>(&self, index: I) -> &'s str
where
I: SliceIndex<str, Output = str>,
{
&self.src[index]
}
/// The full source string up to the current index.
pub fn eaten(&self) -> &'s str {
&self.src[.. self.index]
}
/// The source string from `start` to the current index.
pub fn eaten_from(&self, start: usize) -> &'s str {
&self.src[start .. self.index]
}
/// The remaining source string after the current index.
pub fn rest(&self) -> &'s str {
&self.src[self.index ..]
}
}
impl Debug for Scanner<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Scanner({}|{})", self.eaten(), self.rest())
}
}
|