Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/mbe/src/expander/transcriber.rs')
-rw-r--r--crates/mbe/src/expander/transcriber.rs99
1 files changed, 46 insertions, 53 deletions
diff --git a/crates/mbe/src/expander/transcriber.rs b/crates/mbe/src/expander/transcriber.rs
index 6d3055da28..5e6e45f152 100644
--- a/crates/mbe/src/expander/transcriber.rs
+++ b/crates/mbe/src/expander/transcriber.rs
@@ -1,8 +1,9 @@
//! Transcriber takes a template, like `fn $ident() {}`, a set of bindings like
//! `$ident => foo`, interpolates variables in the template, to get `fn foo() {}`
+use span::Span;
use syntax::SmolStr;
-use tt::{Delimiter, Span};
+use tt::Delimiter;
use crate::{
expander::{Binding, Bindings, Fragment},
@@ -10,8 +11,8 @@ use crate::{
CountError, ExpandError, ExpandResult, MetaTemplate,
};
-impl<S: Span> Bindings<S> {
- fn get(&self, name: &str) -> Result<&Binding<S>, ExpandError> {
+impl Bindings {
+ fn get(&self, name: &str) -> Result<&Binding, ExpandError> {
match self.inner.get(name) {
Some(binding) => Ok(binding),
None => Err(ExpandError::UnresolvedBinding(Box::new(Box::from(name)))),
@@ -21,10 +22,10 @@ impl<S: Span> Bindings<S> {
fn get_fragment(
&self,
name: &str,
- mut span: S,
+ mut span: Span,
nesting: &mut [NestingState],
- marker: impl Fn(&mut S),
- ) -> Result<Fragment<S>, ExpandError> {
+ marker: impl Fn(&mut Span),
+ ) -> Result<Fragment, ExpandError> {
macro_rules! binding_err {
($($arg:tt)*) => { ExpandError::binding_error(format!($($arg)*)) };
}
@@ -134,15 +135,15 @@ impl<S: Span> Bindings<S> {
}
}
-pub(super) fn transcribe<S: Span>(
- template: &MetaTemplate<S>,
- bindings: &Bindings<S>,
- marker: impl Fn(&mut S) + Copy,
+pub(super) fn transcribe(
+ template: &MetaTemplate,
+ bindings: &Bindings,
+ marker: impl Fn(&mut Span) + Copy,
new_meta_vars: bool,
- call_site: S,
-) -> ExpandResult<tt::Subtree<S>> {
+ call_site: Span,
+) -> ExpandResult<tt::Subtree<Span>> {
let mut ctx = ExpandCtx { bindings, nesting: Vec::new(), new_meta_vars, call_site };
- let mut arena: Vec<tt::TokenTree<S>> = Vec::new();
+ let mut arena: Vec<tt::TokenTree<Span>> = Vec::new();
expand_subtree(&mut ctx, template, None, &mut arena, marker)
}
@@ -158,20 +159,20 @@ struct NestingState {
}
#[derive(Debug)]
-struct ExpandCtx<'a, S> {
- bindings: &'a Bindings<S>,
+struct ExpandCtx<'a> {
+ bindings: &'a Bindings,
nesting: Vec<NestingState>,
new_meta_vars: bool,
- call_site: S,
+ call_site: Span,
}
-fn expand_subtree<S: Span>(
- ctx: &mut ExpandCtx<'_, S>,
- template: &MetaTemplate<S>,
- delimiter: Option<Delimiter<S>>,
- arena: &mut Vec<tt::TokenTree<S>>,
- marker: impl Fn(&mut S) + Copy,
-) -> ExpandResult<tt::Subtree<S>> {
+fn expand_subtree(
+ ctx: &mut ExpandCtx<'_>,
+ template: &MetaTemplate,
+ delimiter: Option<Delimiter<Span>>,
+ arena: &mut Vec<tt::TokenTree<Span>>,
+ marker: impl Fn(&mut Span) + Copy,
+) -> ExpandResult<tt::Subtree<Span>> {
// remember how many elements are in the arena now - when returning, we want to drain exactly how many elements we added. This way, the recursive uses of the arena get their own "view" of the arena, but will reuse the allocation
let start_elements = arena.len();
let mut err = None;
@@ -332,12 +333,12 @@ fn expand_subtree<S: Span>(
}
}
-fn expand_var<S: Span>(
- ctx: &mut ExpandCtx<'_, S>,
+fn expand_var(
+ ctx: &mut ExpandCtx<'_>,
v: &SmolStr,
- id: S,
- marker: impl Fn(&mut S),
-) -> ExpandResult<Fragment<S>> {
+ id: Span,
+ marker: impl Fn(&mut Span),
+) -> ExpandResult<Fragment> {
// We already handle $crate case in mbe parser
debug_assert!(v != "crate");
@@ -378,15 +379,15 @@ fn expand_var<S: Span>(
}
}
-fn expand_repeat<S: Span>(
- ctx: &mut ExpandCtx<'_, S>,
- template: &MetaTemplate<S>,
+fn expand_repeat(
+ ctx: &mut ExpandCtx<'_>,
+ template: &MetaTemplate,
kind: RepeatKind,
- separator: &Option<Separator<S>>,
- arena: &mut Vec<tt::TokenTree<S>>,
- marker: impl Fn(&mut S) + Copy,
-) -> ExpandResult<Fragment<S>> {
- let mut buf: Vec<tt::TokenTree<S>> = Vec::new();
+ separator: &Option<Separator>,
+ arena: &mut Vec<tt::TokenTree<Span>>,
+ marker: impl Fn(&mut Span) + Copy,
+) -> ExpandResult<Fragment> {
+ let mut buf: Vec<tt::TokenTree<Span>> = Vec::new();
ctx.nesting.push(NestingState { idx: 0, at_end: false, hit: false });
// Dirty hack to make macro-expansion terminate.
// This should be replaced by a proper macro-by-example implementation
@@ -478,11 +479,7 @@ fn expand_repeat<S: Span>(
ExpandResult { value: Fragment::Tokens(tt), err }
}
-fn push_fragment<S: Span>(
- ctx: &ExpandCtx<'_, S>,
- buf: &mut Vec<tt::TokenTree<S>>,
- fragment: Fragment<S>,
-) {
+fn push_fragment(ctx: &ExpandCtx<'_>, buf: &mut Vec<tt::TokenTree<Span>>, fragment: Fragment) {
match fragment {
Fragment::Tokens(tt::TokenTree::Subtree(tt)) => push_subtree(buf, tt),
Fragment::Expr(sub) => {
@@ -494,7 +491,7 @@ fn push_fragment<S: Span>(
}
}
-fn push_subtree<S>(buf: &mut Vec<tt::TokenTree<S>>, tt: tt::Subtree<S>) {
+fn push_subtree(buf: &mut Vec<tt::TokenTree<Span>>, tt: tt::Subtree<Span>) {
match tt.delimiter.kind {
tt::DelimiterKind::Invisible => buf.extend(Vec::from(tt.token_trees)),
_ => buf.push(tt.into()),
@@ -504,10 +501,10 @@ fn push_subtree<S>(buf: &mut Vec<tt::TokenTree<S>>, tt: tt::Subtree<S>) {
/// Inserts the path separator `::` between an identifier and its following generic
/// argument list, and then pushes into the buffer. See [`Fragment::Path`] for why
/// we need this fixup.
-fn fix_up_and_push_path_tt<S: Span>(
- ctx: &ExpandCtx<'_, S>,
- buf: &mut Vec<tt::TokenTree<S>>,
- subtree: tt::Subtree<S>,
+fn fix_up_and_push_path_tt(
+ ctx: &ExpandCtx<'_>,
+ buf: &mut Vec<tt::TokenTree<Span>>,
+ subtree: tt::Subtree<Span>,
) {
stdx::always!(matches!(subtree.delimiter.kind, tt::DelimiterKind::Invisible));
let mut prev_was_ident = false;
@@ -546,11 +543,7 @@ fn fix_up_and_push_path_tt<S: Span>(
/// Handles `${count(t, depth)}`. `our_depth` is the recursion depth and `count_depth` is the depth
/// defined by the metavar expression.
-fn count<S>(
- binding: &Binding<S>,
- depth_curr: usize,
- depth_max: usize,
-) -> Result<usize, CountError> {
+fn count(binding: &Binding, depth_curr: usize, depth_max: usize) -> Result<usize, CountError> {
match binding {
Binding::Nested(bs) => {
if depth_curr == depth_max {
@@ -564,8 +557,8 @@ fn count<S>(
}
}
-fn count_old<S>(
- binding: &Binding<S>,
+fn count_old(
+ binding: &Binding,
our_depth: usize,
count_depth: Option<usize>,
) -> Result<usize, CountError> {