|
| 1 | +use syn::{ |
| 2 | + parse::{Parse, ParseStream}, |
| 3 | + punctuated::Punctuated, |
| 4 | + token, FieldsNamed, Ident, LitStr, Token, Type, Visibility, |
| 5 | +}; |
| 6 | + |
| 7 | +mod kw { |
| 8 | + use syn::custom_keyword; |
| 9 | + |
| 10 | + custom_keyword!(extends); |
| 11 | + pub type Extends = extends; |
| 12 | + |
| 13 | + custom_keyword!(implements); |
| 14 | + pub type Implements = implements; |
| 15 | +} |
| 16 | + |
| 17 | +pub use kw::{Extends, Implements}; |
| 18 | + |
| 19 | +pub struct ClassInput { |
| 20 | + pub package: LitStr, |
| 21 | + pub extends: Option<(Extends, Type)>, |
| 22 | + pub implements_token: Option<Implements>, |
| 23 | + pub implements: Punctuated<Type, Token![,]>, |
| 24 | +} |
| 25 | + |
| 26 | +impl Parse for ClassInput { |
| 27 | + fn parse(input: ParseStream) -> syn::Result<Self> { |
| 28 | + let package = input.parse()?; |
| 29 | + let extends = if let Ok(extends) = input.parse() { |
| 30 | + let ty = input.parse()?; |
| 31 | + Some((extends, ty)) |
| 32 | + } else { |
| 33 | + None |
| 34 | + }; |
| 35 | + |
| 36 | + let implements_token = input.parse().ok(); |
| 37 | + let implements = if implements_token.is_some() { |
| 38 | + input.parse_terminated(Type::parse, Token![,])? |
| 39 | + } else { |
| 40 | + Punctuated::new() |
| 41 | + }; |
| 42 | + |
| 43 | + Ok(Self { |
| 44 | + package, |
| 45 | + extends, |
| 46 | + implements_token, |
| 47 | + implements, |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +pub struct ClassBody { |
| 53 | + pub vis: Visibility, |
| 54 | + pub struct_token: Token![struct], |
| 55 | + pub ident: Ident, |
| 56 | + pub fields: Option<FieldsNamed>, |
| 57 | +} |
| 58 | + |
| 59 | +impl Parse for ClassBody { |
| 60 | + fn parse(input: ParseStream) -> syn::Result<Self> { |
| 61 | + let vis = input.parse()?; |
| 62 | + let struct_token = input.parse()?; |
| 63 | + let ident = input.parse()?; |
| 64 | + let fields = if input.peek(token::Brace) { |
| 65 | + Some(input.parse()?) |
| 66 | + } else { |
| 67 | + input.parse::<Token![;]>()?; |
| 68 | + None |
| 69 | + }; |
| 70 | + |
| 71 | + Ok(Self { |
| 72 | + vis, |
| 73 | + struct_token, |
| 74 | + ident, |
| 75 | + fields, |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments