anpa 0.11.0

A generic monadic parser combinator library inspired by Haskell's parsec.
Documentation
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
use core::ops::{Add, Div, Mul, Sub};

use crate::{charlike::CharLike, combinators::{bind, map, or, right}, core::{Parser, ParserExt}, parsers::item_if, slicelike::SliceLike};

/// Trait for types that act like numbers.
pub trait NumLike:
Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
+ PartialOrd
+ Copy {
    const MIN: Self;
    const MAX: Self;
    const ZERO: Self;
    const TEN: Self;
    const SIZE: usize;
    fn cast_u8(n: u8) -> Self;
    fn checked_add(self, n: Self) -> Option<Self>;
    fn checked_sub(self, n: Self) -> Option<Self>;
    fn checked_mul(self, n: Self) -> Option<Self>;
}

/// Trait for types that act like floating point numbers.
pub trait FloatLike: Add<Output = Self> + Mul<Output = Self> + Div<Output = Self> + Copy {
    const ZERO: Self;
    const ONE: Self;
    const TEN: Self;
    const MINUS_ONE: Self;
    fn cast_usize(n: usize) -> Self;
    fn cast_isize(n: isize) -> Self;
    fn pow_i(self, exp: i32) -> Self;
}

macro_rules! impl_NumLike {
    ($($type:tt),*) => {
        $(
            impl NumLike for $type {
                const MIN: $type = $type::MIN;
                const MAX: $type = $type::MAX;
                const ZERO: $type = 0;
                const TEN: $type = 10;
                const SIZE: usize = core::mem::size_of::<$type>();

                #[inline(always)]
                fn cast_u8(n: u8) -> Self {
                    n as $type
                }

                #[inline(always)]
                fn checked_add(self, n: Self) -> Option<Self> {
                    self.checked_add(n)
                }

                #[inline(always)]
                fn checked_sub(self, n: Self) -> Option<Self> {
                    self.checked_sub(n)
                }

                #[inline(always)]
                fn checked_mul(self, n: Self) -> Option<Self> {
                    self.checked_mul(n)
                }
            }
        )*
    }
}

macro_rules! impl_FloatLike {
    ($($type:tt),*) => {
        $(
            impl FloatLike for $type {
                const ZERO: Self = 0.0;
                const ONE: Self = 1.0;
                const TEN: Self = 10.0;
                const MINUS_ONE: Self = -1.0;
                #[inline(always)]
                fn cast_usize(n: usize) -> Self {
                    n as $type
                }

                #[inline(always)]
                fn cast_isize(n: isize) -> Self {
                    n as $type
                }

                #[inline(always)]
                fn pow_i(self, exp: i32) -> Self {
                    self.powi(exp)
                }
            }
        )*
    }
}


impl_NumLike!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize);
impl_FloatLike!(f32, f64);

pub const fn integer_internal<const CHECKED: bool,
                              const SIGNED: bool,
                              const LEADING_PLUS: bool,
                              const LEADING_ZEROS: bool,
                              const BASE: u8,
                              const DEC_DIVISOR: bool,
                              O: NumLike,
                              A: CharLike,
                              I: SliceLike<RefItem = A>,
                              S>() -> impl Parser<I, (O, usize, bool), S> {
    create_parser!(s, {
        let mut idx = I::Idx::default();
        let mut acc = O::ZERO;
        let mut dec_divisor = 1;

        let mut iter = s.input.slice_iter();
        let mut consume = |digit: u32, is_negative: bool| -> Option<()> {
            // Digits are between 0 and 9, so they always fit in all types
            let digit = O::cast_u8(digit as u8);

            if !LEADING_ZEROS && acc == O::ZERO && idx != I::Idx::default() {
                    return None
            }

            if CHECKED {
                acc = acc.checked_mul(O::cast_u8(BASE))?;
            } else {
                acc = acc * O::cast_u8(BASE);
            }

            if is_negative {
                if CHECKED {
                    acc = acc.checked_sub(digit)?;
                } else {
                    acc = acc - digit;
                }
            } else if CHECKED {
                acc = acc.checked_add(digit)?;
            } else {
                acc = acc + digit;
            }

            idx += true.into();

            if DEC_DIVISOR {
                if CHECKED {
                    dec_divisor = dec_divisor.checked_mul(BASE as usize)?;
                } else {
                    dec_divisor *= BASE as usize;
                }
            }

            Some(())
        };

        let leading = if SIGNED || LEADING_PLUS {
            let c = iter.next()?.as_char();

            if SIGNED && c == '-' {
                Some(true)
            } else if LEADING_PLUS && c == '+' {
                Some(false)
            } else {
                // We don't care about checking the result here, since a single digit can never fail.
                consume(c.to_digit(BASE as u32)?, false);
                None
            }
        } else {
            None
        };

        let is_negative = leading.unwrap_or(false);

        for digit in iter.map_while(|d| d.as_char().to_digit(BASE as u32)) {
            consume(digit, is_negative)?;
        }

        (idx != Default::default()).then(|| {
            s.input = s.input.slice_from(idx + leading.is_some().into());
            (acc, dec_divisor, is_negative)
        })
    })
}

