Skip to main content

openinfer_dsl/
lib.rs

1//! Procedural macro DSL for building OpenInfer graphs.
2//!
3//! The `graph!` macro parses a compact DSL into `openinfer::Graph` structures.
4//! It is intended for ergonomics in tests and examples.
5//!
6//! ## DSL structure
7//! - Memory sections: `dynamic`, `volatile`, `constant`, `persistent`
8//! - Blocks: `block entry { ... }`
9//! - Nodes: `assign`, `op`, `branch`, `loop`, `yield`, `await`, cache ops
10//!
11//! ## Expansion
12//! The macro expands into Rust code that constructs `Graph` values at runtime.
13//!
14//! ## Example
15//! ```ignore
16//! use openinfer::graph;
17//! let g = graph! {
18//!     dynamic { x: f32[B]; }
19//!     block entry {
20//!         return;
21//!     }
22//! };
23//! ```
24use proc_macro::TokenStream;
25
26mod attributes;
27mod codegen;
28mod parsers;
29mod types;
30mod validation;
31
32mod kw {
33    syn::custom_keyword!(dynamic);
34    syn::custom_keyword!(volatile);
35    syn::custom_keyword!(constant);
36    syn::custom_keyword!(persistent);
37    syn::custom_keyword!(block);
38    syn::custom_keyword!(assign);
39    syn::custom_keyword!(op);
40    syn::custom_keyword!(branch);
41    syn::custom_keyword!(barrier);
42    syn::custom_keyword!(dep);
43    syn::custom_keyword!(after);
44    syn::custom_keyword!(before);
45    syn::custom_keyword!(transfer);
46    syn::custom_keyword!(cache);
47    syn::custom_keyword!(read);
48    syn::custom_keyword!(write);
49    syn::custom_keyword!(increment);
50    syn::custom_keyword!(decrement);
51    syn::custom_keyword!(reset);
52    syn::custom_keyword!(init);
53    syn::custom_keyword!(pattern);
54    syn::custom_keyword!(table);
55    syn::custom_keyword!(fixed);
56    syn::custom_keyword!(auto_dim);
57}
58
59use crate::types::GraphDsl;
60
61/// Build an OpenInfer `Graph` from the DSL input.
62#[proc_macro]
63pub fn graph(input: TokenStream) -> TokenStream {
64    let ast = syn::parse_macro_input!(input as GraphDsl);
65    match ast.expand() {
66        Ok(ts) => ts,
67        Err(err) => err.to_compile_error().into(),
68    }
69}
70
71#[cfg(test)]
72mod parse_tests;