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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! Crate with proc_macros for sscanf. Not usable as a standalone crate.

use proc_macro::TokenStream as TokenStream1;
use proc_macro2::{Literal, Span, TokenStream};
use quote::{quote, quote_spanned, ToTokens};
use syn::{
    parse::{Error, Parse, ParseStream, Result},
    parse_macro_input,
    spanned::Spanned,
    Expr, LitStr, Token, TypePath,
};

struct SscanfInner {
    fmt: String,
    fmt_span: Literal,
    span_offset: usize,
    type_tokens: Vec<TypePath>,
}
struct Sscanf {
    src_str: Expr,
    inner: SscanfInner,
}

impl Parse for SscanfInner {
    fn parse(input: ParseStream) -> Result<Self> {
        if input.is_empty() {
            return Err(Error::new(Span::call_site(), "Missing format string"));
        }

        let fmt: LitStr = input.parse()?;
        let span_offset = {
            // this is a dirty hack to see if the literal is a raw string, which is necessary for
            // subspan to skip the 'r', 'r#', ...
            // this should be a thing that I can check on LitStr or Literal or whatever, but nooo,
            // I have to print it into a String via a TokenStream and check that one -.-
            //
            // "fun" fact: This used to actually be a thing in syn 0.11 where `Lit::Str` gave _two_
            // values: the literal and a `StrStyle`. This was apparently removed at some point for
            // being TOO USEFUL.
            let lit = fmt.to_token_stream().to_string();
            lit.char_indices().find(|c| c.1 == '"').unwrap().0
        };
        let mut fmt_span = Literal::string(&fmt.value());
        fmt_span.set_span(fmt.span());

        let type_tokens;
        if input.is_empty() {
            type_tokens = vec![];
        } else {
            input.parse::<Token![,]>()?;

            type_tokens = input
                .parse_terminated::<_, Token![,]>(TypePath::parse)?
                .into_iter()
                .rev()
                .collect();
        }

        Ok(SscanfInner {
            fmt: fmt.value(),
            fmt_span,
            span_offset,
            type_tokens,
        })
    }
}
impl Parse for Sscanf {
    fn parse(input: ParseStream) -> Result<Self> {
        if input.is_empty() {
            return Err(Error::new(
                Span::call_site(),
                "At least 2 Parameters required: Input and format string",
            ));
        }
        let src_str = input.parse()?;
        if input.is_empty() {
            return Err(Error::new(
                Span::call_site(),
                "At least 2 Parameters required: Missing format string",
            ));
        }
        let comma = input.parse::<Token![,]>()?;
        if input.is_empty() {
            return Err(Error::new_spanned(
                comma,
                "At least 2 Parameters required: Missing format string",
            ));
        }
        let inner = input.parse()?;

        Ok(Sscanf { src_str, inner })
    }
}

#[proc_macro]
pub fn scanf(input: TokenStream1) -> TokenStream1 {
    let input = parse_macro_input!(input as Sscanf);
    scanf_internal(input, true)
}

#[proc_macro]
pub fn scanf_unescaped(input: TokenStream1) -> TokenStream1 {
    let input = parse_macro_input!(input as Sscanf);
    scanf_internal(input, false)
}

