blob: 5366752a62e93afed96bbb37f7b6549e2273691a [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
White-Oak82d0db72016-09-13 21:45:58 +030050#![crate_type = "rustc-macro"]
David Tolnay6c9f5b62016-09-13 15:19:22 -070051#![feature(rustc_macro, rustc_macro_lib)]
White-Oak82d0db72016-09-13 21:45:58 +030052
53extern crate rustc_macro;
David Tolnay6c9f5b62016-09-13 15:19:22 -070054
White-Oak82d0db72016-09-13 21:45:58 +030055#[macro_use]
56extern crate quote;
57extern crate syn;
58
59use rustc_macro::TokenStream;
60
61#[rustc_macro_derive(SpecialItem)]
62pub fn special_item(input: TokenStream) -> TokenStream {
63 let source = input.to_string();
64
65 // Parse a string of items to an AST
David Tolnayf38cdf62016-09-23 19:07:09 -070066 let ast = syn::parse_macro_input(&source).unwrap();
White-Oak82d0db72016-09-13 21:45:58 +030067
David Tolnay6c9f5b62016-09-13 15:19:22 -070068 // Build the output, possibly using quasi-quotation
69 let expanded = quote! {
70 // ...
71 };
72
White-Oak82d0db72016-09-13 21:45:58 +030073 // Parse this back to a token stream and return it
David Tolnay6c9f5b62016-09-13 15:19:22 -070074 expanded.to_string().parse().unwrap()
White-Oak82d0db72016-09-13 21:45:58 +030075}
76```
David Tolnay6c9f5b62016-09-13 15:19:22 -070077
David Tolnay35161ff2016-09-03 11:33:15 -070078## License
79
80Licensed under either of
81
82 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
83 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
84
85at your option.
86
87### Contribution
88
89Unless you explicitly state otherwise, any contribution intentionally submitted
90for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
91be dual licensed as above, without any additional terms or conditions.