// This file is part of ICU4X. For terms of use, please see the file // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). use alloc::borrow::Cow; use alloc::collections::{BTreeMap, BTreeSet}; use alloc::format; use alloc::string::{String, ToString}; use alloc::vec::Vec; use core::fmt::Display; use core::{iter::Peekable, str::CharIndices}; use crate::props::IndicConjunctBreak; use crate::script::ScriptWithExtensions; use crate::{ CodePointMapData, props::{ CanonicalCombiningClass, EastAsianWidth, EnumeratedProperty, GeneralCategory, GeneralCategoryGroup, GraphemeClusterBreak, LineBreak, Script, SentenceBreak, WordBreak, }, }; use crate::{ CodePointSetData, props::{PatternWhiteSpace, XidContinue, XidStart}, }; use crate::{PropertyParser, provider::*}; use icu_collections::{ codepointinvlist::{CodePointInversionList, CodePointInversionListBuilder}, codepointinvliststringlist::CodePointInversionListAndStringList, }; use icu_provider::prelude::*; /// The kind of error that occurred. #[derive(Debug, Clone, Copy, PartialEq, Eq, displaydoc::Display)] #[non_exhaustive] pub enum ParseErrorKind { /// An unexpected character was encountered. /// /// This variant implies the other variants /// (notably `UnknownProperty` and `Unimplemented`) do not apply. #[displaydoc("An unexpected character was encountered")] UnexpectedChar(char), /// The property name or value is unknown. /// /// For property names, make sure you use the spelling /// defined in [ECMA-262](https://tc39.es/ecma262/#table-nonbinary-unicode-properties). #[displaydoc("The property name or value is unknown")] UnknownProperty, /// A reference to an unknown variable. UnknownVariable, /// A variable of a certain type occurring in an unexpected context. UnexpectedVariable, /// The source is an incomplete unicode set. Eof, /// Something unexpected went wrong with our code. Please file a bug report on GitHub. Internal, /// The provided syntax is not supported by us. /// /// Note that unknown properties will return the /// `UnknownProperty` variant, not this one. #[displaydoc("The provided syntax is not supported by us.")] Unimplemented, /// The provided escape sequence is not a valid Unicode code point or represents too many code points. InvalidEscape, } use ParseErrorKind as PEK; use zerovec::VarZeroVec; impl ParseErrorKind { fn with_offset(self, offset: usize) -> ParseError { ParseError { offset: Some(offset), kind: self, } } } impl From for ParseError { fn from(kind: ParseErrorKind) -> Self { ParseError { offset: None, kind } } } /// The error type returned by the `parse` functions in this crate. /// /// See [`ParseError::fmt_with_source`] for pretty-printing and [`ParseErrorKind`] of the /// different types of errors represented by this struct. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct ParseError { // offset is the index to an arbitrary byte in the last character in the source that makes sense // to display as location for the error, e.g., the unexpected character itself or // for an unknown property name the last character of the name. offset: Option, kind: ParseErrorKind, } type Result = core::result::Result; impl ParseError { /// Pretty-prints this error and if applicable, shows where the error occurred in the source. /// /// Must be called with the same source that was used to parse the set. /// /// # Examples /// /// ``` /// use icu::properties::unicodeset_parse::*; /// /// let source = "[[abc]-x]"; /// let set = parse(source); /// assert!(set.is_err()); /// let err = set.unwrap_err(); /// assert_eq!( /// err.fmt_with_source(source).to_string(), /// "[[abc]-x← error: unexpected character 'x'" /// ); /// ``` /// /// ``` /// use icu::properties::unicodeset_parse::*; /// /// let source = r"[\N{LATIN CAPITAL LETTER A}]"; /// let set = parse(source); /// assert!(set.is_err()); /// let err = set.unwrap_err(); /// assert_eq!( /// err.fmt_with_source(source).to_string(), /// r"[\N← error: unimplemented" /// ); /// ``` pub fn fmt_with_source(&self, source: &str) -> impl Display + use<> { let ParseError { offset, kind } = *self; if kind == ParseErrorKind::Eof { return format!("{source}← error: unexpected end of input"); } let mut s = String::new(); if let Some(offset) = offset && offset < source.len() { // offset points to any byte of the last character we want to display. // in the case of ASCII, this is easy - we just display bytes [..=offset]. // however, if the last character is more than one byte in UTF-8 // we cannot use ..=offset, because that would potentially include only partial // bytes of last character in our string. hence we must find the start of the // following character and use that as the (exclusive) end of our string. // offset points into the last character we want to include, hence the start of the // first character we want to exclude is at least offset + 1. let mut exclusive_end = offset + 1; // TODO: replace this loop with str::ceil_char_boundary once stable for _ in 0..3 { // is_char_boundary returns true at the latest once exclusive_end == source.len() if source.is_char_boundary(exclusive_end) { break; } exclusive_end += 1; } // exclusive_end is at most source.len() due to str::is_char_boundary and at least 0 by type s.push_str(&source[..exclusive_end]); s.push_str("← "); } s.push_str("error: "); match kind { ParseErrorKind::UnexpectedChar(c) => { s.push_str(&format!("unexpected character '{}'", c.escape_debug())); } ParseErrorKind::UnknownProperty => { s.push_str("unknown property"); } ParseErrorKind::UnknownVariable => { s.push_str("unknown variable"); } ParseErrorKind::UnexpectedVariable => { s.push_str("unexpected variable"); } ParseErrorKind::Eof => { s.push_str("unexpected end of input"); } ParseErrorKind::Internal => { s.push_str("internal error"); } ParseErrorKind::Unimplemented => { s.push_str("unimplemented"); } ParseErrorKind::InvalidEscape => { s.push_str("invalid escape sequence"); } } s } /// Returns the [`ParseErrorKind`] of this error. pub fn kind(&self) -> ParseErrorKind { self.kind } /// Returns the offset of this error in the source string, if it was specified. pub fn offset(&self) -> Option { self.offset } fn or_with_offset(self, offset: usize) -> Self { match self.offset { Some(_) => self, None => ParseError { offset: Some(offset), ..self }, } } } /// The value of a variable in a `UnicodeSet`. Used as value type in [`VariableMap`]. #[derive(Debug, Clone)] #[non_exhaustive] pub enum VariableValue<'a> { /// A `UnicodeSet`, represented as a [`CodePointInversionListAndStringList`](CodePointInversionListAndStringList). UnicodeSet(CodePointInversionListAndStringList<'a>), // in theory, a one-code-point string is always the same as a char, but we might want to keep // this variant for efficiency? /// A single code point. Char(char), /// A string. It is guaranteed that when returned from a `VariableMap`, this variant contains never exactly one code point. String(Cow<'a, str>), } /// The map used for parsing `UnicodeSets` with variable support. See [`parse_with_variables`]. #[derive(Debug, Clone, Default)] pub struct VariableMap<'a>(BTreeMap>); impl<'a> VariableMap<'a> { /// Creates a new empty map. pub fn new() -> Self { Self::default() } /// Removes a key from the map, returning the value at the key if the key /// was previously in the map. pub fn remove(&mut self, key: &str) -> Option> { self.0.remove(key) } /// Get a reference to the value associated with this key, if it exists. pub fn get(&self, key: &str) -> Option<&VariableValue<'a>> { self.0.get(key) } /// Insert a `VariableValue` into the `VariableMap`. /// /// Returns `Err` with the old value, if it exists, and does not update the map. pub fn insert( &mut self, key: String, value: VariableValue<'a>, ) -> Result<(), &VariableValue<'_>> { // borrow-checker shenanigans, otherwise we could use if let if self.0.contains_key(&key) { // we just checked that this key exists #[expect(clippy::indexing_slicing)] return Err(&self.0[&key]); } if let VariableValue::String(s) = &value { let mut chars = s.chars(); if let (Some(c), None) = (chars.next(), chars.next()) { self.0.insert(key, VariableValue::Char(c)); return Ok(()); }; } self.0.insert(key, value); Ok(()) } /// Insert a `char` into the `VariableMap`. /// /// Returns `Err` with the old value, if it exists, and does not update the map. pub fn insert_char(&mut self, key: String, c: char) -> Result<(), &VariableValue<'_>> { // borrow-checker shenanigans, otherwise we could use if let if self.0.contains_key(&key) { // we just checked that this key exists #[expect(clippy::indexing_slicing)] return Err(&self.0[&key]); } self.0.insert(key, VariableValue::Char(c)); Ok(()) } /// Insert a `String` of any length into the `VariableMap`. /// /// Returns `Err` with the old value, if it exists, and does not update the map. pub fn insert_string(&mut self, key: String, s: String) -> Result<(), &VariableValue<'_>> { // borrow-checker shenanigans, otherwise we could use if let if self.0.contains_key(&key) { // we just checked that this key exists #[expect(clippy::indexing_slicing)] return Err(&self.0[&key]); } let mut chars = s.chars(); let val = match (chars.next(), chars.next()) { (Some(c), None) => VariableValue::Char(c), _ => VariableValue::String(Cow::Owned(s)), }; self.0.insert(key, val); Ok(()) } /// Insert a `&str` of any length into the `VariableMap`. /// /// Returns `Err` with the old value, if it exists, and does not update the map. pub fn insert_str(&mut self, key: String, s: &'a str) -> Result<(), &VariableValue<'_>> { // borrow-checker shenanigans, otherwise we could use if let if self.0.contains_key(&key) { // we just checked that this key exists #[expect(clippy::indexing_slicing)] return Err(&self.0[&key]); } let mut chars = s.chars(); let val = match (chars.next(), chars.next()) { (Some(c), None) => VariableValue::Char(c), _ => VariableValue::String(Cow::Borrowed(s)), }; self.0.insert(key, val); Ok(()) } /// Insert a [`CodePointInversionListAndStringList`](CodePointInversionListAndStringList) into the `VariableMap`. /// /// Returns `Err` with the old value, if it exists, and does not update the map. pub fn insert_set( &mut self, key: String, set: CodePointInversionListAndStringList<'a>, ) -> Result<(), &VariableValue<'_>> { // borrow-checker shenanigans, otherwise we could use if let if self.0.contains_key(&key) { // we just checked that this key exists #[expect(clippy::indexing_slicing)] return Err(&self.0[&key]); } self.0.insert(key, VariableValue::UnicodeSet(set)); Ok(()) } } // this ignores the ambiguity between \-escapes and \p{} perl properties. it assumes it is in a context where \p is just 'p' // returns whether the provided char signifies the start of a literal char (raw or escaped - so \ is a legal char start) // important: assumes c is not pattern_white_space fn legal_char_start(c: char) -> bool { !(c == '&' || c == '-' || c == '$' || c == '^' || c == '[' || c == ']' || c == '{') } // same as `legal_char_start` but adapted to the charInString nonterminal. \ is allowed due to escapes. // important: assumes c is not pattern_white_space fn legal_char_in_string_start(c: char) -> bool { c != '}' } #[derive(Debug)] enum SingleOrMultiChar { Single(char), // Multi is a marker that indicates parsing was paused and needs to be resumed using parse_multi_escape* when // this token is consumed. The contained char is the first char of the multi sequence. Multi(char), } // A char or a string. The Vec represents multi-escapes in the 2+ case. // invariant: a String is either zero or 2+ chars long, a one-char-string is equivalent to a single char. // invariant: a char is 1+ chars long #[derive(Debug)] enum Literal { String(String), CharKind(SingleOrMultiChar), } #[derive(Debug)] enum MainToken<'data> { // to be interpreted as value Literal(Literal), // inner set UnicodeSet(CodePointInversionListAndStringList<'data>), // anchor, only at the end of a set ([... $]) DollarSign, // intersection operator, only inbetween two sets ([[...] & [...]]) Ampersand, // difference operator, only inbetween two sets ([[...] - [...]]) // or // range operator, only inbetween two chars ([a-z], [a-{z}]) Minus, // ] to indicate the end of a set ClosingBracket, } impl<'data> MainToken<'data> { fn from_variable_value(val: VariableValue<'data>) -> Self { match val { VariableValue::Char(c) => { MainToken::Literal(Literal::CharKind(SingleOrMultiChar::Single(c))) } VariableValue::String(s) => { // we know that the VariableMap only contains non-length-1 Strings. MainToken::Literal(Literal::String(s.into_owned())) } VariableValue::UnicodeSet(set) => MainToken::UnicodeSet(set), } } } #[derive(Debug, Clone, Copy)] enum Operation { Union, Difference, Intersection, } // this builds the set on-the-fly while parsing it struct UnicodeSetBuilder<'a, 'b, P: ?Sized> { single_set: CodePointInversionListBuilder, string_set: BTreeSet, iter: &'a mut Peekable>, source: &'b str, inverted: bool, variable_map: &'a VariableMap<'a>, xid_start: &'a CodePointInversionList<'a>, xid_continue: &'a CodePointInversionList<'a>, pat_ws: &'a CodePointInversionList<'a>, property_provider: &'a P, } impl<'a, 'b, P> UnicodeSetBuilder<'a, 'b, P> where P: ?Sized + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider + DataProvider, { fn new_internal( iter: &'a mut Peekable>, source: &'b str, variable_map: &'a VariableMap<'a>, xid_start: &'a CodePointInversionList<'a>, xid_continue: &'a CodePointInversionList<'a>, pat_ws: &'a CodePointInversionList<'a>, provider: &'a P, ) -> Self { UnicodeSetBuilder { single_set: CodePointInversionListBuilder::new(), string_set: Default::default(), iter, source, inverted: false, variable_map, xid_start, xid_continue, pat_ws, property_provider: provider, } } // the entry point, parses a full UnicodeSet. ignores remaining input fn parse_unicode_set(&mut self) -> Result<()> { match self.must_peek_char()? { '\\' => self.parse_property_perl(), '[' => { self.iter.next(); if let Some(':') = self.peek_char() { self.parse_property_posix() } else { self.parse_unicode_set_inner() } } '$' => { // must be variable ref to a UnicodeSet let (offset, v) = self.parse_variable()?; match v { Some(VariableValue::UnicodeSet(s)) => { self.single_set.add_set(s.code_points()); self.string_set .extend(s.strings().iter().map(ToString::to_string)); Ok(()) } Some(_) => Err(PEK::UnexpectedVariable.with_offset(offset)), None => Err(PEK::UnexpectedChar('$').with_offset(offset)), } } c => self.error_here(PEK::UnexpectedChar(c)), } } // beginning [ is already consumed fn parse_unicode_set_inner(&mut self) -> Result<()> { // special cases for the first chars after [ if self.must_peek_char()? == '^' { self.iter.next(); self.inverted = true; } // whitespace allowed between ^ and - in `[^ - ....]` self.skip_whitespace(); if self.must_peek_char()? == '-' { self.iter.next(); self.single_set.add_char('-'); } // repeatedly parse the following: // char // char-char // {string} // unicodeset // & and - operators, but only between unicodesets // $variables in place of strings, chars, or unicodesets #[derive(Debug, Clone, Copy)] enum State { // a state equivalent to the beginning Begin, // a state after a char. implies `prev_char` is Some(_), because we need to buffer it // in case it is part of a range, e.g., a-z Char, // in the middle of parsing a range. implies `prev_char` is Some(_), and the next // element must be a char as well CharMinus, // state directly after parsing a recursive unicode set. operators are only allowed // in this state AfterUnicodeSet, // state directly after parsing an operator. forces the next element to be a recursive // unicode set AfterOp, // state after parsing a $ (that was not a variable reference) // the only valid next option is a closing bracket AfterDollar, // state after parsing a - in an otherwise invalid position // the only valid next option is a closing bracket AfterMinus, } use State::*; const DEFAULT_OP: Operation = Operation::Union; let mut state = Begin; let mut prev_char = None; let mut operation = Operation::Union; loop { self.skip_whitespace(); // for error messages let (immediate_offset, immediate_char) = self.must_peek()?; let (tok_offset, from_var, tok) = self.parse_main_token()?; // warning: self.iter should not be advanced any more after this point on any path to // MT::Literal(Literal::CharKind(SingleOrMultiChar::Multi)), because that variant // expects a certain self.iter state use MainToken as MT; use SingleOrMultiChar as SMC; match (state, tok) { // the end of this unicode set ( Begin | Char | CharMinus | AfterUnicodeSet | AfterDollar | AfterMinus, MT::ClosingBracket, ) => { if let Some(prev) = prev_char.take() { self.single_set.add_char(prev); } if matches!(state, CharMinus) { self.single_set.add_char('-'); } return Ok(()); } // special case ends for - // [[a-z]-] (AfterOp, MT::ClosingBracket) if matches!(operation, Operation::Difference) => { self.single_set.add_char('-'); return Ok(()); } (Begin, MT::Minus) => { self.single_set.add_char('-'); state = AfterMinus; } // inner unicode set (Begin | Char | AfterUnicodeSet | AfterOp, MT::UnicodeSet(set)) => { if let Some(prev) = prev_char.take() { self.single_set.add_char(prev); } self.process_chars(operation, set.code_points().clone()); self.process_strings( operation, set.strings().iter().map(ToString::to_string).collect(), ); operation = DEFAULT_OP; state = AfterUnicodeSet; } // a literal char (either individually or as the start of a range if char) ( Begin | Char | AfterUnicodeSet, MT::Literal(Literal::CharKind(SMC::Single(c))), ) => { if let Some(prev) = prev_char.take() { self.single_set.add_char(prev); } prev_char = Some(c); state = Char; } // a bunch of literal chars as part of a multi-escape sequence ( Begin | Char | AfterUnicodeSet, MT::Literal(Literal::CharKind(SMC::Multi(first_c))), ) => { if let Some(prev) = prev_char.take() { self.single_set.add_char(prev); } self.single_set.add_char(first_c); self.parse_multi_escape_into_set()?; // Note we cannot go to the Char state, because a multi-escape sequence of // length > 1 cannot initiate a range state = Begin; } // a literal string (length != 1, by CharOrString invariant) (Begin | Char | AfterUnicodeSet, MT::Literal(Literal::String(s))) => { if let Some(prev) = prev_char.take() { self.single_set.add_char(prev); } self.string_set.insert(s); state = Begin; } // parse a literal char as the end of a range (CharMinus, MT::Literal(Literal::CharKind(SMC::Single(c)))) => { let start = prev_char.ok_or(ParseError { offset: Some(tok_offset), kind: PEK::Internal, })?; let end = c; if start > end { // TODO(#3558): Better error message (e.g., "start greater than end in range")? return Err(PEK::UnexpectedChar(end).with_offset(tok_offset)); } self.single_set.add_range(start..=end); prev_char = None; state = Begin; } // start parsing a char range (Char, MT::Minus) => { state = CharMinus; } // start parsing a unicode set difference (AfterUnicodeSet, MT::Minus) => { operation = Operation::Difference; state = AfterOp; } // start parsing a unicode set difference (AfterUnicodeSet, MT::Ampersand) => { operation = Operation::Intersection; state = AfterOp; } (Begin | Char | AfterUnicodeSet, MT::DollarSign) => { if let Some(prev) = prev_char.take() { self.single_set.add_char(prev); } self.single_set.add_char('\u{FFFF}'); state = AfterDollar; } _ => { // TODO(#3558): We have precise knowledge about the following MainToken here, // should we make use of that? if from_var { // otherwise we get error messages such as // [$a-$← error: unexpected character '$' // for input [$a-$b], $a = 'a', $b = "string" ; return Err(PEK::UnexpectedVariable.with_offset(tok_offset)); } return Err(PEK::UnexpectedChar(immediate_char).with_offset(immediate_offset)); } } } } fn parse_main_token(&mut self) -> Result<(usize, bool, MainToken<'a>)> { let (initial_offset, first) = self.must_peek()?; if first == ']' { self.iter.next(); return Ok((initial_offset, false, MainToken::ClosingBracket)); } let (_, second) = self.must_peek_double()?; match (first, second) { // variable or anchor ('$', _) => { let (offset, var_or_anchor) = self.parse_variable()?; match var_or_anchor { None => Ok((offset, false, MainToken::DollarSign)), Some(v) => Ok((offset, true, MainToken::from_variable_value(v.clone()))), } } // string ('{', _) => self .parse_string() .map(|(offset, l)| (offset, false, MainToken::Literal(l))), // inner set ('\\', 'p' | 'P') | ('[', _) => { let mut inner_builder = UnicodeSetBuilder::new_internal( self.iter, self.source, self.variable_map, self.xid_start, self.xid_continue, self.pat_ws, self.property_provider, ); inner_builder.parse_unicode_set()?; let (single, string_set) = inner_builder.finalize(); // note: offset - 1, because we already consumed full set let offset = self.must_peek_index()? - 1; let mut strings = string_set.into_iter().collect::>(); strings.sort(); let cpilasl = CodePointInversionListAndStringList::try_from( single.build(), VarZeroVec::from(&strings), ) .map_err(|_| PEK::Internal.with_offset(offset))?; Ok((offset, false, MainToken::UnicodeSet(cpilasl))) } // note: c cannot be a whitespace, because we called skip_whitespace just before // (in the main parse loop), so it's safe to call this guard function (c, _) if legal_char_start(c) => self .parse_char() .map(|(offset, c)| (offset, false, MainToken::Literal(Literal::CharKind(c)))), ('-', _) => { self.iter.next(); Ok((initial_offset, false, MainToken::Minus)) } ('&', _) => { self.iter.next(); Ok((initial_offset, false, MainToken::Ampersand)) } (c, _) => Err(PEK::UnexpectedChar(c).with_offset(initial_offset)), } } // parses a variable or an anchor. expects '$' as next token. // if this is a single $ (eg `[... $ ]` or the invalid `$ a`), then this function returns Ok(None), // otherwise Ok(Some(variable_value)). fn parse_variable(&mut self) -> Result<(usize, Option<&'a VariableValue<'a>>)> { self.consume('$')?; let mut res = String::new(); let (mut var_offset, first_c) = self.must_peek()?; if !self.xid_start.contains(first_c) { // -1 because we already consumed the '$' return Ok((var_offset - 1, None)); } res.push(first_c); self.iter.next(); // important: if we are parsing a root unicodeset as a variable, we might reach EOF as // a valid end of the variable name, so we cannot use must_peek here. while let Some(&(offset, c)) = self.iter.peek() { if !self.xid_continue.contains(c) { break; } // only update the offset if we're adding a new char to our variable var_offset = offset; self.iter.next(); res.push(c); } if let Some(v) = self.variable_map.0.get(&res) { return Ok((var_offset, Some(v))); } Err(PEK::UnknownVariable.with_offset(var_offset)) } // parses and consumes: '{' (s charInString)* s '}' fn parse_string(&mut self) -> Result<(usize, Literal)> { self.consume('{')?; let mut buffer = String::new(); let mut last_offset; loop { self.skip_whitespace(); last_offset = self.must_peek_index()?; match self.must_peek_char()? { '}' => { self.iter.next(); break; } // note: c cannot be a whitespace, because we called skip_whitespace just before, // so it's safe to call this guard function c if legal_char_in_string_start(c) => { // don't need the offset, because '}' will always be the last char let (_, c) = self.parse_char()?; match c { SingleOrMultiChar::Single(c) => buffer.push(c), SingleOrMultiChar::Multi(first) => { buffer.push(first); self.parse_multi_escape_into_string(&mut buffer)?; } } } c => return self.error_here(PEK::UnexpectedChar(c)), } } let mut chars = buffer.chars(); let literal = match (chars.next(), chars.next()) { (Some(c), None) => Literal::CharKind(SingleOrMultiChar::Single(c)), _ => Literal::String(buffer), }; Ok((last_offset, literal)) } // finishes a partial multi escape parse. in case of a parse error, self.single_set // may be left in an inconsistent state fn parse_multi_escape_into_set(&mut self) -> Result<()> { // note: would be good to somehow merge the two multi_escape methods. splitting up the UnicodeSetBuilder into a more // conventional parser + lexer combo might allow this. // issue is that we cannot pass this method an argument that somehow mutates `self` in the current architecture. // self.lexer.parse_multi_into_charappendable(&mut self.single_set) should work because the lifetimes are separate // whitespace before first char of this loop (ie, second char in this multi_escape) must be // enforced when creating the SingleOrMultiChar::Multi. let mut first = true; loop { let skipped = self.skip_whitespace(); match self.must_peek_char()? { '}' => { self.iter.next(); return Ok(()); } initial_c => { if skipped == 0 && !first { // bracketed hex code points must be separated by whitespace return self.error_here(PEK::UnexpectedChar(initial_c)); } first = false; let (_, c) = self.parse_hex_digits_into_char(1, 6)?; self.single_set.add_char(c); } } } } // finishes a partial multi escape parse. in case of a parse error, the caller must clean up the // string if necessary. fn parse_multi_escape_into_string(&mut self, s: &mut String) -> Result<()> { // whitespace before first char of this loop (ie, second char in this multi_escape) must be // enforced when creating the SingleOrMultiChar::Multi. let mut first = true; loop { let skipped = self.skip_whitespace(); match self.must_peek_char()? { '}' => { self.iter.next(); return Ok(()); } initial_c => { if skipped == 0 && !first { // bracketed hex code points must be separated by whitespace return self.error_here(PEK::UnexpectedChar(initial_c)); } first = false; let (_, c) = self.parse_hex_digits_into_char(1, 6)?; s.push(c); } } } } // starts with \ and consumes the whole escape sequence if a single // char is escaped, otherwise pauses the parse after the first char fn parse_escaped_char(&mut self) -> Result<(usize, SingleOrMultiChar)> { self.consume('\\')?; let (offset, next_char) = self.must_next()?; match next_char { 'u' | 'x' if self.peek_char() == Some('{') => { // bracketedHex self.iter.next(); self.skip_whitespace(); let (_, first_c) = self.parse_hex_digits_into_char(1, 6)?; let skipped = self.skip_whitespace(); match self.must_peek()? { (offset, '}') => { self.iter.next(); Ok((offset, SingleOrMultiChar::Single(first_c))) } // note: enforcing whitespace after the first char here, because the parse_multi_escape functions // won't have access to this information anymore (offset, c) if c.is_ascii_hexdigit() && skipped > 0 => { Ok((offset, SingleOrMultiChar::Multi(first_c))) } (_, c) => self.error_here(PEK::UnexpectedChar(c)), } } 'u' => { // 'u' hex{4} self.parse_hex_digits_into_char(4, 4) .map(|(offset, c)| (offset, SingleOrMultiChar::Single(c))) } 'x' => { // 'x' hex{2} self.parse_hex_digits_into_char(2, 2) .map(|(offset, c)| (offset, SingleOrMultiChar::Single(c))) } 'U' => { // 'U00' ('0' hex{5} | '10' hex{4}) self.consume('0')?; self.consume('0')?; self.parse_hex_digits_into_char(6, 6) .map(|(offset, c)| (offset, SingleOrMultiChar::Single(c))) } 'N' => { // parse code point with name in {} // tracking issue: https://github.com/unicode-org/icu4x/issues/1397 Err(PEK::Unimplemented.with_offset(offset)) } 'a' => Ok((offset, SingleOrMultiChar::Single('\u{0007}'))), 'b' => Ok((offset, SingleOrMultiChar::Single('\u{0008}'))), 't' => Ok((offset, SingleOrMultiChar::Single('\u{0009}'))), 'n' => Ok((offset, SingleOrMultiChar::Single('\u{000A}'))), 'v' => Ok((offset, SingleOrMultiChar::Single('\u{000B}'))), 'f' => Ok((offset, SingleOrMultiChar::Single('\u{000C}'))), 'r' => Ok((offset, SingleOrMultiChar::Single('\u{000D}'))), _ => Ok((offset, SingleOrMultiChar::Single(next_char))), } } // starts with :, consumes the trailing :] fn parse_property_posix(&mut self) -> Result<()> { self.consume(':')?; if self.must_peek_char()? == '^' { self.inverted = true; self.iter.next(); } self.parse_property_inner(':')?; self.consume(']')?; Ok(()) } // starts with \p{ or \P{, consumes the trailing } fn parse_property_perl(&mut self) -> Result<()> { self.consume('\\')?; match self.must_next()? { (_, 'p') => {} (_, 'P') => self.inverted = true, (offset, c) => return Err(PEK::UnexpectedChar(c).with_offset(offset)), } self.consume('{')?; self.parse_property_inner('}')?; Ok(()) } fn parse_property_inner(&mut self, end: char) -> Result<()> { // UnicodeSet spec ignores whitespace, '-', and '_', // but ECMA-262 requires '_', so we'll allow that. // TODO(#3559): support loose matching on property names (e.g., "AS -_- CII_Hex_ D-igit") // TODO(#3559): support more properties than ECMA-262 let property_offset; let mut key_buffer = String::new(); let mut value_buffer = String::new(); enum State { // initial state, nothing parsed yet Begin, // non-empty property name PropertyName, // property name parsed, '=' or '≠' parsed, no value parsed yet PropertyValueBegin, // non-empty property name, non-empty property value PropertyValue, } use State::*; let mut state = Begin; // whether '=' (true) or '≠' (false) was parsed let mut equality = true; loop { self.skip_whitespace(); match (state, self.must_peek_char()?) { // parse the end of the property expression (PropertyName | PropertyValue, c) if c == end => { // byte index of (full) property name/value is one back property_offset = self.must_peek_index()? - 1; self.iter.next(); break; } // parse the property name // NOTE: this might be too strict, because in the case of e.g. [:value:], we might want to // allow [:lower-case-letter:] ([:gc=lower-case-letter:] works) (Begin | PropertyName, c) if c.is_ascii_alphanumeric() || c == '_' => { key_buffer.push(c); self.iter.next(); state = PropertyName; } // parse the name-value separator (PropertyName, c @ ('=' | '≠')) => { equality = c == '='; self.iter.next(); state = PropertyValueBegin; } // parse the property value (PropertyValue | PropertyValueBegin, c) if c != end => { value_buffer.push(c); self.iter.next(); state = PropertyValue; } (_, c) => return self.error_here(PEK::UnexpectedChar(c)), } } if !equality { self.inverted = !self.inverted; } let inverted = self .load_property_codepoints(&key_buffer, &value_buffer) // any error that does not already have an offset should use the appropriate property offset .map_err(|e| e.or_with_offset(property_offset))?; if inverted { self.inverted = !self.inverted; } Ok(()) } // returns whether the set needs to be inverted or not fn load_property_codepoints(&mut self, key: &str, value: &str) -> Result { // we support: // [:gc = value:] // [:sc = value:] // [:scx = value:] // [:Grapheme_Cluster_Break = value:] // [:Sentence_Break = value:] // [:Word_Break = value:] // [:value:] - looks up value in gc, sc // [:prop:] - binary property, returns codepoints that have the property // [:prop = truthy/falsy:] - same as above let mut inverted = false; // contains a value for the General_Category property that needs to be tried let mut try_gc = Err(PEK::UnknownProperty.into()); // contains a value for the Script property that needs to be tried let mut try_sc = Err(PEK::UnknownProperty.into()); // contains a value for the Script_Extensions property that needs to be tried let mut try_scx = Err(PEK::UnknownProperty.into()); // contains a value for the Grapheme_Cluster_Break property that needs to be tried let mut try_gcb = Err(PEK::UnknownProperty.into()); // contains a value for the Indic_Conjuct_Break property that needs to be tried let mut try_incb = Err(PEK::UnknownProperty.into()); // contains a value for the Line_Break property that needs to be tried let mut try_lb = Err(PEK::UnknownProperty.into()); // contains a value for the Sentence_Break property that needs to be tried let mut try_sb = Err(PEK::UnknownProperty.into()); // contains a value for the Word_Break property that needs to be tried let mut try_wb = Err(PEK::UnknownProperty.into()); // contains a value for the East_Asian_Width property that needs to be tried let mut try_eaw = Err(PEK::UnknownProperty.into()); // contains a supposed binary property name that needs to be tried let mut try_binary = Err(PEK::UnknownProperty.into()); // contains a supposed canonical combining class property name that needs to be tried let mut try_ccc: Result<&str, ParseError> = Err(PEK::UnknownProperty.into()); // contains a supposed block property name that needs to be tried let mut try_block: Result<&str, ParseError> = Err(PEK::UnknownProperty.into()); if !value.is_empty() { // key is gc, sc, scx, grapheme cluster break, sentence break, word break // value is a property value // OR // key is a binary property and value is a truthy/falsy value fn loose(s: &[u8]) -> impl Iterator + '_ { s.iter() .map(|&c| c.to_ascii_lowercase()) .filter(|&c| c != b'_') } if loose(key.as_bytes()).eq(loose(GeneralCategory::NAME)) || loose(key.as_bytes()).eq(loose(GeneralCategory::SHORT_NAME)) { try_gc = Ok(value) } else if loose(key.as_bytes()).eq(loose(GraphemeClusterBreak::NAME)) || loose(key.as_bytes()).eq(loose(GraphemeClusterBreak::SHORT_NAME)) { try_gcb = Ok(value) } else if loose(key.as_bytes()).eq(loose(IndicConjunctBreak::NAME)) || loose(key.as_bytes()).eq(loose(IndicConjunctBreak::SHORT_NAME)) { try_incb = Ok(value) } else if loose(key.as_bytes()).eq(loose(LineBreak::NAME)) || loose(key.as_bytes()).eq(loose(LineBreak::SHORT_NAME)) { try_lb = Ok(value) } else if loose(key.as_bytes()).eq(loose(Script::NAME)) || loose(key.as_bytes()).eq(loose(Script::SHORT_NAME)) { try_sc = Ok(value) } else if loose(key.as_bytes()).eq(loose(SentenceBreak::NAME)) || loose(key.as_bytes()).eq(loose(SentenceBreak::SHORT_NAME)) { try_sb = Ok(value) } else if loose(key.as_bytes()).eq(loose(WordBreak::NAME)) || loose(key.as_bytes()).eq(loose(WordBreak::SHORT_NAME)) { try_wb = Ok(value) } else if loose(key.as_bytes()).eq(loose(EastAsianWidth::NAME)) || loose(key.as_bytes()).eq(loose(EastAsianWidth::SHORT_NAME)) { try_eaw = Ok(value) } else if loose(key.as_bytes()).eq(loose(CanonicalCombiningClass::NAME)) || loose(key.as_bytes()).eq(loose(CanonicalCombiningClass::SHORT_NAME)) { try_ccc = Ok(value) } else if loose(key.as_bytes()).eq(loose(b"Script_Extensions")) || loose(key.as_bytes()).eq(loose(b"scx")) { try_scx = Ok(value) } else if loose(key.as_bytes()).eq(loose(b"Block")) || loose(key.as_bytes()).eq(loose(b"blk")) { try_block = Ok(value) } else { let normalized_value = value.to_ascii_lowercase(); let truthy = matches!(normalized_value.as_str(), "true" | "t" | "yes" | "y"); let falsy = matches!(normalized_value.as_str(), "false" | "f" | "no" | "n"); // value must either match truthy or falsy if truthy == falsy { return Err(PEK::UnknownProperty.into()); } // correctness: if we reach this point, only `try_binary` can be Ok, hence // it does not matter that further down we unconditionally return `inverted`, // because only `try_binary` can enter that code path. inverted = falsy; try_binary = Ok(key); } } else { // key is binary property // OR a value of gc, sc (only gc or sc are supported as implicit keys by UTS35!) try_gc = Ok(key); try_sc = Ok(key); try_binary = Ok(key); } try_gc .and_then(|value| self.try_load_general_category_set(value)) .or_else(|_| try_sc.and_then(|value| self.try_load_script_set(value))) .or_else(|_| try_scx.and_then(|value| self.try_load_script_extensions_set(value))) .or_else(|_| try_binary.and_then(|value| self.try_load_ecma262_binary_set(value))) .or_else(|_| try_gcb.and_then(|value| self.try_load_grapheme_cluster_break_set(value))) .or_else(|_| try_incb.and_then(|value| self.try_load_indic_conjunct_break_set(value))) .or_else(|_| try_lb.and_then(|value| self.try_load_line_break_set(value))) .or_else(|_| try_sb.and_then(|value| self.try_load_sentence_break_set(value))) .or_else(|_| try_wb.and_then(|value| self.try_load_word_break_set(value))) .or_else(|_| try_eaw.and_then(|value| self.try_load_east_asian_width_set(value))) .or_else(|_| try_ccc.and_then(|value| self.try_load_ccc_set(value))) .or_else(|_| try_block.and_then(|value| self.try_load_block_set(value)))?; Ok(inverted) } fn finalize(mut self) -> (CodePointInversionListBuilder, BTreeSet) { if self.inverted { // code point inversion; removes all strings #[cfg(feature = "log")] { let single_set = self.single_set.clone().build(); if !self .string_set .iter() // if the string starts with a cp in the cp-set, then it'll be rejected through that .all(|s| s.chars().next().is_some_and(|c| single_set.contains(c))) { log::info!( "Inverting a unicode set with strings. This removes all strings entirely." ); } } self.string_set.clear(); self.single_set.complement(); } (self.single_set, self.string_set) } // parses either a raw char or an escaped char. all chars are allowed, the caller must make sure to handle // cases where some characters are not allowed fn parse_char(&mut self) -> Result<(usize, SingleOrMultiChar)> { let (offset, c) = self.must_peek()?; match c { '\\' => self.parse_escaped_char(), _ => { self.iter.next(); Ok((offset, SingleOrMultiChar::Single(c))) } } } // note: could turn this from the current two-pass approach into a one-pass approach // by manually parsing the digits instead of using u32::from_str_radix. fn parse_hex_digits_into_char(&mut self, min: usize, max: usize) -> Result<(usize, char)> { let first_offset = self.must_peek_index()?; let end_offset = self.validate_hex_digits(min, max)?; // validate_hex_digits ensures that chars (including the last one) are ascii hex digits, // which are all exactly one UTF-8 byte long, so slicing on these offsets always respects char boundaries let hex_source = &self.source[first_offset..=end_offset]; let num = u32::from_str_radix(hex_source, 16).map_err(|_| PEK::Internal)?; char::try_from(num) .map(|c| (end_offset, c)) .map_err(|_| PEK::InvalidEscape.with_offset(end_offset)) } // validates [0-9a-fA-F]{min,max}, returns the offset of the last digit, consuming everything in the process fn validate_hex_digits(&mut self, min: usize, max: usize) -> Result { let mut last_offset = 0; for count in 0..max { let (offset, c) = self.must_peek()?; if !c.is_ascii_hexdigit() { if count < min { return Err(PEK::UnexpectedChar(c).with_offset(offset)); } else { break; } } self.iter.next(); last_offset = offset; } Ok(last_offset) } // returns the number of skipped whitespace chars fn skip_whitespace(&mut self) -> usize { let mut num = 0; while let Some(c) = self.peek_char() { if !self.pat_ws.contains(c) { break; } self.iter.next(); num += 1; } num } fn consume(&mut self, expected: char) -> Result<()> { match self.must_next()? { (offset, c) if c != expected => Err(PEK::UnexpectedChar(c).with_offset(offset)), _ => Ok(()), } } // use this whenever an empty iterator would imply an Eof error fn must_next(&mut self) -> Result<(usize, char)> { self.iter.next().ok_or(ParseError { offset: None, kind: PEK::Eof, }) } // use this whenever an empty iterator would imply an Eof error fn must_peek(&mut self) -> Result<(usize, char)> { self.iter.peek().copied().ok_or(ParseError { offset: None, kind: PEK::Eof, }) } // must_peek, but looks two chars ahead. use sparingly fn must_peek_double(&mut self) -> Result<(usize, char)> { let mut copy = self.iter.clone(); copy.next(); copy.next().ok_or(ParseError { offset: None, kind: PEK::Eof, }) } // see must_peek fn must_peek_char(&mut self) -> Result { self.must_peek().map(|(_, c)| c) } // see must_peek fn must_peek_index(&mut self) -> Result { self.must_peek().map(|(idx, _)| idx) } fn peek_char(&mut self) -> Option { self.iter.peek().map(|&(_, c)| c) } // TODO: return Result once ! is stable #[inline] fn error_here(&mut self, kind: ParseErrorKind) -> Result { match self.iter.peek() { None => Err(kind.into()), Some(&(offset, _)) => Err(kind.with_offset(offset)), } } fn process_strings(&mut self, op: Operation, other_strings: BTreeSet) { match op { Operation::Union => self.string_set.extend(other_strings), Operation::Difference => { self.string_set = self .string_set .difference(&other_strings) .cloned() .collect() } Operation::Intersection => { self.string_set = self .string_set .intersection(&other_strings) .cloned() .collect() } } } fn process_chars(&mut self, op: Operation, other_chars: CodePointInversionList) { match op { Operation::Union => self.single_set.add_set(&other_chars), Operation::Difference => self.single_set.remove_set(&other_chars), Operation::Intersection => self.single_set.retain_set(&other_chars), } } fn try_load_general_category_set(&mut self, name: &str) -> Result<()> { // TODO(#3550): This could be cached; does not depend on name. let name_map = PropertyParser::::try_new_unstable(self.property_provider) .map_err(|_| PEK::Internal)?; let gc_value = name_map .as_borrowed() .get_loose(name) .ok_or(PEK::UnknownProperty)?; // TODO(#3550): This could be cached; does not depend on name. let set = CodePointMapData::::try_new_unstable(self.property_provider) .map_err(|_| PEK::Internal)? .as_borrowed() .get_set_for_value_group(gc_value); self.single_set.add_set(&set.to_code_point_inversion_list()); Ok(()) } fn try_get_script(&self, name: &str) -> Result