aboutsummaryrefslogtreecommitdiff
path: root/pw_format/rust/pw_format/macros.rs
blob: d08b634e93c83673a5d9ac3217e92f34f1730d69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// Copyright 2023 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

//! The `macro` module provides helpers that simplify writing proc macros
//! that take format strings and arguments.  This is accomplish with three
//! main constructs:
//! * [`FormatAndArgs`]: A struct that implements [syn::parse::Parse] to
//!   parse a format string and its following arguments.
//! * [`FormatMacroGenerator`]: A trait used to implement the macro specific
//!   logic to generate code.
//! * [`generate`]: A function to handle the execution of the proc macro by
//!   calling into a [FormatMacroGenerator].
//!
//! Additionally [`PrintfFormatMacroGenerator`] trait and [`generate_printf`]
//! function are provided to help when implementing generators that need to
//! produce `printf` style format strings as part of their code generation.
//!
//! ## Example
//!
//! An example of implementing a proc macro is provided in the
//! [pw_format_example_macro crate](https://pigweed.googlesource.com/pigweed/pigweed/+/refs/heads/main/pw_format/rust/pw_format_example_macro.rs)
//!
//!

use std::collections::VecDeque;

use proc_macro2::Ident;
use quote::{format_ident, quote, ToTokens};
use syn::{
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    spanned::Spanned,
    Expr, LitStr, Token,
};

use crate::{
    ConversionSpec, FormatFragment, FormatString, Length, MinFieldWidth, Precision, Specifier,
};

mod keywords {
    syn::custom_keyword!(PW_FMT_CONCAT);
}

type TokenStream2 = proc_macro2::TokenStream;

/// An error occurring during proc macro evaluation.
///
/// In order to stay as flexible as possible to implementors of
/// [`FormatMacroGenerator`], the error is simply represent by a
/// string.
#[derive(Debug)]
pub struct Error {
    text: String,
}

impl Error {
    /// Create a new proc macro evaluation error.
    pub fn new(text: &str) -> Self {
        Self {
            text: text.to_string(),
        }
    }
}

/// An alias for a Result with an ``Error``
pub type Result<T> = core::result::Result<T, Error>;

/// Style in which to display the an integer.
///
/// In order to maintain compatibility with `printf` style systems, sign
/// and base are combined.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum IntegerDisplayType {
    /// Signed integer
    Signed,
    /// Unsigned integer
    Unsigned,
    /// Unsigned octal
    Octal,
    /// Unsigned hex with lower case letters
    Hex,
    /// Unsigned hex with upper case letters
    UpperHex,
}

impl TryFrom<crate::Specifier> for IntegerDisplayType {
    type Error = Error;

    fn try_from(value: Specifier) -> Result<Self> {
        match value {
            Specifier::Decimal | Specifier::Integer => Ok(Self::Signed),
            Specifier::Unsigned => Ok(Self::Unsigned),
            Specifier::Octal => Ok(Self::Octal),
            Specifier::Hex => Ok(Self::Hex),
            Specifier::UpperHex => Ok(Self::UpperHex),
            _ => Err(Error::new("No valid IntegerDisplayType for {value:?}.")),
        }
    }
}

/// Implemented for testing through the pw_format_test_macros crate.
impl ToTokens for IntegerDisplayType {
    fn to_tokens(&self, tokens: &mut TokenStream2) {
        let new_tokens = match self {
            IntegerDisplayType::Signed => quote!(pw_format::macros::IntegerDisplayType::Signed),
            IntegerDisplayType::Unsigned => {
                quote!(pw_format::macros::IntegerDisplayType::Unsigned)
            }
            IntegerDisplayType::Octal => quote!(pw_format::macros::IntegerDisplayType::Octal),
            IntegerDisplayType::Hex => quote!(pw_format::macros::IntegerDisplayType::Hex),
            IntegerDisplayType::UpperHex => {
                quote!(pw_format::macros::IntegerDisplayType::UpperHex)
            }
        };
        new_tokens.to_tokens(tokens);
    }
}

