rsx proc macro
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "osui-macros"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = { version = "2", features = ["full"] }
|
||||
quote = "1"
|
||||
proc-macro2 = "1"
|
||||
@@ -0,0 +1,98 @@
|
||||
use crate::parse::*;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{ToTokens, quote};
|
||||
|
||||
pub fn emit_rsx(root: RsxRoot) -> TokenStream {
|
||||
let nodes = root.nodes.iter().map(emit_node);
|
||||
|
||||
quote! {{
|
||||
let mut r = osui::frontend::Rsx::new();
|
||||
#(#nodes)*
|
||||
r
|
||||
}}
|
||||
}
|
||||
|
||||
fn emit_deps(deps: &[Dep]) -> TokenStream {
|
||||
deps.iter()
|
||||
.map(|d| {
|
||||
let ident = &d.ident;
|
||||
if let Some(pat) = &d.pat {
|
||||
quote!( let #pat = #ident.clone(); )
|
||||
} else {
|
||||
quote!( let #ident = #ident.clone(); )
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn emit_node(node: &RsxNode) -> TokenStream {
|
||||
match node {
|
||||
RsxNode::Text(text) => quote! {
|
||||
{
|
||||
let scope = osui::component::Scope::new();
|
||||
scope.view(Arc::new(move |ctx| {
|
||||
ctx.draw_text(Point { x: 0, y: 0 }, &format!(#text))
|
||||
}));
|
||||
r.static_scope(scope);
|
||||
}
|
||||
},
|
||||
|
||||
RsxNode::Component(name) => quote! {
|
||||
{
|
||||
let scope = osui::component::Scope::new();
|
||||
scope.child(#name, None);
|
||||
r.static_scope(scope);
|
||||
}
|
||||
},
|
||||
|
||||
RsxNode::Mount(m) => quote! {
|
||||
#m.mount();
|
||||
},
|
||||
|
||||
RsxNode::If {
|
||||
deps,
|
||||
cond,
|
||||
children,
|
||||
} => {
|
||||
let deps_emit = emit_deps(deps);
|
||||
let kids = children.iter().map(emit_node);
|
||||
|
||||
quote! {
|
||||
{
|
||||
#deps_emit
|
||||
r.dynamic_scope(move |scope| {
|
||||
if #cond {
|
||||
if scope.children.lock().unwrap().is_empty() {
|
||||
#(#kids)*
|
||||
}
|
||||
} else {
|
||||
scope.children.lock().unwrap().clear();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RsxNode::For {
|
||||
deps,
|
||||
pat,
|
||||
expr,
|
||||
children,
|
||||
} => {
|
||||
let deps_emit = emit_deps(deps);
|
||||
let kids = children.iter().map(emit_node);
|
||||
|
||||
quote! {
|
||||
{
|
||||
#deps_emit
|
||||
r.dynamic_scope(move |scope| {
|
||||
scope.children.lock().unwrap().clear();
|
||||
for #pat in #expr {
|
||||
#(#kids)*
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
use proc_macro::TokenStream;
|
||||
|
||||
mod emit;
|
||||
mod parse;
|
||||
|
||||
#[proc_macro]
|
||||
pub fn rsx(input: TokenStream) -> TokenStream {
|
||||
let ast = syn::parse_macro_input!(input as parse::RsxRoot);
|
||||
emit::emit_rsx(ast).into()
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
use syn::braced;
|
||||
use syn::{
|
||||
Expr, Ident, LitStr, Pat, Result, Token,
|
||||
parse::{Parse, ParseStream},
|
||||
};
|
||||
|
||||
pub struct RsxRoot {
|
||||
pub nodes: Vec<RsxNode>,
|
||||
}
|
||||
|
||||
impl Parse for RsxRoot {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
let mut nodes = Vec::new();
|
||||
while !input.is_empty() {
|
||||
nodes.push(input.parse()?);
|
||||
}
|
||||
Ok(Self { nodes })
|
||||
}
|
||||
}
|
||||
|
||||
pub enum RsxNode {
|
||||
Text(LitStr),
|
||||
Component(Ident),
|
||||
Mount(Ident),
|
||||
If {
|
||||
deps: Vec<Dep>,
|
||||
cond: Expr,
|
||||
children: Vec<RsxNode>,
|
||||
},
|
||||
For {
|
||||
deps: Vec<Dep>,
|
||||
pat: Pat,
|
||||
expr: Expr,
|
||||
children: Vec<RsxNode>,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct Dep {
|
||||
pub ident: Ident,
|
||||
pub pat: Option<Pat>,
|
||||
}
|
||||
|
||||
fn parse_deps(input: ParseStream) -> Result<Vec<Dep>> {
|
||||
let mut deps = Vec::new();
|
||||
|
||||
if input.peek(Token![%]) {
|
||||
input.parse::<Token![%]>()?;
|
||||
loop {
|
||||
let ident: Ident = input.parse()?;
|
||||
let pat = if input.peek(Token![as]) {
|
||||
input.parse::<Token![as]>()?;
|
||||
// Patterns in syn 2.0 are parsed via `Pat::parse_multi`
|
||||
// instead of implementing the `Parse` trait directly.
|
||||
Some(Pat::parse_multi(input)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
deps.push(Dep { ident, pat });
|
||||
|
||||
if !input.peek(Token![,]) {
|
||||
break;
|
||||
}
|
||||
input.parse::<Token![,]>()?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(deps)
|
||||
}
|
||||
|
||||
impl Parse for RsxNode {
|
||||
fn parse(input: ParseStream) -> Result<Self> {
|
||||
if input.peek(Token![!]) {
|
||||
input.parse::<Token![!]>()?;
|
||||
let mount: Ident = input.parse()?;
|
||||
return Ok(RsxNode::Mount(mount));
|
||||
}
|
||||
|
||||
let deps = parse_deps(input)?;
|
||||
|
||||
if input.peek(Token![if]) {
|
||||
input.parse::<Token![if]>()?;
|
||||
let cond: Expr = input.parse()?;
|
||||
|
||||
let content;
|
||||
braced!(content in input);
|
||||
|
||||
let children = parse_children(&content)?;
|
||||
return Ok(RsxNode::If {
|
||||
deps,
|
||||
cond,
|
||||
children,
|
||||
});
|
||||
}
|
||||
|
||||
if input.peek(Token![for]) {
|
||||
input.parse::<Token![for]>()?;
|
||||
// Parse a possibly multi-pattern (e.g. with `|`) for the `for` binding.
|
||||
let pat: Pat = Pat::parse_multi(input)?;
|
||||
input.parse::<Token![in]>()?;
|
||||
let expr: Expr = input.parse()?;
|
||||
|
||||
let content;
|
||||
braced!(content in input);
|
||||
|
||||
let children = parse_children(&content)?;
|
||||
return Ok(RsxNode::For {
|
||||
deps,
|
||||
pat,
|
||||
expr,
|
||||
children,
|
||||
});
|
||||
}
|
||||
|
||||
if input.peek(LitStr) {
|
||||
return Ok(RsxNode::Text(input.parse()?));
|
||||
}
|
||||
|
||||
Ok(RsxNode::Component(input.parse()?))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_children(input: ParseStream) -> Result<Vec<RsxNode>> {
|
||||
let mut nodes = Vec::new();
|
||||
while !input.is_empty() {
|
||||
nodes.push(input.parse()?);
|
||||
}
|
||||
Ok(nodes)
|
||||
}
|
||||
Reference in New Issue
Block a user