[lexer] Clean up named_char module
This commit is contained in:
parent
38be7d2eee
commit
5cb66d932f
1 changed files with 42 additions and 27 deletions
|
@ -2,8 +2,8 @@
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashMap;
|
||||||
use sibiltypes::Char;
|
use std::sync::{Once, ONCE_INIT};
|
||||||
|
|
||||||
const ALARM: &'static str = "alarm";
|
const ALARM: &'static str = "alarm";
|
||||||
const BACKSPACE: &'static str = "backspace";
|
const BACKSPACE: &'static str = "backspace";
|
||||||
|
@ -15,31 +15,46 @@ const RETURN: &'static str = "return";
|
||||||
const SPACE: &'static str = "space";
|
const SPACE: &'static str = "space";
|
||||||
const TAB: &'static str = "tab";
|
const TAB: &'static str = "tab";
|
||||||
|
|
||||||
pub fn set() -> HashSet<&'static str> {
|
/// Mapping of names to `char` values. Returns the name of the given character, if a name exists.
|
||||||
let mut set: HashSet<&'static str> = HashSet::new();
|
/// Otherwise, returns `None`.
|
||||||
set.insert(ALARM);
|
fn char_for(named: &str) -> Option<char> {
|
||||||
set.insert(BACKSPACE);
|
static ONCE = ONCE_INIT;
|
||||||
set.insert(DELETE);
|
static mut names_to_chars: HashMap<&'static str, char>;
|
||||||
set.insert(ESCAPE);
|
unsafe {
|
||||||
set.insert(NEWLINE);
|
ONCE.call_once(|| {
|
||||||
set.insert(NULL);
|
names_to_chars = HashMap::new();
|
||||||
set.insert(RETURN);
|
names_to_chars.insert(ALARM, '\x07');
|
||||||
set.insert(SPACE);
|
names_to_chars.insert(BACKSPACE, '\x08');
|
||||||
set.insert(TAB);
|
names_to_chars.insert(DELETE, '\x7F');
|
||||||
set
|
names_to_chars.insert(ESCAPE, '\x1B');
|
||||||
|
names_to_chars.insert(NEWLINE, '\n');
|
||||||
|
names_to_chars.insert(NULL, '\0');
|
||||||
|
names_to_chars.insert(RETURN, '\r');
|
||||||
|
names_to_chars.insert(SPACE, ' ');
|
||||||
|
names_to_chars.insert(TAB, '\t');
|
||||||
|
});
|
||||||
|
names_to_chars.get(named)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn char_named_by(named: &str) -> Char {
|
/// Mapping of `char` values to names. Returns the name of the given character, if a name exists.
|
||||||
Char(match named {
|
/// Otherwise, returns `None`.
|
||||||
ALARM => '\x07',
|
fn name_of(c: char) -> Option<&'static str> {
|
||||||
BACKSPACE => '\x08',
|
static ONCE = ONCE_INIT;
|
||||||
DELETE => '\x7F',
|
static mut chars_to_names: HashMap<char, &'static str>;
|
||||||
ESCAPE => '\x1B',
|
unsafe {
|
||||||
NEWLINE => '\n',
|
ONCE.call_once(|| {
|
||||||
NULL => '\0',
|
chars_to_names = HashMap::new();
|
||||||
RETURN => '\r',
|
chars_to_names.insert('\x07', ALARM);
|
||||||
SPACE => ' ',
|
chars_to_names.insert('\x08', BACKSPACE);
|
||||||
TAB => '\t',
|
chars_to_names.insert('\x7F', DELETE);
|
||||||
_ => panic!("char_named_by called with invalid named char string")
|
chars_to_names.insert('\x1B', ESCAPE);
|
||||||
})
|
chars_to_names.insert('\n', NEWLINE);
|
||||||
|
chars_to_names.insert('\0', NULL);
|
||||||
|
chars_to_names.insert('\r', RETURN);
|
||||||
|
chars_to_names.insert(' ', SPACE);
|
||||||
|
chars_to_names.insert('\t', TAB);
|
||||||
|
});
|
||||||
|
chars_to_names.get(c)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue