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
// 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 `pw_format` crate is a parser used to implement proc macros that:
//! * Understand format string argument types at compile time.
//! * Syntax check format strings.
//!
//! `pw_format` is written against `std` and is not intended to be
//! used in an embedded context.  Some efficiency and memory is traded for a
//! more expressive interface that exposes the format string's "syntax tree"
//! to the API client.
//!
//! # Proc Macros
//!
//! The [`macros`] module provides infrastructure for implementing proc macros
//! that take format strings as arguments.
//!
//! # Example
//!
//! ```
//! use pw_format::{
//!     Alignment, Argument, ConversionSpec, Flag, FormatFragment, FormatString,
//!     Length, MinFieldWidth, Precision, Primitive, Style,
//! };
//!
//! let format_string =
//!   FormatString::parse_printf("long double %+ 4.2Lf is %-03hd%%.").unwrap();
//!
//! assert_eq!(format_string, FormatString {
//!   fragments: vec![
//!       FormatFragment::Literal("long double ".to_string()),
//!       FormatFragment::Conversion(ConversionSpec {
//!           argument: Argument::None,
//!           fill: ' ',
//!           alignment: Alignment::None,
//!           flags: [Flag::ForceSign, Flag::SpaceSign].into_iter().collect(),
//!           min_field_width: MinFieldWidth::Fixed(4),
//!           precision: Precision::Fixed(2),
//!           length: Some(Length::LongDouble),
//!           primitive: Primitive::Float,
//!           style: Style::None,
//!       }),
//!       FormatFragment::Literal(" is ".to_string()),
//!       FormatFragment::Conversion(ConversionSpec {
//!           argument: Argument::None,
//!           fill: ' ',
//!           alignment: Alignment::Left,
//!           flags: [Flag::LeftJustify, Flag::LeadingZeros]
//!               .into_iter()
//!               .collect(),
//!           min_field_width: MinFieldWidth::Fixed(3),
//!           precision: Precision::None,
//!           length: Some(Length::Short),
//!           primitive: Primitive::Integer,
//!           style: Style::None,
//!       }),
//!       FormatFragment::Literal("%.".to_string()),
//!   ]
//! });
//! ```
#![deny(missing_docs)]
//#![feature(type_alias_impl_trait)]

use std::collections::HashSet;

use nom::{
    branch::alt,
    bytes::complete::tag,
    character::complete::digit1,
    combinator::{map, map_res},
    IResult,
};
use quote::{quote, ToTokens};

pub mod macros;

mod core_fmt;
mod printf;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// Primitive type of a conversion (integer, float, string, etc.)
pub enum Primitive {
    /// Signed integer primitive.
    Integer,

    /// Unsigned integer primitive.
    Unsigned,

    /// Floating point primitive.
    Float,

    /// String primitive.
    String,

    /// Character primitive.
    Character,

    /// Pointer primitive.
    Pointer,

    /// Untyped primitive.
    Untyped,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// The abstract formatting style for a conversion.
pub enum Style {
    /// No style specified, use defaults.
    None,

    /// Octal rendering (i.e. "%o" or "{:o}").
    Octal,

    /// Hexadecimal rendering (i.e. "%x" or "{:x}").
    Hex,

    /// Upper case hexadecimal rendering (i.e. "%X" or "{:X}").
    UpperHex,

    /// Exponential rendering (i.e. "%e" or "{:e}".
    Exponential,

    /// Upper case exponential rendering (i.e. "%E" or "{:E}".
    UpperExponential,

    /// Pointer type rendering (i.e. "%p" or "{:p}").
    Pointer,

    /// `core::fmt`'s `{:?}`
    Debug,

    /// `core::fmt`'s `{:x?}`
    HexDebug,

    /// `core::fmt`'s `{:X?}`
    UpperHexDebug,

