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
#![macro_use]

/// A macro to define a helper struct that represents the rule.
macro_rules! rule_struct {
    ($name: ident) => {
        #[derive(Clone, Debug, Eq, PartialEq)]
        struct $name {
            b: Vec<u8>,
            s: Vec<u8>,
        }

        impl $name {
            /// Construct the struct from `b` / `s` data.
            fn from_bs(b: Vec<u8>, s: Vec<u8>) -> Self {
                $name { b, s }
            }

            /// Construct the Generations struct from `b` / `s` data and the number of states.
            fn from_bsg(b: Vec<u8>, s: Vec<u8>, gen: usize) -> Gen<Self> {
                Gen {
                    rule: $name { b, s },
                    gen,
                }
            }

            /// A parser for numbers.
            fn parse_num<I>(chars: &mut std::iter::Peekable<I>) -> Result<usize, ParseRuleError>
            where
                I: Iterator<Item = char>,
            {
                let mut n: usize = 0;
                if chars.peek().is_none() || !chars.peek().unwrap().is_digit(10) {
                    return Err(ParseRuleError::MissingNumber);
                }
                while let Some(&c) = chars.peek() {
                    match c {
                        c if c.is_digit(10) => {
                            chars.next();
                            n = n
                                .checked_mul(10)
                                .ok_or(ParseRuleError::GenOverflow)?
                                .checked_add(c.to_digit(10).unwrap() as usize)
                                .ok_or(ParseRuleError::GenOverflow)?;
                        }
                        _ => return Ok(n),
                    }
                }
                Ok(n)
            }
        }
    };
}

/// A macro to define a function to parse the helper struct.
macro_rules! parse_rule {
    ($($suffix: expr)?) => {
        /// A parser for the struct.
        fn parse_rule(input: &str) -> Result<Self, ParseRuleError> {
            let mut chars = input.chars().peekable();
            let (b, s);

            match chars.peek() {
                Some('B') | Some('b') => {
                    // Rule strings using B/S notation
                    chars.next();
                    b = Self::parse_bs(&mut chars)?;
                    if chars.peek() == Some(&'/') {
                        chars.next();
                    }
                    match chars.next() {
                        Some('S') | Some('s') => (),
                        _ => return Err(ParseRuleError::Missing('S')),
                    }
                    s = Self::parse_bs(&mut chars)?;
                }
                _ => {
                    // Rule strings using S/B notation
                    s = Self::parse_bs(&mut chars)?;
                    match chars.next() {
                        Some('/') => (),
                        _ => return Err(ParseRuleError::Missing('/')),
                    }
                    b = Self::parse_bs(&mut chars)?;
                }
            }

            $(
                // Suffix
                if let Some(c) = chars.next() {
                    if $suffix.to_lowercase().chain($suffix.to_uppercase()).all(|s| s != c) {
                        return Err(ParseRuleError::Missing($suffix));
                    }
                } else {
                    return Err(ParseRuleError::Missing($suffix));
                }
            )?

            match chars.next() {
                None => Ok(Self::from_bs(b, s)),
                _ => Err(ParseRuleError::ExtraJunk),
            }
        }

        /// A parser for the Generations struct.
        fn parse_rule_gen(input: &str) -> Result<Gen<Self>, ParseRuleError> {
            let mut chars = input.chars().peekable();
            let (b, s);
            let mut gen = 2;

            match chars.peek() {
                // Rule strings using B/S/G notation
                Some('B') | Some('b') => {
                    chars.next();
                    b = Self::parse_bs(&mut chars)?;
                    if chars.peek() == Some(&'/') {
                        chars.next();
                    }
                    match chars.next() {
                        Some('S') | Some('s') => (),
                        _ => return Err(ParseRuleError::Missing('S')),
                    }
                    s = Self::parse_bs(&mut chars)?;
                    match chars.peek() {
                        Some('/') => {
                            chars.next();
                            match chars.peek() {
                                Some('C') | Some('c') | Some('G') | Some('g') => {
                                    chars.next();
                                }
                                _ => (),
                            }
                            gen = Self::parse_num(&mut chars)?;
                        }
                        Some('C') | Some('c') | Some('G') | Some('g') => {
                            chars.next();
                            gen = Self::parse_num(&mut chars)?;
                        }
                        _ => (),
                    }
                }

                // Rule strings using G/B/S notation
                Some('C') | Some('c') | Some('G') | Some('g') => {
                    chars.next();
                    gen = Self::parse_num(&mut chars)?;
                    if chars.peek() == Some(&'/') {
                        chars.next();
                    }
                    match chars.next() {
                        Some('B') | Some('b') => (),
                        _ => return Err(ParseRuleError::Missing('B')),
                    }
                    b = Self::parse_bs(&mut chars)?;
                    if chars.peek() == Some(&'/') {
                        chars.next();
                    }
                    match chars.next() {
                        Some('S') | Some('s') => (),
                        _ => return Err(ParseRuleError::Missing('S')),
                    }
                    s = Self::parse_bs(&mut chars)?;
                }

                // Rule strings using S/B/G notation
                _ => {
                    s = Self::parse_bs(&mut chars)?;
                    match chars.next() {
                        Some('/') => (),
                        _ => return Err(ParseRuleError::Missing('/')),
                    }
                    b = Self::parse_bs(&mut chars)?;
                    if chars.peek() == Some(&'/') {
                        chars.next();
                        gen = Self::parse_num(&mut chars)?;
                    }
                }
            }

            $(
                // Suffix
                if let Some(c) = chars.next() {
                    if $suffix.to_lowercase().chain($suffix.to_uppercase()).all(|s| s != c) {
                        return Err(ParseRuleError::Missing($suffix));
                    }
                } else {
                    return Err(ParseRuleError::Missing($suffix));
                }
            )?

            if gen < 2 {
                Err(ParseRuleError::GenLessThan2)
            } else {
                match chars.next() {
                    None => Ok(Self::from_bsg(b, s, gen)),
                    _ => Err(ParseRuleError::ExtraJunk),
                }
            }
        }
    };
}

