| 1 | use proc_macro2rust-analyzer cargo proc-macro2 1.0.95 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#; |
| 2 | use quoterust-analyzer cargo quote 1.0.40 crate/::quoterust-analyzer cargo quote 1.0.40 quote!; |
| 3 | |
| 4 | use craterust-analyzer cargo logos-codegen 0.15.1 crate/::generatorrust-analyzer cargo logos-codegen 0.15.1 generator/::Generatorrust-analyzer cargo logos-codegen 0.15.1 generator/Generator#; |
| 5 | use craterust-analyzer cargo logos-codegen 0.15.1 crate/::graphrust-analyzer cargo logos-codegen 0.15.1 graph/::NodeIdrust-analyzer cargo logos-codegen 0.15.1 graph/NodeId#; |
| 6 | |
| 7 | /// This struct keeps track of bytes available to be read without |
| 8 | /// bounds checking across the tree. |
| 9 | /// |
| 10 | /// For example, a branch that matches 4 bytes followed by a fork |
| 11 | /// with smallest branch containing of 2 bytes can do a bounds check |
| 12 | /// for 6 bytes ahead, and leave the remaining 2 byte array (fixed size) |
| 13 | /// to be handled by the fork, avoiding bound checks there. |
| 14 | #[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:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 15 | pub struct Contextrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context# { |
| 16 | /// Amount of bytes that haven't been bumped yet but should |
| 17 | /// before a new read is performed |
| 18 | atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at.: usize, |
| 19 | /// Number of bytes available without bound checks |
| 20 | availablerust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#available.: usize, |
| 21 | /// Whether or not the Lexer has been bumped at least by 1 byte |
| 22 | bumpedrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#bumped.: bool, |
| 23 | /// Node to backtrack to to in case an explicit match has failed. |
| 24 | /// If `None` will instead produce an error token. |
| 25 | backtrackrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#backtrack.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<NodeIdrust-analyzer cargo logos-codegen 0.15.1 graph/NodeId#>, |
| 26 | } |
| 27 | |
| 28 | impl Contextrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context# { |
| 29 | pub fn can_backtrackrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]can_backtrack().(&self) -> bool { |
| 30 | self.backtrackrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#backtrack..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() |
| 31 | } |
| 32 | |
| 33 | pub fn switchrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]switch().(&mut self, miss: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<NodeIdrust-analyzer cargo logos-codegen 0.15.1 graph/NodeId#>) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#> { |
| 34 | self.backtrackrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#backtrack. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(miss?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().); |
| 35 | self.bumprust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]bump().() |
| 36 | } |
| 37 | |
| 38 | pub const fn advancerust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]advance().(self, n: usize) -> Selfrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context] { |
| 39 | Contextrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context# { |
| 40 | atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at.: self.atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at. 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().n, |
| 41 | ..self |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub fn bumprust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]bump().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#> { |
| 46 | match self.atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at. { |
| 47 | 0 => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 48 | n => { |
| 49 | let tokens = quoterust-analyzer cargo quote 1.0.40 quote!!(lex.bump_unchecked(#n);); |
| 50 | self.atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at. = 0; |
| 51 | self.availablerust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#available. = 0; |
| 52 | self.bumpedrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#bumped. = true; |
| 53 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(tokens) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | pub fn remainderrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]remainder().(&self) -> usize { |
| 59 | self.availablerust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#available..saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(self.atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at.) |
| 60 | } |
| 61 | |
| 62 | /// Reads the next byte without checking bounds. |
| 63 | /// |
| 64 | /// # Safety |
| 65 | /// The caller must ensure that the next byte is within bounds. |
| 66 | #[cfg(not(feature = "forbid_unsafe"))] |
| 67 | pub unsafe fn read_byte(&mut self) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# { |
| 68 | let at = selfrust-analyzer cargo logos-codegen 0.15.1 generator/context/.at; |
| 69 | selfrust-analyzer cargo logos-codegen 0.15.1 generator/context/.advance(1); |
| 70 | quoterust-analyzer cargo quote 1.0.40 crate/!(unsafe { lex.read_byte_unchecked(#at) }) |
| 71 | } |
| 72 | |
| 73 | #[cfg(feature = "forbid_unsafe")] |
| 74 | pub fn read_byterust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]read_byte().(&mut self) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# { |
| 75 | let at = self.atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at.; |
| 76 | self.advancerust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]advance().(1); |
| 77 | quoterust-analyzer cargo quote 1.0.40 quote!!(lex.read_byte(#at)) |
| 78 | } |
| 79 | |
| 80 | pub fn readrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]read().(&mut self, len: usize) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# { |
| 81 | self.availablerust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#available. = len; |
| 82 | |
| 83 | match (self.atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at., len) { |
| 84 | (0, 0) => quoterust-analyzer cargo quote 1.0.40 quote!!(lex.read::<u8>()), |
| 85 | (a, 0) => quoterust-analyzer cargo quote 1.0.40 quote!!(lex.read_at::<u8>(#a)), |
| 86 | (0, l) => quoterust-analyzer cargo quote 1.0.40 quote!!(lex.read::<&[u8; #l]>()), |
| 87 | (a, l) => quoterust-analyzer cargo quote 1.0.40 quote!!(lex.read_at::<&[u8; #l]>(#a)), |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | pub fn wiperust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]wipe().(&mut self) { |
| 92 | self.availablerust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#available. = 0; |
| 93 | } |
| 94 | |
| 95 | const fn backtrackrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]backtrack().(self) -> Selfrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context] { |
| 96 | Contextrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context# { |
| 97 | atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at.: 0, |
| 98 | availablerust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#available.: 0, |
| 99 | bumpedrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#bumped.: self.bumpedrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#bumped., |
| 100 | backtrackrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#backtrack.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | pub fn missrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]miss().(mut self, miss: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<NodeIdrust-analyzer cargo logos-codegen 0.15.1 graph/NodeId#>, gen: &mut Generatorrust-analyzer cargo logos-codegen 0.15.1 generator/Generator#) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# { |
| 105 | self.wiperust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]wipe().(); |
| 106 | match (miss, self.backtrackrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#backtrack.) { |
| 107 | (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(id), _) => gen.gotorust-analyzer cargo logos-codegen 0.15.1 generator/impl#[`Generator<'a>`]goto().(id, self).clonerust-analyzer cargo proc-macro2 1.0.95 impl#[TokenStream][Clone]clone().(), |
| 108 | (_, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(id)) => gen.gotorust-analyzer cargo logos-codegen 0.15.1 generator/impl#[`Generator<'a>`]goto().(id, self.backtrackrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]backtrack().()).clonerust-analyzer cargo proc-macro2 1.0.95 impl#[TokenStream][Clone]clone().(), |
| 109 | _ if self.bumpedrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#bumped. => quoterust-analyzer cargo quote 1.0.40 quote!!(lex.error()), |
| 110 | _ => quoterust-analyzer cargo quote 1.0.40 quote!!(_error(lex)), |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | pub fn write_suffixrust-analyzer cargo logos-codegen 0.15.1 generator/context/impl#[Context]write_suffix().(&self, buf: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) { |
| 115 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#; |
| 116 | |
| 117 | if self.atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 118 | let _ = writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(buf, "_at{}", self.atrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#at.); |
| 119 | } |
| 120 | if self.availablerust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#available. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 121 | let _ = writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(buf, "_with{}", self.availablerust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#available.); |
| 122 | } |
| 123 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(id) = self.backtrackrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#backtrack. { |
| 124 | let _ = writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(buf, "_ctx{}", id); |
| 125 | } |
| 126 | if self.bumpedrust-analyzer cargo logos-codegen 0.15.1 generator/context/Context#bumped. { |
| 127 | buf.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("_x"); |
| 128 | } |
| 129 | } |
| 130 | } |