Skip to content

Commit c39ac33

Browse files
committed
Add unicode name string escapes
1 parent 8c13eea commit c39ac33

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ num-bigint = "0.2"
1818
num-traits = "0.2"
1919
unic-emoji-char = "0.9"
2020
unic-ucd-ident = "0.9"
21+
unicode_names2 = "0.4"

parser/src/lexer.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,37 @@ where
475475
u8::from_str_radix(&octet_content, 8).unwrap() as char
476476
}
477477

478+
fn parse_unicode_name(&mut self) -> Result<char, LexicalError> {
479+
let start_pos = self.get_pos();
480+
match self.next_char() {
481+
Some('{') => {}
482+
_ => {
483+
return Err(LexicalError {
484+
error: LexicalErrorType::StringError,
485+
location: start_pos,
486+
})
487+
}
488+
}
489+
let start_pos = self.get_pos();
490+
let mut name = String::new();
491+
loop {
492+
match self.next_char() {
493+
Some('}') => break,
494+
Some(c) => name.push(c),
495+
None => {
496+
return Err(LexicalError {
497+
error: LexicalErrorType::StringError,
498+
location: self.get_pos(),
499+
})
500+
}
501+
}
502+
}
503+
unicode_names2::character(&name).ok_or(LexicalError {
504+
error: LexicalErrorType::UnicodeError,
505+
location: start_pos,
506+
})
507+
}
508+
478509
fn lex_string(
479510
&mut self,
480511
is_bytes: bool,
@@ -532,11 +563,14 @@ where
532563
Some('t') => {
533564
string_content.push('\t');
534565
}
535-
Some('u') => string_content.push(self.unicode_literal(4)?),
536-
Some('U') => string_content.push(self.unicode_literal(8)?),
537-
Some('x') => string_content.push(self.unicode_literal(2)?),
538566
Some('v') => string_content.push('\x0b'),
539567
Some(o @ '0'..='7') => string_content.push(self.parse_octet(o)),
568+
Some('x') => string_content.push(self.unicode_literal(2)?),
569+
Some('u') if !is_bytes => string_content.push(self.unicode_literal(4)?),
570+
Some('U') if !is_bytes => string_content.push(self.unicode_literal(8)?),
571+
Some('N') if !is_bytes => {
572+
string_content.push(self.parse_unicode_name()?)
573+
}
540574
Some(c) => {
541575
string_content.push('\\');
542576
string_content.push(c);

0 commit comments

Comments
 (0)