/// A code generator for implementing a `pw_format` style macro.
///
/// This trait serves as the primary interface between `pw_format` and a
/// proc macro using it to implement format string and argument parsing.  When
/// evaluating the proc macro and generating code, [`generate`] will make
/// repeated calls to [`string_fragment`](FormatMacroGenerator::string_fragment)
/// and the conversion functions.  These calls will be made in the order they
/// appear in the format string.  After all fragments and conversions are
/// processed, [`generate`] will call
/// [`finalize`](FormatMacroGenerator::finalize).
///
/// For an example of implementing a `FormatMacroGenerator` see the
/// [pw_format_example_macro crate](https://pigweed.googlesource.com/pigweed/pigweed/+/refs/heads/main/pw_format/rust/pw_format_example_macro.rs).
pub trait FormatMacroGenerator {
    /// Called by [`generate`] at the end of code generation.
    ///
    /// Consumes `self` and returns the code to be emitted by the proc macro of
    /// and [`Error`].
    fn finalize(self) -> Result<TokenStream2>;

    /// Process a string fragment.
    ///
    /// A string fragment is a string of characters that appear in a format
    /// string.  This is different than a
    /// [`string_conversion`](FormatMacroGenerator::string_conversion) which is
    /// a string provided through a conversion specifier (i.e. `"%s"`).
    fn string_fragment(&mut self, string: &str) -> Result<()>;

    /// Process an integer conversion.
    fn integer_conversion(
        &mut self,
        display: IntegerDisplayType,
        type_width: u8, // This should probably be an enum
        expression: Expr,
    ) -> Result<()>;

    /// Process a string conversion.
    ///
    /// See [`string_fragment`](FormatMacroGenerator::string_fragment) for a
    /// disambiguation between that function and this one.
    fn string_conversion(&mut self, expression: Expr) -> Result<()>;

    /// Process a character conversion.
    fn char_conversion(&mut self, expression: Expr) -> Result<()>;
}

/// A parsed format string and it's arguments.
///
/// `FormatAndArgs` implements [`syn::parse::Parse`] and can be used to parse
/// arguments to proc maros that take format strings.  Arguments are parsed
/// according to the pattern: `($format_string:literal, $($args:expr),*)`
///
/// To support uses where format strings need to be built by macros at compile
/// time, the format string can be specified as a set of string literals
/// separated by the custom `PW_FMT_CONCAT` keyword.
#[derive(Debug)]
pub struct FormatAndArgs {
    format_string: LitStr,
    parsed: FormatString,
    args: VecDeque<Expr>,
}

impl Parse for FormatAndArgs {
    fn parse(input: ParseStream) -> syn::parse::Result<Self> {
        let punctuated =
            Punctuated::<LitStr, keywords::PW_FMT_CONCAT>::parse_separated_nonempty(input)?;
        let span = punctuated.span();
        let format_string = LitStr::new(
            &punctuated.into_iter().fold(String::new(), |mut acc, s| {
                acc.push_str(&s.value());
                acc
            }),
            span,
        );

        let args = if input.is_empty() {
            // If there are no more tokens, no arguments were specified.
            VecDeque::new()
        } else {
            // Eat the `,` following the format string.
            input.parse::<Token![,]>()?;

            let punctuated = Punctuated::<Expr, Token![,]>::parse_terminated(input)?;
            punctuated.into_iter().collect()
        };

        let parsed = FormatString::parse(&format_string.value()).map_err(|e| {
            syn::Error::new_spanned(
                format_string.to_token_stream(),
                format!("Error parsing format string {e}"),
            )
        })?;

        Ok(FormatAndArgs {
            format_string,
            parsed,
            args,
        })
    }
}

// Grab the next argument returning a descriptive error if no more args are left.
fn next_arg(spec: &ConversionSpec, args: &mut VecDeque<Expr>) -> Result<Expr> {
    args.pop_front()
        .ok_or_else(|| Error::new(&format!("No argument given for {spec:?}")))
}

