rust-decl-macros

How to parse single tokens in rust macros

♀尐吖头ヾ 提交于 2019-12-10 10:16:12
问题 I'm starting playing with Rust macros and I came to try this little practice example. I want to define a macro that expands into a variable initialization (name doesn't matter) of type i32 (for example, but not really important) and a series of operations to that variable, in this case a var += 1 or a var -= 1 and finally it will call println!("{}", var) . The macro will take a series of tokens based on + and - that matches the operations described above. So for example: operate_integer![+++-

Macro matching tokens recursive expansion

。_饼干妹妹 提交于 2019-12-08 10:03:01
问题 I'm triying to implement a macro that will expand a brainfuck program (after starting with some simpler code, in which I had problems coming up with a solution already: How to parse single tokens in rust macros). The problem is that at some point of the recursive matching it can never match the end: error: recursion limit reached while expanding the macro `brainfuck` --> src/lib.rs:119:9 | 119 | brainfuck!(@impl cell; $($all_tokens)*); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 124 |

How to parse single tokens in rust macros

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 21:49:28
I'm starting playing with Rust macros and I came to try this little practice example. I want to define a macro that expands into a variable initialization (name doesn't matter) of type i32 (for example, but not really important) and a series of operations to that variable, in this case a var += 1 or a var -= 1 and finally it will call println!("{}", var) . The macro will take a series of tokens based on + and - that matches the operations described above. So for example: operate_integer![+++---] would expand to: let mut var: i32 = 0; var += 1; var += 1; var += 1; var -= 1; var -= 1; var -= 1;

What does an @ symbol mean in a declarative macro?

我与影子孤独终老i 提交于 2019-11-29 14:41:15
I have seen the @ symbol used in macros but I cannot find mention of it in the Rust Book or in any official documentation or blog posts. For example, in this Stack Overflow answer it is used like this: macro_rules! instructions { (enum $ename:ident { $($vname:ident ( $($vty: ty),* )),* }) => { enum $ename { $($vname ( $($vty),* )),* } impl $ename { fn len(&self) -> usize { match self { $($ename::$vname(..) => instructions!(@count ($($vty),*))),* } } } }; (@count ()) => (0); (@count ($a:ty)) => (1); (@count ($a:ty, $b:ty)) => (2); (@count ($a:ty, $b:ty, $c:ty)) => (3); } instructions! { enum

What does an @ symbol mean in a declarative macro?

家住魔仙堡 提交于 2019-11-27 07:54:17
问题 I have seen the @ symbol used in macros but I cannot find mention of it in the Rust Book or in any official documentation or blog posts. For example, in this Stack Overflow answer it is used like this: macro_rules! instructions { (enum $ename:ident { $($vname:ident ( $($vty: ty),* )),* }) => { enum $ename { $($vname ( $($vty),* )),* } impl $ename { fn len(&self) -> usize { match self { $($ename::$vname(..) => instructions!(@count ($($vty),*))),* } } } }; (@count ()) => (0); (@count ($a:ty)) =