    /// Unsupported binary rendering
    ///
    /// This variant exists so that the proc macros can give useful error
    /// messages.
    Binary,
}

/// Implemented for testing through the pw_format_test_macros crate.
impl ToTokens for Style {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let new_tokens = match self {
            Style::None => quote!(pw_format::Style::None),
            Style::Octal => quote!(pw_format::Style::Octal),
            Style::Hex => quote!(pw_format::Style::Hex),
            Style::UpperHex => quote!(pw_format::Style::UpperHex),
            Style::Exponential => quote!(pw_format::Style::Exponential),
            Style::UpperExponential => quote!(pw_format::Style::UpperExponential),
            Style::Debug => quote!(pw_format::Style::Debug),
            Style::HexDebug => quote!(pw_format::Style::HexDebug),
            Style::UpperHexDebug => quote!(pw_format::Style::UpperHexDebug),
            Style::Pointer => quote!(pw_format::Style::Pointer),
            Style::Binary => quote!(pw_format::Style::Binary),
        };
        new_tokens.to_tokens(tokens);
    }
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
/// A printf flag (the '+' in %+d).
pub enum Flag {
    /// `-`
    LeftJustify,

    /// `+`
    ForceSign,

    /// ` `
    SpaceSign,

    /// `#`
    AlternateSyntax,

    /// `0`
    LeadingZeros,
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// A printf minimum field width (the 5 in %5d).
pub enum MinFieldWidth {
    /// No field width specified.
    None,

    /// Fixed field with.
    Fixed(u32),

    /// Variable field width passed as an argument (i.e. %*d).
    Variable,
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// A printf precision (the .5 in %.5d).
///
/// For string conversions (%s) this is treated as the maximum number of
/// bytes of the string to output.
pub enum Precision {
    /// No precision specified.
    None,

    /// Fixed precision.
    Fixed(u32),

    /// Variable precision passed as an argument (i.e. %.*f).
    Variable,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// A printf length (the l in %ld).
pub enum Length {
    /// `hh`
    Char,

    /// `h`
    Short,

    /// `l`
    Long,

    /// `ll`
    LongLong,

    /// `L`
    LongDouble,

    /// `j`
    IntMax,

    /// `z`
    Size,

    /// `t`
    PointerDiff,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// A core::fmt alignment spec.
pub enum Alignment {
    /// No alignment
    None,

    /// Left alignment (`<`)
    Left,

    /// Center alignment (`^`)
    Center,

    /// Right alignment (`>`)
    Right,
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// An argument in a core::fmt style alignment spec.
///
/// i.e. the var_name in `{var_name:#0x}`
pub enum Argument {
    /// No argument
    None,

    /// A positional argument (i.e. `{0}`).
    Positional(usize),

    /// A named argument (i.e. `{var_name}`).
    Named(String),
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// A printf conversion specification aka a % clause.
pub struct ConversionSpec {
    /// ConversionSpec's argument.
    pub argument: Argument,
    /// ConversionSpec's fill character.
    pub fill: char,
    /// ConversionSpec's field alignment.
    pub alignment: Alignment,
    /// ConversionSpec's set of [Flag]s.
    pub flags: HashSet<Flag>,
    /// ConversionSpec's minimum field width argument.
    pub min_field_width: MinFieldWidth,
    /// ConversionSpec's [Precision] argument.
    pub precision: Precision,
    /// ConversionSpec's [Length] argument.
    pub length: Option<Length>,
    /// ConversionSpec's [Primitive].
    pub primitive: Primitive,
    /// ConversionSpec's [Style].
    pub style: Style,
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// A fragment of a printf format string.
pub enum FormatFragment {
    /// A literal string value.
    Literal(String),

    /// A conversion specification (i.e. %d).
    Conversion(ConversionSpec),
}

impl FormatFragment {
    /// Try to append `fragment` to `self`.
    ///
    /// Returns `None` if the appending succeeds and `Some<fragment>` if it fails.
    fn try_append<'a>(&mut self, fragment: &'a FormatFragment) -> Option<&'a FormatFragment> {
        let Self::Literal(literal_fragment) = &fragment else {
            return Some(fragment);
        };

