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
use crate::common::*;

#[derive(Debug, Diagnostic, thiserror::Error)]
pub enum ParseNumberError {
    #[error("expected unsigned number but found sign")]
    #[diagnostic()]
    UnexpectedSign {
        #[label("occurs here")]
        span: SourceSpan,
    },
    #[error("expected hexadecimal number with 0x prefix, but no prefix was present")]
    #[diagnostic()]
    MissingPrefix {
        #[label("occurs here")]
        span: SourceSpan,
    },
    #[error("input string has incorrect precision: expected {precision} got {actual}")]
    #[diagnostic()]
    PrecisionMismatch {
        #[label("occurs here")]
        span: SourceSpan,
        precision: u8,
        actual: usize,
    },
    #[error("input string has incorrect numeric format: {reason:?}")]
    #[diagnostic()]
    InvalidFormat {
        #[label("occurs here")]
        span: SourceSpan,
        reason: core::num::IntErrorKind,
    },
}

#[derive(Debug, Clone)]
pub struct Number {
    pub span: SourceSpan,
    pub format: NumberFormat,
    pub value: i64,
}
impl Number {
    pub fn new(span: SourceSpan, value: i64) -> Self {
        Self {
            span,
            format: NumberFormat::default(),
            value,
        }
    }

    pub fn new_with_format(span: SourceSpan, value: i64, format: NumberFormat) -> Self {
        Self {
            span,
            format,
            value,
        }
    }

    pub fn parse_with_format(
        input: Span<&str>,
        format: NumberFormat,
    ) -> Result<Self, ParseNumberError> {
        let (span, input) = input.into_parts();

        match format {
            NumberFormat::Unsigned { precision } => {
                if input.starts_with(['-', '+']) {
                    return Err(ParseNumberError::UnexpectedSign { span });
                }
                let value = input
                    .parse::<i64>()
                    .map(|value| Self {
                        span,
                        format,
                        value,
                    })
                    .map_err(|error| ParseNumberError::InvalidFormat {
                        span,
                        reason: error.kind().clone(),
                    })?;
                if precision == 0 {
                    return Ok(value);
                }
                if input.len() != precision as usize {
                    Err(ParseNumberError::PrecisionMismatch {
                        span,
                        precision,
                        actual: input.len(),
                    })
                } else {
                    Ok(value)
                }
            }
            NumberFormat::Signed { precision } => {
                let value = input
                    .parse::<i64>()
                    .map(|value| Self {
                        span,
                        format,
                        value,
                    })
                    .map_err(|error| ParseNumberError::InvalidFormat {
                        span,
                        reason: error.kind().clone(),
                    })?;
                if precision == 0 {
                    return Ok(value);
                }
                let actual = if let Some(input) = input.strip_prefix(['-', '+']) {
                    input.len()
                } else {
                    input.len()
                };
                if actual != precision as usize {
                    Err(ParseNumberError::PrecisionMismatch {
                        span,
                        precision,
                        actual,
                    })
                } else {
                    Ok(value)
                }
            }
            NumberFormat::Hex {
                require_prefix,
                precision,
            } => {
                let input = match input.strip_prefix("0x") {
                    None if require_prefix => return Err(ParseNumberError::MissingPrefix { span }),
                    None => input,
                    Some(input) => input,
                };
                let value = i64::from_str_radix(input, 16)
                    .map(|value| Self {
                        span,
                        format,
                        value,
                    })
                    .map_err(|error| ParseNumberError::InvalidFormat {
                        span,
                        reason: error.kind().clone(),
                    })?;
                if input.len() != precision as usize {
                    Err(ParseNumberError::PrecisionMismatch {
                        span,
                        precision,
                        actual: input.len(),
                    })
                } else {
                    Ok(value)
                }
            }
        }
    }
}
impl Eq for Number {}
impl PartialEq for Number {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value && self.format == other.format
    }
}
impl PartialOrd for Number {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.value.cmp(&other.value))
    }
}
impl Ord for Number {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.value.cmp(&other.value)
    }
}
impl Spanned for Number {
    fn span(&self) -> SourceSpan {
        self.span
    }
}
impl fmt::Display for Number {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let value = self.value;
        match self.format {
            NumberFormat::Unsigned { precision: 0 } => write!(f, "{}", value as u64),
            NumberFormat::Unsigned { precision: n } => {
                write!(f, "{:0n$}", value as u64, n = n as usize)
            }
            NumberFormat::Signed { precision: 0 } => write!(f, "{value}"),
            NumberFormat::Signed { precision: n } => write!(f, "{value:0n$}", n = n as usize),
            NumberFormat::Hex {
                require_prefix: true,
                precision: 0,
            } => write!(f, "{value:#x?}"),
            NumberFormat::Hex {
                require_prefix: false,
                precision: 0,
            } => write!(f, "{value:x?}"),
            NumberFormat::Hex {
                require_prefix: true,
                precision: n,
            } => write!(f, "{value:#0n$x?}", n = n as usize),
            NumberFormat::Hex {
                require_prefix: false,
                precision: n,
            } => write!(f, "{value:0n$x?}", n = n as usize),
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum NumberFormat {
    Unsigned { precision: u8 },
    Signed { precision: u8 },
    Hex { precision: u8, require_prefix: bool },
}
impl NumberFormat {
    pub fn describe(&self) -> Cow<'static, str> {
        match self {
            Self::Unsigned { precision: 0 } => Cow::Borrowed("any unsigned 64-bit integer"),
            Self::Unsigned { precision } => {
                Cow::Owned(format!("an unsigned {precision}-digit 64-bit integer"))
            }
            Self::Signed { precision: 0 } => Cow::Borrowed("any signed 64-bit integer"),
            Self::Signed { precision } => {
                Cow::Owned(format!("a signed {precision}-digit 64-bit integer"))
            }
            Self::Hex {
                require_prefix: true,
                precision: 0,
            } => Cow::Borrowed("any 64-bit integer in hex format, prefixed with 0x"),
            Self::Hex {
                require_prefix: false,
                precision: 0,
            } => Cow::Borrowed("any 64-bit integer in hex format"),
            Self::Hex {
                require_prefix: true,
                precision,
            } => Cow::Owned(format!(
                "a {precision}-digit 64-bit integer in hex format, prefixed with 0x"
            )),
            Self::Hex { precision, .. } => {
                Cow::Owned(format!("a {precision}-digit 64-bit integer in hex format"))
            }
        }
    }

    pub fn is_signed(&self) -> bool {
        matches!(self, Self::Signed { .. })
    }

    pub fn is_hex(&self) -> bool {
        matches!(self, Self::Hex { .. })
    }

    pub fn precision(&self) -> usize {
        match self {
            Self::Unsigned { precision }
            | Self::Signed { precision }
            | Self::Hex { precision, .. } => *precision as usize,
        }
    }

    pub fn discriminant(&self) -> u8 {
        // SAFETY: Because `Self` is marked `repr(u8)`, its layout is a `repr(C)` `union`
        // between `repr(C)` structs, each of which has the `u8` discriminant as its first
        // field, so we can read the discriminant without offsetting the pointer.
        unsafe { *<*const _>::from(self).cast::<u8>() }
    }

    pub fn pattern_nocapture(&self) -> Cow<'static, str> {
        // NOTE: The patterns below with precision 0 have their
        // range capped at 19, which is the maximum number of digits
        // in i64::MAX, or the largest possible number that could
        // be represented in decimal form
        match self {
            NumberFormat::Signed { precision: 0 } => Cow::Borrowed(r"(?:[-+]?[0-9]{1,19})"),
            NumberFormat::Signed { precision } => {
                Cow::Owned(format!("(?:[-+]?[0-9]{{{precision}}})"))
            }
            NumberFormat::Unsigned { precision: 0 } => Cow::Borrowed(r"(?:[0-9]{1,19})"),
            NumberFormat::Unsigned { precision } => Cow::Owned(format!("(?:[0-9]{{{precision}}})")),
            // The hex value for i64::MAX is 7fffffffffffffff,
            // or 16 digits
            NumberFormat::Hex {
                require_prefix: true,
                precision: 0,
            } => Cow::Borrowed(r"(?:0x[A-Fa-f0-9]{1,16})"),
            NumberFormat::Hex {
                require_prefix: true,
                precision,
            } => Cow::Owned(format!("(?:0x[A-Fa-f0-9]{{{precision}}})")),
            NumberFormat::Hex {
                require_prefix: false,
                precision: 0,
            } => Cow::Borrowed(r"(?:[A-Fa-f0-9]{1,16})"),
            NumberFormat::Hex {
                require_prefix: false,
                precision,
            } => Cow::Owned(format!("(?:[A-Fa-f0-9]{{{precision}}})")),
        }
    }

