From ce3636798bc9481ec712b84b5cad9973b7844425 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 31 Jan 2019 13:40:05 +0300 Subject: [PATCH] move macros to a separate crate --- Cargo.lock | 8 ++++++ crates/ra_hir/Cargo.toml | 1 + crates/ra_hir/src/macros.rs | 6 +---- crates/ra_macros/Cargo.toml | 8 ++++++ crates/ra_macros/src/lib.rs | 14 ++++++++++ .../src/macros => ra_macros/src}/mbe.rs | 10 +++---- .../src/macros => ra_macros/src}/tt.rs | 26 +++++++++---------- 7 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 crates/ra_macros/Cargo.toml create mode 100644 crates/ra_macros/src/lib.rs rename crates/{ra_hir/src/macros => ra_macros/src}/mbe.rs (96%) rename crates/{ra_hir/src/macros => ra_macros/src}/tt.rs (50%) diff --git a/Cargo.lock b/Cargo.lock index 568d1aeef1..b6367bf303 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -940,6 +940,7 @@ dependencies = [ "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "ra_arena 0.1.0", "ra_db 0.1.0", + "ra_macros 0.1.0", "ra_syntax 0.1.0", "relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1017,6 +1018,13 @@ dependencies = [ "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ra_macros" +version = "0.1.0" +dependencies = [ + "smol_str 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ra_syntax" version = "0.1.0" diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml index 57a4b155bb..f0988acc80 100644 --- a/crates/ra_hir/Cargo.toml +++ b/crates/ra_hir/Cargo.toml @@ -16,6 +16,7 @@ join_to_string = "0.1.3" ra_syntax = { path = "../ra_syntax" } ra_arena = { path = "../ra_arena" } ra_db = { path = "../ra_db" } +ra_macros = { path = "../ra_macros" } test_utils = { path = "../test_utils" } [dev-dependencies] diff --git a/crates/ra_hir/src/macros.rs b/crates/ra_hir/src/macros.rs index dc016a704c..7e9aba3f26 100644 --- a/crates/ra_hir/src/macros.rs +++ b/crates/ra_hir/src/macros.rs @@ -1,8 +1,3 @@ -#[allow(unused)] -mod tt; -#[allow(unused)] -mod mbe; - /// Machinery for macro expansion. /// /// One of the more complicated things about macros is managing the source code @@ -19,6 +14,7 @@ use ra_syntax::{ SyntaxKind::*, ast::{self, NameOwner}, }; +use ra_macros::{tt, mbe}; use crate::{HirDatabase, MacroCallId}; diff --git a/crates/ra_macros/Cargo.toml b/crates/ra_macros/Cargo.toml new file mode 100644 index 0000000000..b4fdbfd184 --- /dev/null +++ b/crates/ra_macros/Cargo.toml @@ -0,0 +1,8 @@ +[package] +edition = "2018" +name = "ra_macros" +version = "0.1.0" +authors = ["Aleksey Kladov "] + +[dependencies] +smol_str = "0.1.9" diff --git a/crates/ra_macros/src/lib.rs b/crates/ra_macros/src/lib.rs new file mode 100644 index 0000000000..6063c06c20 --- /dev/null +++ b/crates/ra_macros/src/lib.rs @@ -0,0 +1,14 @@ +macro_rules! impl_froms { + ($e:ident: $($v:ident), *) => { + $( + impl From<$v> for $e { + fn from(it: $v) -> $e { + $e::$v(it) + } + } + )* + } +} + +pub mod tt; +pub mod mbe; diff --git a/crates/ra_hir/src/macros/mbe.rs b/crates/ra_macros/src/mbe.rs similarity index 96% rename from crates/ra_hir/src/macros/mbe.rs rename to crates/ra_macros/src/mbe.rs index 6c93ae5e30..1aa6be7364 100644 --- a/crates/ra_hir/src/macros/mbe.rs +++ b/crates/ra_macros/src/mbe.rs @@ -1,9 +1,9 @@ -use ra_syntax::SmolStr; +use smol_str::SmolStr; -use crate::macros::tt::{self, Delimiter}; +use crate::tt::{self, Delimiter}; #[derive(Debug)] -pub(crate) struct MacroRules { +pub struct MacroRules { rules: Vec, } @@ -71,7 +71,7 @@ struct Var { kind: Option, } -pub(crate) fn parse(tt: &tt::Subtree) -> Option { +pub fn parse(tt: &tt::Subtree) -> Option { let mut parser = RulesParser::new(tt); let mut rules = Vec::new(); while !parser.is_eof() { @@ -140,7 +140,7 @@ fn parse_var(p: &mut RulesParser) -> Option { fn parse_repeat(p: &mut RulesParser) -> Option { let subtree = p.eat_subtree().unwrap(); let subtree = parse_subtree(subtree)?; - let mut sep = p.eat_punct()?; + let sep = p.eat_punct()?; let (separator, rep) = match sep.char { '*' | '+' | '?' => (None, sep.char), char => (Some(Punct { char }), p.eat_punct()?.char), diff --git a/crates/ra_hir/src/macros/tt.rs b/crates/ra_macros/src/tt.rs similarity index 50% rename from crates/ra_hir/src/macros/tt.rs rename to crates/ra_macros/src/tt.rs index 64e88ddc55..364eed9e65 100644 --- a/crates/ra_hir/src/macros/tt.rs +++ b/crates/ra_macros/src/tt.rs @@ -1,14 +1,14 @@ -use ra_syntax::SmolStr; +use smol_str::SmolStr; #[derive(Debug)] -pub(crate) enum TokenTree { +pub enum TokenTree { Leaf(Leaf), Subtree(Subtree), } impl_froms!(TokenTree: Leaf, Subtree); #[derive(Debug)] -pub(crate) enum Leaf { +pub enum Leaf { Literal(Literal), Punct(Punct), Ident(Ident), @@ -16,13 +16,13 @@ pub(crate) enum Leaf { impl_froms!(Leaf: Literal, Punct, Ident); #[derive(Debug)] -pub(crate) struct Subtree { - pub(crate) delimiter: Delimiter, - pub(crate) token_trees: Vec, +pub struct Subtree { + pub delimiter: Delimiter, + pub token_trees: Vec, } #[derive(Clone, Copy, Debug)] -pub(crate) enum Delimiter { +pub enum Delimiter { Parenthesis, Brace, Bracket, @@ -30,16 +30,16 @@ pub(crate) enum Delimiter { } #[derive(Debug)] -pub(crate) struct Literal { - pub(crate) text: SmolStr, +pub struct Literal { + pub text: SmolStr, } #[derive(Debug)] -pub(crate) struct Punct { - pub(crate) char: char, +pub struct Punct { + pub char: char, } #[derive(Debug)] -pub(crate) struct Ident { - pub(crate) text: SmolStr, +pub struct Ident { + pub text: SmolStr, }