12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- extern crate proc_macro;
- use proc_macro::TokenStream;
- use quote::ToTokens;
- use syn::parse_macro_input;
- mod parsing;
- use parsing::Protocol;
- mod error;
- mod generation;
- mod validation;
- macro_rules! unwrap_or_compile_err {
- ($result:expr) => {
- match $result {
- Ok(value) => value,
- Err(err) => return err.into_compile_error().into(),
- }
- };
- }
- /// Generates types for the actors participating in a messaging protocol.
- ///
- /// ## Usage Restrictions
- /// A type named `Result` which accepts a single type parameter must be in scope where you call this
- /// macro. This allows you to define the error type used by the generated traits by defining a
- /// type alias:
- /// ```
- /// struct MyError;
- /// type Result<T> = std::result::Result<T, MyError>;
- /// ```
- ///
- /// You must also ensure all message types referenced by the protocol are in scope.
- ///
- /// ## Grammar
- /// The grammar recognized by this macro is given below in the dialect of Extended Backus-Naur Form
- /// recognized by the `llgen` tool. The terminal symbol `Ident` has the same meaning as it does in
- /// the regular Rust syntax.
- ///
- /// ```ebnf
- /// protocol : name_def states_def ( transition ';' )* ;
- /// name_def : "let" "name" '=' Ident ';' ;
- /// states_def : "let" "states" '=' ident_array ';' ;
- /// ident_array : '[' Ident ( ',' Ident )* ','? ']' ;
- /// transition : state ( '?' message )? "->" states_list ( '>' dest_list )? ;
- /// state : Ident ident_array? ;
- /// states_list : state ( ',' state )* ','? ;
- /// dest_list : dest ( ',' dest )* ;
- /// dest : dest_state '!' message
- /// dest_state : ( "service" '(' Ident ')' ) | state ;
- /// message : Ident ( "::" "Reply" )? ident_array? ;
- /// ```
- #[proc_macro]
- pub fn protocol(input: TokenStream) -> TokenStream {
- let input = parse_macro_input!(input as Protocol);
- unwrap_or_compile_err!(input.validate());
- input.to_token_stream().into()
- }
|