aoc_main/parse/
mod.rs

1//! Macro used to parse input tokens.
2
3mod gen_bench;
4mod gen_run;
5
6// Call `apply` macro on this generated form of token tree;
7// $ctx, { day DAY { { gen GENERATOR } { { sol SOLUTION } { sol SOLUTION } } } }
8#[macro_export]
9macro_rules! parse {
10    // Read day: default generator
11    (
12        day $apply: ident, $ctx: tt, $val: expr;
13        $day: ident => $( $tail: tt )*
14    ) => {
15        $crate::parse!(
16            sol $apply, $ctx, $val;
17            { day $day { { gen_default } { } } }; $( $tail )*
18        )
19    };
20
21    // Read day: regular generator
22    (
23        day $apply: ident, $ctx: tt, $val: expr;
24        $day: ident : $generator: ident => $( $tail: tt )*
25    ) => {
26        $crate::parse!(
27            sol $apply, $ctx, $val;
28            { day $day { { gen $generator } { } } }; $( $tail )*
29        )
30    };
31
32    // Read day: fallible generator
33    (
34        day $apply: ident, $ctx: tt, $val: expr;
35        $day: ident : $generator: ident ? => $( $tail: tt )*
36    ) => {
37        $crate::parse!(
38            sol $apply, $ctx, $val;
39            { day $day { { gen_fallible $generator } { } } }; $( $tail )*
40        )
41    };
42
43    // Empty rules
44    ( day $apply: ident, $ctx: tt, $val: expr; ) => {};
45
46    // Read fallible solution
47    (
48        sol $apply: ident, $ctx: tt, $val: expr;
49        { day $day: tt { $gen: tt { $( $acc: tt )* } } } ;
50        $sol: ident ? $( $tail: tt )*
51    ) => {
52        $crate::parse!(
53            post_sol $apply, $ctx, $val;
54            { day $day { $gen { $( $acc )* { sol_fallible $sol } } } }; $( $tail )*
55        )
56    };
57
58    // Read solution
59    (
60        sol $apply: ident, $ctx: tt, $val: expr;
61        { day $day: tt { $gen: tt { $( $acc: tt )* } } } ;
62        $sol: ident $( $tail: tt )*
63    ) => {
64        $crate::parse!(
65            post_sol $apply, $ctx, $val;
66            { day $day { $gen { $( $acc )* { sol $sol } } } }; $( $tail )*
67        )
68    };
69
70    // After solution: there is new solutions
71    (
72        post_sol $apply: ident, $ctx: tt, $val: expr;
73        $curr: tt ; , $( $tail: tt )*
74    ) => {
75        $crate::parse!(sol $apply, $ctx, $val; $curr; $( $tail )* )
76    };
77
78    // After solution: end of day
79    (
80        post_sol $apply: ident, $ctx: tt, $val: expr;
81        $curr: tt ; ; $( $tail: tt )*
82    ) => {{
83        $val.push($apply!{ $ctx, $curr });
84        $crate::parse!( day $apply, $ctx, $val; $( $tail )* );
85    }};
86
87    // Initialization
88    ( $apply: ident $ctx: tt; $( $tt: tt )* ) => {{
89        let mut val = Vec::new();
90        $crate::parse!( day $apply, $ctx, val; $( $tt )* );
91        val
92    }};
93}
94
95// Extract day names from a parsed token tree
96#[macro_export]
97macro_rules! extract_day {
98    ({}, { day $day: ident $other: tt }) => {
99        stringify!($day)
100    };
101}