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
use nom::{
    branch::alt,
    bytes::complete::tag,
    character::complete::{multispace0, u64},
    sequence::delimited,
    IResult,
};

use crate::typ::RustType;
use crate::{labelling::Label, typ::CIntegralSize};

/// A combinator that takes a parser `inner` and produces a parser that also consumes both leading and
/// trailing whitespace, returning the output of `inner`.
pub fn ws<'a, F: 'a, O, E: nom::error::ParseError<&'a str>>(
    inner: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
where
    F: Fn(&'a str) -> IResult<&'a str, O, E>,
{
    delimited(multispace0, inner, multispace0)
}

pub fn rust_type(s: &str) -> IResult<&str, RustType> {
    fn int(s: &str) -> IResult<&str, RustType> {
        tag("c_int")(s).map(|(rest, _)| {
            (
                rest,
                RustType::CInt {
                    unsigned: false,
                    size: CIntegralSize::Int,
                },
            )
        })
    }
    fn ulong(s: &str) -> IResult<&str, RustType> {
        tag("c_ulong")(s).map(|(rest, _)| {
            (
                rest,
                RustType::CInt {
                    unsigned: true,
                    size: CIntegralSize::Long,
                },
            )
        })
    }
    fn void(s: &str) -> IResult<&str, RustType> {
        tag("c_void")(s).map(|(rest, _)| (rest, RustType::CVoid))
    }
    fn i32_ty(s: &str) -> IResult<&str, RustType> {
        tag("i32")(s).map(|(rest, _)| (rest, RustType::I32))
    }
    fn isize(s: &str) -> IResult<&str, RustType> {
        tag("isize")(s).map(|(rest, _)| (rest, RustType::Isize))
    }
    fn usize(s: &str) -> IResult<&str, RustType> {
        tag("usize")(s).map(|(rest, _)| (rest, RustType::Usize))
    }
    fn size_t(s: &str) -> IResult<&str, RustType> {
        tag("size_t")(s).map(|(rest, _)| (rest, RustType::Usize))
    }
    fn uint(s: &str) -> IResult<&str, RustType> {
        tag("c_uint")(s).map(|(rest, _)| {
            (
                rest,
                RustType::CInt {
                    unsigned: true,
                    size: CIntegralSize::Int,
                },
            )
        })
    }
    fn uchar(s: &str) -> IResult<&str, RustType> {
        tag("c_uchar")(s).map(|(rest, _)| {
            (
                rest,
                RustType::CInt {
                    unsigned: true,
                    size: CIntegralSize::Char,
                },
            )
        })
    }
    fn int_ptr(s: &str) -> IResult<&str, RustType> {
        tag("mut_ptr_c_int")(s).map(|(rest, _)| {
            (
                rest,
                RustType::Pointer(Box::new(RustType::CInt {
                    unsigned: false,
                    size: CIntegralSize::Int,
                })),
            )
        })
    }
    fn uint_ptr(s: &str) -> IResult<&str, RustType> {
        tag("mut_ptr_c_uint")(s).map(|(rest, _)| {
            (
                rest,
                RustType::Pointer(Box::new(RustType::CInt {
                    unsigned: true,
                    size: CIntegralSize::Int,
                })),
            )
        })
    }
    fn uchar_ptr(s: &str) -> IResult<&str, RustType> {
        tag("mut_ptr_c_uchar")(s).map(|(rest, _)| {
            (
                rest,
                RustType::Pointer(Box::new(RustType::CInt {
                    unsigned: true,
                    size: CIntegralSize::Char,
                })),
            )
        })
    }
    fn void_ptr(s: &str) -> IResult<&str, RustType> {
        tag("mut_ptr_c_void")(s)
            .map(|(rest, _)| (rest, RustType::Pointer(Box::new(RustType::CVoid))))
    }
    // not sure how to make wildcard/recursively handle nested pointers so hardcoding it in for now...
    fn nested_int_ptr(s: &str) -> IResult<&str, RustType> {
        tag("mut_ptr_mut_ptr_c_int")(s).map(|(rest, _)| {
            (
                rest,
                RustType::Pointer(Box::new(RustType::Pointer(Box::new(RustType::CInt {
                    unsigned: true,
                    size: CIntegralSize::Int,
                })))),
            )
        })
    }
    fn nested_uint_ptr(s: &str) -> IResult<&str, RustType> {
        tag("mut_ptr_mut_ptr_c_uint")(s).map(|(rest, _)| {
            (
                rest,
                RustType::Pointer(Box::new(RustType::Pointer(Box::new(RustType::CInt {
                    unsigned: true,
                    size: CIntegralSize::Int,
                })))),
            )
        })
    }

    alt((
        nested_int_ptr,
        nested_uint_ptr,
        uint_ptr,
        uchar_ptr,
        void_ptr,
        int_ptr,
        uint,
        size_t,
        uchar,
        int,
        ulong,
        void,
        i32_ty,
        isize,
        usize,
    ))(s)
}

pub fn label(s: &str) -> IResult<&str, Label> {
    let (s, _) = tag("A")(s)?;
    let (s, digits) = u64(s)?;
    Ok((s, Label::of_raw(digits as usize)))
}