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
//! Macro used to parse input tokens.

mod gen_bench;
mod gen_run;

// Call `apply` macro on this generated form of token tree;
// $ctx, { day DAY { { gen GENERATOR } { { sol SOLUTION } { sol SOLUTION } } } }
#[macro_export]
macro_rules! parse {
    // Read day: default generator
    (
        day $apply: ident, $ctx: tt, $val: expr;
        $day: ident => $( $tail: tt )*
    ) => {
        $crate::parse!(
            sol $apply, $ctx, $val;
            { day $day { { gen_default } { } } }; $( $tail )*
        )
    };

    // Read day: regular generator
    (
        day $apply: ident, $ctx: tt, $val: expr;
        $day: ident : $generator: ident => $( $tail: tt )*
    ) => {
        $crate::parse!(
            sol $apply, $ctx, $val;
            { day $day { { gen $generator } { } } }; $( $tail )*
        )
    };

    // Read day: fallible generator
    (
        day $apply: ident, $ctx: tt, $val: expr;
        $day: ident : $generator: ident ? => $( $tail: tt )*
    ) => {
        $crate::parse!(
            sol $apply, $ctx, $val;
            { day $day { { gen_fallible $generator } { } } }; $( $tail )*
        )
    };

    // Empty rules
    ( day $apply: ident, $ctx: tt, $val: expr; ) => {};

    // Read fallible solution
    (
        sol $apply: ident, $ctx: tt, $val: expr;
        { day $day: tt { $gen: tt { $( $acc: tt )* } } } ;
        $sol: ident ? $( $tail: tt )*
    ) => {
        $crate::parse!(
            post_sol $apply, $ctx, $val;
            { day $day { $gen { $( $acc )* { sol_fallible $sol } } } }; $( $tail )*
        )
    };

    // Read solution
    (
        sol $apply: ident, $ctx: tt, $val: expr;
        { day $day: tt { $gen: tt { $( $acc: tt )* } } } ;
        $sol: ident $( $tail: tt )*
    ) => {
        $crate::parse!(
            post_sol $apply, $ctx, $val;
            { day $day { $gen { $( $acc )* { sol $sol } } } }; $( $tail )*
        )
    };

    // After solution: there is new solutions
    (
        post_sol $apply: ident, $ctx: tt, $val: expr;
        $curr: tt ; , $( $tail: tt )*
    ) => {
        $crate::parse!(sol $apply, $ctx, $val; $curr; $( $tail )* )
    };

    // After solution: end of day
    (
        post_sol $apply: ident, $ctx: tt, $val: expr;
        $curr: tt ; ; $( $tail: tt )*
    ) => {{
        $val.push($apply!{ $ctx, $curr });
        $crate::parse!( day $apply, $ctx, $val; $( $tail )* );
    }};

    // Initialization
    ( $apply: ident $ctx: tt; $( $tt: tt )* ) => {{
        let mut val = Vec::new();
        $crate::parse!( day $apply, $ctx, val; $( $tt )* );
        val
    }};
}

// Extract day names from a parsed token tree
#[macro_export]
macro_rules! extract_day {
    ({}, { day $day: ident $other: tt }) => {
        stringify!($day)
    };
}