Debian Codegraph @

src/parser/subpattern.rs

raw symbols SCIP
1 use proc_macro2rust-analyzer cargo proc-macro2 1.0.95 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#;
2 use synrust-analyzer cargo syn 2.0.101 crate/::Identrust-analyzer cargo proc-macro2 1.0.95 Ident#;
3
4 use craterust-analyzer cargo logos-codegen 0.15.1 crate/::errorrust-analyzer cargo logos-codegen 0.15.1 error/::Errorsrust-analyzer cargo logos-codegen 0.15.1 error/Errors#;
5 use craterust-analyzer cargo logos-codegen 0.15.1 crate/::mirrust-analyzer cargo logos-codegen 0.15.1 mir/::Mirrust-analyzer cargo logos-codegen 0.15.1 mir/Mir#;
6 use craterust-analyzer cargo logos-codegen 0.15.1 crate/::parserrust-analyzer cargo logos-codegen 0.15.1 parser/::definitionrust-analyzer cargo logos-codegen 0.15.1 parser/definition/::{bytes_to_regex_stringrust-analyzer cargo logos-codegen 0.15.1 parser/definition/bytes_to_regex_string()., Literalrust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#};
7
8 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
9 pub struct Subpatternsrust-analyzer cargo logos-codegen 0.15.1 parser/subpattern/Subpatterns# {
10 maprust-analyzer cargo logos-codegen 0.15.1 parser/subpattern/Subpatterns#map.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(Identrust-analyzer cargo proc-macro2 1.0.95 Ident#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#)>,
11 }
12
13 impl Subpatternsrust-analyzer cargo logos-codegen 0.15.1 parser/subpattern/Subpatterns# {
14 pub fn addrust-analyzer cargo logos-codegen 0.15.1 parser/subpattern/impl#[Subpatterns]add().(&mut self, param: Identrust-analyzer cargo proc-macro2 1.0.95 Ident#, pattern: TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#, errors: &mut Errorsrust-analyzer cargo logos-codegen 0.15.1 error/Errors#) {
15 let lit = match synrust-analyzer cargo syn 2.0.101 crate/::parse2rust-analyzer cargo syn 2.0.101 parse2().::<Literalrust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#>(pattern) {
16 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(lit) => lit,
17 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => {
18 errors.errrust-analyzer cargo logos-codegen 0.15.1 error/impl#[Errors]err().(e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), e.spanrust-analyzer cargo syn 2.0.101 error/impl#[Error]span().());
19 return;
20 }
21 };
22
23 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((name, _)) = self.maprust-analyzer cargo logos-codegen 0.15.1 parser/subpattern/Subpatterns#map..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]find().(|(name, _)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().name rust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][`PartialEq<Self>`]eq().==rust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][`PartialEq<Self>`]eq(). rust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][`PartialEq<Self>`]eq().param) {
24 errors
25 .errrust-analyzer cargo logos-codegen 0.15.1 error/impl#[Errors]err().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{} can only be assigned once", param), param.spanrust-analyzer cargo proc-macro2 1.0.95 impl#[Ident]span().())
26 .errrust-analyzer cargo logos-codegen 0.15.1 error/impl#[Errors]err().("Previously assigned here", name.spanrust-analyzer cargo proc-macro2 1.0.95 impl#[Ident]span().());
27 return;
28 }
29
30 let fixed = self.fixrust-analyzer cargo logos-codegen 0.15.1 parser/subpattern/impl#[Subpatterns]fix().(&lit, errors);
31
32 // Validate the literal as proper regex. If it's not, emit an error.
33 let mir = match &lit {
34 Literalrust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#::Utf8rust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#Utf8#(_) => Mirrust-analyzer cargo logos-codegen 0.15.1 mir/Mir#::utf8rust-analyzer cargo logos-codegen 0.15.1 mir/impl#[Mir]utf8().(&fixed),
35 Literalrust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#::Bytesrust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#Bytes#(_) => Mirrust-analyzer cargo logos-codegen 0.15.1 mir/Mir#::binaryrust-analyzer cargo logos-codegen 0.15.1 mir/impl#[Mir]binary().(&fixed),
36 };
37
38 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = mir {
39 errors.errrust-analyzer cargo logos-codegen 0.15.1 error/impl#[Errors]err().(err, lit.spanrust-analyzer cargo logos-codegen 0.15.1 parser/definition/impl#[Literal]span().());
40 };
41
42 self.maprust-analyzer cargo logos-codegen 0.15.1 parser/subpattern/Subpatterns#map..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((param, fixed));
43 }
44
45 pub fn fixrust-analyzer cargo logos-codegen 0.15.1 parser/subpattern/impl#[Subpatterns]fix().(&self, lit: &Literalrust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#, errors: &mut Errorsrust-analyzer cargo logos-codegen 0.15.1 error/Errors#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
46 let mut i = 0;
47 let mut pattern = match lit {
48 Literalrust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#::Utf8rust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#Utf8#(s) => s.valuerust-analyzer cargo syn 2.0.101 lit/impl#[LitStr]value().(),
49 Literalrust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#::Bytesrust-analyzer cargo logos-codegen 0.15.1 parser/definition/Literal#Bytes#(b) => bytes_to_regex_stringrust-analyzer cargo logos-codegen 0.15.1 parser/definition/bytes_to_regex_string().(b.valuerust-analyzer cargo syn 2.0.101 lit/impl#[LitByteStr]value().()),
50 };
51
52 while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(f) = pattern[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()..findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().("(?&") {
53 i 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().f;
54 pattern.replace_rangerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]replace_range().(i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#i 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().3, "(?:");
55 i 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().3;
56
57 let subref_end = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(f) = pattern[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()..findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().(')') {
58 i 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().f
59 } else {
60 pattern.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]truncate().(i); // truncate so latter error doesn't suppress
61 break; // regex-syntax will report the unclosed group
62 };
63
64 let name = &pattern[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#subref_end]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().;
65 let name = match synrust-analyzer cargo syn 2.0.101 crate/::parse_strrust-analyzer cargo syn 2.0.101 parse_str().::<Identrust-analyzer cargo proc-macro2 1.0.95 Ident#>(name) {
66 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(name) => name,
67 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
68 errors.errrust-analyzer cargo logos-codegen 0.15.1 error/impl#[Errors]err().(
69 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("subpattern reference `{}` is not an identifier", name),
70 lit.spanrust-analyzer cargo logos-codegen 0.15.1 parser/definition/impl#[Literal]span().(),
71 );
72 // we emitted the error; make something up and continue
73 pattern.replace_rangerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]replace_range().(i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#subref_end, "_");
74 i 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().2;
75 continue;
76 }
77 };
78
79 match self.maprust-analyzer cargo logos-codegen 0.15.1 parser/subpattern/Subpatterns#map..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]find().(|(def, _)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().def rust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][`PartialEq<Self>`]eq().==rust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][`PartialEq<Self>`]eq(). rust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][`PartialEq<Self>`]eq().name) {
80 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((_, subpattern)) => {
81 pattern.replace_rangerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]replace_range().(i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#subref_end, subpattern);
82 i 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().subpattern.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() 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;
83 }
84 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
85 errors.errrust-analyzer cargo logos-codegen 0.15.1 error/impl#[Errors]err().(
86 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("subpattern reference `{}` has not been defined", name),
87 lit.spanrust-analyzer cargo logos-codegen 0.15.1 parser/definition/impl#[Literal]span().(),
88 );
89 // leaving `(?:name)` is fine
90 i = subref_end 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;
91 }
92 }
93 }
94
95 pattern
96 }
97 }