// Handle a single format conversion specifier (i.e. `%08x`).  Grabs the
// necessary arguments for the specifier from `args` and generates code
// to marshal the arguments into the buffer declared in `_tokenize_to_buffer`.
// Returns an error if args is too short of if a format specifier is unsupported.
fn handle_conversion(
    generator: &mut dyn FormatMacroGenerator,
    spec: &ConversionSpec,
    args: &mut VecDeque<Expr>,
) -> Result<()> {
    match spec.specifier {
        Specifier::Decimal
        | Specifier::Integer
        | Specifier::Octal
        | Specifier::Unsigned
        | Specifier::Hex
        | Specifier::UpperHex => {
            // TODO: b/281862660 - Support Width::Variable and Precision::Variable.
            if spec.min_field_width == MinFieldWidth::Variable {
                return Err(Error::new(
                    "Variable width '*' integer formats are not supported.",
                ));
            }

            if spec.precision == Precision::Variable {
                return Err(Error::new(
                    "Variable precision '*' integer formats are not supported.",
                ));
            }

            let arg = next_arg(spec, args)?;
            let bits = match spec.length.unwrap_or(Length::Long) {
                Length::Char => 8,
                Length::Short => 16,
                Length::Long => 32,
                Length::LongLong => 64,
                Length::IntMax => 64,
                Length::Size => 32,
                Length::PointerDiff => 32,
                Length::LongDouble => {
                    return Err(Error::new(
                        "Long double length parameter invalid for integer formats",
                    ))
                }
            };

            let display: IntegerDisplayType =
                spec.specifier.clone().try_into().expect(
                    "Specifier is guaranteed to convert display type but enclosing match arm.",
                );
            generator.integer_conversion(display, bits, arg)
        }
        Specifier::String => {
            // TODO: b/281862660 - Support Width::Variable and Precision::Variable.
            if spec.min_field_width == MinFieldWidth::Variable {
                return Err(Error::new(
                    "Variable width '*' string formats are not supported.",
                ));
            }

            if spec.precision == Precision::Variable {
                return Err(Error::new(
                    "Variable precision '*' string formats are not supported.",
                ));
            }

            let arg = next_arg(spec, args)?;
            generator.string_conversion(arg)
        }
        Specifier::Char => {
            let arg = next_arg(spec, args)?;
            generator.char_conversion(arg)
        }

        Specifier::Double
        | Specifier::UpperDouble
        | Specifier::Exponential
        | Specifier::UpperExponential
        | Specifier::SmallDouble
        | Specifier::UpperSmallDouble => {
            // TODO: b/281862328 - Support floating point numbers.
            Err(Error::new("Floating point numbers are not supported."))
        }

        // TODO: b/281862333 - Support pointers.
        Specifier::Pointer => Err(Error::new("Pointer types are not supported.")),
    }
}

/// Generate code for a `pw_format` style proc macro.
///
/// `generate` takes a [`FormatMacroGenerator`] and a [`FormatAndArgs`] struct
/// and uses them to produce the code output for a proc macro.
pub fn generate(
    mut generator: impl FormatMacroGenerator,
    format_and_args: FormatAndArgs,
) -> core::result::Result<TokenStream2, syn::Error> {
    let mut args = format_and_args.args;
    let mut errors = Vec::new();

    for fragment in format_and_args.parsed.fragments {
        let result = match fragment {
            FormatFragment::Conversion(spec) => handle_conversion(&mut generator, &spec, &mut args),
            FormatFragment::Literal(string) => generator.string_fragment(&string),
            FormatFragment::Percent => generator.string_fragment("%"),
        };
        if let Err(e) = result {
            errors.push(syn::Error::new_spanned(
                format_and_args.format_string.to_token_stream(),
                e.text,
            ));
        }
    }

    if !errors.is_empty() {
        return Err(errors
            .into_iter()
            .reduce(|mut accumulated_errors, error| {
                accumulated_errors.combine(error);
                accumulated_errors
            })
            .expect("errors should not be empty"));
    }

    generator.finalize().map_err(|e| {
        syn::Error::new_spanned(format_and_args.format_string.to_token_stream(), e.text)
    })
}

/// A specialized generator for proc macros that produce `printf` style format strings.
///
/// For proc macros that need to translate a `pw_format` invocation into a
/// `printf` style format string, `PrintfFormatMacroGenerator` offer a
/// specialized form of [`FormatMacroGenerator`] that builds the format string
/// and provides it as an argument to
/// [`finalize`](PrintfFormatMacroGenerator::finalize).
///
/// In cases where a generator needs to override the conversion specifier it
/// can return it from its appropriate conversion method.  An example of using
/// this would be wanting to pass a Rust string directly to a `printf` call
/// over FFI.  In that case,
/// [`string_conversion`](PrintfFormatMacroGenerator::string_conversion) could
/// return `Ok(Some("%.*s".to_string()))` to allow both the length and string
/// pointer to be passed to `printf`.
pub trait PrintfFormatMacroGenerator {
    /// Called by [`generate_printf`] at the end of code generation.
    ///
    /// Works like [`FormatMacroGenerator::finalize`] with the addition of
    /// being provided a `printf_style` format string.
    fn finalize(self, format_string: String) -> Result<TokenStream2>;

