blob: 76f54fcdef8f6d620d5b8f48c5ed9102f35da4d7 [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
13- Compile time for `syn` (from scratch including all dependencies): **6 seconds**
14- Compile time for the `syntex`/`quasi`/`aster` stack: **60+ seconds**
15
David Tolnay35161ff2016-09-03 11:33:15 -070016```toml
17[dependencies]
David Tolnay7b3973e2016-09-11 17:46:52 -070018syn = "0.5"
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 Tolnayb0a2c3a2016-09-11 17:48:11 -070035let ast = syn::parse_item(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)
39```rust
40// lib.rs
41#![crate_type = "rustc-macro"]
42#![feature(rustc_macro)]
43#![feature(rustc_macro_lib)]
44
45extern crate rustc_macro;
46#[macro_use]
47extern crate quote;
48extern crate syn;
49
50use rustc_macro::TokenStream;
51
52#[rustc_macro_derive(SpecialItem)]
53pub fn special_item(input: TokenStream) -> TokenStream {
54 let source = input.to_string();
55
56 // Parse a string of items to an AST
57 let ast = syn::parse_item(&source).unwrap();
58
59 // Parse this back to a token stream and return it
60 quote!(#ast).to_string().parse().unwrap()
61}
62```
63```toml
64# Cargo.toml
65# ...
66[dependencies]
67syn = "0.5"
68quote = "0.1"
69
70[lib]
71rustc-macro = true
72```
73
David Tolnay35161ff2016-09-03 11:33:15 -070074## License
75
76Licensed under either of
77
78 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
79 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
80
81at your option.
82
83### Contribution
84
85Unless you explicitly state otherwise, any contribution intentionally submitted
86for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
87be dual licensed as above, without any additional terms or conditions.