rsx proc macro

This commit is contained in:
2026-01-28 15:21:45 +01:00
parent 011b1ab963
commit 46587ff822
7 changed files with 255 additions and 167 deletions
+4
View File
@@ -25,6 +25,7 @@ cc = "1.0"
[dependencies] [dependencies]
crossterm = "0.28.1" crossterm = "0.28.1"
figlet-rs = "0.1.5" figlet-rs = "0.1.5"
osui-macros = { path = "macros" }
[profile.dev] [profile.dev]
opt-level = 1 opt-level = 1
@@ -42,3 +43,6 @@ overflow-checks = false
lto = true lto = true
panic = 'abort' panic = 'abort'
codegen-units = 1 codegen-units = 1
[workspace]
members = [".", "macros"]
+13
View File
@@ -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"
+98
View File
@@ -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)*
}
});
}
}
}
}
}
+10
View File
@@ -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()
}
+129
View File
@@ -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)
}
+1 -2
View File
@@ -5,7 +5,6 @@ use crate::render::DrawContext;
pub mod component; pub mod component;
pub mod engine; pub mod engine;
pub mod frontend; pub mod frontend;
pub mod macros;
pub mod render; pub mod render;
pub mod state; pub mod state;
@@ -13,9 +12,9 @@ pub mod prelude {
pub use crate::component::*; pub use crate::component::*;
pub use crate::engine::*; pub use crate::engine::*;
pub use crate::render::*; pub use crate::render::*;
pub use crate::rsx;
pub use crate::state::*; pub use crate::state::*;
pub use crate::View; pub use crate::View;
pub use osui_macros::rsx;
} }
pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>; pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>;
-165
View File
@@ -1,165 +0,0 @@
#[macro_export]
macro_rules! rsx {
() => {
$crate::frontend::Rsx::new()
};
($($rsx:tt)+) => {{
let mut r = $crate::frontend::Rsx::new();
{
$crate::rsx_scope!(r, $($rsx)+);
}
r
}};
}
#[macro_export]
macro_rules! rsx_scope {
($rsx:expr, for $(%$($dep:ident $(as $dp:pat,)?),+)? ($p:pat in $v:expr) {$($inner:tt)*} $($rest:tt)*) => {
{
$($($crate::rsx_dep!($dep);)+)?
$rsx.dynamic_scope({
$($($crate::rsx_dep!($dep $(as $dp)?);)+)?
move |scope| {
scope.children.lock().unwrap().clear();
for $p in $v {
$crate::rsx_child!(scope, $($inner)*);
}
}
}, vec![$($(Box::new($dep)),+)?]);
}
$crate::rsx_scope!($rsx, $($rest)*);
};
($rsx:expr, if $(%$($dep:ident $(as $dp:pat,)?),+)? ($st:expr) {$($inner:tt)*} $($rest:tt)*) => {
{
$($($crate::rsx_dep!($dep);)+)?
$rsx.dynamic_scope({
$($($crate::rsx_dep!($dep $(as $dp)?);)+)?
move |scope| {
if $st {
if scope.children.lock().unwrap().len() == 0 {
$crate::rsx_child!(scope, $($inner)*);
}
} else {
scope.children.lock().unwrap().clear();
}
}
}, vec![$($(Box::new($dep)),+)?]);
}
$crate::rsx_scope!($rsx, $($rest)*);
};
($rsx:expr, $(%$($dep:ident $(as $dp:pat,)?),+)? $text:literal @$d:expr; $($rest:tt)*) => {
{
let scope = $crate::component::Scope::new();
$($($crate::rsx_dep!($dep $(as $dp)?);)+)?
$crate::rsx_child!(scope, $text @$d;);
$rsx.static_scope(scope);
}
$crate::rsx_scope!($rsx, $($rest)*);
};
($rsx:expr, $(%$($dep:ident $(as $dp:pat,)?),+)? $text:literal $($rest:tt)*) => {
{
let scope = $crate::component::Scope::new();
$($($crate::rsx_dep!($dep $(as $dp)?);)+)?
$crate::rsx_child!(scope, $text);
$rsx.static_scope(scope);
}
$crate::rsx_scope!($rsx, $($rest)*);
};
($rsx:expr, $component:ident ($ctx:ident, $view:ident) $body:block $($rest:tt)*) => {
{
let scope = $crate::component::Scope::new();
$crate::rsx_child!(scope, $component ($ctx, $view) $body);
$rsx.static_scope(scope);
}
$crate::rsx_scope!($rsx, $($rest)*);
};
($rsx:expr, $component:ident $($rest:tt)*) => {
{
let scope = $crate::component::Scope::new();
$crate::rsx_child!(scope, $component);
$rsx.static_scope(scope);
}
$crate::rsx_scope!($rsx, $($rest)*);
};
($rsx:expr, !mount $mount:ident $($rest:tt)*) => {
{
$mount.mount();
}
$crate::rsx_scope!($rsx, $($rest)*);
};
($rsx:expr,) => {};
}
#[macro_export]
macro_rules! rsx_child {
($scope:expr, $text:literal @$d:expr; $($rest:tt)*) => {
{
$scope.view(Arc::new(move |ctx| ctx.draw_text($d, &format!($text))));
}
$crate::rsx_child!($scope, $($rest)*);
};
($scope:expr, $text:literal $($rest:tt)*) => {
{
$scope.view(Arc::new(move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!($text))));
}
$crate::rsx_child!($scope, $($rest)*);
};
($scope:expr, $component:ident ($ctx:ident, $view:ident) $body:block $($rest:tt)*) => {
{
$scope.child($component, Some(Arc::new(|$ctx, $view| $body)));
}
$crate::rsx_child!($scope, $($rest)*);
};
($scope:expr, $component:ident $($rest:tt)*) => {
{
$scope.child($component, None);
}
$crate::rsx_child!($scope, $($rest)*);
};
($rsx:expr,) => {};
}
#[macro_export]
macro_rules! rsx_dep {
($dep:ident as $dp:pat) => {
let $dp = $dep.clone();
};
($dep:ident) => {
let $dep = $dep.clone();
};
}