From ce3636798bc9481ec712b84b5cad9973b7844425 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 31 Jan 2019 13:40:05 +0300 Subject: move macros to a separate crate --- Cargo.lock | 8 ++ crates/ra_hir/Cargo.toml | 1 + crates/ra_hir/src/macros.rs | 6 +- crates/ra_hir/src/macros/mbe.rs | 249 ---------------------------------------- crates/ra_hir/src/macros/tt.rs | 45 -------- crates/ra_macros/Cargo.toml | 8 ++ crates/ra_macros/src/lib.rs | 14 +++ crates/ra_macros/src/mbe.rs | 249 ++++++++++++++++++++++++++++++++++++++++ crates/ra_macros/src/tt.rs | 45 ++++++++ 9 files changed, 326 insertions(+), 299 deletions(-) delete mode 100644 crates/ra_hir/src/macros/mbe.rs delete mode 100644 crates/ra_hir/src/macros/tt.rs create mode 100644 crates/ra_macros/Cargo.toml create mode 100644 crates/ra_macros/src/lib.rs create mode 100644 crates/ra_macros/src/mbe.rs create mode 100644 crates/ra_macros/src/tt.rs diff --git a/Cargo.lock b/Cargo.lock index 568d1aeef..b6367bf30 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 57a4b155b..f0988acc8 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 dc016a704..7e9aba3f2 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_hir/src/macros/mbe.rs b/crates/ra_hir/src/macros/mbe.rs deleted file mode 100644 index 6c93ae5e3..000000000 --- a/crates/ra_hir/src/macros/mbe.rs +++ /dev/null @@ -1,249 +0,0 @@ -use ra_syntax::SmolStr; - -use crate::macros::tt::{self, Delimiter}; - -#[derive(Debug)] -pub(crate) struct MacroRules { - rules: Vec, -} - -#[derive(Debug)] -struct Rule { - lhs: Subtree, - rhs: Subtree, -} - -#[derive(Debug)] -enum TokenTree { - Leaf(Leaf), - Subtree(Subtree), - Repeat(Repeat), -} -impl_froms!(TokenTree: Leaf, Subtree, Repeat); - -#[derive(Debug)] -enum Leaf { - Literal(Literal), - Punct(Punct), - Ident(Ident), - Var(Var), -} -impl_froms!(Leaf: Literal, Punct, Ident, Var); - -#[derive(Debug)] -struct Subtree { - delimiter: Delimiter, - token_trees: Vec, -} - -#[derive(Debug)] -struct Repeat { - subtree: Subtree, - kind: RepeatKind, - separator: Option, -} - -#[derive(Debug)] -enum RepeatKind { - ZeroOrMore, - OneOrMore, - ZeroOrOne, -} - -#[derive(Debug)] -struct Literal { - text: SmolStr, -} - -#[derive(Debug)] -struct Punct { - char: char, -} - -#[derive(Debug)] -struct Ident { - text: SmolStr, -} - -#[derive(Debug)] -struct Var { - text: SmolStr, - kind: Option, -} - -pub(crate) fn parse(tt: &tt::Subtree) -> Option { - let mut parser = RulesParser::new(tt); - let mut rules = Vec::new(); - while !parser.is_eof() { - rules.push(parse_rule(&mut parser)?) - } - Some(MacroRules { rules }) -} - -fn parse_rule(p: &mut RulesParser) -> Option { - let lhs = parse_subtree(p.eat_subtree()?)?; - p.expect_char('=')?; - p.expect_char('>')?; - let rhs = parse_subtree(p.eat_subtree()?)?; - Some(Rule { lhs, rhs }) -} - -fn parse_subtree(tt: &tt::Subtree) -> Option { - let mut token_trees = Vec::new(); - let mut p = RulesParser::new(tt); - while let Some(tt) = p.eat() { - let child: TokenTree = match tt { - tt::TokenTree::Leaf(leaf) => match leaf { - tt::Leaf::Punct(tt::Punct { char: '$' }) => { - if p.at_ident().is_some() { - Leaf::from(parse_var(&mut p)?).into() - } else { - parse_repeat(&mut p)?.into() - } - } - tt::Leaf::Punct(tt::Punct { char }) => Leaf::from(Punct { char: *char }).into(), - tt::Leaf::Ident(tt::Ident { text }) => { - Leaf::from(Ident { text: text.clone() }).into() - } - tt::Leaf::Literal(tt::Literal { text }) => { - Leaf::from(Literal { text: text.clone() }).into() - } - }, - tt::TokenTree::Subtree(subtree) => parse_subtree(subtree)?.into(), - }; - token_trees.push(child); - } - Some(Subtree { - token_trees, - delimiter: tt.delimiter, - }) -} - -fn parse_var(p: &mut RulesParser) -> Option { - let ident = p.eat_ident().unwrap(); - let text = ident.text.clone(); - let kind = if p.at_char(':') { - p.bump(); - if let Some(ident) = p.eat_ident() { - Some(ident.text.clone()) - } else { - // ugly as hell :( - p.pos -= 1; - None - } - } else { - None - }; - Some(Var { text, kind }) -} - -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 (separator, rep) = match sep.char { - '*' | '+' | '?' => (None, sep.char), - char => (Some(Punct { char }), p.eat_punct()?.char), - }; - - let kind = match rep { - '*' => RepeatKind::ZeroOrMore, - '+' => RepeatKind::OneOrMore, - '?' => RepeatKind::ZeroOrMore, - _ => return None, - }; - p.bump(); - Some(Repeat { - subtree, - kind, - separator, - }) -} - -struct RulesParser<'a> { - subtree: &'a tt::Subtree, - pos: usize, -} - -impl<'a> RulesParser<'a> { - fn new(subtree: &'a tt::Subtree) -> RulesParser<'a> { - RulesParser { subtree, pos: 0 } - } - - fn is_eof(&self) -> bool { - self.pos == self.subtree.token_trees.len() - } - - fn current(&self) -> Option<&'a tt::TokenTree> { - self.subtree.token_trees.get(self.pos) - } - - fn at_punct(&self) -> Option<&'a tt::Punct> { - match self.current() { - Some(tt::TokenTree::Leaf(tt::Leaf::Punct(it))) => Some(it), - _ => None, - } - } - - fn at_char(&self, char: char) -> bool { - match self.at_punct() { - Some(tt::Punct { char: c }) if *c == char => true, - _ => false, - } - } - - fn at_ident(&mut self) -> Option<&'a tt::Ident> { - match self.current() { - Some(tt::TokenTree::Leaf(tt::Leaf::Ident(i))) => Some(i), - _ => None, - } - } - - fn bump(&mut self) { - self.pos += 1; - } - - fn eat(&mut self) -> Option<&'a tt::TokenTree> { - match self.current() { - Some(it) => { - self.bump(); - Some(it) - } - None => None, - } - } - - fn eat_subtree(&mut self) -> Option<&'a tt::Subtree> { - match self.current()? { - tt::TokenTree::Subtree(sub) => { - self.bump(); - Some(sub) - } - _ => return None, - } - } - - fn eat_punct(&mut self) -> Option<&'a tt::Punct> { - if let Some(it) = self.at_punct() { - self.bump(); - return Some(it); - } - None - } - - fn eat_ident(&mut self) -> Option<&'a tt::Ident> { - if let Some(i) = self.at_ident() { - self.bump(); - return Some(i); - } - None - } - - fn expect_char(&mut self, char: char) -> Option<()> { - if self.at_char(char) { - self.bump(); - return Some(()); - } - None - } -} diff --git a/crates/ra_hir/src/macros/tt.rs b/crates/ra_hir/src/macros/tt.rs deleted file mode 100644 index 64e88ddc5..000000000 --- a/crates/ra_hir/src/macros/tt.rs +++ /dev/null @@ -1,45 +0,0 @@ -use ra_syntax::SmolStr; - -#[derive(Debug)] -pub(crate) enum TokenTree { - Leaf(Leaf), - Subtree(Subtree), -} -impl_froms!(TokenTree: Leaf, Subtree); - -#[derive(Debug)] -pub(crate) enum Leaf { - Literal(Literal), - Punct(Punct), - Ident(Ident), -} -impl_froms!(Leaf: Literal, Punct, Ident); - -#[derive(Debug)] -pub(crate) struct Subtree { - pub(crate) delimiter: Delimiter, - pub(crate) token_trees: Vec, -} - -#[derive(Clone, Copy, Debug)] -pub(crate) enum Delimiter { - Parenthesis, - Brace, - Bracket, - None, -} - -#[derive(Debug)] -pub(crate) struct Literal { - pub(crate) text: SmolStr, -} - -#[derive(Debug)] -pub(crate) struct Punct { - pub(crate) char: char, -} - -#[derive(Debug)] -pub(crate) struct Ident { - pub(crate) text: SmolStr, -} diff --git a/crates/ra_macros/Cargo.toml b/crates/ra_macros/Cargo.toml new file mode 100644 index 000000000..b4fdbfd18 --- /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 000000000..6063c06c2 --- /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_macros/src/mbe.rs b/crates/ra_macros/src/mbe.rs new file mode 100644 index 000000000..1aa6be736 --- /dev/null +++ b/crates/ra_macros/src/mbe.rs @@ -0,0 +1,249 @@ +use smol_str::SmolStr; + +use crate::tt::{self, Delimiter}; + +#[derive(Debug)] +pub struct MacroRules { + rules: Vec, +} + +#[derive(Debug)] +struct Rule { + lhs: Subtree, + rhs: Subtree, +} + +#[derive(Debug)] +enum TokenTree { + Leaf(Leaf), + Subtree(Subtree), + Repeat(Repeat), +} +impl_froms!(TokenTree: Leaf, Subtree, Repeat); + +#[derive(Debug)] +enum Leaf { + Literal(Literal), + Punct(Punct), + Ident(Ident), + Var(Var), +} +impl_froms!(Leaf: Literal, Punct, Ident, Var); + +#[derive(Debug)] +struct Subtree { + delimiter: Delimiter, + token_trees: Vec, +} + +#[derive(Debug)] +struct Repeat { + subtree: Subtree, + kind: RepeatKind, + separator: Option, +} + +#[derive(Debug)] +enum RepeatKind { + ZeroOrMore, + OneOrMore, + ZeroOrOne, +} + +#[derive(Debug)] +struct Literal { + text: SmolStr, +} + +#[derive(Debug)] +struct Punct { + char: char, +} + +#[derive(Debug)] +struct Ident { + text: SmolStr, +} + +#[derive(Debug)] +struct Var { + text: SmolStr, + kind: Option, +} + +pub fn parse(tt: &tt::Subtree) -> Option { + let mut parser = RulesParser::new(tt); + let mut rules = Vec::new(); + while !parser.is_eof() { + rules.push(parse_rule(&mut parser)?) + } + Some(MacroRules { rules }) +} + +fn parse_rule(p: &mut RulesParser) -> Option { + let lhs = parse_subtree(p.eat_subtree()?)?; + p.expect_char('=')?; + p.expect_char('>')?; + let rhs = parse_subtree(p.eat_subtree()?)?; + Some(Rule { lhs, rhs }) +} + +fn parse_subtree(tt: &tt::Subtree) -> Option { + let mut token_trees = Vec::new(); + let mut p = RulesParser::new(tt); + while let Some(tt) = p.eat() { + let child: TokenTree = match tt { + tt::TokenTree::Leaf(leaf) => match leaf { + tt::Leaf::Punct(tt::Punct { char: '$' }) => { + if p.at_ident().is_some() { + Leaf::from(parse_var(&mut p)?).into() + } else { + parse_repeat(&mut p)?.into() + } + } + tt::Leaf::Punct(tt::Punct { char }) => Leaf::from(Punct { char: *char }).into(), + tt::Leaf::Ident(tt::Ident { text }) => { + Leaf::from(Ident { text: text.clone() }).into() + } + tt::Leaf::Literal(tt::Literal { text }) => { + Leaf::from(Literal { text: text.clone() }).into() + } + }, + tt::TokenTree::Subtree(subtree) => parse_subtree(subtree)?.into(), + }; + token_trees.push(child); + } + Some(Subtree { + token_trees, + delimiter: tt.delimiter, + }) +} + +fn parse_var(p: &mut RulesParser) -> Option { + let ident = p.eat_ident().unwrap(); + let text = ident.text.clone(); + let kind = if p.at_char(':') { + p.bump(); + if let Some(ident) = p.eat_ident() { + Some(ident.text.clone()) + } else { + // ugly as hell :( + p.pos -= 1; + None + } + } else { + None + }; + Some(Var { text, kind }) +} + +fn parse_repeat(p: &mut RulesParser) -> Option { + let subtree = p.eat_subtree().unwrap(); + let subtree = parse_subtree(subtree)?; + let sep = p.eat_punct()?; + let (separator, rep) = match sep.char { + '*' | '+' | '?' => (None, sep.char), + char => (Some(Punct { char }), p.eat_punct()?.char), + }; + + let kind = match rep { + '*' => RepeatKind::ZeroOrMore, + '+' => RepeatKind::OneOrMore, + '?' => RepeatKind::ZeroOrMore, + _ => return None, + }; + p.bump(); + Some(Repeat { + subtree, + kind, + separator, + }) +} + +struct RulesParser<'a> { + subtree: &'a tt::Subtree, + pos: usize, +} + +impl<'a> RulesParser<'a> { + fn new(subtree: &'a tt::Subtree) -> RulesParser<'a> { + RulesParser { subtree, pos: 0 } + } + + fn is_eof(&self) -> bool { + self.pos == self.subtree.token_trees.len() + } + + fn current(&self) -> Option<&'a tt::TokenTree> { + self.subtree.token_trees.get(self.pos) + } + + fn at_punct(&self) -> Option<&'a tt::Punct> { + match self.current() { + Some(tt::TokenTree::Leaf(tt::Leaf::Punct(it))) => Some(it), + _ => None, + } + } + + fn at_char(&self, char: char) -> bool { + match self.at_punct() { + Some(tt::Punct { char: c }) if *c == char => true, + _ => false, + } + } + + fn at_ident(&mut self) -> Option<&'a tt::Ident> { + match self.current() { + Some(tt::TokenTree::Leaf(tt::Leaf::Ident(i))) => Some(i), + _ => None, + } + } + + fn bump(&mut self) { + self.pos += 1; + } + + fn eat(&mut self) -> Option<&'a tt::TokenTree> { + match self.current() { + Some(it) => { + self.bump(); + Some(it) + } + None => None, + } + } + + fn eat_subtree(&mut self) -> Option<&'a tt::Subtree> { + match self.current()? { + tt::TokenTree::Subtree(sub) => { + self.bump(); + Some(sub) + } + _ => return None, + } + } + + fn eat_punct(&mut self) -> Option<&'a tt::Punct> { + if let Some(it) = self.at_punct() { + self.bump(); + return Some(it); + } + None + } + + fn eat_ident(&mut self) -> Option<&'a tt::Ident> { + if let Some(i) = self.at_ident() { + self.bump(); + return Some(i); + } + None + } + + fn expect_char(&mut self, char: char) -> Option<()> { + if self.at_char(char) { + self.bump(); + return Some(()); + } + None + } +} diff --git a/crates/ra_macros/src/tt.rs b/crates/ra_macros/src/tt.rs new file mode 100644 index 000000000..364eed9e6 --- /dev/null +++ b/crates/ra_macros/src/tt.rs @@ -0,0 +1,45 @@ +use smol_str::SmolStr; + +#[derive(Debug)] +pub enum TokenTree { + Leaf(Leaf), + Subtree(Subtree), +} +impl_froms!(TokenTree: Leaf, Subtree); + +#[derive(Debug)] +pub enum Leaf { + Literal(Literal), + Punct(Punct), + Ident(Ident), +} +impl_froms!(Leaf: Literal, Punct, Ident); + +#[derive(Debug)] +pub struct Subtree { + pub delimiter: Delimiter, + pub token_trees: Vec, +} + +#[derive(Clone, Copy, Debug)] +pub enum Delimiter { + Parenthesis, + Brace, + Bracket, + None, +} + +#[derive(Debug)] +pub struct Literal { + pub text: SmolStr, +} + +#[derive(Debug)] +pub struct Punct { + pub char: char, +} + +#[derive(Debug)] +pub struct Ident { + pub text: SmolStr, +} -- cgit v1.2.3