Skip to content

Commit abeb51c

Browse files
committed
use usual token tree for macro expansion
1 parent 37ef892 commit abeb51c

File tree

9 files changed

+677
-840
lines changed

9 files changed

+677
-840
lines changed

crates/ra_mbe/src/lib.rs

+5-70
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,13 @@
33
/// interface, although it contains some code to bridge `SyntaxNode`s and
44
/// `TokenTree`s as well!
55
6-
macro_rules! impl_froms {
7-
($e:ident: $($v:ident), *) => {
8-
$(
9-
impl From<$v> for $e {
10-
fn from(it: $v) -> $e {
11-
$e::$v(it)
12-
}
13-
}
14-
)*
15-
}
16-
}
17-
6+
mod parser;
187
mod mbe_parser;
198
mod mbe_expander;
209
mod syntax_bridge;
21-
mod tt_cursor;
10+
mod tt_iter;
2211
mod subtree_source;
23-
mod subtree_parser;
2412

25-
use ra_syntax::SmolStr;
2613
use smallvec::SmallVec;
2714

2815
pub use tt::{Delimiter, Punct};
@@ -38,6 +25,7 @@ pub enum ExpandError {
3825
UnexpectedToken,
3926
BindingError(String),
4027
ConversionError,
28+
InvalidRepeat,
4129
}
4230

4331
pub use crate::syntax_bridge::{
@@ -65,31 +53,8 @@ impl MacroRules {
6553

6654
#[derive(Clone, Debug, PartialEq, Eq)]
6755
pub(crate) struct Rule {
68-
pub(crate) lhs: Subtree,
69-
pub(crate) rhs: Subtree,
70-
}
71-
72-
#[derive(Clone, Debug, PartialEq, Eq)]
73-
pub(crate) enum TokenTree {
74-
Leaf(Leaf),
75-
Subtree(Subtree),
76-
Repeat(Repeat),
77-
}
78-
impl_froms!(TokenTree: Leaf, Subtree, Repeat);
79-
80-
#[derive(Clone, Debug, PartialEq, Eq)]
81-
pub(crate) enum Leaf {
82-
Literal(Literal),
83-
Punct(Punct),
84-
Ident(Ident),
85-
Var(Var),
86-
}
87-
impl_froms!(Leaf: Literal, Punct, Ident, Var);
88-
89-
#[derive(Clone, Debug, PartialEq, Eq)]
90-
pub(crate) struct Subtree {
91-
pub(crate) delimiter: Delimiter,
92-
pub(crate) token_trees: Vec<TokenTree>,
56+
pub(crate) lhs: tt::Subtree,
57+
pub(crate) rhs: tt::Subtree,
9358
}
9459

9560
#[derive(Clone, Debug, Eq)]
@@ -117,35 +82,5 @@ impl PartialEq for crate::Separator {
11782
}
11883
}
11984

120-
#[derive(Clone, Debug, PartialEq, Eq)]
121-
pub(crate) struct Repeat {
122-
pub(crate) subtree: Subtree,
123-
pub(crate) kind: RepeatKind,
124-
pub(crate) separator: Option<Separator>,
125-
}
126-
127-
#[derive(Clone, Debug, PartialEq, Eq)]
128-
pub(crate) enum RepeatKind {
129-
ZeroOrMore,
130-
OneOrMore,
131-
ZeroOrOne,
132-
}
133-
134-
#[derive(Clone, Debug, PartialEq, Eq)]
135-
pub(crate) struct Literal {
136-
pub(crate) text: SmolStr,
137-
}
138-
139-
#[derive(Clone, Debug, PartialEq, Eq)]
140-
pub(crate) struct Ident {
141-
pub(crate) text: SmolStr,
142-
}
143-
144-
#[derive(Clone, Debug, PartialEq, Eq)]
145-
pub(crate) struct Var {
146-
pub(crate) text: SmolStr,
147-
pub(crate) kind: Option<SmolStr>,
148-
}
149-
15085
#[cfg(test)]
15186
mod tests;

crates/ra_mbe/src/mbe_expander.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ mod transcriber;
88
use ra_syntax::SmolStr;
99
use rustc_hash::FxHashMap;
1010

11-
use crate::tt_cursor::TtCursor;
1211
use crate::ExpandError;
1312

1413
pub(crate) fn expand(
@@ -19,12 +18,8 @@ pub(crate) fn expand(
1918
}
2019

2120
fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Result<tt::Subtree, ExpandError> {
22-
let mut input = TtCursor::new(input);
23-
let bindings = matcher::match_lhs(&rule.lhs, &mut input)?;
24-
if !input.is_eof() {
25-
return Err(ExpandError::UnexpectedToken);
26-
}
27-
let res = transcriber::transcribe(&bindings, &rule.rhs)?;
21+
let bindings = matcher::match_(&rule.lhs, input)?;
22+
let res = transcriber::transcribe(&rule.rhs, &bindings)?;
2823
Ok(res)
2924
}
3025

@@ -103,13 +98,6 @@ mod tests {
10398

10499
#[test]
105100
fn test_expand_rule() {
106-
// FIXME: The missing $var check should be in parsing phase
107-
// assert_err(
108-
// "($i:ident) => ($j)",
109-
// "foo!{a}",
110-
// ExpandError::BindingError(String::from("could not find binding `j`")),
111-
// );
112-
113101
assert_err(
114102
"($($i:ident);*) => ($i)",
115103
"foo!{a}",
@@ -118,9 +106,6 @@ mod tests {
118106
)),
119107
);
120108

121-
assert_err("($i) => ($i)", "foo!{a}", ExpandError::UnexpectedToken);
122-
assert_err("($i:) => ($i)", "foo!{a}", ExpandError::UnexpectedToken);
123-
124109
// FIXME:
125110
// Add an err test case for ($($i:ident)) => ($())
126111
}

0 commit comments

Comments
 (0)