Debian Codegraph @

src/generator/fork.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().;
2
3 use fnvrust-analyzer cargo fnv 1.0.7 crate/::FnvHashMaprust-analyzer cargo fnv 1.0.7 FnvHashMap# as Maprust-analyzer cargo fnv 1.0.7 FnvHashMap#;
4 use proc_macro2rust-analyzer cargo proc-macro2 1.0.95 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#;
5 use quoterust-analyzer cargo quote 1.0.40 crate/::quoterust-analyzer cargo quote 1.0.40 quote!;
6
7 use craterust-analyzer cargo logos-codegen 0.15.1 crate/::generatorrust-analyzer cargo logos-codegen 0.15.1 generator/::{Contextrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#, Generatorrust-analyzer cargo logos-codegen 0.15.1 generator/Generator#};
8 use craterust-analyzer cargo logos-codegen 0.15.1 crate/::graphrust-analyzer cargo logos-codegen 0.15.1 graph/::{Forkrust-analyzer cargo logos-codegen 0.15.1 graph/fork/Fork#, NodeIdrust-analyzer cargo logos-codegen 0.15.1 graph/NodeId#, Rangerust-analyzer cargo logos-codegen 0.15.1 graph/range/Range#};
9 use craterust-analyzer cargo logos-codegen 0.15.1 crate/::utilrust-analyzer cargo logos-codegen 0.15.1 util/::ToIdentrust-analyzer cargo logos-codegen 0.15.1 util/ToIdent#;
10
11 type Targetsrust-analyzer cargo logos-codegen 0.15.1 generator/fork/Targets# = Maprust-analyzer cargo fnv 1.0.7 FnvHashMap#<NodeIdrust-analyzer cargo logos-codegen 0.15.1 graph/NodeId#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Rangerust-analyzer cargo logos-codegen 0.15.1 graph/range/Range#>>;
12
13 impl Generatorrust-analyzer cargo logos-codegen 0.15.1 generator/Generator#<'_> {
14 pub fn generate_forkrust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]generate_fork().(&mut self, this: NodeIdrust-analyzer cargo logos-codegen 0.15.1 graph/NodeId#, fork: &Forkrust-analyzer cargo logos-codegen 0.15.1 graph/fork/Fork#, mut ctx: Contextrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# {
15 let mut targets: Targetsrust-analyzer cargo logos-codegen 0.15.1 generator/fork/Targets# = Maprust-analyzer cargo fnv 1.0.7 FnvHashMap#::defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S>`][Default]default().();
16
17 for (range, then) in fork.branchesrust-analyzer cargo logos-codegen 0.15.1 graph/fork/impl#[Fork]branches().() {
18 targets.entryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]entry().(then).or_defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`Entry<'a, K, V>`]or_default().().pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(range);
19 }
20 let loops_to_self = self.metarust-analyzer cargo logos-codegen 0.15.1 generator/Generator#meta.[rust-analyzer cargo logos-codegen 0.15.1 graph/meta/impl#[Meta][`Index<NodeId>`]index().this]rust-analyzer cargo logos-codegen 0.15.1 graph/meta/impl#[Meta][`Index<NodeId>`]index()..loop_entry_fromrust-analyzer cargo logos-codegen 0.15.1 graph/meta/MetaItem#loop_entry_from..containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&this);
21
22 match targets.lenrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]len().() {
23 1 if loops_to_self => return self.generate_fast_looprust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]generate_fast_loop().(fork, ctx),
24 0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#2 => (),
25 _ => return self.generate_fork_jump_tablerust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]generate_fork_jump_table().(this, fork, targets, ctx),
26 }
27 let miss = ctx.missrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]miss().(fork.missrust-analyzer cargo logos-codegen 0.15.1 graph/fork/Fork#miss., self);
28 let end = self.fork_endrust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]fork_end().(this, &miss);
29 let (byte, read) = self.fork_readrust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]fork_read().(this, end, &mut ctx);
30 let branches = targets.into_iterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(id, ranges)| {
31 let next = self.gotorust-analyzer cargo logos-codegen 0.15.1 generator/impl#[`Generator<'a>`]goto().(id, ctx.advancerust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]advance().(1));
32
33 match *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Deref]deref().ranges {
34 [range] => {
35 quoterust-analyzer cargo quote 1.0.40 quote!!(#range => #next,)
36 }
37 [a, b] if a.is_byterust-analyzer cargo logos-codegen 0.15.1 graph/range/impl#[Range]is_byte().() && b.is_byterust-analyzer cargo logos-codegen 0.15.1 graph/range/impl#[Range]is_byte().() => {
38 quoterust-analyzer cargo quote 1.0.40 quote!!(#a | #b => #next,)
39 }
40 _ => {
41 let test = self.generate_testrust-analyzer cargo logos-codegen 0.15.1 generator/impl#[`Generator<'a>`]generate_test().(ranges).clonerust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][Clone]clone().();
42 let next = self.gotorust-analyzer cargo logos-codegen 0.15.1 generator/impl#[`Generator<'a>`]goto().(id, ctx.advancerust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]advance().(1));
43
44 quoterust-analyzer cargo quote 1.0.40 quote!!(byte if #test(byte) => #next,)
45 }
46 }
47 });
48
49 quoterust-analyzer cargo quote 1.0.40 quote!! {
50 #read
51
52 match #byte {
53 #(#branches)*
54 _ => #miss,
55 }
56 }
57 }
58
59 fn generate_fork_jump_tablerust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]generate_fork_jump_table().(
60 &mut self,
61 this: NodeIdrust-analyzer cargo logos-codegen 0.15.1 graph/NodeId#,
62 fork: &Forkrust-analyzer cargo logos-codegen 0.15.1 graph/fork/Fork#,
63 targets: Targetsrust-analyzer cargo logos-codegen 0.15.1 generator/fork/Targets#,
64 mut ctx: Contextrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#,
65 ) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# {
66 let miss = ctx.missrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]miss().(fork.missrust-analyzer cargo logos-codegen 0.15.1 graph/fork/Fork#miss., self);
67 let end = self.fork_endrust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]fork_end().(this, &miss);
68 let (byte, read) = self.fork_readrust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]fork_read().(this, end, &mut ctx);
69
70 let mut table: [u8; 256] = [0; 256];
71 let mut jumps = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["__".to_identrust-analyzer cargo logos-codegen 0.15.1 util/impl#[str][ToIdent]to_ident().()];
72
73 let branches = targets
74 .into_iterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][IntoIterator]into_iter().()
75 .enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().()
76 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(idx, (id, ranges))| {
77 let idx = (idx as u8) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().1;
78 let next = self.gotorust-analyzer cargo logos-codegen 0.15.1 generator/impl#[`Generator<'a>`]goto().(id, ctx.advancerust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]advance().(1));
79 jumps.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("J{}", id).to_identrust-analyzer cargo logos-codegen 0.15.1 util/impl#[str][ToIdent]to_ident().());
80
81 for byte in ranges.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().flattenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flatten().() {
82 table[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().byte as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut(). = idx;
83 }
84 let jump = jumps.lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
85
86 quoterust-analyzer cargo quote 1.0.40 quote!!(Jump::#jump => #next,)
87 })
88 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#>();
89
90 let may_error = table.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&0);
91
92 let jumps = jumps.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().();
93 let table = table.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|idx| &jumps[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().idx as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
94
95 let jumps = if may_error { jumps } else { &jumps[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..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 let error_branch = if may_error {
97 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quoterust-analyzer cargo quote 1.0.40 quote!!(Jump::__ => #miss))
98 } else {
99 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
100 };
101
102 quoterust-analyzer cargo quote 1.0.40 quote!! {
103 enum Jump {
104 #(#jumps,)*
105 }
106
107 const LUT: [Jump; 256] = {
108 use Jump::*;
109
110 [#(#table),*]
111 };
112
113 #read
114
115 match LUT[#byte as usize] {
116 #branches
117 #error_branch
118 }
119 }
120 }
121
122 fn fork_endrust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]fork_end().(&self, this: NodeIdrust-analyzer cargo logos-codegen 0.15.1 graph/NodeId#, miss: &TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# {
123 if this rust-analyzer cargo logos-codegen 0.15.1 graph/impl#[NodeId][`PartialEq<Self>`]eq().==rust-analyzer cargo logos-codegen 0.15.1 graph/impl#[NodeId][`PartialEq<Self>`]eq(). rust-analyzer cargo logos-codegen 0.15.1 graph/impl#[NodeId][`PartialEq<Self>`]eq().self.rootrust-analyzer cargo logos-codegen 0.15.1 generator/Generator#root. {
124 quoterust-analyzer cargo quote 1.0.40 quote!!(_end(lex))
125 } else {
126 miss.clonerust-analyzer cargo proc-macro2 1.0.95 impl#[TokenStream][Clone]clone().()
127 }
128 }
129
130 fn fork_readrust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]fork_read().(
131 &self,
132 this: NodeIdrust-analyzer cargo logos-codegen 0.15.1 graph/NodeId#,
133 end: TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#,
134 ctx: &mut Contextrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#,
135 ) -> (TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#, TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#) {
136 let min_read = self.metarust-analyzer cargo logos-codegen 0.15.1 generator/Generator#meta.[rust-analyzer cargo logos-codegen 0.15.1 graph/meta/impl#[Meta][`Index<NodeId>`]index().this]rust-analyzer cargo logos-codegen 0.15.1 graph/meta/impl#[Meta][`Index<NodeId>`]index()..min_readrust-analyzer cargo logos-codegen 0.15.1 graph/meta/MetaItem#min_read.;
137
138 if ctx.remainderrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]remainder().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(min_read, 1) {
139 // SAFETY: The read_byte below is safe because the if statement
140 // above checks it's still within bounds.
141 #[cfg(not(feature = "forbid_unsafe"))]
142 let read = unsafe { ctx.read_byte() };
143
144 #[cfg(feature = "forbid_unsafe")]
145 let read = ctx.read_byterust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]read_byte().();
146
147 return (quoterust-analyzer cargo quote 1.0.40 quote!!(byte), quoterust-analyzer cargo quote 1.0.40 quote!!(let byte = #read;));
148 }
149
150 match min_read {
151 0 | 1 => {
152 let read = ctx.readrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]read().(0);
153
154 (
155 quoterust-analyzer cargo quote 1.0.40 quote!!(byte),
156 quoterust-analyzer cargo quote 1.0.40 quote!! {
157 let byte = match #read {
158 Some(byte) => byte,
159 None => return #end,
160 };
161 },
162 )
163 }
164 len => {
165 let read = ctx.readrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]read().(len);
166
167 (
168 quoterust-analyzer cargo quote 1.0.40 quote!!(arr[0]),
169 quoterust-analyzer cargo quote 1.0.40 quote!! {
170 let arr = match #read {
171 Some(arr) => arr,
172 None => return #end,
173 };
174 },
175 )
176 }
177 }
178 }
179
180 fn generate_fast_looprust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]generate_fast_loop().(&mut self, fork: &Forkrust-analyzer cargo logos-codegen 0.15.1 graph/fork/Fork#, ctx: Contextrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# {
181 let miss = ctx.missrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]miss().(fork.missrust-analyzer cargo logos-codegen 0.15.1 graph/fork/Fork#miss., self);
182 let ranges = fork.branchesrust-analyzer cargo logos-codegen 0.15.1 graph/fork/impl#[Fork]branches().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(range, _)| range).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>();
183 let test = self.generate_testrust-analyzer cargo logos-codegen 0.15.1 generator/impl#[`Generator<'a>`]generate_test().(ranges);
184
185 quoterust-analyzer cargo quote 1.0.40 quote!! {
186 _fast_loop!(lex, #test, #miss);
187 }
188 }
189
190 pub fn fast_loop_macrorust-analyzer cargo logos-codegen 0.15.1 generator/fork/impl#[`Generator<'_>`]fast_loop_macro().() -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# {
191 quoterust-analyzer cargo quote 1.0.40 quote!! {
192 macro_rules! _fast_loop {
193 ($lex:ident, $test:ident, $miss:expr) => {
194 // Do one bounds check for multiple bytes till EOF
195 while let Some(arr) = $lex.read::<&[u8; 16]>() {
196 if $test(arr[0]) { if $test(arr[1]) { if $test(arr[2]) { if $test(arr[3]) {
197 if $test(arr[4]) { if $test(arr[5]) { if $test(arr[6]) { if $test(arr[7]) {
198 if $test(arr[8]) { if $test(arr[9]) { if $test(arr[10]) { if $test(arr[11]) {
199 if $test(arr[12]) { if $test(arr[13]) { if $test(arr[14]) { if $test(arr[15]) {
200
201 $lex.bump_unchecked(16); continue; } $lex.bump_unchecked(15); return $miss; }
202 $lex.bump_unchecked(14); return $miss; } $lex.bump_unchecked(13); return $miss; }
203 $lex.bump_unchecked(12); return $miss; } $lex.bump_unchecked(11); return $miss; }
204 $lex.bump_unchecked(10); return $miss; } $lex.bump_unchecked(9); return $miss; }
205 $lex.bump_unchecked(8); return $miss; } $lex.bump_unchecked(7); return $miss; }
206 $lex.bump_unchecked(6); return $miss; } $lex.bump_unchecked(5); return $miss; }
207 $lex.bump_unchecked(4); return $miss; } $lex.bump_unchecked(3); return $miss; }
208 $lex.bump_unchecked(2); return $miss; } $lex.bump_unchecked(1); return $miss; }
209
210 return $miss;
211 }
212
213 while $lex.test($test) {
214 $lex.bump_unchecked(1);
215 }
216
217 $miss
218 };
219 }
220 }
221 }
222 }