diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2021-10-02 20:21:23 +0300 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2021-10-02 20:38:39 +0300 |
| commit | 613609cc5eb31a87c08b63b8d5a5ac8123a7b84a (patch) | |
| tree | 3c7e41d725397cb3a501149f9eeaeedc0c3f9d15 | |
| parent | 77bf7612035cdec0d2a7b27e54087ed6cb19f9e8 (diff) | |
| download | rust-613609cc5eb31a87c08b63b8d5a5ac8123a7b84a.tar.gz rust-613609cc5eb31a87c08b63b8d5a5ac8123a7b84a.zip | |
minor: cleanup
| -rw-r--r-- | crates/mbe/src/expander/matcher.rs | 2 | ||||
| -rw-r--r-- | crates/mbe/src/lib.rs | 6 | ||||
| -rw-r--r-- | crates/mbe/src/parser.rs | 45 |
3 files changed, 35 insertions, 18 deletions
diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index 233ca08dc13..06c21dfd70f 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs @@ -765,7 +765,7 @@ enum OpDelimited<'a> { #[derive(Debug, Clone, Copy)] struct OpDelimitedIter<'a> { - inner: &'a Vec<Op>, + inner: &'a [Op], delimited: Option<&'a tt::Delimiter>, idx: usize, } diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index 0a1b7cc8489..8470ea0aaf5 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -19,7 +19,7 @@ mod token_map; use std::fmt; use crate::{ - parser::{parse_pattern, parse_template, MetaTemplate, Op}, + parser::{MetaTemplate, Op}, tt_iter::TtIter, }; @@ -275,8 +275,8 @@ impl Rule { .expect_subtree() .map_err(|()| ParseError::Expected("expected subtree".to_string()))?; - let lhs = MetaTemplate(parse_pattern(lhs)?); - let rhs = MetaTemplate(parse_template(rhs)?); + let lhs = MetaTemplate::parse_pattern(lhs)?; + let rhs = MetaTemplate::parse_template(rhs)?; Ok(crate::Rule { lhs, rhs }) } diff --git a/crates/mbe/src/parser.rs b/crates/mbe/src/parser.rs index 0cce4146fb0..f0a9dd4a51e 100644 --- a/crates/mbe/src/parser.rs +++ b/crates/mbe/src/parser.rs @@ -6,12 +6,38 @@ use syntax::SmolStr; use crate::{tt_iter::TtIter, ParseError}; -pub(crate) fn parse_template(template: &tt::Subtree) -> Result<Vec<Op>, ParseError> { - parse_inner(template, Mode::Template).into_iter().collect() -} +/// Consider +/// +/// ``` +/// macro_rules! an_macro { +/// ($x:expr + $y:expr) => ($y * $x) +/// } +/// ``` +/// +/// Stuff to the left of `=>` is a [`MetaTemplate`] pattern (which is matched +/// with input). +/// +/// Stuff to the right is a [`MetaTemplate`] template which is used to produce +/// output. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct MetaTemplate(pub(crate) Vec<Op>); + +impl MetaTemplate { + pub(crate) fn parse_pattern(pattern: &tt::Subtree) -> Result<MetaTemplate, ParseError> { + let ops = + parse_inner(pattern, Mode::Pattern).into_iter().collect::<Result<_, ParseError>>()?; + Ok(MetaTemplate(ops)) + } + + pub(crate) fn parse_template(template: &tt::Subtree) -> Result<MetaTemplate, ParseError> { + let ops = + parse_inner(template, Mode::Template).into_iter().collect::<Result<_, ParseError>>()?; + Ok(MetaTemplate(ops)) + } -pub(crate) fn parse_pattern(pattern: &tt::Subtree) -> Result<Vec<Op>, ParseError> { - parse_inner(pattern, Mode::Pattern).into_iter().collect() + pub(crate) fn iter(&self) -> impl Iterator<Item = &Op> { + self.0.iter() + } } #[derive(Clone, Debug, PartialEq, Eq)] @@ -36,15 +62,6 @@ pub(crate) enum Separator { Puncts(SmallVec<[tt::Punct; 3]>), } -#[derive(Clone, Debug, PartialEq, Eq)] -pub(crate) struct MetaTemplate(pub(crate) Vec<Op>); - -impl MetaTemplate { - pub(crate) fn iter(&self) -> impl Iterator<Item = &Op> { - self.0.iter() - } -} - // Note that when we compare a Separator, we just care about its textual value. impl PartialEq for Separator { fn eq(&self, other: &Separator) -> bool { |