/// Configuration for integer parsing. The instance functions can be used to
/// change the behavior of the parse.
#[derive(Clone, Copy)]
pub struct IntConfig<const CHECKED: bool = true,
                     const SIGNED: bool = false,
                     const LEADING_PLUS: bool = false,
                     const LEADING_ZEROS: bool = true,
                     const BASE: u8 = 10>;

impl IntConfig {
    pub const fn new() -> Self {
        IntConfig
    }
}

impl Default for IntConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl<const CHECKED: bool,
     const SIGNED: bool,
     const LEADING_PLUS: bool,
     const LEADING_ZEROS: bool,
     const BASE: u8>
     IntConfig<CHECKED, SIGNED, LEADING_PLUS, LEADING_ZEROS, BASE> {

    pub const fn unchecked(self) -> IntConfig<false, SIGNED, LEADING_PLUS, LEADING_ZEROS, BASE> {
        IntConfig
    }

    pub const fn signed(self) -> IntConfig<CHECKED, true, LEADING_PLUS, LEADING_ZEROS, BASE> {
        IntConfig
    }

    pub const fn no_leading_zero(self) -> IntConfig<CHECKED, SIGNED, LEADING_PLUS, false, BASE> {
        IntConfig
    }

    pub const fn leading_plus(self) -> IntConfig<CHECKED, SIGNED, true, LEADING_ZEROS, BASE> {
        IntConfig
    }

    pub const fn base<const NEW_BASE: u8>(self) -> IntConfig<CHECKED, SIGNED, LEADING_PLUS, LEADING_ZEROS, NEW_BASE> {
        assert!(NEW_BASE >= 2 && NEW_BASE <= 36, "Base must be between 2 and 36");
        IntConfig
    }
}

/// Parse an unsigned integer. The type of the integer will be inferred from the context.
/// General verison taking a config object. See [`IntConfig`] for more information.
///
/// ### Arguments
/// * `_config` - the configuration object
///
/// ### Example
/// ```
/// use anpa::core::*;
/// use anpa::number::{integer_custom, IntConfig};
///
/// let parse_plus = integer_custom(IntConfig::new().leading_plus());
/// let parse_no_plus = integer_custom(IntConfig::new());
/// let input1 = "+10";
/// let input2 = "10";
///
/// assert_eq!(Some(10), parse(parse_plus, input1).result);
/// assert_eq!(Some(10), parse(parse_plus, input2).result);
///
/// assert_eq!(None, parse(parse_no_plus, input1).result);
/// assert_eq!(Some(10), parse(parse_no_plus, input2).result);
/// ```
#[inline]
pub const fn integer_custom<const CHECKED: bool,
                            const SIGNED: bool,
                            const LEADING_PLUS: bool,
                            const LEADING_ZEROS: bool,
                            const BASE: u8,
                            O: NumLike,
                            A: CharLike,
                            I: SliceLike<RefItem = A>,
                            S>(_config: IntConfig<CHECKED, SIGNED, LEADING_PLUS, LEADING_ZEROS, BASE>) -> impl Parser<I, O, S> {
    map(integer_internal::<CHECKED, SIGNED, LEADING_PLUS, LEADING_ZEROS, BASE, false,_,_,_,_>(), |(n,_,_)| n)
}

/// Parse an unsigned integer. The type of the integer will be inferred from the context.
/// This parser will fail if the result does not fit in the inferred integer type.
#[inline]
pub const fn integer<O: NumLike,
                     A: CharLike,
                     I: SliceLike<RefItem = A>,
                     S>() -> impl Parser<I, O, S> {
    integer_custom(IntConfig::new())
}

/// Parse an signed integer. The type of the integer will be inferred from the context.
/// This parser will fail if the result does not fit in the inferred integer type.
#[inline]
pub const fn integer_signed<O: NumLike,
                            A: CharLike,
                            I: SliceLike<RefItem = A>,
                            S>() -> impl Parser<I, O, S> {
    integer_custom(IntConfig::new().signed())
}

