blob: 30e3d8940a9565dc5a719be82f5fd141aae7252c [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 Tolnay35161ff2016-09-03 11:33:15 -070016```toml
17[dependencies]
David Tolnayab09d462016-09-23 19:27:54 -070018syn = "0.6"
David Tolnay35161ff2016-09-03 11:33:15 -070019```
20
21```rust
David Tolnaya0976082016-09-07 08:24:28 -070022extern crate syn;
David Tolnay35161ff2016-09-03 11:33:15 -070023
24let raw = "
David Tolnay99ef8c92016-09-04 11:31:37 -070025 #[derive(Debug, Clone, Eq, PartialEq)]
David Tolnay35161ff2016-09-03 11:33:15 -070026 pub struct Item {
27 pub ident: Ident,
David Tolnay99ef8c92016-09-04 11:31:37 -070028 pub vis: Visibility,
David Tolnay35161ff2016-09-03 11:33:15 -070029 pub attrs: Vec<Attribute>,
David Tolnay99ef8c92016-09-04 11:31:37 -070030 pub generics: Generics,
31 pub body: Body,
David Tolnay35161ff2016-09-03 11:33:15 -070032 }
33";
34
David Tolnayf38cdf62016-09-23 19:07:09 -070035let ast = syn::parse_macro_input(raw).unwrap();
David Tolnay35161ff2016-09-03 11:33:15 -070036```
37
White-Oak82d0db72016-09-13 21:45:58 +030038## Usage with [Macros 1.1](https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md)
David Tolnay6c9f5b62016-09-13 15:19:22 -070039
David Tolnay69b538e2016-09-23 19:59:48 -070040```toml
41[dependencies]
42syn = "0.6"
43quote = "0.1"
44
45[lib]
46rustc-macro = true
47```
48
White-Oak82d0db72016-09-13 21:45:58 +030049```rust
David Tolnay6c9f5b62016-09-13 15:19:22 -070050#![feature(rustc_macro, rustc_macro_lib)]
White-Oak82d0db72016-09-13 21:45:58 +030051
52extern crate rustc_macro;
David Tolnayb4c63262016-09-23 20:03:06 -070053use rustc_macro::TokenStream;
54
55extern crate syn;
David Tolnay6c9f5b62016-09-13 15:19:22 -070056
White-Oak82d0db72016-09-13 21:45:58 +030057#[macro_use]
58extern crate quote;
White-Oak82d0db72016-09-13 21:45:58 +030059
David Tolnayb4c63262016-09-23 20:03:06 -070060#[rustc_macro_derive(MyMacro)]
61pub fn my_macro(input: TokenStream) -> TokenStream {
White-Oak82d0db72016-09-13 21:45:58 +030062 let source = input.to_string();
63
64 // Parse a string of items to an AST
David Tolnayf38cdf62016-09-23 19:07:09 -070065 let ast = syn::parse_macro_input(&source).unwrap();
White-Oak82d0db72016-09-13 21:45:58 +030066
David Tolnay6c9f5b62016-09-13 15:19:22 -070067 // Build the output, possibly using quasi-quotation
68 let expanded = quote! {
69 // ...
70 };
71
White-Oak82d0db72016-09-13 21:45:58 +030072 // Parse this back to a token stream and return it
David Tolnay6c9f5b62016-09-13 15:19:22 -070073 expanded.to_string().parse().unwrap()
White-Oak82d0db72016-09-13 21:45:58 +030074}
75```
David Tolnay6c9f5b62016-09-13 15:19:22 -070076
David Tolnay35161ff2016-09-03 11:33:15 -070077## License
78
79Licensed under either of
80
81 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
82 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
83
84at your option.
85
86### Contribution
87
88Unless you explicitly state otherwise, any contribution intentionally submitted
89for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
90be dual licensed as above, without any additional terms or conditions.