        let Self::Literal(ref mut literal_self) = self else {
            return Some(fragment);
        };

        literal_self.push_str(literal_fragment);

        None
    }
}

#[derive(Debug, PartialEq, Eq)]
/// A parsed printf format string.
pub struct FormatString {
    /// The [FormatFragment]s that comprise the [FormatString].
    pub fragments: Vec<FormatFragment>,
}

impl FormatString {
    /// Parses a printf style format string.
    pub fn parse_printf(s: &str) -> Result<Self, String> {
        // TODO: b/281858500 - Add better errors to failed parses.
        let (rest, result) = printf::format_string(s)
            .map_err(|e| format!("Failed to parse format string \"{s}\": {e}"))?;

        // If the parser did not consume all the input, return an error.
        if !rest.is_empty() {
            return Err(format!(
                "Failed to parse format string fragment: \"{rest}\""
            ));
        }

        Ok(result)
    }

    /// Parses a core::fmt style format string.
    pub fn parse_core_fmt(s: &str) -> Result<Self, String> {
        // TODO: b/281858500 - Add better errors to failed parses.
        let (rest, result) = core_fmt::format_string(s)
            .map_err(|e| format!("Failed to parse format string \"{s}\": {e}"))?;

        // If the parser did not consume all the input, return an error.
        if !rest.is_empty() {
            return Err(format!("Failed to parse format string: \"{rest}\""));
        }

        Ok(result)
    }

    /// Creates a `FormatString` from a slice of fragments.
    ///
    /// This primary responsibility of this function is to merge literal
    /// fragments.  Adjacent literal fragments occur when a parser parses
    /// escape sequences.  Merging them here allows a
    /// [`macros::FormatMacroGenerator`] to not worry about the escape codes.
    pub(crate) fn from_fragments(fragments: &[FormatFragment]) -> Self {
        Self {
            fragments: fragments
                .iter()
                .fold(Vec::<_>::new(), |mut fragments, fragment| {
                    // Collapse adjacent literal fragments.
                    let Some(last) = fragments.last_mut() else {
                        // If there are no accumulated fragments, add this one and return.
                        fragments.push((*fragment).clone());
                        return fragments;
                    };
                    if let Some(fragment) = last.try_append(fragment) {
                        // If the fragments were able to append, no more work to do
                        fragments.push((*fragment).clone());
                    };
                    fragments
                }),
        }
    }
}

fn variable_width(input: &str) -> IResult<&str, MinFieldWidth> {
    map(tag("*"), |_| MinFieldWidth::Variable)(input)
}

fn fixed_width(input: &str) -> IResult<&str, MinFieldWidth> {
    map_res(
        digit1,
        |value: &str| -> Result<MinFieldWidth, std::num::ParseIntError> {
            Ok(MinFieldWidth::Fixed(value.parse()?))
        },
    )(input)
}

fn no_width(input: &str) -> IResult<&str, MinFieldWidth> {
    Ok((input, MinFieldWidth::None))
}

fn width(input: &str) -> IResult<&str, MinFieldWidth> {
    alt((variable_width, fixed_width, no_width))(input)
}

fn variable_precision(input: &str) -> IResult<&str, Precision> {
    let (input, _) = tag(".")(input)?;
    map(tag("*"), |_| Precision::Variable)(input)
}

fn fixed_precision(input: &str) -> IResult<&str, Precision> {
    let (input, _) = tag(".")(input)?;
    map_res(
        digit1,
        |value: &str| -> Result<Precision, std::num::ParseIntError> {
            Ok(Precision::Fixed(value.parse()?))
        },
    )(input)
}

fn no_precision(input: &str) -> IResult<&str, Precision> {
    Ok((input, Precision::None))
}

fn precision(input: &str) -> IResult<&str, Precision> {
    alt((variable_precision, fixed_precision, no_precision))(input)
}

#[cfg(test)]
mod tests;