]> git.lizzy.rs Git - rust.git/commitdiff
cleanup the api
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 31 Jan 2019 19:14:28 +0000 (22:14 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 31 Jan 2019 20:23:30 +0000 (22:23 +0200)
crates/ra_mbe/src/lib.rs
crates/ra_mbe/src/mbe_parser.rs
crates/ra_mbe/src/syntax_bridge.rs

index c7be33b19910a6404c763cea99fa0927702f24b5..af3ccc0f598de1f7c5a5a5dd01b37da1d26b30be 100644 (file)
@@ -24,17 +24,26 @@ fn from(it: $v) -> $e {
 
 pub use tt::{Delimiter, Punct};
 
-pub use crate::{
-    mbe_parser::parse,
-    mbe_expander::exapnd,
-    syntax_bridge::macro_call_to_tt,
-};
+pub use crate::syntax_bridge::ast_to_token_tree;
 
+/// This struct contains AST for a single `macro_rules` defenition. What might
+/// 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)
 #[derive(Debug)]
 pub struct MacroRules {
     pub(crate) rules: Vec<Rule>,
 }
 
+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)
+    }
+}
+
 #[derive(Debug)]
 pub(crate) struct Rule {
     pub(crate) lhs: Subtree,
@@ -93,3 +102,55 @@ pub(crate) struct Var {
     pub(crate) text: SmolStr,
     pub(crate) kind: Option<SmolStr>,
 }
+
+#[cfg(test)]
+mod tests {
+    use ra_syntax::{ast, AstNode};
+
+    use super::*;
+
+    #[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);
+        let macro_definition = source_file
+            .syntax()
+            .descendants()
+            .find_map(ast::MacroCall::cast)
+            .unwrap();
+
+        let source_file = ast::SourceFile::parse(macro_invocation);
+        let macro_invocation = source_file
+            .syntax()
+            .descendants()
+            .find_map(ast::MacroCall::cast)
+            .unwrap();
+
+        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)}}"
+    )
+    }
+}
index a76ea84dbe4d77d4258de06f2d17e88d29b91e12..a3e6abffceae9f4dd1170a637a257130ee59d486 100644 (file)
@@ -1,10 +1,9 @@
-use crate::tt_cursor::TtCursor;
-
 /// This module parses a raw `tt::TokenStream` into macro-by-example token
 /// stream. This is a *mostly* identify function, expect for handling of
 /// `$var:tt_kind` and `$(repeat),*` constructs.
+use crate::tt_cursor::TtCursor;
 
-pub fn parse(tt: &tt::Subtree) -> Option<crate::MacroRules> {
+pub(crate) fn parse(tt: &tt::Subtree) -> Option<crate::MacroRules> {
     let mut parser = TtCursor::new(tt);
     let mut rules = Vec::new();
     while !parser.is_eof() {
index 3223f6ea5f565c42d0cbf8925b886f2db14343a2..2dc04d4e731680f07d26a5618b45951a110b0b88 100644 (file)
@@ -1,8 +1,7 @@
 use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxKind::*};
 
-pub fn macro_call_to_tt(call: &ast::MacroCall) -> Option<tt::Subtree> {
-    let tt = call.token_tree()?;
-    convert_tt(tt.syntax())
+pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<tt::Subtree> {
+    convert_tt(ast.syntax())
 }
 
 fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
@@ -66,48 +65,3 @@ fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
     };
     Some(res)
 }
-
-#[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);
-    let macro_definition = source_file
-        .syntax()
-        .descendants()
-        .find_map(ast::MacroCall::cast)
-        .unwrap();
-
-    let source_file = ast::SourceFile::parse(macro_invocation);
-    let macro_invocation = source_file
-        .syntax()
-        .descendants()
-        .find_map(ast::MacroCall::cast)
-        .unwrap();
-
-    let definition_tt = macro_call_to_tt(macro_definition).unwrap();
-    let invocation_tt = macro_call_to_tt(macro_invocation).unwrap();
-    let mbe = crate::parse(&definition_tt).unwrap();
-    let expansion = crate::exapnd(&mbe, &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)}}"
-    )
-}