blob: 25fe4da4b7bf23b0bf273876e80bb1e291d26547 [file] [log] [blame] [view]
David Tolnay35161ff2016-09-03 11:33:15 -07001Nom parser for Rust items
2=========================
3
David Tolnayac9953b2016-09-07 08:37:12 -07004[![Build Status](https://api.travis-ci.org/dtolnay/syn.svg?branch=master)](https://travis-ci.org/dtolnay/syn)
5[![Latest Version](https://img.shields.io/crates/v/syn.svg)](https://crates.io/crates/syn)
David Tolnay50e62202016-09-12 09:49:49 -07006[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://dtolnay.github.io/syn/syn/)
David Tolnayac9953b2016-09-07 08:37:12 -07007
David Tolnay35161ff2016-09-03 11:33:15 -07008Parse Rust structs and enums without a Syntex dependency, intended for use with
9[Macros 1.1](https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md).
10
David Tolnayf939f352016-09-11 18:00:09 -070011Designed for fast compile time.
12
David Tolnayb5a7b142016-09-13 22:46:39 -070013- Compile time for `syn` (from scratch including all dependencies): **4 seconds**
David Tolnayf939f352016-09-11 18:00:09 -070014- Compile time for the `syntex`/`quasi`/`aster` stack: **60+ seconds**
15
David Tolnayed8e23e2016-09-27 08:18:22 -070016## Usage with Macros 1.1
David Tolnay6c9f5b62016-09-13 15:19:22 -070017
David Tolnay69b538e2016-09-23 19:59:48 -070018```toml
19[dependencies]
David Tolnay5a064752016-09-24 09:34:31 -070020syn = "0.7"
David Tolnay69b538e2016-09-23 19:59:48 -070021quote = "0.1"
22
23[lib]
24rustc-macro = true
25```
26
White-Oak82d0db72016-09-13 21:45:58 +030027```rust
David Tolnay6c9f5b62016-09-13 15:19:22 -070028#![feature(rustc_macro, rustc_macro_lib)]
White-Oak82d0db72016-09-13 21:45:58 +030029
30extern crate rustc_macro;
David Tolnayb4c63262016-09-23 20:03:06 -070031use rustc_macro::TokenStream;
32
33extern crate syn;
David Tolnay6c9f5b62016-09-13 15:19:22 -070034
White-Oak82d0db72016-09-13 21:45:58 +030035#[macro_use]
36extern crate quote;
White-Oak82d0db72016-09-13 21:45:58 +030037
David Tolnayb4c63262016-09-23 20:03:06 -070038#[rustc_macro_derive(MyMacro)]
39pub fn my_macro(input: TokenStream) -> TokenStream {
White-Oak82d0db72016-09-13 21:45:58 +030040 let source = input.to_string();
41
42 // Parse a string of items to an AST
David Tolnayf38cdf62016-09-23 19:07:09 -070043 let ast = syn::parse_macro_input(&source).unwrap();
White-Oak82d0db72016-09-13 21:45:58 +030044
David Tolnay6c9f5b62016-09-13 15:19:22 -070045 // Build the output, possibly using quasi-quotation
46 let expanded = quote! {
47 // ...
48 };
49
White-Oak82d0db72016-09-13 21:45:58 +030050 // Parse this back to a token stream and return it
David Tolnay6c9f5b62016-09-13 15:19:22 -070051 expanded.to_string().parse().unwrap()
White-Oak82d0db72016-09-13 21:45:58 +030052}
53```
David Tolnay6c9f5b62016-09-13 15:19:22 -070054
David Tolnay35161ff2016-09-03 11:33:15 -070055## License
56
57Licensed under either of
58
59 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
60 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
61
62at your option.
63
64### Contribution
65
66Unless you explicitly state otherwise, any contribution intentionally submitted
67for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
68be dual licensed as above, without any additional terms or conditions.