blob: c0582ba63d3e34ba865deb31dc3bdd4150e58b45 [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
White-Oak82d0db72016-09-13 21:45:58 +030040```rust
41// lib.rs
42#![crate_type = "rustc-macro"]
David Tolnay6c9f5b62016-09-13 15:19:22 -070043#![feature(rustc_macro, rustc_macro_lib)]
White-Oak82d0db72016-09-13 21:45:58 +030044
45extern crate rustc_macro;
David Tolnay6c9f5b62016-09-13 15:19:22 -070046
White-Oak82d0db72016-09-13 21:45:58 +030047#[macro_use]
48extern crate quote;
49extern crate syn;
50
51use rustc_macro::TokenStream;
52
53#[rustc_macro_derive(SpecialItem)]
54pub fn special_item(input: TokenStream) -> TokenStream {
55 let source = input.to_string();
56
57 // Parse a string of items to an AST
David Tolnayf38cdf62016-09-23 19:07:09 -070058 let ast = syn::parse_macro_input(&source).unwrap();
White-Oak82d0db72016-09-13 21:45:58 +030059
David Tolnay6c9f5b62016-09-13 15:19:22 -070060 // Build the output, possibly using quasi-quotation
61 let expanded = quote! {
62 // ...
63 };
64
White-Oak82d0db72016-09-13 21:45:58 +030065 // Parse this back to a token stream and return it
David Tolnay6c9f5b62016-09-13 15:19:22 -070066 expanded.to_string().parse().unwrap()
White-Oak82d0db72016-09-13 21:45:58 +030067}
68```
David Tolnay6c9f5b62016-09-13 15:19:22 -070069
White-Oak82d0db72016-09-13 21:45:58 +030070```toml
71# Cargo.toml
72# ...
73[dependencies]
74syn = "0.5"
75quote = "0.1"
76
77[lib]
78rustc-macro = true
79```
80
David Tolnay35161ff2016-09-03 11:33:15 -070081## License
82
83Licensed under either of
84
85 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
86 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
87
88at your option.
89
90### Contribution
91
92Unless you explicitly state otherwise, any contribution intentionally submitted
93for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
94be dual licensed as above, without any additional terms or conditions.