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
use syn::parse::{self, Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::token;
use syn::{braced, Attribute, Expr, Generics, Ident, Path, Token, Type, Visibility};

mod kw {
    syn::custom_keyword!(Sync);
    syn::custom_keyword!(Send);
    syn::custom_keyword!(PhantomData);
    syn::custom_keyword!(CType);
    syn::custom_keyword!(drop);
    syn::custom_keyword!(clone);
}

pub struct Input {
    pub crate_: Path,
    pub types: Vec<ForeignType>,
}

impl Parse for Input {
    fn parse(input: ParseStream) -> parse::Result<Input> {
        let crate_ = input.parse()?;
        let mut types = vec![];
        while !input.is_empty() {
            types.push(input.parse()?);
        }

        Ok(Input { crate_, types })
    }
}

pub struct ForeignType {
    pub attrs: Vec<Attribute>,
    pub visibility: Visibility,
    pub name: Ident,
    pub generics: Generics,
    pub oibits: Punctuated<Ident, Token![+]>,
    pub phantom_data: Option<Type>,
    pub ctype: Type,
    pub drop: Expr,
    pub clone: Option<Expr>,
}

impl Parse for ForeignType {
    fn parse(input: ParseStream) -> parse::Result<ForeignType> {
        let attrs = input.call(Attribute::parse_outer)?;
        let visibility = input.parse()?;
        input.parse::<Token![unsafe]>()?;
        input.parse::<Token![type]>()?;
        let name = input.parse()?;
        let generics = input.parse()?;
        let oibits = input.call(parse_oibits)?;
        let inner;
        braced!(inner in input);
        let ctype = inner.call(parse_type::<kw::CType>)?;
        let phantom_data = inner.call(parse_phantom_data)?;
        let drop = inner.call(parse_fn::<kw::drop>)?;
        let clone = inner.call(parse_clone)?;

        Ok(ForeignType {
            attrs,
            visibility,
            name,
            generics,
            oibits,
            ctype,
            phantom_data,
            drop,
            clone,
        })
    }
}

fn parse_oibit(input: ParseStream) -> parse::Result<Ident> {
    let lookahead = input.lookahead1();
    if lookahead.peek(kw::Sync) || lookahead.peek(kw::Send) {
        input.parse()
    } else {
        Err(lookahead.error())
    }
}

fn parse_oibits(input: ParseStream) -> parse::Result<Punctuated<Ident, Token![+]>> {
    let mut out = Punctuated::new();

    if input.parse::<Option<Token![:]>>()?.is_some() {
        loop {
            out.push_value(input.call(parse_oibit)?);
            if input.peek(token::Brace) {
                break;
            }
            out.push_punct(input.parse()?);
            if input.peek(token::Brace) {
                break;
            }
        }
    }

    Ok(out)
}

fn parse_type<T>(input: ParseStream) -> parse::Result<Type>
where
    T: Parse,
{
    input.parse::<Token![type]>()?;
    input.parse::<T>()?;
    input.parse::<Token![=]>()?;
    let type_ = input.parse()?;
    input.parse::<Token![;]>()?;
    Ok(type_)
}

fn parse_phantom_data(input: ParseStream) -> parse::Result<Option<Type>> {
    if input.peek(Token![type]) && input.peek2(kw::PhantomData) {
        input.call(parse_type::<kw::PhantomData>).map(Some)
    } else {
        Ok(None)
    }
}

fn parse_fn<T>(input: ParseStream) -> parse::Result<Expr>
where
    T: Parse,
{
    input.parse::<Token![fn]>()?;
    input.parse::<T>()?;
    input.parse::<Token![=]>()?;
    let path = input.parse()?;
    input.parse::<Token![;]>()?;
    Ok(path)
}

fn parse_clone(input: ParseStream) -> parse::Result<Option<Expr>> {
    if input.peek(Token![fn]) && input.peek2(kw::clone) {
        input.call(parse_fn::<kw::clone>).map(Some)
    } else {
        Ok(None)
    }
}