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
//! Handling for generating a `Parse` implementation from `enum` variants

use crate::fields::generate_fn_body;
use proc_macro2::{Span, TokenStream};
use quote::{quote, quote_spanned};
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{Attribute, Expr, Ident, LitStr, Result, Token, Variant};

pub(crate) fn generate_impl(
    variants: impl ExactSizeIterator<Item = Variant>,
) -> Result<TokenStream> {
    // generate each `peek` and corresponding inner implementation

    if variants.len() == 0 {
        return Err(syn::Error::new(
            Span::call_site(),
            "cannot derive `Parse` for an empty `enum`",
        ));
    }

    let input_source = crate::parse_input();

    let mut names = Vec::new();
    let conditional_return_if_peek_success = variants
        .into_iter()
        .map(|var| {
            let (name, if_condition) = impl_for_variant(&input_source, var)?;
            names.push(name);
            Ok(if_condition)
        })
        .collect::<Result<Vec<_>>>()?;

    let error_msg = implemented_error_msg(names);

    Ok(quote! {
        #( #conditional_return_if_peek_success )*

        Err(#input_source.error(#error_msg))
    })
}

fn implemented_error_msg(names: Vec<LitStr>) -> String {
    let one_of = match names.len() {
        1 => "",
        2 => "either ",
        _ => "one of ",
    };

    let name_list = match names.len() {
        0 => unreachable!(),
        1 => names[0].value(),
        2 => format!("{} or {}", names[0].value(), names[1].value()),
        _ => {
            let middle = names[1..names.len() - 1]
                .iter()
                .map(|name| format!(", {}", name.value()))
                .collect::<String>();
            format!(
                "{}{}, or {}",
                names[0].value(),
                middle,
                names.last().unwrap().value()
            )
        }
    };

    format!("expected {}{}", one_of, name_list)
}

enum VariantAttr {
    Peek(PeekInfo),
    PeekWith(PeekInfo),
}

mod kwd {
    syn::custom_keyword!(name);
}

struct PeekInfo {
    expr: Expr,
    _comma: Token![,],
    _name_token: kwd::name,
    _eq: Token![=],
    name: LitStr,
}

// If successful, the first element in the tuple is the name to use to refer to the variant in
// error messages. The second element is an `if` expression that looks something like:
//
//   if $input_source.peek($peek_value) {
//       Ok(Self::$variant_name {
//          $( $field: $input_source.parse()?, )*
//       })
//   }
fn impl_for_variant(input_source: &Ident, variant: Variant) -> Result<(LitStr, TokenStream)> {
    use VariantAttr::{Peek, PeekWith};

    let variant_span = variant.span();

    let diagnositc_name: LitStr;
    let peek_expr = match extract_single_attr(variant_span, variant.attrs)? {
        Peek(PeekInfo { expr, name, .. }) => {
            diagnositc_name = name;
            quote_spanned! {
                expr.span()=>
                #input_source.peek(#expr)
            }
        }
        PeekWith(PeekInfo { expr, name, .. }) => {
            diagnositc_name = name;
            quote_spanned! {
                expr.span()=>
                (#expr)(#input_source)
            }
        }
    };

    let ident = variant.ident;
    let variant_path = quote!( Self::#ident );
    let parse_implementation = generate_fn_body(&variant_path, variant.fields, true)?;

    let output = quote! {
        if #peek_expr {
            #parse_implementation;
        }
    };

    Ok((diagnositc_name, output))
}

fn extract_single_attr(variant_span: Span, attrs: Vec<Attribute>) -> Result<VariantAttr> {
    let mut attrs: Vec<_> = attrs
        .into_iter()
        .filter_map(try_as_variant_attr)
        .collect::<Result<_>>()?;

    match attrs.len() {
        0 => Err(syn::Error::new(
            variant_span,
            "enum variants must have `#[peek(..)]` or `#[peek_with(..)]` to derive `Parse`",
        )),
        1 => Ok(attrs.remove(0)),
        _ => Err(syn::Error::new(
            variant_span,
            "more than one peeking attribute is disallowed; please use `#[peek_with(..)]` for a custom function",
        )),
    }
}

fn try_as_variant_attr(attr: Attribute) -> Option<Result<VariantAttr>> {
    let name = attr.path().get_ident()?.to_string();

    match name.as_str() {
        "peek" => Some(attr.parse_args().map(VariantAttr::Peek)),
        "peek_with" => Some(attr.parse_args().map(VariantAttr::PeekWith)),
        _ => None,
    }
}

////////////////////////////////////////////
// Boilerplate `Parse` implementations 🙃 //
////////////////////////////////////////////

impl Parse for PeekInfo {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(PeekInfo {
            expr: input.parse()?,
            _comma: input.parse()?,
            _name_token: input.parse()?,
            _eq: input.parse()?,
            name: input.parse()?,
        })
    }
}