diff options
| author | Nick Cameron <ncameron@mozilla.com> | 2016-10-21 20:55:39 +1300 |
|---|---|---|
| committer | Nick Cameron <ncameron@mozilla.com> | 2016-10-28 12:17:17 +1300 |
| commit | 15821caee9b6f6eecbf8e405c7ee3d6278b932ca (patch) | |
| tree | 982c57daf203891b28b7f8a741025094fb2fb9e7 /src/libproc_macro_plugin | |
| parent | 3a25b65c1fbdd6101b77e8a8b06a5e42d775dc3f (diff) | |
| download | rust-15821caee9b6f6eecbf8e405c7ee3d6278b932ca.tar.gz rust-15821caee9b6f6eecbf8e405c7ee3d6278b932ca.zip | |
Split up libproc_macro_plugin
Separate the plugin code from non-plugin code to break a potential cycle in crates. This will allow us to merge the new libproc_macro_tokens into libproc_macro.
Diffstat (limited to 'src/libproc_macro_plugin')
| -rw-r--r-- | src/libproc_macro_plugin/Cargo.toml | 1 | ||||
| -rw-r--r-- | src/libproc_macro_plugin/build.rs | 89 | ||||
| -rw-r--r-- | src/libproc_macro_plugin/lib.rs | 42 | ||||
| -rw-r--r-- | src/libproc_macro_plugin/parse.rs | 26 | ||||
| -rw-r--r-- | src/libproc_macro_plugin/prelude.rs | 12 | ||||
| -rw-r--r-- | src/libproc_macro_plugin/qquote.rs | 18 |
6 files changed, 13 insertions, 175 deletions
diff --git a/src/libproc_macro_plugin/Cargo.toml b/src/libproc_macro_plugin/Cargo.toml index 70bb86d0f58..4bc3f488d32 100644 --- a/src/libproc_macro_plugin/Cargo.toml +++ b/src/libproc_macro_plugin/Cargo.toml @@ -12,3 +12,4 @@ log = { path = "../liblog" } rustc_plugin = { path = "../librustc_plugin" } syntax = { path = "../libsyntax" } syntax_pos = { path = "../libsyntax_pos" } +proc_macro_tokens = { path = "../libproc_macro_tokens" } diff --git a/src/libproc_macro_plugin/build.rs b/src/libproc_macro_plugin/build.rs deleted file mode 100644 index 7b7590b863b..00000000000 --- a/src/libproc_macro_plugin/build.rs +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2016 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -extern crate syntax; -extern crate syntax_pos; - -use syntax::ast::Ident; -use syntax::codemap::DUMMY_SP; -use syntax::parse::token::{self, Token, keywords, str_to_ident}; -use syntax::tokenstream::{self, TokenTree, TokenStream}; -use std::rc::Rc; - -/// A wrapper around `TokenStream::concat` to avoid extra namespace specification and -/// provide TokenStream concatenation as a generic operator. -pub fn concat(ts1: TokenStream, ts2: TokenStream) -> TokenStream { - TokenStream::concat(ts1, ts2) -} - -/// Checks if two identifiers have the same name, disregarding context. This allows us to -/// fake 'reserved' keywords. -// FIXME We really want `free-identifier-=?` (a la Dybvig 1993). von Tander 2007 is -// probably the easiest way to do that. -pub fn ident_eq(tident: &TokenTree, id: Ident) -> bool { - let tid = match *tident { - TokenTree::Token(_, Token::Ident(ref id)) => id, - _ => { - return false; - } - }; - - tid.name == id.name -} - -// ____________________________________________________________________________________________ -// Conversion operators - -/// Convert a `&str` into a Token. -pub fn str_to_token_ident(s: &str) -> Token { - Token::Ident(str_to_ident(s)) -} - -/// Converts a keyword (from `syntax::parse::token::keywords`) into a Token that -/// corresponds to it. -pub fn keyword_to_token_ident(kw: keywords::Keyword) -> Token { - Token::Ident(str_to_ident(&kw.name().as_str()[..])) -} - -// ____________________________________________________________________________________________ -// Build Procedures - -/// Generically takes a `ts` and delimiter and returns `ts` delimited by the specified -/// delimiter. -pub fn build_delimited(ts: TokenStream, delim: token::DelimToken) -> TokenStream { - let tts = ts.to_tts(); - TokenStream::from_tts(vec![TokenTree::Delimited(DUMMY_SP, - Rc::new(tokenstream::Delimited { - delim: delim, - open_span: DUMMY_SP, - tts: tts, - close_span: DUMMY_SP, - }))]) -} - -/// Takes `ts` and returns `[ts]`. -pub fn build_bracket_delimited(ts: TokenStream) -> TokenStream { - build_delimited(ts, token::DelimToken::Bracket) -} - -/// Takes `ts` and returns `{ts}`. -pub fn build_brace_delimited(ts: TokenStream) -> TokenStream { - build_delimited(ts, token::DelimToken::Brace) -} - -/// Takes `ts` and returns `(ts)`. -pub fn build_paren_delimited(ts: TokenStream) -> TokenStream { - build_delimited(ts, token::DelimToken::Paren) -} - -/// Constructs `()`. -pub fn build_empty_args() -> TokenStream { - build_paren_delimited(TokenStream::mk_empty()) -} diff --git a/src/libproc_macro_plugin/lib.rs b/src/libproc_macro_plugin/lib.rs index e82e97b5134..c45762bfb6e 100644 --- a/src/libproc_macro_plugin/lib.rs +++ b/src/libproc_macro_plugin/lib.rs @@ -13,43 +13,14 @@ //! A library for procedural macro writers. //! //! ## Usage -//! This package provides the `qquote!` macro for syntax creation, and the prelude -//! (at libproc_macro::prelude) provides a number of operations: -//! - `concat`, for concatenating two TokenStreams. -//! - `ident_eq`, for checking if two identifiers are equal regardless of syntax context. -//! - `str_to_token_ident`, for converting an `&str` into a Token. -//! - `keyword_to_token_delim`, for converting a `parse::token::keywords::Keyword` into a -//! Token. -//! - `build_delimited`, for creating a new TokenStream from an existing one and a delimiter -//! by wrapping the TokenStream in the delimiter. -//! - `build_bracket_delimited`, `build_brace_delimited`, and `build_paren_delimited`, for -//! easing the above. -//! - `build_empty_args`, which returns a TokenStream containing `()`. -//! - `lex`, which takes an `&str` and returns the TokenStream it represents. -//! -//! The `qquote!` macro also imports `syntax::ext::proc_macro_shim::prelude::*`, so you +//! This crate provides the `qquote!` macro for syntax creation. +//! +//! The `qquote!` macro imports `syntax::ext::proc_macro_shim::prelude::*`, so you //! will need to `extern crate syntax` for usage. (This is a temporary solution until more -//! of the external API in libproc_macro is stabilized to support the token construction +//! of the external API in libproc_macro_tokens is stabilized to support the token construction //! operations that the qausiquoter relies on.) The shim file also provides additional //! operations, such as `build_block_emitter` (as used in the `cond` example below). //! -//! ## TokenStreams -//! -//! TokenStreams serve as the basis of the macro system. They are, in essence, vectors of -//! TokenTrees, where indexing treats delimited values as a single term. That is, the term -//! `even(a+c) && even(b)` will be indexibly encoded as `even | (a+c) | even | (b)` where, -//! in reality, `(a+c)` is actually a decorated pointer to `a | + | c`. -//! -//! If a user has a TokenStream that is a single, delimited value, they can use -//! `maybe_delimited` to destruct it and receive the internal vector as a new TokenStream -//! as: -//! ``` -//! `(a+c)`.maybe_delimited() ~> Some(a | + | c)` -//! ``` -//! -//! Check the TokenStream documentation for more information; the structure also provides -//! cheap concatenation and slicing. -//! //! ## Quasiquotation //! //! The quasiquoter creates output that, when run, constructs the tokenstream specified as @@ -118,12 +89,11 @@ extern crate rustc_plugin; extern crate syntax; extern crate syntax_pos; +extern crate proc_macro_tokens; #[macro_use] extern crate log; mod qquote; -pub mod build; -pub mod parse; -pub mod prelude; + use qquote::qquote; use rustc_plugin::Registry; diff --git a/src/libproc_macro_plugin/parse.rs b/src/libproc_macro_plugin/parse.rs deleted file mode 100644 index 9af8a68cdcf..00000000000 --- a/src/libproc_macro_plugin/parse.rs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2016 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! Parsing utilities for writing procedural macros. - -extern crate syntax; - -use syntax::parse::{ParseSess, filemap_to_tts}; -use syntax::tokenstream::TokenStream; - -/// Map a string to tts, using a made-up filename. For example, `lex(15)` will return a -/// TokenStream containing the literal 15. -pub fn lex(source_str: &str) -> TokenStream { - let ps = ParseSess::new(); - TokenStream::from_tts(filemap_to_tts(&ps, - ps.codemap().new_filemap("procmacro_lex".to_string(), - None, - source_str.to_owned()))) -} diff --git a/src/libproc_macro_plugin/prelude.rs b/src/libproc_macro_plugin/prelude.rs deleted file mode 100644 index 4c0c8ba6c66..00000000000 --- a/src/libproc_macro_plugin/prelude.rs +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2016 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -pub use build::*; -pub use parse::*; diff --git a/src/libproc_macro_plugin/qquote.rs b/src/libproc_macro_plugin/qquote.rs index b73d085656e..e5a3abc2ea9 100644 --- a/src/libproc_macro_plugin/qquote.rs +++ b/src/libproc_macro_plugin/qquote.rs @@ -24,12 +24,9 @@ //! TokenStream that resembles the output syntax. //! -extern crate rustc_plugin; -extern crate syntax; -extern crate syntax_pos; +use proc_macro_tokens::build::*; +use proc_macro_tokens::parse::lex; -use build::*; -use parse::lex; use qquote::int_build::*; use syntax::ast::Ident; @@ -51,7 +48,7 @@ pub fn qquote<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[TokenTree]) let output = qquoter(cx, TokenStream::from_tts(tts.clone().to_owned())); debug!("\nQQ out: {}\n", pprust::tts_to_string(&output.to_tts()[..])); let imports = concat(lex("use syntax::ext::proc_macro_shim::prelude::*;"), - lex("use proc_macro_plugin::prelude::*;")); + lex("use proc_macro_tokens::prelude::*;")); build_block_emitter(cx, sp, build_brace_delimited(concat(imports, output))) } @@ -219,7 +216,7 @@ fn convert_complex_tts<'cx>(cx: &'cx mut ExtCtxt, tts: Vec<QTT>) -> (Bindings, T let sep = build_delim_tok(qdl.delim); - pushes.push(build_mod_call(vec![str_to_ident("proc_macro_plugin"), + pushes.push(build_mod_call(vec![str_to_ident("proc_macro_tokens"), str_to_ident("build"), str_to_ident("build_delimited")], concat(from_tokens(vec![Token::Ident(new_id)]), @@ -264,11 +261,8 @@ fn is_qquote(id: Ident) -> bool { } mod int_build { - extern crate syntax; - extern crate syntax_pos; - - use parse::*; - use build::*; + use proc_macro_tokens::build::*; + use proc_macro_tokens::parse::*; use syntax::ast::{self, Ident}; use syntax::codemap::{DUMMY_SP}; |
