Support longer and shorter escape sequences

This commit is contained in:
imaqtkatt 2024-01-11 10:26:18 -03:00
parent eef36e6b3b
commit 1fa587400d
3 changed files with 9 additions and 3 deletions

View File

@ -36,7 +36,7 @@ pub enum Token {
#[regex(r#""([^"\\]|\\t|\\u|\\n|\\")*""#, |lex| normalized_string(lex).ok())]
Str(String),
#[regex(r#"'(.|\\t|\\u[0-9a-fA-F]{4}|\\n|\\')'"#, normalized_char)]
#[regex(r#"'(.|\\t|\\u[0-9a-fA-F]{1,8}|\\n|\\')'"#, normalized_char)]
Char(u64),
#[token("#")]
@ -145,7 +145,7 @@ fn normalized_string(lexer: &mut Lexer<Token>) -> Result<String, ParseIntError>
Some('n') => s.push('\n'),
Some('t') => s.push('\t'),
Some('u') => {
let hex = chars.take(4).collect::<String>();
let hex = chars.take(8).collect::<String>();
let hex_val = u32::from_str_radix(&hex, 16)?;
let char = char::from_u32(hex_val).unwrap_or(char::REPLACEMENT_CHARACTER);
s.push(char);
@ -219,7 +219,7 @@ fn normalized_char(lexer: &mut Lexer<Token>) -> Option<u64> {
Some('t') => '\t',
Some('\'') => '\'',
Some('u') => {
let hex = chars.take(4).collect::<String>();
let hex = chars.take(8).collect::<String>();
let hex_val = u32::from_str_radix(&hex, 16).unwrap();
char::from_u32(hex_val).unwrap_or(char::REPLACEMENT_CHARACTER)
}

View File

@ -0,0 +1 @@
main = (1, @#str s ('\u1F30E', s))

View File

@ -0,0 +1,5 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/run_file/world.hvm
---
"🌎"