mirror of
https://gitee.com/openharmony/third_party_rust_syn
synced 2025-02-18 14:40:40 +00:00
Nom parser for Rust items
Parse Rust structs and enums without a Syntex dependency, intended for use with Macros 1.1.
Designed for fast compile time.
- Compile time for
syn
(from scratch including all dependencies): 4 seconds - Compile time for the
syntex
/quasi
/aster
stack: 60+ seconds
Usage with Macros 1.1
[dependencies]
syn = "0.8"
quote = "0.2"
[lib]
rustc-macro = true
#![feature(rustc_macro, rustc_macro_lib)]
extern crate rustc_macro;
use rustc_macro::TokenStream;
extern crate syn;
#[macro_use]
extern crate quote;
#[rustc_macro_derive(MyMacro)]
pub fn my_macro(input: TokenStream) -> TokenStream {
let source = input.to_string();
// Parse a string of items to an AST
let ast = syn::parse_macro_input(&source).unwrap();
// Build the output, possibly using quasi-quotation
let expanded = quote! {
// ...
};
// Parse this back to a token stream and return it
expanded.to_string().parse().unwrap()
}
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Description
Languages
Rust
100%