/// Configuration for float parsing. The instance functions can be used to
/// change the behavior of the parse.
#[derive(Clone, Copy)]
pub struct FloatConfig<const CHECKED: bool = true,
                       const SIGNED: bool = true,
                       const SCI: bool = false,
                       const LEADING_PLUS: bool = false,
                       const LEADING_ZEROS_INT: bool = true,
                       const LEADING_ZEROS_EXP: bool = true,
                       const DECIMAL_COMMA: bool = false>;

impl FloatConfig {
    pub const fn new() -> Self {
        FloatConfig
    }
}

impl Default for FloatConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl<const CHECKED: bool,
     const SIGNED: bool,
     const SCI: bool,
     const LEADING_PLUS: bool,
     const LEADING_ZERO_INT: bool,
     const LEADING_ZERO_EXP: bool,
     const DECIMAL_COMMA: bool>
     FloatConfig<CHECKED, SIGNED, SCI, LEADING_PLUS, LEADING_ZERO_INT, LEADING_ZERO_EXP, DECIMAL_COMMA> {

    pub const fn unchecked(self) -> FloatConfig::<false, SIGNED, SCI, LEADING_PLUS, LEADING_ZERO_INT, LEADING_ZERO_EXP, DECIMAL_COMMA> {
        FloatConfig
    }

    pub const fn unsigned(self) -> FloatConfig::<CHECKED, false, SCI, LEADING_PLUS, LEADING_ZERO_INT, LEADING_ZERO_EXP, DECIMAL_COMMA> {
        FloatConfig
    }

    pub const fn scientific(self) -> FloatConfig::<CHECKED, SIGNED, true, LEADING_PLUS, LEADING_ZERO_INT, LEADING_ZERO_EXP, DECIMAL_COMMA> {
        FloatConfig
    }

    pub const fn leading_plus(self) -> FloatConfig::<CHECKED, SIGNED, SCI, true, LEADING_ZERO_INT, LEADING_ZERO_EXP, DECIMAL_COMMA> {
        FloatConfig
    }

    pub const fn no_leading_zero_int(self) -> FloatConfig::<CHECKED, SIGNED, SCI, LEADING_PLUS, false, LEADING_ZERO_EXP, DECIMAL_COMMA> {
        FloatConfig
    }

    pub const fn no_leading_zero_exp(self) -> FloatConfig::<CHECKED, SIGNED, SCI, LEADING_PLUS, LEADING_ZERO_INT, false, DECIMAL_COMMA> {
        FloatConfig
    }

    pub const fn decimal_comma(self) -> FloatConfig::<CHECKED, SIGNED, SCI, LEADING_PLUS, LEADING_ZERO_INT, LEADING_ZERO_EXP, true> {
        FloatConfig
    }
}

/// Parse a float. The type of the float will be inferred from the context.
/// General verison taking a config object. See [`FloatConfig`] for more information.
///
/// ### Arguments
/// * `_config` - the configuration object
///
/// ### Example
/// ```
/// use anpa::core::*;
/// use anpa::number::{float_custom, FloatConfig};
///
/// let parse_sci = float_custom(FloatConfig::new().scientific());
/// let parse_no_sci = float_custom(FloatConfig::new());
/// let input1 = "1.02e30";
/// let input2 = "10.2";
///
/// assert_eq!(Some(1.02e30), parse(parse_sci, input1).result);
/// assert_eq!(Some(10.2), parse(parse_sci, input2).result);
///
/// assert_eq!(Some(1.02), parse(parse_no_sci, input1).result);
/// assert_eq!(Some(10.2), parse(parse_no_sci, input2).result);
/// ```
#[inline]
pub const fn float_custom<const CHECKED: bool,
                          const SIGNED: bool,
                          const SCI: bool,
                          const LEADING_PLUS: bool,
                          const LEADING_ZERO_INT: bool,
                          const LEADING_ZERO_EXP: bool,
                          const DECIMAL_COMMA: bool,
                          O: FloatLike,
                          A: CharLike,
                          I: SliceLike<RefItem = A>,
                          S>(_config: FloatConfig<CHECKED, SIGNED, SCI, LEADING_PLUS, LEADING_ZERO_INT, LEADING_ZERO_EXP, DECIMAL_COMMA>)
                          -> impl Parser<I, O, S> {

    // First parse a possibly negative signed integer
    bind(integer_internal::<CHECKED, SIGNED, LEADING_PLUS, LEADING_ZERO_INT, 10, false,_,_,_,_>(), |(n, _, is_neg)| {
        // Then parse a period followed by an unsigned integer.
        let int = O::cast_isize(n);
        let dec = right(item_if(|c: I::RefItem| c.as_char() ==  if DECIMAL_COMMA {','} else {'.'}),
                  integer_internal::<CHECKED, false, false, true, 10, true,_,_,_,_>())
            .map(move |(dec, div, _)|
                int + if is_neg {O::MINUS_ONE} else {O::ONE} * O::cast_usize(dec) / O::cast_usize(div));

        let pre_exp_parser = or(dec, pure!(int));

        choose_pure!(SCI;
            true => bind(pre_exp_parser, |pre_exp| {
                let exp = right(item_if(|c: I::RefItem| matches!(c.as_char(), 'e' | 'E')),
                                integer_custom(IntConfig::<CHECKED, true, true, LEADING_ZERO_EXP>))
                    .map(move |exp| pre_exp * O::TEN.pow_i(exp));
                or(exp, pure!(pre_exp))
            }),
            false => pre_exp_parser
        )
    })
}