#[proc_macro]
pub fn scanf_get_regex(input: TokenStream1) -> TokenStream1 {
    let input = parse_macro_input!(input as SscanfInner);
    let (regex, _) = match generate_regex(input, true) {
        Ok(v) => v,
        Err(e) => return e.to_compile_error().into(),
    };
    quote!({
        let regex = ::sscanf::const_format::concatcp!(#(#regex),*);
        ::sscanf::regex::Regex::new(regex).unwrap()
    })
    .into()
}

fn scanf_internal(input: Sscanf, escape_input: bool) -> TokenStream1 {
    let (regex, matcher) = match generate_regex(input.inner, escape_input) {
        Ok(v) => v,
        Err(e) => return e.to_compile_error().into(),
    };
    let src_str = {
        let src_str = input.src_str;
        let (start, end) = full_span(&src_str);
        let mut param = quote_spanned!(start => &);
        param.extend(quote_spanned!(end => (#src_str)));
        quote!(::std::convert::AsRef::<str>::as_ref(#param))
    };
    quote!(
        {
            let regex = ::sscanf::const_format::concatcp!( #(#regex),* );
            let regex = ::sscanf::regex::Regex::new(regex).unwrap();
            #[allow(clippy::needless_question_mark)]
            regex.captures(#src_str).and_then(|cap| Some(( #(#matcher),* )))
        }
    )
    .into()
}

fn generate_regex(
    mut input: SscanfInner,
    escape_input: bool,
) -> Result<(Vec<TokenStream>, Vec<TokenStream>)> {
    let mut type_tokens = vec![];
    let mut regex = vec![];

    let mut name_index = 1;

    let mut current_regex = String::from("^");
    let mut last_was_open = false;
    let mut last_was_close = false;
    for (i, c) in input.fmt.chars().enumerate().map(|(i, c)| (i + 1, c)) {
        if c == '{' && !last_was_close {
            if last_was_open {
                last_was_open = false;
            } else {
                last_was_open = true;
                continue;
            }
        } else if c == '}' {
            if last_was_open {
                let ty = match input.type_tokens.pop() {
                    Some(token) => token,
                    None => return sub_error("Missing Type for given '{}'", &input, i - 1, i),
                };

                let name = format!("type_{}", name_index);
                name_index += 1;

                current_regex += &format!("(?P<{}>", name);
                regex.push(current_regex);
                current_regex = String::from(")");

                type_tokens.push((ty, name));

                last_was_open = false;
                continue;
            }
            if last_was_close {
                last_was_close = false;
            } else {
                last_was_close = true;
                continue;
            }
        } else if last_was_open {
            return sub_error(
                "Expected '}' after '{'. Literal '{' need to be escaped as '{{'",
                &input,
                i - 1,
                i - 1,
            );
        } else if last_was_close {
            return sub_error(
                "Unexpected standalone '}'. Literal '}' need to be escaped as '}}'",
                &input,
                i - 1,
                i - 1,
            );
        }

        if escape_input && regex_syntax::is_meta_character(c) {
            current_regex.push('\\');
        }

        current_regex.push(c);
    }

    current_regex.push('$');

    let end = input.fmt.len();
    if last_was_open {
        return sub_error(
            "Expected '}' after '{'. Literal '{' need to be escaped as '{{'",
            &input,
            end,
            end,
        );
    } else if last_was_close {
        return sub_error(
            "Unexpected standalone '}'. Literal '}' need to be escaped as '}}'",
            &input,
            end,
            end,
        );
    }

    if let Some(token) = input.type_tokens.pop() {
        return Err(Error::new_spanned(token, "More Types than '{}' provided"));
    }

    let mut regex_builder = vec![];
    let mut match_grabber = vec![];
    for (prefix, (ty, name)) in regex.iter().zip(type_tokens) {
        regex_builder.push(quote!(#prefix));

        let (start, end) = full_span(&ty);

        {
            let mut s = quote_spanned!(start => <#ty as );
            s.extend(quote_spanned!(end => ::sscanf::RegexRepresentation>::REGEX));
            regex_builder.push(s);
        }
        {
            let mut s = quote_spanned!(start => <#ty as );
            s.extend(quote_spanned!(end => ::std::str::FromStr>::from_str(cap.name(#name)?.as_str()).ok()?));
            match_grabber.push(s);
        }
    }

    regex_builder.push(quote!(#current_regex));

    Ok((regex_builder, match_grabber))
}

fn sub_error<T>(message: &str, src: &SscanfInner, start: usize, end: usize) -> Result<T> {
    let s = start + src.span_offset;
    let e = end + src.span_offset;
    if let Some(span) = src.fmt_span.subspan(s..=e) {
        Err(Error::new(span, message))
    } else {
        let m = format!("{}.  At \"{}\" <--", message, &src.fmt[0..end]);
        Err(Error::new_spanned(&src.fmt_span, m))
    }
}

fn full_span<T: ToTokens + Spanned>(span: &T) -> (Span, Span) {
    // dirty hack stolen from syn::Error::new_spanned
    // because _once again_, spans don't really work on stable, so instead we set part of the
    // target to the beginning of the type, part to the end, and then the rust compiler joins
    // them for us. Isn't that a nice?

    let start = span.span();
    let end = span
        .to_token_stream()
        .into_iter()
        .last()
        .map(|t| t.span())
        .unwrap_or(start);
    (start, end)
}