    pub fn pattern(&self, group_name_override: Option<&str>) -> Cow<'static, str> {
        // NOTE: The patterns below with precision 0 have their
        // range capped at 19, which is the maximum number of digits
        // in i64::MAX, or the largest possible number that could
        // be represented in decimal form
        let group_name = group_name_override.unwrap_or("digits");
        match self {
            NumberFormat::Signed { precision: 0 } => match group_name_override {
                None => Cow::Borrowed(r"(?P<digits>[-+]?[0-9]{1,19})"),
                Some(group_name) => Cow::Owned(format!("(?P<{group_name}>[-+]?[0-9]{{1,19}})")),
            },
            NumberFormat::Signed { precision } => {
                Cow::Owned(format!("(?P<{group_name}>[-+]?[0-9]{{{precision}}})"))
            }
            NumberFormat::Unsigned { precision: 0 } => match group_name_override {
                None => Cow::Borrowed(r"(?P<digits>[0-9]{1,19})"),
                Some(group_name) => Cow::Owned(format!("(?P<{group_name}>[0-9]{{1,19}})")),
            },
            NumberFormat::Unsigned { precision } => {
                Cow::Owned(format!("(?P<{group_name}>[0-9]{{{precision}}})"))
            }
            // The hex value for i64::MAX is 7fffffffffffffff,
            // or 16 digits
            NumberFormat::Hex {
                require_prefix: true,
                precision: 0,
            } => match group_name_override {
                None => Cow::Borrowed(r"(?P<digits>0x[A-Fa-f0-9]{1,16})"),
                Some(group_name) => Cow::Owned(format!("(?P<{group_name}>0x[A-Fa-f0-9]{{1,16}})")),
            },
            NumberFormat::Hex {
                require_prefix: true,
                precision,
            } => Cow::Owned(format!("(?P<{group_name}>0x[A-Fa-f0-9]{{{precision}}})")),
            NumberFormat::Hex {
                require_prefix: false,
                precision: 0,
            } => match group_name_override {
                None => Cow::Borrowed(r"(?P<digits>[A-Fa-f0-9]{1,16})"),
                Some(group_name) => Cow::Owned(format!("(?P<{group_name}>[A-Fa-f0-9]{{1,16}})")),
            },
            NumberFormat::Hex {
                require_prefix: false,
                precision,
            } => Cow::Owned(format!("(?P<{group_name}>[A-Fa-f0-9]{{{precision}}})")),
        }
    }
}
impl Default for NumberFormat {
    fn default() -> Self {
        Self::Unsigned { precision: 0 }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub enum FormatSpecifier {
    #[default]
    Unsigned,
    Signed,
    Hex,
}