/// Parse a floating point number. The type of the number will be inferred from the context.
/// This parser will attempt to parse the float as `isize.usize`, and if the parsed number
/// does not fit within those types, it will fail.
#[inline]
pub const fn float<O: FloatLike,
                   A: CharLike,
                   I: SliceLike<RefItem = A>,
                   S>() -> impl Parser<I, O, S> {
    float_custom(FloatConfig::new())
}

#[cfg(test)]
mod tests {
    use crate::{core::parse, number::{FloatConfig, IntConfig, float, float_custom, integer, integer_custom, integer_signed}};

    #[test]
    fn unsigned_integer() {
        assert_eq!(0, parse(integer(), "0").result.unwrap());
        assert_eq!(127, parse(integer(), "127").result.unwrap());
        assert_eq!(255, parse(integer(), "255").result.unwrap());

        assert!((parse(integer(), "-1").result as Option<u8>).is_none());
        assert!((parse(integer(), "256").result as Option<u8>).is_none());
    }

    #[test]
    fn unsigned_integer_hex() {
        let p = integer_custom(IntConfig::new().base::<16>());
        assert_eq!(0u8, parse(p, "0").result.unwrap());
        assert_eq!(127, parse(p, "7F").result.unwrap());
        assert_eq!(255, parse(p, "FF").result.unwrap());

        assert!((parse(p, "100").result as Option<u8>).is_none());
        assert!((parse(p, "-1").result as Option<u8>).is_none());
    }

    #[test]
    fn signed_integer() {
        assert_eq!(0, parse(integer_signed(), "0").result.unwrap());
        assert_eq!(127, parse(integer_signed(), "127").result.unwrap());
        assert_eq!(-1, parse(integer_signed(), "-1").result.unwrap());
        assert_eq!(-128, parse(integer_signed(), "-128").result.unwrap());

        assert_eq!(128u8, parse(integer_signed(), "128").result.unwrap());

        assert!((parse(integer_signed(), "-129").result as Option<u8>).is_none());
        assert!((parse(integer_signed(), "128").result as Option<i8>).is_none());
    }

    #[test]
    fn float_test() {
        assert_eq!(0f32, parse(float(), "0").result.unwrap());
        assert_eq!(100000000f32, parse(float(), "100000000").result.unwrap());
        assert_eq!(-100000000f32, parse(float(), "-100000000").result.unwrap());
        assert_eq!(13.37f32, parse(float(), "13.37").result.unwrap());
        assert_eq!(-13.37f32, parse(float(), "-13.37").result.unwrap());
        assert_eq!(13.07f32, parse(float(), "13.07").result.unwrap());
        assert_eq!(-13.07f32, parse(float(), "-13.07").result.unwrap());
        assert_eq!(1.123f32, parse(float(), "1.123").result.unwrap());
        assert_eq!(0.001f32, parse(float(), "0.001").result.unwrap());
        assert_eq!(-0.001f32, parse(float(), "-0.001").result.unwrap());
    }

    #[test]
    fn float_sci_test() {
        let p = float_custom(FloatConfig::new().scientific());
        assert_eq!(1e10, parse(p, "1e10").result.unwrap());
        assert_eq!(0.05e15, parse(p, "0.05e15").result.unwrap());
        assert_eq!(-1.3e12, parse(p, "-1.3e12").result.unwrap());
        assert_eq!(-1.3e+12, parse(p, "-1.3e+12").result.unwrap());
    }
}