Skip to content

Commit 744378c

Browse files
Feat: Handle hex and unicode escape sequences in parser unescape function.
1 parent 6c9a4da commit 744378c

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

compiler/src/modules/parser/types.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ pub(super) fn parse_string(s: &str) -> String {
173173
fn unescape(s: &str) -> String {
174174
let mut out = String::with_capacity(s.len());
175175
let mut chars = s.chars().peekable();
176+
177+
let mut take_hex = |chars: &mut core::iter::Peekable<core::str::Chars>, n: usize| -> char {
178+
let hex: String = chars.by_ref().take(n).collect();
179+
u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32).unwrap_or('\u{FFFD}')
180+
};
181+
176182
while let Some(c) = chars.next() {
177183
if c != '\\' { out.push(c); continue; }
178184
match chars.next() {
@@ -182,6 +188,9 @@ fn unescape(s: &str) -> String {
182188
Some('\\') => out.push('\\'),
183189
Some('\'') => out.push('\''),
184190
Some('"') => out.push('"'),
191+
Some('x') => out.push(take_hex(&mut chars, 2)),
192+
Some('u') => out.push(take_hex(&mut chars, 4)),
193+
Some('U') => out.push(take_hex(&mut chars, 8)),
185194
Some('0') => out.push('\0'),
186195
Some(c) => { out.push('\\'); out.push(c); }
187196
None => out.push('\\'),

compiler/tests/cases/parser_cases.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,5 +1604,19 @@
16041604
"instructions": [["ReturnValue", 0]],
16051605
"annotations": {},
16061606
"errors": ["expected Rpar"]
1607+
},
1608+
{
1609+
"src": "x = '\\x41'",
1610+
"constants": ["A"],
1611+
"names": ["x_1"],
1612+
"instructions": [["LoadConst",0], ["StoreName",0], ["ReturnValue",0]],
1613+
"annotations": {}
1614+
},
1615+
{
1616+
"src": "x = '\\u00e9'",
1617+
"constants": ["é"],
1618+
"names": ["x_1"],
1619+
"instructions": [["LoadConst",0], ["StoreName",0], ["ReturnValue",0]],
1620+
"annotations": {}
16071621
}
16081622
]

0 commit comments

Comments
 (0)