/// A macro to define a function to parse `b` / `s` data.
macro_rules! parse_bs {
    ($n: expr) => {
        /// A parser for `b` / `s` data.
        fn parse_bs<I>(chars: &mut std::iter::Peekable<I>) -> Result<Vec<u8>, ParseRuleError>
        where
            I: Iterator<Item = char>,
        {
            let mut bs = Vec::new();

            while let Some(&c) = chars.peek() {
                match c {
                    c if c.is_digit($n + 1) => {
                        chars.next();
                        bs.push(c.to_digit($n + 1).unwrap() as u8);
                    }
                    _ => break,
                }
            }

            bs.sort_unstable();
            Ok(bs)
        }
    };

    { $($count: expr => { $($key: expr => $value: expr),* $(,)? }),*  $(,)? } => {
        /// A parser for `b` / `s` data.
        fn parse_bs<I>(chars: &mut std::iter::Peekable<I>) -> Result<Vec<u8>, ParseRuleError>
        where
            I: Iterator<Item = char>,
        {
            let mut bs = Vec::new();

            while let Some(&c) = chars.peek() {
                match c {
                    $(
                        $count => {
                            chars.next();
                            let all_keys = vec![$($key),*];
                            let keys = match chars.peek() {
                                Some('-') => {
                                    chars.next();
                                    let mut keys = Vec::new();
                                    while let Some(&c) = chars.peek() {
                                        if all_keys.contains(&c) {
                                            chars.next();
                                            keys.push(c);
                                        } else {
                                            break;
                                        }
                                    }
                                    all_keys.into_iter().filter(|c| !keys.contains(c)).collect()
                                }
                                Some(c) if all_keys.contains(&c) => {
                                    let mut keys = Vec::new();
                                    while let Some(&c) = chars.peek() {
                                        if all_keys.contains(&c) {
                                            chars.next();
                                            keys.push(c);
                                        } else {
                                            break;
                                        }
                                    }
                                    keys
                                }
                                Some(_) => {
                                    all_keys
                                }
                                None => all_keys
                            };
                            for &c in keys.iter() {
                                match c {
                                    $(
                                        $key => bs.extend_from_slice(&($value)),
                                    )*
                                    _ => unreachable!(),
                                }
                            }
                        }
                    ),*
                    _ => break,
                }
            }

            bs.sort_unstable();
            Ok(bs)
        }
    };
}

