minor: cleanup
This commit is contained in:
parent
77bf761203
commit
613609cc5e
3 changed files with 35 additions and 18 deletions
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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>);
|
||||
|
||||
pub(crate) fn parse_pattern(pattern: &tt::Subtree) -> Result<Vec<Op>, ParseError> {
|
||||
parse_inner(pattern, Mode::Pattern).into_iter().collect()
|
||||
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 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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue