1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
//! # Introduction
//!
//! `astmaker` is a DSL for programming language designers to build Abstract
//! Syntax Trees and tree-walking models quickly.
//!
//! # Features
//!
//! AST definition:
//!
//! - custom location type
//! - structural nodes (`struct`) and variant nodes (`enum`)
//! - custom node attributes type
//!
//! Model definition:
//!
//! - visitor pattern
//! - support for generics and lifetimes
//!
//! # Architecture
//!
//! When creating an AST, this crate will define the following types and traits:
//!
//! ```rust
//! pub trait NodeAttributes {
//! type Attributes;
//! }
//!
//! #[derive(Debug, Clone, PartialEq)]
//! pub struct Node<T: NodeAttributes> {
//! pub location: LocationType,
//! pub data: Box<T>,
//! pub attrs: Option<T::NodeAttributes>,
//! }
//! ```
//!
//! When creating a model, this crate will define the following types:
//!
//! ```rust
//! pub trait Visitor: Sized {
//! fn visit<T: NodeAttributes + Visitable<Self, T>>(
//! &mut self,
//! node: &mut Node<T>,
//! ) -> OutputType;
//! }
//!
//! pub trait Visitable<C: Visitor, T: NodeAttributes> {
//! fn visit(context: &mut C, node: &mut Node<T>) -> OutputType;
//! }
//! ```
//!
//! # Basic usage
//!
//! This crates provide 2 macros:
//!
//! - `ast!`: to define the AST
//! - `model!`: to implement the tree-walking model
//!
//! Each macro provide a custom DSL.
//!
//! ## Defining Abstract Syntax Tress
//!
//! ```rust
//! use astmaker::{ast, model};
//!
//! ast!{
//! location = (usize, usize);
//!
//! pub node VariantNode =
//! | A -> Node<StructuralNodeA>
//! | B -> Node<StructuralNodeB>
//! ;
//!
//! pub node StructuralNodeA = {
//! data: u8,
//! }
//!
//! pub node StructuralNodeB = {
//! data: u16,
//! }
//!
//! pub node NodeWithAttributes where attrs: String = {
//! data: u32,
//! }
//! }
//! ```
//!
//! When not specified, the default attributes type is the unit type `()`.
//!
//! The generated code will contain the `struct`s and `enum`s as well as their
//! implementation of the `NodeAttributes` trait.
//!
//! Every generated type implements the traits `Debug`, `Clone` and `PartialEq`.
//!
//! ## Defining tree-walking models
//!
//! ```rust
//! pub struct Model;
//!
//! model!{
//! impl Model -> Result<(), ()> {
//! where VariantNode => {
//! match node.data.as_mut() {
//! VariantNode::A(child) => context.visit(child)?,
//! VariantNode::B(child) => context.visit(child)?,
//! }
//!
//! Ok(())
//! },
//! where StructuralNodeA => {
//! Ok(())
//! },
//! where StructuralNodeB => {
//! Ok(())
//! },
//! }
//! }
//! ```
//!
//! The `impl for Type` part will implement the `Visitor` trait for the supplied
//! type. Each `where` clause will implement the `Visitable` trait for the node
//! type.
//!
//! Generics and lifetimes are also supported:
//!
//! ```rust
//! pub struct Model<'a, T> {
//! data: &'a T,
//! }
//!
//! model!{
//! impl<'a, T> Model -> Result<(), ()> {
//! // ...
//! }
//! }
//! ```
use proc_macro::TokenStream;
mod parser;
mod codegen;
#[proc_macro]
pub fn ast(input: TokenStream) -> TokenStream {
codegen::ast::generate(input)
}
#[proc_macro]
pub fn model(input: TokenStream) -> TokenStream {
codegen::model::generate(input)
}