| David Tolnay | 05ef6ff | 2020-08-29 11:27:05 -0700 | [diff] [blame^] | 1 | use proc_macro2::Span; |
| 2 | use syn::parse::{Error, Parse, ParseStream, Result}; |
| 3 | use syn::{braced, token, Attribute, Ident, Item, Token, Visibility}; |
| 4 | |
| 5 | pub struct Module { |
| 6 | pub attrs: Vec<Attribute>, |
| 7 | pub vis: Visibility, |
| 8 | // TODO: unsafety |
| 9 | pub mod_token: Token![mod], |
| 10 | pub ident: Ident, |
| 11 | pub brace_token: token::Brace, |
| 12 | pub content: Vec<Item>, |
| 13 | } |
| 14 | |
| 15 | impl Parse for Module { |
| 16 | fn parse(input: ParseStream) -> Result<Self> { |
| 17 | let mut attrs = input.call(Attribute::parse_outer)?; |
| 18 | let vis: Visibility = input.parse()?; |
| 19 | let mod_token: Token![mod] = input.parse()?; |
| 20 | let ident: Ident = input.parse()?; |
| 21 | |
| 22 | if input.peek(Token![;]) { |
| 23 | return Err(Error::new( |
| 24 | Span::call_site(), |
| 25 | "#[cxx::bridge] module must have inline contents", |
| 26 | ))?; |
| 27 | } |
| 28 | |
| 29 | let content; |
| 30 | let brace_token = braced!(content in input); |
| 31 | attrs.extend(content.call(Attribute::parse_inner)?); |
| 32 | |
| 33 | let mut items = Vec::new(); |
| 34 | while !content.is_empty() { |
| 35 | items.push(content.parse()?); |
| 36 | } |
| 37 | |
| 38 | Ok(Module { |
| 39 | attrs, |
| 40 | vis, |
| 41 | mod_token, |
| 42 | ident, |
| 43 | brace_token, |
| 44 | content: items, |
| 45 | }) |
| 46 | } |
| 47 | } |