Skip to content

Commit 0a401b6

Browse files
committed
Tweak Mode.
- Add `use Mode::*` to avoid all the qualifiers. - Reorder the variants. The existing order makes no particular sense, which has bugged me for some time. I've chosen an order that makes sense to me.
1 parent adc46e5 commit 0a401b6

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

compiler/rustc_lexer/src/unescape.rs

+38-34
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use std::ops::Range;
55
use std::str::Chars;
66

7+
use Mode::*;
8+
79
#[cfg(test)]
810
mod tests;
911

@@ -85,15 +87,14 @@ where
8587
F: FnMut(Range<usize>, Result<char, EscapeError>),
8688
{
8789
match mode {
88-
Mode::Char | Mode::Byte => {
90+
Char | Byte => {
8991
let mut chars = src.chars();
9092
let res = unescape_char_or_byte(&mut chars, mode);
9193
callback(0..(src.len() - chars.as_str().len()), res);
9294
}
93-
Mode::Str | Mode::ByteStr => unescape_str_common(src, mode, callback),
94-
95-
Mode::RawStr | Mode::RawByteStr => unescape_raw_str_or_raw_byte_str(src, mode, callback),
96-
Mode::CStr | Mode::RawCStr => unreachable!(),
95+
Str | ByteStr => unescape_str_common(src, mode, callback),
96+
RawStr | RawByteStr => unescape_raw_str_or_raw_byte_str(src, mode, callback),
97+
CStr | RawCStr => unreachable!(),
9798
}
9899
}
99100

@@ -119,84 +120,87 @@ pub fn unescape_c_string<F>(src: &str, mode: Mode, callback: &mut F)
119120
where
120121
F: FnMut(Range<usize>, Result<CStrUnit, EscapeError>),
121122
{
122-
if mode == Mode::RawCStr {
123-
unescape_raw_str_or_raw_byte_str(src, mode, &mut |r, result| {
124-
callback(r, result.map(CStrUnit::Char))
125-
});
126-
} else {
127-
unescape_str_common(src, mode, callback);
123+
match mode {
124+
CStr => {
125+
unescape_str_common(src, mode, callback);
126+
}
127+
RawCStr => {
128+
unescape_raw_str_or_raw_byte_str(src, mode, &mut |r, result| {
129+
callback(r, result.map(CStrUnit::Char))
130+
});
131+
}
132+
Char | Byte | Str | RawStr | ByteStr | RawByteStr => unreachable!(),
128133
}
129134
}
130135

131136
/// Takes a contents of a char literal (without quotes), and returns an
132137
/// unescaped char or an error.
133138
pub fn unescape_char(src: &str) -> Result<char, EscapeError> {
134-
unescape_char_or_byte(&mut src.chars(), Mode::Char)
139+
unescape_char_or_byte(&mut src.chars(), Char)
135140
}
136141

137142
/// Takes a contents of a byte literal (without quotes), and returns an
138143
/// unescaped byte or an error.
139144
pub fn unescape_byte(src: &str) -> Result<u8, EscapeError> {
140-
unescape_char_or_byte(&mut src.chars(), Mode::Byte).map(byte_from_char)
145+
unescape_char_or_byte(&mut src.chars(), Byte).map(byte_from_char)
141146
}
142147

143148
/// What kind of literal do we parse.
144149
#[derive(Debug, Clone, Copy, PartialEq)]
145150
pub enum Mode {
146151
Char,
147-
Str,
152+
148153
Byte,
149-
ByteStr,
154+
155+
Str,
150156
RawStr,
157+
158+
ByteStr,
151159
RawByteStr,
160+
152161
CStr,
153162
RawCStr,
154163
}
155164

156165
impl Mode {
157166
pub fn in_double_quotes(self) -> bool {
158167
match self {
159-
Mode::Str
160-
| Mode::ByteStr
161-
| Mode::RawStr
162-
| Mode::RawByteStr
163-
| Mode::CStr
164-
| Mode::RawCStr => true,
165-
Mode::Char | Mode::Byte => false,
168+
Str | RawStr | ByteStr | RawByteStr | CStr | RawCStr => true,
169+
Char | Byte => false,
166170
}
167171
}
168172

169173
/// Non-byte literals should have `\xXX` escapes that are within the ASCII range.
170174
fn ascii_escapes_should_be_ascii(self) -> bool {
171175
match self {
172-
Mode::Char | Mode::Str => true,
173-
Mode::Byte | Mode::ByteStr | Mode::CStr => false,
174-
Mode::RawStr | Mode::RawByteStr | Mode::RawCStr => unreachable!(),
176+
Char | Str => true,
177+
Byte | ByteStr | CStr => false,
178+
RawStr | RawByteStr | RawCStr => unreachable!(),
175179
}
176180
}
177181

178-
/// Whether characters within the literal must be within the ASCII range
182+
/// Whether characters within the literal must be within the ASCII range.
179183
#[inline]
180184
fn chars_should_be_ascii(self) -> bool {
181185
match self {
182-
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true,
183-
Mode::Char | Mode::Str | Mode::RawStr | Mode::CStr | Mode::RawCStr => false,
186+
Byte | ByteStr | RawByteStr => true,
187+
Char | Str | RawStr | CStr | RawCStr => false,
184188
}
185189
}
186190

187191
/// Byte literals do not allow unicode escape.
188192
fn is_unicode_escape_disallowed(self) -> bool {
189193
match self {
190-
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true,
191-
Mode::Char | Mode::Str | Mode::RawStr | Mode::CStr | Mode::RawCStr => false,
194+
Byte | ByteStr | RawByteStr => true,
195+
Char | Str | RawStr | CStr | RawCStr => false,
192196
}
193197
}
194198

195199
pub fn prefix_noraw(self) -> &'static str {
196200
match self {
197-
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => "b",
198-
Mode::CStr | Mode::RawCStr => "c",
199-
Mode::Char | Mode::Str | Mode::RawStr => "",
201+
Char | Str | RawStr => "",
202+
Byte | ByteStr | RawByteStr => "b",
203+
CStr | RawCStr => "c",
200204
}
201205
}
202206
}
@@ -411,7 +415,7 @@ where
411415
#[inline]
412416
pub fn byte_from_char(c: char) -> u8 {
413417
let res = c as u32;
414-
debug_assert!(res <= u8::MAX as u32, "guaranteed because of Mode::ByteStr");
418+
debug_assert!(res <= u8::MAX as u32, "guaranteed because of ByteStr");
415419
res as u8
416420
}
417421

0 commit comments

Comments
 (0)