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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro]
pub fn _cssparser_internal_max_len(input: TokenStream) -> TokenStream {
    struct Input {
        max_length: usize,
    }

    impl syn::parse::Parse for Input {
        fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
            let mut max_length = 0;
            while !input.is_empty() {
                if input.peek(syn::Token![_]) {
                    input.parse::<syn::Token![_]>().unwrap();
                    continue;
                }
                let lit: syn::LitStr = input.parse()?;
                let value = lit.value();
                if value.to_ascii_lowercase() != value {
                    return Err(syn::Error::new(lit.span(), "must be ASCII-lowercase"));
                }
                max_length = max_length.max(value.len());
            }
            Ok(Input { max_length })
        }
    }

    let Input { max_length } = syn::parse_macro_input!(input);
    quote::quote!(
        pub(super) const MAX_LENGTH: usize = #max_length;
    )
    .into()
}

fn get_byte_from_lit(lit: &syn::Lit) -> u8 {
    if let syn::Lit::Byte(ref byte) = *lit {
        byte.value()
    } else {
        panic!("Found a pattern that wasn't a byte")
    }
}

fn get_byte_from_expr_lit(expr: &syn::Expr) -> u8 {
    match *expr {
        syn::Expr::Lit(syn::ExprLit { ref lit, .. }) => {
            get_byte_from_lit(lit)
        }
        _ => unreachable!(),
    }
}

/// Parse a pattern and fill the table accordingly
fn parse_pat_to_table<'a>(
    pat: &'a syn::Pat,
    case_id: u8,
    wildcard: &mut Option<&'a syn::Ident>,
    table: &mut [u8; 256],
) {
    match pat {
        &syn::Pat::Lit(syn::PatLit { ref lit, .. }) => {
            let value = get_byte_from_lit(lit);
            if table[value as usize] == 0 {
                table[value as usize] = case_id;
            }
        }
        &syn::Pat::Range(syn::PatRange { ref start, ref end, .. }) => {
            let lo = get_byte_from_expr_lit(&start.as_ref().unwrap());
            let hi = get_byte_from_expr_lit(&end.as_ref().unwrap());
            for value in lo..hi {
                if table[value as usize] == 0 {
                    table[value as usize] = case_id;
                }
            }
            if table[hi as usize] == 0 {
                table[hi as usize] = case_id;
            }
        }
        &syn::Pat::Wild(_) => {
            for byte in table.iter_mut() {
                if *byte == 0 {
                    *byte = case_id;
                }
            }
        }
        &syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
            assert_eq!(*wildcard, None);
            *wildcard = Some(ident);
            for byte in table.iter_mut() {
                if *byte == 0 {
                    *byte = case_id;
                }
            }
        }
        &syn::Pat::Or(syn::PatOr { ref cases, .. }) => {
            for case in cases {
                parse_pat_to_table(case, case_id, wildcard, table);
            }
        }
        _ => {
            panic!("Unexpected pattern: {:?}. Buggy code ?", pat);
        }
    }
}

/// Expand a TokenStream corresponding to the `match_byte` macro.
///
/// ## Example
///
/// ```rust
/// match_byte! { tokenizer.next_byte_unchecked(),
///     b'a'..b'z' => { ... }
///     b'0'..b'9' => { ... }
///     b'\n' | b'\\' => { ... }
///     foo => { ... }
///  }
///  ```
///
#[proc_macro]
pub fn match_byte(input: TokenStream) -> TokenStream {
    use syn::spanned::Spanned;
    struct MatchByte {
        expr: syn::Expr,
        arms: Vec<syn::Arm>,
    }

    impl syn::parse::Parse for MatchByte {
        fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
            Ok(MatchByte {
                expr: {
                    let expr = input.parse()?;
                    input.parse::<syn::Token![,]>()?;
                    expr
                },
                arms: {
                    let mut arms = Vec::new();
                    while !input.is_empty() {
                        let arm = input.call(syn::Arm::parse)?;
                        assert!(arm.guard.is_none(), "match_byte doesn't support guards");
                        assert!(
                            arm.attrs.is_empty(),
                            "match_byte doesn't support attributes"
                        );
                        arms.push(arm);
                    }
                    arms
                },
            })
        }
    }
    let MatchByte { expr, arms } = syn::parse_macro_input!(input);

    let mut cases = Vec::new();
    let mut table = [0u8; 256];
    let mut match_body = Vec::new();
    let mut wildcard = None;
    for (i, ref arm) in arms.iter().enumerate() {
        let case_id = i + 1;
        let index = case_id as isize;
        let name = syn::Ident::new(&format!("Case{}", case_id), arm.span());
        let pat = &arm.pat;
        parse_pat_to_table(pat, case_id as u8, &mut wildcard, &mut table);

        cases.push(quote::quote!(#name = #index));
        let body = &arm.body;
        match_body.push(quote::quote!(Case::#name => { #body }))
    }

    let en = quote::quote!(enum Case {
        #(#cases),*
    });

    let mut table_content = Vec::new();
    for entry in table.iter() {
        let name: syn::Path = syn::parse_str(&format!("Case::Case{}", entry)).unwrap();
        table_content.push(name);
    }
    let table = quote::quote!(static __CASES: [Case; 256] = [#(#table_content),*];);

    if let Some(binding) = wildcard {
        quote::quote!({ #en #table let #binding = #expr; match __CASES[#binding as usize] { #(#match_body),* }})
    } else {
        quote::quote!({ #en #table match __CASES[#expr as usize] { #(#match_body),* }})
    }.into()
}