    /// Process a string fragment.
    ///
    /// **NOTE**: This string may contain unescaped `%` characters.
    /// However, most implementations of this train can simply ignore string
    /// fragments as they will be included (with properly escaped `%`
    /// characters) as part of the format string passed to
    /// [`PrintfFormatMacroGenerator::finalize`].
    ///
    /// See [`FormatMacroGenerator::string_fragment`] for a disambiguation
    /// between a string fragment and string conversion.
    fn string_fragment(&mut self, string: &str) -> Result<()>;

    /// Process an integer conversion.
    ///
    /// May optionally return a printf format string (i.e. "%d") to override the
    /// default.
    fn integer_conversion(&mut self, ty: Ident, expression: Expr) -> Result<Option<String>>;

    /// Process a string conversion.
    ///
    /// May optionally return a printf format string (i.e. "%s") to override the
    /// default.
    ///
    /// See [`FormatMacroGenerator::string_fragment`] for a disambiguation
    /// between a string fragment and string conversion.
    fn string_conversion(&mut self, expression: Expr) -> Result<Option<String>>;

    /// Process a character conversion.
    ///
    /// May optionally return a printf format string (i.e. "%c") to override the
    /// default.
    fn char_conversion(&mut self, expression: Expr) -> Result<Option<String>>;
}

// Wraps a `PrintfFormatMacroGenerator` in a `FormatMacroGenerator` that
// generates the format string as it goes.
struct PrintfGenerator<GENERATOR: PrintfFormatMacroGenerator> {
    inner: GENERATOR,
    format_string: String,
}

impl<GENERATOR: PrintfFormatMacroGenerator> FormatMacroGenerator for PrintfGenerator<GENERATOR> {
    fn finalize(self) -> Result<TokenStream2> {
        self.inner.finalize(self.format_string)
    }

    fn string_fragment(&mut self, string: &str) -> Result<()> {
        // Escape '%' characters.
        let format_string = string.replace("%", "%%");

        self.format_string.push_str(&format_string);
        self.inner.string_fragment(string)
    }

    fn integer_conversion(
        &mut self,
        display: IntegerDisplayType,
        type_width: u8, // in bits
        expression: Expr,
    ) -> Result<()> {
        let length_modifer = match type_width {
            8 => "hh",
            16 => "h",
            32 => "",
            64 => "ll",
            _ => {
                return Err(Error::new(&format!(
                    "printf backend does not support {} bit field width",
                    type_width
                )))
            }
        };

        let (conversion, ty) = match display {
            IntegerDisplayType::Signed => ("d", format_ident!("i{type_width}")),
            IntegerDisplayType::Unsigned => ("u", format_ident!("u{type_width}")),
            IntegerDisplayType::Octal => ("o", format_ident!("u{type_width}")),
            IntegerDisplayType::Hex => ("x", format_ident!("u{type_width}")),
            IntegerDisplayType::UpperHex => ("X", format_ident!("u{type_width}")),
        };

        match self.inner.integer_conversion(ty, expression)? {
            Some(s) => self.format_string.push_str(&s),
            None => self
                .format_string
                .push_str(&format!("%{}{}", length_modifer, conversion)),
        }

        Ok(())
    }

    fn string_conversion(&mut self, expression: Expr) -> Result<()> {
        match self.inner.string_conversion(expression)? {
            Some(s) => self.format_string.push_str(&s),
            None => self.format_string.push_str("%s"),
        }
        Ok(())
    }

    fn char_conversion(&mut self, expression: Expr) -> Result<()> {
        match self.inner.char_conversion(expression)? {
            Some(s) => self.format_string.push_str(&s),
            None => self.format_string.push_str("%c"),
        }
        Ok(())
    }
}

/// Generate code for a `pw_format` style proc macro that needs a `printf` format string.
///
/// `generate_printf` is a specialized version of [`generate`] which works with
/// [`PrintfFormatMacroGenerator`]
pub fn generate_printf(
    generator: impl PrintfFormatMacroGenerator,
    format_and_args: FormatAndArgs,
) -> core::result::Result<TokenStream2, syn::Error> {
    let generator = PrintfGenerator {
        inner: generator,
        format_string: "".into(),
    };
    generate(generator, format_and_args)
}

