Skip to content

Commit 613609c

Browse files
committed
minor: cleanup
1 parent 77bf761 commit 613609c

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

crates/mbe/src/expander/matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ enum OpDelimited<'a> {
765765

766766
#[derive(Debug, Clone, Copy)]
767767
struct OpDelimitedIter<'a> {
768-
inner: &'a Vec<Op>,
768+
inner: &'a [Op],
769769
delimited: Option<&'a tt::Delimiter>,
770770
idx: usize,
771771
}

crates/mbe/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod token_map;
1919
use std::fmt;
2020

2121
use crate::{
22-
parser::{parse_pattern, parse_template, MetaTemplate, Op},
22+
parser::{MetaTemplate, Op},
2323
tt_iter::TtIter,
2424
};
2525

@@ -275,8 +275,8 @@ impl Rule {
275275
.expect_subtree()
276276
.map_err(|()| ParseError::Expected("expected subtree".to_string()))?;
277277

278-
let lhs = MetaTemplate(parse_pattern(lhs)?);
279-
let rhs = MetaTemplate(parse_template(rhs)?);
278+
let lhs = MetaTemplate::parse_pattern(lhs)?;
279+
let rhs = MetaTemplate::parse_template(rhs)?;
280280

281281
Ok(crate::Rule { lhs, rhs })
282282
}

crates/mbe/src/parser.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,38 @@ use syntax::SmolStr;
66

77
use crate::{tt_iter::TtIter, ParseError};
88

9-
pub(crate) fn parse_template(template: &tt::Subtree) -> Result<Vec<Op>, ParseError> {
10-
parse_inner(template, Mode::Template).into_iter().collect()
11-
}
9+
/// Consider
10+
///
11+
/// ```
12+
/// macro_rules! an_macro {
13+
/// ($x:expr + $y:expr) => ($y * $x)
14+
/// }
15+
/// ```
16+
///
17+
/// Stuff to the left of `=>` is a [`MetaTemplate`] pattern (which is matched
18+
/// with input).
19+
///
20+
/// Stuff to the right is a [`MetaTemplate`] template which is used to produce
21+
/// output.
22+
#[derive(Clone, Debug, PartialEq, Eq)]
23+
pub(crate) struct MetaTemplate(pub(crate) Vec<Op>);
24+
25+
impl MetaTemplate {
26+
pub(crate) fn parse_pattern(pattern: &tt::Subtree) -> Result<MetaTemplate, ParseError> {
27+
let ops =
28+
parse_inner(pattern, Mode::Pattern).into_iter().collect::<Result<_, ParseError>>()?;
29+
Ok(MetaTemplate(ops))
30+
}
31+
32+
pub(crate) fn parse_template(template: &tt::Subtree) -> Result<MetaTemplate, ParseError> {
33+
let ops =
34+
parse_inner(template, Mode::Template).into_iter().collect::<Result<_, ParseError>>()?;
35+
Ok(MetaTemplate(ops))
36+
}
1237

13-
pub(crate) fn parse_pattern(pattern: &tt::Subtree) -> Result<Vec<Op>, ParseError> {
14-
parse_inner(pattern, Mode::Pattern).into_iter().collect()
38+
pub(crate) fn iter(&self) -> impl Iterator<Item = &Op> {
39+
self.0.iter()
40+
}
1541
}
1642

1743
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -36,15 +62,6 @@ pub(crate) enum Separator {
3662
Puncts(SmallVec<[tt::Punct; 3]>),
3763
}
3864

39-
#[derive(Clone, Debug, PartialEq, Eq)]
40-
pub(crate) struct MetaTemplate(pub(crate) Vec<Op>);
41-
42-
impl MetaTemplate {
43-
pub(crate) fn iter(&self) -> impl Iterator<Item = &Op> {
44-
self.0.iter()
45-
}
46-
}
47-
4865
// Note that when we compare a Separator, we just care about its textual value.
4966
impl PartialEq for Separator {
5067
fn eq(&self, other: &Separator) -> bool {

0 commit comments

Comments
 (0)