Support \u escapes in strings

This commit is contained in:
Greg Shuflin 2021-11-21 02:11:20 -08:00
parent 884c8e515f
commit 4b11a6622a
2 changed files with 10 additions and 2 deletions

View File

@ -841,8 +841,15 @@ fn string_literal(input: Span) -> ParseResult<ExpressionKind> {
}
fn bare_string_literal(input: Span) -> ParseResult<String> {
let string_escape_transforms =
alt((value("\\", tag("\\")), value("\"", tag("\"")), value("\n", tag("n")), value("\t", tag("t"))));
let string_escape_transforms = alt((
value('\\', tag("\\")),
value('"', tag("\"")),
value('\n', tag("n")),
value('\t', tag("t")),
map(delimited(tag(r#"u{"#), recognize(digit_group_hex), tag("}")), |value| {
char::from_u32(u32::from_str_radix(value.fragment(), 16).unwrap()).unwrap()
}),
));
alt((
map(tag(r#""""#), |_| String::new()),
preceded(

View File

@ -190,6 +190,7 @@ fn string_literals() {
expr(StringLiteral { s: rc("some bytestring"), prefix: Some(rc("b")) })
);
assert_expr!(r#""Do \n \" escapes work\t""#, expr(strlit("Do \n \" escapes work\t")));
assert_expr!(r#""Georgian letter jani \u{10ef}""#, expr(strlit("Georgian letter jani ჯ")));
}
#[test]