Forbid underscores in unicode escapes

This commit is contained in:
Joscha 2022-11-20 18:17:19 +01:00
parent dfaa878820
commit 43d5b6d5ae

View file

@ -87,9 +87,15 @@ fn string_lit_elem() -> impl Parser<char, StringLitElem, Error = Error> {
.collect::<String>() .collect::<String>()
.map(StringLitElem::Plain); .map(StringLitElem::Plain);
let unicode_char = num_lit_str_radix(16).try_map(|(n, _), span| { // The maximum unicode codepoint is 10ffff, which has 6 digits.
let unicode_char = filter(|c: &char| c.is_ascii_hexdigit())
.repeated()
.at_least(1)
.at_most(6)
.collect::<String>()
.try_map(|str, span| {
let msg = "not a valid unicode code point"; let msg = "not a valid unicode code point";
let n: u32 = n.try_into().map_err(|_| Simple::custom(span, msg))?; let n = u32::from_str_radix(&str, 16).unwrap();
let c: char = n.try_into().map_err(|_| Simple::custom(span, msg))?; let c: char = n.try_into().map_err(|_| Simple::custom(span, msg))?;
Ok(c) Ok(c)
}); });