Debian Codegraph @

src/ecma.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
2
3 use regex_syntaxrust-analyzer cargo regex-syntax 0.8.8 crate/::astrust-analyzer cargo regex-syntax 0.8.8 ast/::{
4 selfrust-analyzer cargo regex-syntax 0.8.8 ast/, parserust-analyzer cargo regex-syntax 0.8.8 ast/parse/::Parserrust-analyzer cargo regex-syntax 0.8.8 ast/parse/Parser#, Astrust-analyzer cargo regex-syntax 0.8.8 ast/Ast#, ClassPerlrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerl#, ClassPerlKindrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerlKind#, ClassSetItemrust-analyzer cargo regex-syntax 0.8.8 ast/ClassSetItem#, ErrorKindrust-analyzer cargo regex-syntax 0.8.8 ast/ErrorKind#, Literalrust-analyzer cargo regex-syntax 0.8.8 ast/Literal#,
5 LiteralKindrust-analyzer cargo regex-syntax 0.8.8 ast/LiteralKind#, Spanrust-analyzer cargo regex-syntax 0.8.8 ast/Span#, SpecialLiteralKindrust-analyzer cargo regex-syntax 0.8.8 ast/SpecialLiteralKind#, Visitorrust-analyzer cargo regex-syntax 0.8.8 ast/visitor/Visitor#,
6 };
7
8 /// Convert ECMA Script 262 regex to Rust regex on the best effort basiso.
9 ///
10 /// NOTE: Patterns with look arounds and backreferecnes are not supported.
11 pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) fn to_rust_regexrust-analyzer cargo jsonschema 0.37.4 ecma/to_rust_regex().(pattern: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str>, ()> {
12 let mut pattern = Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(pattern);
13 let mut ast = loop {
14 match Parserrust-analyzer cargo regex-syntax 0.8.8 ast/parse/Parser#::newrust-analyzer cargo regex-syntax 0.8.8 ast/parse/impl#[Parser]new().().parserust-analyzer cargo regex-syntax 0.8.8 ast/parse/impl#[Parser]parse().(&pattern) {
15 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ast) => break ast,
16 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().error.kindrust-analyzer cargo regex-syntax 0.8.8 ast/impl#[Error]kind().() rust-analyzer cargo regex-syntax 0.8.8 ast/impl#[ErrorKind][`PartialEq<Self>`]eq().==rust-analyzer cargo regex-syntax 0.8.8 ast/impl#[ErrorKind][`PartialEq<Self>`]eq(). rust-analyzer cargo regex-syntax 0.8.8 ast/impl#[ErrorKind][`PartialEq<Self>`]eq().ErrorKindrust-analyzer cargo regex-syntax 0.8.8 ast/ErrorKind#::EscapeUnrecognizedrust-analyzer cargo regex-syntax 0.8.8 ast/ErrorKind#EscapeUnrecognized# => {
17 let Spanrust-analyzer cargo regex-syntax 0.8.8 ast/Span# { startrust-analyzer cargo regex-syntax 0.8.8 ast/Span#start., endrust-analyzer cargo regex-syntax 0.8.8 ast/Span#end. } = error.spanrust-analyzer cargo regex-syntax 0.8.8 ast/impl#[Error]span().();
18 let source = error.patternrust-analyzer cargo regex-syntax 0.8.8 ast/impl#[Error]pattern().();
19 if &source[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().r"\c" {
20 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(letter) = &source[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().end.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() {
21 if letter.is_ascii_alphabeticrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_alphabetic().() {
22 let start = start.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset.;
23 let end = end.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1;
24 let replacement = ((*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().letter as u8) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Rem<Self>`]rem().32) as char;
25 match pattern {
26 Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(_) => {
27 let prefix = &source[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#start]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
28 let suffix = &source[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().end..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
29 pattern = Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{prefix}{replacement}{suffix}"));
30 }
31 Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(ref mut buffer) => {
32 let mut char_buffer = [0; 4];
33 let replacement = replacement.encode_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]encode_utf8().(&mut char_buffer);
34 buffer.replace_rangerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]replace_range().(start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end, replacement);
35 }
36 }
37 continue;
38 }
39 }
40 }
41 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(());
42 }
43 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error)
44 if matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(
45 error.kindrust-analyzer cargo regex-syntax 0.8.8 ast/impl#[Error]kind().(),
46 ErrorKindrust-analyzer cargo regex-syntax 0.8.8 ast/ErrorKind#::UnsupportedLookAroundrust-analyzer cargo regex-syntax 0.8.8 ast/ErrorKind#UnsupportedLookAround# | ErrorKindrust-analyzer cargo regex-syntax 0.8.8 ast/ErrorKind#::UnsupportedBackreferencerust-analyzer cargo regex-syntax 0.8.8 ast/ErrorKind#UnsupportedBackreference#
47 ) =>
48 {
49 // Can't translate patterns with look arounds & backreferences
50 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(pattern);
51 }
52 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
53 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(());
54 }
55 };
56 };
57 let mut has_changes;
58 loop {
59 let translator = Ecma262Translatorrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#::newrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`]new().(pattern);
60 (pattern, has_changes) = astrust-analyzer cargo regex-syntax 0.8.8 ast/::visitrust-analyzer cargo regex-syntax 0.8.8 ast/visitor/visit().(&ast, translator).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| ())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
61 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().has_changes {
62 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(pattern);
63 }
64 match Parserrust-analyzer cargo regex-syntax 0.8.8 ast/parse/Parser#::newrust-analyzer cargo regex-syntax 0.8.8 ast/parse/impl#[Parser]new().().parserust-analyzer cargo regex-syntax 0.8.8 ast/parse/impl#[Parser]parse().(&pattern) {
65 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(updated_ast) => {
66 ast = updated_ast;
67 }
68 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
69 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(());
70 }
71 }
72 }
73 }
74
75 struct Ecma262Translatorrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#<'a> {
76 patternrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#pattern.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>,
77 offsetrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#offset.: usize,
78 has_changesrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#has_changes.: bool,
79 }
80
81 impl<'a> Ecma262Translatorrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#<'a> {
82 fn newrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`]new().(input: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>) -> Selfrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`] {
83 Selfrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`] {
84 patternrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#pattern.: input,
85 offsetrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#offset.: 0,
86 has_changesrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#has_changes.: false,
87 }
88 }
89
90 fn replace_implrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`]replace_impl().(&mut self, span: &Spanrust-analyzer cargo regex-syntax 0.8.8 ast/Span#, replacement: &str) {
91 let Spanrust-analyzer cargo regex-syntax 0.8.8 ast/Span# { startrust-analyzer cargo regex-syntax 0.8.8 ast/Span#start., endrust-analyzer cargo regex-syntax 0.8.8 ast/Span#end. } = span;
92 match self.patternrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#pattern. {
93 Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(pattern) => {
94 let prefix = &pattern[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#start.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
95 let suffix = &pattern[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().end.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
96 self.patternrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#pattern. = Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{prefix}{replacement}{suffix}"));
97 }
98 Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(ref mut buffer) => {
99 buffer.replace_rangerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]replace_range().(
100 start.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().self.offsetrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#offset...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().self.offsetrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#offset.,
101 replacement,
102 );
103 }
104 }
105 self.offsetrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#offset. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().replacement.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().(end.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().start.offsetrust-analyzer cargo regex-syntax 0.8.8 ast/Position#offset.);
106 self.has_changesrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#has_changes. = true;
107 }
108
109 fn replacerust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`]replace().(&mut self, cls: &ClassPerlrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerl#) {
110 match cls.kindrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerl#kind. {
111 ClassPerlKindrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerlKind#::Digitrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerlKind#Digit# => {
112 let replacement = if cls.negatedrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerl#negated. { "[^0-9]" } else { "[0-9]" };
113 self.replace_implrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`]replace_impl().(&cls.spanrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerl#span., replacement);
114 }
115 ClassPerlKindrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerlKind#::Wordrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerlKind#Word# => {
116 let replacement = if cls.negatedrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerl#negated. {
117 "[^A-Za-z0-9_]"
118 } else {
119 "[A-Za-z0-9_]"
120 };
121 self.replace_implrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`]replace_impl().(&cls.spanrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerl#span., replacement);
122 }
123 ClassPerlKindrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerlKind#::Spacerust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerlKind#Space# => {
124 let replacement = &if cls.negatedrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerl#negated. {
125 "[^ \t\n\r\u{000b}\u{000c}\u{00a0}\u{feff}\u{2003}\u{2029}]"
126 } else {
127 "[ \t\n\r\u{000b}\u{000c}\u{00a0}\u{feff}\u{2003}\u{2029}]"
128 };
129 self.replace_implrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`]replace_impl().(&cls.spanrust-analyzer cargo regex-syntax 0.8.8 ast/ClassPerl#span., replacement);
130 }
131 }
132 }
133 }
134
135 impl<'a> Visitorrust-analyzer cargo regex-syntax 0.8.8 ast/visitor/Visitor# for Ecma262Translatorrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#<'a> {
136 type Outputrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`][Visitor]Output# = (Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>, bool);
137 type Errrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`][Visitor]Err# = astrust-analyzer cargo regex-syntax 0.8.8 ast/::Errorrust-analyzer cargo regex-syntax 0.8.8 ast/Error#;
138
139 fn finishrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`][Visitor]finish().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`][Visitor]::Outputrust-analyzer cargo regex-syntax 0.8.8 ast/visitor/Visitor#Output#, Selfrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`][Visitor]::Errrust-analyzer cargo regex-syntax 0.8.8 ast/visitor/Visitor#Err#> {
140 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((self.patternrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#pattern., self.has_changesrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#has_changes.))
141 }
142
143 fn visit_class_set_item_prerust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`][Visitor]visit_class_set_item_pre().(&mut self, item: &astrust-analyzer cargo regex-syntax 0.8.8 ast/::ClassSetItemrust-analyzer cargo regex-syntax 0.8.8 ast/ClassSetItem#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Selfrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`][Visitor]::Errrust-analyzer cargo regex-syntax 0.8.8 ast/visitor/Visitor#Err#> {
144 if let ClassSetItemrust-analyzer cargo regex-syntax 0.8.8 ast/ClassSetItem#::Perlrust-analyzer cargo regex-syntax 0.8.8 ast/ClassSetItem#Perl#(cls) = item {
145 self.replacerust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`]replace().(cls);
146 }
147 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
148 }
149 fn visit_postrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`][Visitor]visit_post().(&mut self, ast: &Astrust-analyzer cargo regex-syntax 0.8.8 ast/Ast#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Selfrust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`][Visitor]::Errrust-analyzer cargo regex-syntax 0.8.8 ast/visitor/Visitor#Err#> {
150 if self.has_changesrust-analyzer cargo jsonschema 0.37.4 ecma/Ecma262Translator#has_changes. {
151 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(());
152 }
153 match ast {
154 Astrust-analyzer cargo regex-syntax 0.8.8 ast/Ast#::ClassPerlrust-analyzer cargo regex-syntax 0.8.8 ast/Ast#ClassPerl#(perl) => {
155 self.replacerust-analyzer cargo jsonschema 0.37.4 ecma/impl#[`Ecma262Translator<'a>`]replace().(perl);
156 }
157 Astrust-analyzer cargo regex-syntax 0.8.8 ast/Ast#::Literalrust-analyzer cargo regex-syntax 0.8.8 ast/Ast#Literal#(literal) => {
158 if let Literalrust-analyzer cargo regex-syntax 0.8.8 ast/Literal# {
159 kindrust-analyzer cargo regex-syntax 0.8.8 ast/Literal#kind.: LiteralKindrust-analyzer cargo regex-syntax 0.8.8 ast/LiteralKind#::Specialrust-analyzer cargo regex-syntax 0.8.8 ast/LiteralKind#Special#(SpecialLiteralKindrust-analyzer cargo regex-syntax 0.8.8 ast/SpecialLiteralKind#::Bellrust-analyzer cargo regex-syntax 0.8.8 ast/SpecialLiteralKind#Bell#),
160 ..
161 } = literal.as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][`AsRef<T>`]as_ref().()
162 {
163 // Not possible to create a custom error, hence throw an arbitrary one from a
164 // known invalid pattern.
165 return Parserrust-analyzer cargo regex-syntax 0.8.8 ast/parse/Parser#::newrust-analyzer cargo regex-syntax 0.8.8 ast/parse/impl#[Parser]new().().parserust-analyzer cargo regex-syntax 0.8.8 ast/parse/impl#[Parser]parse().("[").maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|_| ());
166 }
167 }
168 _ => (),
169 }
170 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
171 }
172 }
173
174 #[cfg(test)]
175 mod testsrust-analyzer cargo jsonschema 0.37.4 ecma/tests/ {
176 use superrust-analyzer cargo jsonschema 0.37.4 ecma/::*;
177 use test_caserust-analyzer cargo test-case 3.3.1 crate/::test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:;
178
179 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\d", "[0-9]"; "digit class")]
180 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\D", "[^0-9]"; "non-digit class")]
181 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\w", "[A-Za-z0-9_]"; "word class")]
182 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\W", "[^A-Za-z0-9_]"; "non-word class")]
183 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"[\d]", "[[0-9]]"; "digit class in character set")]
184 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"[\D]", "[[^0-9]]"; "non-digit class in character set")]
185 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"[\w]", "[[A-Za-z0-9_]]"; "word class in character set")]
186 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"[\W]", "[[^A-Za-z0-9_]]"; "non-word class in character set")]
187 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\d+\w*", "[0-9]+[A-Za-z0-9_]*"; "combination of digit and word classes")]
188 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\D*\W+", "[^0-9]*[^A-Za-z0-9_]+"; "combination of non-digit and non-word classes")]
189 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"[\d\w]", "[[0-9][A-Za-z0-9_]]"; "digit and word classes in character set")]
190 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"[^\d\w]", "[^[0-9][A-Za-z0-9_]]"; "negated digit and word classes in character set")]
191 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"[\d\w\d\w]", "[[0-9][A-Za-z0-9_][0-9][A-Za-z0-9_]]"; "multiple replacements")]
192 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\cA\cB\cC", "\x01\x02\x03"; "multiple control characters")]
193 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"foo\cIbar\cXbaz", "foo\x09bar\x18baz"; "control characters mixed with text")]
194 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\ca\cb\cc", "\x01\x02\x03"; "lowercase control characters")]
195 fn test_ecma262_to_rust_regexrust-analyzer cargo jsonschema 0.37.4 ecma/tests/test_ecma262_to_rust_regex().(input: &str, expected: &str) {
196 let result = to_rust_regexrust-analyzer cargo jsonschema 0.37.4 ecma/to_rust_regex().(input).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
197 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(result, expected);
198 }
199
200 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\c"; "incomplete control character")]
201 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\c?"; "invalid control character")]
202 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\mA"; "another invalid control character")]
203 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"[a-z"; "unclosed character class")]
204 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"(abc"; "unclosed parenthesis")]
205 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"abc)"; "unmatched closing parenthesis")]
206 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"a{3,2}"; "invalid quantifier range")]
207 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"\"; "trailing backslash")]
208 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(r"[a-\w]"; "invalid character range")]
209 fn test_invalid_regexrust-analyzer cargo jsonschema 0.37.4 ecma/tests/test_invalid_regex().(input: &str) {
210 let result = to_rust_regexrust-analyzer cargo jsonschema 0.37.4 ecma/to_rust_regex().(input);
211 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(result.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().(), "Expected error for input: {input}");
212 }
213 }