rust-analyzer/crates/ra_mbe/src/lib.rs

272 lines
7.3 KiB
Rust
Raw Normal View History

2019-01-31 18:29:04 +00:00
/// `mbe` (short for Macro By Example) crate contains code for handling
/// `macro_rules` macros. It uses `TokenTree` (from `ra_tt` package) as the
/// interface, although it contains some code to bridge `SyntaxNode`s and
/// `TokenTree`s as well!
2019-01-31 18:09:43 +00:00
macro_rules! impl_froms {
($e:ident: $($v:ident), *) => {
$(
impl From<$v> for $e {
fn from(it: $v) -> $e {
$e::$v(it)
}
}
)*
}
}
mod tt_cursor;
mod mbe_parser;
mod mbe_expander;
2019-01-31 18:29:04 +00:00
mod syntax_bridge;
2019-01-31 18:09:43 +00:00
2019-01-31 18:29:04 +00:00
use ra_syntax::SmolStr;
2019-01-30 20:17:32 +00:00
2019-01-31 18:09:43 +00:00
pub use tt::{Delimiter, Punct};
2019-01-30 20:25:02 +00:00
2019-01-31 19:14:28 +00:00
pub use crate::syntax_bridge::ast_to_token_tree;
2019-01-31 10:46:40 +00:00
2019-02-11 16:07:49 +00:00
/// This struct contains AST for a single `macro_rules` definition. What might
2019-01-31 19:14:28 +00:00
/// be very confusing is that AST has almost exactly the same shape as
/// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident`
/// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:40:05 +00:00
pub struct MacroRules {
2019-01-31 10:46:40 +00:00
pub(crate) rules: Vec<Rule>,
2019-01-30 20:17:32 +00:00
}
2019-01-31 19:14:28 +00:00
impl MacroRules {
pub fn parse(tt: &tt::Subtree) -> Option<MacroRules> {
mbe_parser::parse(tt)
}
pub fn expand(&self, tt: &tt::Subtree) -> Option<tt::Subtree> {
mbe_expander::exapnd(self, tt)
}
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Rule {
pub(crate) lhs: Subtree,
pub(crate) rhs: Subtree,
2019-01-30 20:17:32 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) enum TokenTree {
2019-01-30 20:17:32 +00:00
Leaf(Leaf),
Subtree(Subtree),
2019-01-31 07:33:11 +00:00
Repeat(Repeat),
2019-01-30 20:17:32 +00:00
}
2019-01-31 10:32:40 +00:00
impl_froms!(TokenTree: Leaf, Subtree, Repeat);
2019-01-30 20:17:32 +00:00
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) enum Leaf {
2019-01-30 20:17:32 +00:00
Literal(Literal),
Punct(Punct),
Ident(Ident),
Var(Var),
}
2019-01-31 10:32:40 +00:00
impl_froms!(Leaf: Literal, Punct, Ident, Var);
2019-01-30 20:17:32 +00:00
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Subtree {
pub(crate) delimiter: Delimiter,
pub(crate) token_trees: Vec<TokenTree>,
2019-01-30 20:17:32 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Repeat {
pub(crate) subtree: Subtree,
pub(crate) kind: RepeatKind,
2019-01-31 15:51:17 +00:00
pub(crate) separator: Option<char>,
2019-01-31 07:33:11 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) enum RepeatKind {
2019-01-31 07:33:11 +00:00
ZeroOrMore,
OneOrMore,
ZeroOrOne,
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Literal {
pub(crate) text: SmolStr,
2019-01-30 20:17:32 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Ident {
pub(crate) text: SmolStr,
2019-01-30 20:17:32 +00:00
}
2019-01-31 08:09:19 +00:00
#[derive(Debug)]
2019-01-31 10:46:40 +00:00
pub(crate) struct Var {
pub(crate) text: SmolStr,
pub(crate) kind: Option<SmolStr>,
2019-01-30 20:25:02 +00:00
}
2019-01-31 19:14:28 +00:00
#[cfg(test)]
mod tests {
use ra_syntax::{ast, AstNode};
use super::*;
2019-01-31 20:01:34 +00:00
// Good first issue (although a slightly chellegning one):
//
// * Pick a random test from here
// https://github.com/intellij-rust/intellij-rust/blob/c4e9feee4ad46e7953b1948c112533360b6087bb/src/test/kotlin/org/rust/lang/core/macros/RsMacroExpansionTest.kt
// * Port the test to rust and add it to this module
// * Make it pass :-)
2019-01-31 19:14:28 +00:00
#[test]
fn test_convert_tt() {
let macro_definition = r#"
macro_rules! impl_froms {
($e:ident: $($v:ident),*) => {
$(
impl From<$v> for $e {
fn from(it: $v) -> $e {
$e::$v(it)
}
}
)*
}
}
"#;
let macro_invocation = r#"
impl_froms!(TokenTree: Leaf, Subtree);
"#;
let source_file = ast::SourceFile::parse(macro_definition);
2019-02-08 11:49:43 +00:00
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
2019-01-31 19:14:28 +00:00
let source_file = ast::SourceFile::parse(macro_invocation);
2019-02-08 11:49:43 +00:00
let macro_invocation =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
2019-01-31 19:14:28 +00:00
let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
let rules = crate::MacroRules::parse(&definition_tt).unwrap();
let expansion = rules.expand(&invocation_tt).unwrap();
assert_eq!(
expansion.to_string(),
"impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (it)}} \
impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}}"
)
}
2019-02-03 20:06:59 +00:00
fn create_rules(macro_definition: &str) -> MacroRules {
let source_file = ast::SourceFile::parse(macro_definition);
2019-02-08 11:49:43 +00:00
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
2019-02-03 20:06:59 +00:00
let definition_tt = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
crate::MacroRules::parse(&definition_tt).unwrap()
}
fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
let source_file = ast::SourceFile::parse(invocation);
2019-02-08 11:49:43 +00:00
let macro_invocation =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
let invocation_tt = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
let expaned = rules.expand(&invocation_tt).unwrap();
assert_eq!(expaned.to_string(), expansion);
}
#[test]
2019-02-03 15:06:09 +00:00
fn test_fail_match_pattern_by_first_token() {
2019-02-03 20:06:59 +00:00
let rules = create_rules(
r#"
macro_rules! foo {
($ i:ident) => (
mod $ i {}
);
(= $ i:ident) => (
fn $ i() {}
);
(+ $ i:ident) => (
struct $ i;
)
}
2019-02-03 20:06:59 +00:00
"#,
);
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
assert_expansion(&rules, "foo! { = bar }", "fn bar () {}");
assert_expansion(&rules, "foo! { + Baz }", "struct Baz ;");
}
2019-02-03 15:06:09 +00:00
#[test]
fn test_fail_match_pattern_by_last_token() {
2019-02-03 20:06:59 +00:00
let rules = create_rules(
r#"
2019-02-03 15:06:09 +00:00
macro_rules! foo {
($ i:ident) => (
mod $ i {}
);
($ i:ident =) => (
fn $ i() {}
);
($ i:ident +) => (
struct $ i;
)
}
2019-02-03 20:06:59 +00:00
"#,
);
2019-02-03 15:06:09 +00:00
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
assert_expansion(&rules, "foo! { bar = }", "fn bar () {}");
assert_expansion(&rules, "foo! { Baz + }", "struct Baz ;");
}
#[test]
fn test_fail_match_pattern_by_word_token() {
let rules = create_rules(
r#"
macro_rules! foo {
($ i:ident) => (
mod $ i {}
);
(spam $ i:ident) => (
fn $ i() {}
);
(eggs $ i:ident) => (
struct $ i;
)
}
"#,
);
assert_expansion(&rules, "foo! { foo }", "mod foo {}");
assert_expansion(&rules, "foo! { spam bar }", "fn bar () {}");
assert_expansion(&rules, "foo! { eggs Baz }", "struct Baz ;");
}
#[test]
fn test_match_group_pattern_by_separator_token() {
let rules = create_rules(
r#"
macro_rules! foo {
($ ($ i:ident),*) => ($ (
mod $ i {}
)*);
($ ($ i:ident)#*) => ($ (
fn $ i() {}
)*);
($ i:ident ,# $ j:ident) => (
struct $ i;
struct $ j;
)
}
"#,
);
assert_expansion(&rules, "foo! { foo, bar }", "mod foo {} mod bar {}");
assert_expansion(&rules, "foo! { foo# bar }", "fn foo () {} fn bar () {}");
assert_expansion(&rules, "foo! { Foo,# Bar }", "struct Foo ; struct Bar ;");
}
2019-01-31 19:14:28 +00:00
}