Lower values of char and byte literals

This commit is contained in:
Laurențiu Nicola 2022-05-05 08:10:07 +03:00
parent 0218aeba7a
commit 9856144b0b
8 changed files with 178 additions and 11 deletions

View file

@ -2,7 +2,7 @@
use std::borrow::Cow;
use rustc_lexer::unescape::{unescape_literal, Mode};
use rustc_lexer::unescape::{unescape_byte, unescape_char, unescape_literal, Mode};
use crate::{
ast::{self, AstToken},
@ -406,3 +406,35 @@ mod tests {
check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\");
}
}
impl ast::Char {
pub fn value(&self) -> Option<char> {
let mut text = self.text();
if text.starts_with('\'') {
text = &text[1..];
} else {
return None;
}
if text.ends_with('\'') {
text = &text[0..text.len() - 1];
}
unescape_char(text).ok()
}
}
impl ast::Byte {
pub fn value(&self) -> Option<u8> {
let mut text = self.text();
if text.starts_with("b\'") {
text = &text[2..];
} else {
return None;
}
if text.ends_with('\'') {
text = &text[0..text.len() - 1];
}
unescape_byte(text).ok()
}
}