diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
| commit | 8cdfa445d6629ffef4cb84967ff7017654045bc2 (patch) | |
| tree | 22f0b0907c024c78d26a731e2e1f5219407d8102 /vendor/logos-codegen/src/generator/tables.rs | |
| parent | 4351c74c7c5f97156bc94d3a8549b9940ac80e3f (diff) | |
chore: add vendor directory
Diffstat (limited to 'vendor/logos-codegen/src/generator/tables.rs')
| -rw-r--r-- | vendor/logos-codegen/src/generator/tables.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/vendor/logos-codegen/src/generator/tables.rs b/vendor/logos-codegen/src/generator/tables.rs new file mode 100644 index 00000000..f1e53273 --- /dev/null +++ b/vendor/logos-codegen/src/generator/tables.rs @@ -0,0 +1,77 @@ +use crate::util::ToIdent; +use proc_macro2::{Literal, TokenStream}; +use quote::{quote, ToTokens}; +use syn::Ident; + +pub struct TableStack { + tables: Vec<(Ident, [u8; 256])>, + shift: u8, +} + +pub struct TableView<'a> { + ident: &'a Ident, + table: &'a mut [u8; 256], + mask: u8, +} + +impl TableStack { + pub fn new() -> Self { + TableStack { + tables: vec![("COMPACT_TABLE_0".to_ident(), [0; 256])], + shift: 0, + } + } + + pub fn view(&mut self) -> TableView { + let mask = if self.shift < 8 { + // Reusing existing table with a shifted mask + let mask = 1u8 << self.shift; + + self.shift += 1; + + mask + } else { + // Need to create a new table + let ident = format!("COMPACT_TABLE_{}", self.tables.len()).to_ident(); + + self.tables.push((ident, [0; 256])); + self.shift = 1; + + 1 + }; + + let (ref ident, ref mut table) = self.tables.last_mut().unwrap(); + + TableView { ident, table, mask } + } +} + +impl<'a> TableView<'a> { + pub fn ident(&self) -> &'a Ident { + self.ident + } + + pub fn flag(&mut self, byte: u8) { + self.table[byte as usize] |= self.mask; + } + + pub fn mask(&self) -> Literal { + Literal::u8_unsuffixed(self.mask) + } +} + +impl ToTokens for TableStack { + fn to_tokens(&self, out: &mut TokenStream) { + if self.shift == 0 { + return; + } + + for (ident, table) in self.tables.iter() { + let bytes = table.iter().copied().map(Literal::u8_unsuffixed); + + out.extend(quote! { + static #ident: [u8; 256] = [#(#bytes),*]; + }); + } + } +} |
