]> git.lizzy.rs Git - rust.git/commitdiff
binders boilerplate
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 31 Jan 2019 12:22:55 +0000 (15:22 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 31 Jan 2019 20:23:30 +0000 (22:23 +0200)
crates/ra_macros/src/mbe_expander.rs

index 545bd2002c082845d5cc129423cdbbc7354ba456..f55c337da6f670a950d163472e1417762c9da87b 100644 (file)
@@ -14,11 +14,51 @@ fn expand_rule(rule: &mbe::Rule, input: &tt::Subtree) -> Option<tt::Subtree> {
 
 #[derive(Debug, Default)]
 struct Bindings {
-    inner: FxHashMap<SmolStr, tt::TokenTree>,
+    inner: FxHashMap<SmolStr, Binding>,
 }
 
+#[derive(Debug)]
+enum Binding {
+    Simple(tt::TokenTree),
+    Nested(Vec<Binding>),
+}
+
+/*
+
+macro_rules! impl_froms {
+    ($e:ident: $($v:ident),*) => {
+        $(
+            impl From<$v> for $e {
+                fn from(it: $v) -> $e {
+                    $e::$v(it)
+                }
+            }
+        )*
+    }
+}
+
+impl_froms! (Foo: Bar, Baz)
+
+*/
+
 fn match_lhs(pattern: &mbe::Subtree, input: &tt::Subtree) -> Option<Bindings> {
-    Some(Bindings::default())
+    let mut res = Bindings::default();
+    for pat in pattern.token_trees.iter() {
+        match pat {
+            mbe::TokenTree::Leaf(leaf) => match leaf {
+                mbe::Leaf::Var(mbe::Var { text, kind }) => {
+                    let kind = kind.clone()?;
+                    match kind.as_str() {
+                        "ident" => (),
+                        _ => return None,
+                    }
+                }
+                _ => return None,
+            },
+            _ => {}
+        }
+    }
+    Some(res)
 }
 
 fn expand_rhs(template: &mbe::Subtree, bindings: &Bindings) -> Option<tt::Subtree> {