| 1 | use proc_macro2::TokenStream; |
| 2 | use quote::quote; |
| 3 | |
| 4 | use crate::generator::Generator; |
| 5 | use crate::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 | #[derive(Default, Clone, Copy, PartialEq, Eq, Hash, Debug)] |
| 15 | pub struct Context { |
| 16 | /// Amount of bytes that haven't been bumped yet but should |
| 17 | /// before a new read is performed |
| 18 | at: usize, |
| 19 | /// Number of bytes available without bound checks |
| 20 | available: usize, |
| 21 | /// Whether or not the Lexer has been bumped at least by 1 byte |
| 22 | 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 | backtrack: Option<NodeId>, |
| 26 | } |
| 27 | |
| 28 | impl Context { |
| 29 | pub fn can_backtrack(&self) -> bool { |
| 30 | self.backtrack.is_some() |
| 31 | } |
| 32 | |
| 33 | pub fn switch(&mut self, miss: Option<NodeId>) -> Option<TokenStream> { |
| 34 | self.backtrack = Some(miss?); |
| 35 | self.bump() |
| 36 | } |
| 37 | |
| 38 | pub const fn advance(self, n: usize) -> Self { |
| 39 | Context { |
| 40 | at: self.at + n, |
| 41 | ..self |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub fn bump(&mut self) -> Option<TokenStream> { |
| 46 | match self.at { |
| 47 | 0 => None, |
| 48 | n => { |
| 49 | let tokens = quote!(lex.bump_unchecked(#n);); |
| 50 | self.at = 0; |
| 51 | self.available = 0; |
| 52 | self.bumped = true; |
| 53 | Some(tokens) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | pub fn remainder(&self) -> usize { |
| 59 | self.available.saturating_sub(self.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) -> TokenStream { |
| 68 | let at = self.at; |
| 69 | self.advance(1); |
| 70 | quote!(unsafe { lex.read_byte_unchecked(#at) }) |
| 71 | } |
| 72 | |
| 73 | #[cfg(feature = "forbid_unsafe")] |
| 74 | pub fn read_byte(&mut self) -> TokenStream { |
| 75 | let at = self.at; |
| 76 | self.advance(1); |
| 77 | quote!(lex.read_byte(#at)) |
| 78 | } |
| 79 | |
| 80 | pub fn read(&mut self, len: usize) -> TokenStream { |
| 81 | self.available = len; |
| 82 | |
| 83 | match (self.at, len) { |
| 84 | (0, 0) => quote!(lex.read::<u8>()), |
| 85 | (a, 0) => quote!(lex.read_at::<u8>(#a)), |
| 86 | (0, l) => quote!(lex.read::<&[u8; #l]>()), |
| 87 | (a, l) => quote!(lex.read_at::<&[u8; #l]>(#a)), |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | pub fn wipe(&mut self) { |
| 92 | self.available = 0; |
| 93 | } |
| 94 | |
| 95 | const fn backtrack(self) -> Self { |
| 96 | Context { |
| 97 | at: 0, |
| 98 | available: 0, |
| 99 | bumped: self.bumped, |
| 100 | backtrack: None, |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | pub fn miss(mut self, miss: Option<NodeId>, gen: &mut Generator) -> TokenStream { |
| 105 | self.wipe(); |
| 106 | match (miss, self.backtrack) { |
| 107 | (Some(id), _) => gen.goto(id, self).clone(), |
| 108 | (_, Some(id)) => gen.goto(id, self.backtrack()).clone(), |
| 109 | _ if self.bumped => quote!(lex.error()), |
| 110 | _ => quote!(_error(lex)), |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | pub fn write_suffix(&self, buf: &mut String) { |
| 115 | use std::fmt::Write; |
| 116 | |
| 117 | if self.at > 0 { |
| 118 | let _ = write!(buf, "_at{}", self.at); |
| 119 | } |
| 120 | if self.available > 0 { |
| 121 | let _ = write!(buf, "_with{}", self.available); |
| 122 | } |
| 123 | if let Some(id) = self.backtrack { |
| 124 | let _ = write!(buf, "_ctx{}", id); |
| 125 | } |
| 126 | if self.bumped { |
| 127 | buf.push_str("_x"); |
| 128 | } |
| 129 | } |
| 130 | } |