aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r--crates/ra_mbe/src/lib.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
new file mode 100644
index 000000000..38bf3431a
--- /dev/null
+++ b/crates/ra_mbe/src/lib.rs
@@ -0,0 +1,88 @@
1macro_rules! impl_froms {
2 ($e:ident: $($v:ident), *) => {
3 $(
4 impl From<$v> for $e {
5 fn from(it: $v) -> $e {
6 $e::$v(it)
7 }
8 }
9 )*
10 }
11}
12
13mod tt_cursor;
14mod mbe_parser;
15mod mbe_expander;
16
17use smol_str::SmolStr;
18
19pub use tt::{Delimiter, Punct};
20
21pub use crate::{
22 mbe_parser::parse,
23 mbe_expander::exapnd,
24};
25
26#[derive(Debug)]
27pub struct MacroRules {
28 pub(crate) rules: Vec<Rule>,
29}
30
31#[derive(Debug)]
32pub(crate) struct Rule {
33 pub(crate) lhs: Subtree,
34 pub(crate) rhs: Subtree,
35}
36
37#[derive(Debug)]
38pub(crate) enum TokenTree {
39 Leaf(Leaf),
40 Subtree(Subtree),
41 Repeat(Repeat),
42}
43impl_froms!(TokenTree: Leaf, Subtree, Repeat);
44
45#[derive(Debug)]
46pub(crate) enum Leaf {
47 Literal(Literal),
48 Punct(Punct),
49 Ident(Ident),
50 Var(Var),
51}
52impl_froms!(Leaf: Literal, Punct, Ident, Var);
53
54#[derive(Debug)]
55pub(crate) struct Subtree {
56 pub(crate) delimiter: Delimiter,
57 pub(crate) token_trees: Vec<TokenTree>,
58}
59
60#[derive(Debug)]
61pub(crate) struct Repeat {
62 pub(crate) subtree: Subtree,
63 pub(crate) kind: RepeatKind,
64 pub(crate) separator: Option<char>,
65}
66
67#[derive(Debug)]
68pub(crate) enum RepeatKind {
69 ZeroOrMore,
70 OneOrMore,
71 ZeroOrOne,
72}
73
74#[derive(Debug)]
75pub(crate) struct Literal {
76 pub(crate) text: SmolStr,
77}
78
79#[derive(Debug)]
80pub(crate) struct Ident {
81 pub(crate) text: SmolStr,
82}
83
84#[derive(Debug)]
85pub(crate) struct Var {
86 pub(crate) text: SmolStr,
87 pub(crate) kind: Option<SmolStr>,
88}