/// A macro to define a function to parse MAP strings.
macro_rules! parse_rule_map {
    ($n: expr) => {
        /// A parser for the struct that parses MAP strings.
        fn parse_rule_map(input: &str) -> Result<Self, ParseRuleError> {
            use base64::{
                alphabet::STANDARD,
                engine::{
                    general_purpose::{GeneralPurpose, GeneralPurposeConfig},
                    DecodePaddingMode, Engine,
                },
            };

            const CENTER_MARK: usize = 1 << ($n / 2);
            const RIGHT_MARK: usize = CENTER_MARK - 1;
            const LEFT_MARK: usize = RIGHT_MARK << ($n / 2 + 1);
            const ENGINE_CONFIG: GeneralPurposeConfig = GeneralPurposeConfig::new()
                .with_decode_padding_mode(DecodePaddingMode::Indifferent);
            const ENGINE: GeneralPurpose = GeneralPurpose::new(&STANDARD, ENGINE_CONFIG);

            if !input.starts_with("MAP") {
                return Err(ParseRuleError::NotMapRule);
            }
            let bytes = ENGINE
                .decode(&input[3..])
                .map_err(|_| ParseRuleError::Base64Error)?;
            if bytes.len() * 8 != 2 << $n {
                return Err(ParseRuleError::InvalidLength);
            }
            let mut b = Vec::new();
            let mut s = Vec::new();
            for (i, x) in bytes.iter().map(|x| x.reverse_bits()).enumerate() {
                for j in 0..8 {
                    if x & (1 << j) != 0 {
                        let k = i * 8 + j;
                        let n = ((k & LEFT_MARK) >> 1 | (k & RIGHT_MARK)) as u8;
                        if k & CENTER_MARK == 0 {
                            b.push(n);
                        } else {
                            s.push(n);
                        }
                    }
                }
            }
            Ok(Self::from_bs(b, s))
        }

        /// A parser for the Generations struct that parses MAP strings.
        fn parse_rule_gen_map(input: &str) -> Result<Gen<Self>, ParseRuleError> {
            use base64::{
                alphabet::STANDARD,
                engine::{
                    general_purpose::{GeneralPurpose, GeneralPurposeConfig},
                    DecodePaddingMode, Engine,
                },
            };

            const CENTER_MARK: usize = 1 << ($n / 2);
            const RIGHT_MARK: usize = CENTER_MARK - 1;
            const LEFT_MARK: usize = RIGHT_MARK << ($n / 2 + 1);
            const ENGINE_CONFIG: GeneralPurposeConfig = GeneralPurposeConfig::new()
                .with_decode_padding_mode(DecodePaddingMode::Indifferent);
            const ENGINE: GeneralPurpose = GeneralPurpose::new(&STANDARD, ENGINE_CONFIG);

            let mut gen = 2;
            let mut slash = input.len();
            if !input.starts_with("MAP") {
                return Err(ParseRuleError::NotMapRule);
            }
            if let Some(n) = input.rfind('/') {
                if (n - 3) * 6 >= 2 << $n {
                    slash = n;
                    let mut chars = input[n + 1..].chars().peekable();
                    if chars.peek().is_some() {
                        gen = Self::parse_num(&mut chars)?;
                        if chars.next().is_some() {
                            return Err(ParseRuleError::ExtraJunk);
                        }
                    }
                }
            }
            let bytes = ENGINE
                .decode(&input[3..slash])
                .map_err(|_| ParseRuleError::Base64Error)?;
            if bytes.len() * 8 != 2 << $n {
                return Err(ParseRuleError::InvalidLength);
            }
            let mut b = Vec::new();
            let mut s = Vec::new();
            for (i, x) in bytes.iter().map(|x| x.reverse_bits()).enumerate() {
                for j in 0..8 {
                    if x & (1 << j) != 0 {
                        let k = i * 8 + j;
                        let n = ((k & LEFT_MARK) >> 1 | (k & RIGHT_MARK)) as u8;
                        if k & CENTER_MARK == 0 {
                            b.push(n);
                        } else {
                            s.push(n);
                        }
                    }
                }
            }
            Ok(Self::from_bsg(b, s, gen))
        }
    };
}

/// A macro for implementing traits for helper structs.
///
/// `$f` is a function that converts the `b` / `s` data of the struct to those of the trait.
/// `$n` is the upper bound of `b` / `s` data of the struct.
macro_rules! impl_parser {
    (
        ($trait_name: ident, $trait_name_gen: ident) for $struct_name: ident,
        $f: expr,
        $n: expr $(,)?
    ) => {
        impl $trait_name for $struct_name {
            fn from_bs(b: Vec<u8>, s: Vec<u8>) -> Self {
                let mut new_b = Vec::new();
                let mut new_s = Vec::new();
                let f = $f;
                for i in 0_u8..=$n {
                    let j = f(i);
                    if b.contains(&j) {
                        new_b.push(i);
                    }
                    if s.contains(&j) {
                        new_s.push(i);
                    }
                }
                $struct_name::from_bs(new_b, new_s)
            }
        }

        impl $trait_name_gen for Gen<$struct_name> {
            fn from_bsg(b: Vec<u8>, s: Vec<u8>, gen: usize) -> Self {
                let mut new_b = Vec::new();
                let mut new_s = Vec::new();
                let f = $f;
                for i in 0_u8..=$n {
                    let j = f(i);
                    if b.contains(&j) {
                        new_b.push(i);
                    }
                    if s.contains(&j) {
                        new_s.push(i);
                    }
                }
                $struct_name::from_bsg(new_b, new_s, gen)
            }
        }
    };
}