v_escape_codegen_base/lib.rs
1#![allow(unused)]
2#![deny(missing_docs)]
3
4//! Code generation base library for v_escape
5//!
6//! This crate provides the core functionality for generating SIMD-optimized escape functions
7//! from character mappings. It parses template files and generates the necessary code
8//! for efficient string escaping operations.
9
10use generator::{Generator, parse_template};
11use proc_macro2::{Span, TokenStream};
12
13mod generator;
14mod pairs;
15mod switch;
16
17/// Generate escape functions from a token stream template
18///
19/// This function takes a token stream representing character mappings and generates
20/// the corresponding escape function implementations. It returns both the generated
21/// code and metadata about the escape mappings.
22///
23/// # Arguments
24///
25/// * `tokens` - A token stream containing the character mappings in the format `new!(char -> "escape", ...)`
26/// * `crate_name` - The name of the crate where the generated code will be used
27///
28/// # Returns
29///
30/// Returns a tuple containing:
31/// * The generated code as a `TokenStream`
32/// * A tuple of `(escapes, escaped)` where `escapes` is a string of characters to escape
33/// and `escaped` is the concatenated escape sequences
34///
35/// # Errors
36///
37/// Returns a `syn::Error` if the token stream cannot be parsed or if the character
38/// mappings are invalid.
39pub fn generate(
40 tokens: TokenStream,
41 crate_name: &str,
42) -> syn::Result<(TokenStream, (String, String))> {
43 let pairs = parse_template(tokens)?;
44 let generator = Generator::new(&pairs, crate_name);
45 let generated = generator.build();
46 let (escapes, escaped): (Vec<u8>, Vec<String>) =
47 pairs.into_iter().map(|p| (p.ch, p.quote)).unzip();
48 let escapes = String::from_utf8(escapes)
49 .map_err(|e| syn::Error::new(Span::call_site(), e.to_string()))?;
50 let escaped = escaped.join("");
51
52 Ok((generated, (escapes, escaped)))
53}