/// A specialized generator for proc macros that produce [`core::fmt`] style format strings.
///
/// For proc macros that need to translate a `pw_format` invocation into a
/// [`core::fmt`] style format string, `CoreFmtFormatMacroGenerator` offer a
/// specialized form of [`FormatMacroGenerator`] that builds the format string
/// and provides it as an argument to
/// [`finalize`](CoreFmtFormatMacroGenerator::finalize).
///
/// In cases where a generator needs to override the conversion specifier (i.e.
/// `{}`, it can return it from its appropriate conversion method.
pub trait CoreFmtFormatMacroGenerator {
    /// Called by [`generate_core_fmt`] at the end of code generation.
    ///
    /// Works like [`FormatMacroGenerator::finalize`] with the addition of
    /// being provided a [`core::fmt`] format string.
    fn finalize(self, format_string: String) -> Result<TokenStream2>;

    /// Process a string fragment.
    ///
    /// **NOTE**: This string may contain unescaped `{` and `}` characters.
    /// However, most implementations of this train can simply ignore string
    /// fragments as they will be included (with properly escaped `{` and `}`
    /// characters) as part of the format string passed to
    /// [`CoreFmtFormatMacroGenerator::finalize`].
    ///
    ///
    /// See [`FormatMacroGenerator::string_fragment`] for a disambiguation
    /// between a string fragment and string conversion.
    fn string_fragment(&mut self, string: &str) -> Result<()>;

    /// Process an integer conversion.
    fn integer_conversion(&mut self, ty: Ident, expression: Expr) -> Result<Option<String>>;

    /// Process a string conversion.
    fn string_conversion(&mut self, expression: Expr) -> Result<Option<String>>;

    /// Process a character conversion.
    fn char_conversion(&mut self, expression: Expr) -> Result<Option<String>>;
}

// Wraps a `CoreFmtFormatMacroGenerator` in a `FormatMacroGenerator` that
// generates the format string as it goes.
struct CoreFmtGenerator<GENERATOR: CoreFmtFormatMacroGenerator> {
    inner: GENERATOR,
    format_string: String,
}

impl<GENERATOR: CoreFmtFormatMacroGenerator> FormatMacroGenerator for CoreFmtGenerator<GENERATOR> {
    fn finalize(self) -> Result<TokenStream2> {
        self.inner.finalize(self.format_string)
    }

    fn string_fragment(&mut self, string: &str) -> Result<()> {
        // Escape '{' and '} characters.
        let format_string = string.replace("{", "{{").replace("}", "}}");

        self.format_string.push_str(&format_string);
        self.inner.string_fragment(string)
    }

    fn integer_conversion(
        &mut self,
        display: IntegerDisplayType,
        type_width: u8, // in bits
        expression: Expr,
    ) -> Result<()> {
        let (conversion, ty) = match display {
            IntegerDisplayType::Signed => ("{}", format_ident!("i{type_width}")),
            IntegerDisplayType::Unsigned => ("{}", format_ident!("u{type_width}")),
            IntegerDisplayType::Octal => ("{:o}", format_ident!("u{type_width}")),
            IntegerDisplayType::Hex => ("{:x}", format_ident!("u{type_width}")),
            IntegerDisplayType::UpperHex => ("{:X}", format_ident!("u{type_width}")),
        };

        match self.inner.integer_conversion(ty, expression)? {
            Some(s) => self.format_string.push_str(&s),
            None => self.format_string.push_str(conversion),
        }

        Ok(())
    }

    fn string_conversion(&mut self, expression: Expr) -> Result<()> {
        match self.inner.string_conversion(expression)? {
            Some(s) => self.format_string.push_str(&s),
            None => self.format_string.push_str("{}"),
        }
        Ok(())
    }

    fn char_conversion(&mut self, expression: Expr) -> Result<()> {
        match self.inner.char_conversion(expression)? {
            Some(s) => self.format_string.push_str(&s),
            None => self.format_string.push_str("{}"),
        }
        Ok(())
    }
}

/// Generate code for a `pw_format` style proc macro that needs a [`core::fmt`] format string.
///
/// `generate_core_fmt` is a specialized version of [`generate`] which works with
/// [`CoreFmtFormatMacroGenerator`]
pub fn generate_core_fmt(
    generator: impl CoreFmtFormatMacroGenerator,
    format_and_args: FormatAndArgs,
) -> core::result::Result<TokenStream2, syn::Error> {
    let generator = CoreFmtGenerator {
        inner: generator,
        format_string: "".into(),
    };
    generate(generator, format_and_args)
}