Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nrc/proc-macro-rules
Macro-rules-style syntax matching for procedural macros
https://github.com/nrc/proc-macro-rules
macros parsing pattern-matching rust
Last synced: 1 day ago
JSON representation
Macro-rules-style syntax matching for procedural macros
- Host: GitHub
- URL: https://github.com/nrc/proc-macro-rules
- Owner: nrc
- Created: 2019-01-02T01:16:20.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-11-07T03:12:12.000Z (about 1 year ago)
- Last Synced: 2024-11-29T03:51:32.710Z (27 days ago)
- Topics: macros, parsing, pattern-matching, rust
- Language: Rust
- Homepage:
- Size: 43.9 KB
- Stars: 51
- Watchers: 4
- Forks: 5
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-rust-list - nrc/proc-macro-rules - macro-rules?style=social"/> : Macro-rules-style syntax matching for procedural macros. (Summary)
- awesome-rust-list - nrc/proc-macro-rules - macro-rules?style=social"/> : Macro-rules-style syntax matching for procedural macros. (Summary)
README
# proc-macro-rules
`macro_rules`-style syntax matching for procedural macros.
This crate is work-in-progress, incomplete, and probably buggy!
Example:
```rust
rules!(tokens => {
($finish:ident ($($found:ident)*) # [ $($inner:tt)* ] $($rest:tt)*) => {
for f in found {
do_something(finish, f, inner, rest[0]);
}
}
(foo $($bar:expr)?) => {
match bar {
Some(e) => foo_with_expr(e),
None => foo_no_expr(),
}
}
});
```## Using proc-macro-rules
Add `proc-macro-rules = "0.3.0"` (or `proc-macro-rules = "0.2.1"` for versions between 1.31 and 1.56) to your Cargo.toml.
Import the `rules` macro with `use proc_macro_rules::rules`, then use with `rules!(tokens => { branches });` where `tokens` is an expression which evaluates to a `TokenStream` (such as the argument in the definition of a procedural macro).
Each branch in `branches` should have the form `( pattern ) => { body }` where `pattern` is a macro-rules-style pattern (using all the same syntax for meta-variables, AST nodes, repetition, etc.) and `body` is rust code executed when the pattern is matched. Within `body`, any meta-variables in the pattern are bound to variables of an appropriate type from either the [proc_macro2](https://github.com/alexcrichton/proc-macro2) or [syn](https://github.com/dtolnay/syn) crates. Where a meta-variable is inside a repetition or option clause, it will be wrapped in a `Vec` or `Option`, respectively.
For example, in the first branch in the above example `ident` has type `syn::Ident` and `inner` has type `Vec`.
## Building and testing
Use `cargo build` to build, `cargo test --all` to test.
## Contributing
Contributions are very welcome! It would be great to know things which are missing or incorrect (in general we should have the same behaviour as `macro_rules`, so anything different is incorrect). Issues, code, docs, tests, and corrections are all welcome.