use crate::kw;
use proc_macro2::TokenStream;
use syn::parse::{Parse, ParseStream};
use syn::token::{Brace, Colon, Eq, Semi};
use syn::{Expr, Ident, Type, braced};
#[allow(dead_code)]
pub struct LayerBlock {
pub key: kw::layer,
pub brace_token: Brace,
pub contents: Vec<LayerStmt>,
pub eos: Option<Semi>,
}
#[allow(dead_code)]
pub struct LayerStmt {
pub key: Ident,
pub colon_token: Colon,
pub type_token: Type,
pub eq_token: Eq,
pub layer: LayerExpr,
pub eos: Semi,
}
#[allow(dead_code)]
pub enum LayerExpr {
Expr(Expr),
Named(Ident),
Raw(TokenStream),
}
impl Parse for LayerExpr {
fn parse(input: ParseStream) -> syn::Result<Self> {
if input.peek(Ident) {
let key = input.parse::<Ident>()?;
return Ok(LayerExpr::Named(key));
}
Ok(Self::Raw(input.parse()?))
}
}
impl Parse for LayerStmt {
fn parse(input: ParseStream) -> syn::Result<Self> {
let key = input.parse::<Ident>()?;
let colon_token = input.parse::<Colon>()?;
let type_token = input.parse::<Type>()?;
let eq_token = input.parse::<Eq>()?;
let layer = input.parse::<LayerExpr>()?;
let eos = input.parse::<Semi>()?;
Ok(LayerStmt {
key,
colon_token,
type_token,
eq_token,
layer,
eos,
})
}
}
impl Parse for LayerBlock {
fn parse(input: ParseStream) -> syn::Result<Self> {
let key = input.parse::<kw::layer>()?;
let mut contents = Vec::new();
let content; let brace_token = braced!(content in input); while !content.is_empty() {
let stmt: LayerStmt = content.parse()?;
contents.push(stmt);
}
let eos = if content.peek(Semi) {
Some(content.parse::<Semi>()?)
} else {
None
};
Ok(LayerBlock {
key,
brace_token,
contents,
eos,
})
}
}