Remove span generics from most of the mbe crate

This commit is contained in:
Lukas Wirth 2024-03-19 17:06:50 +01:00
parent 255a8aef92
commit 7e88fa5d3a
8 changed files with 187 additions and 228 deletions

View file

@ -16,7 +16,7 @@ use crate::{
/// Old-style `macro_rules` or the new macros 2.0
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DeclarativeMacroExpander {
pub mac: mbe::DeclarativeMacro<span::Span>,
pub mac: mbe::DeclarativeMacro,
pub transparency: Transparency,
}

View file

@ -54,11 +54,9 @@ use crate::{
pub use crate::files::{AstId, ErasedAstId, InFile, InMacroFile, InRealFile};
pub use mbe::ValueResult;
pub use mbe::{DeclarativeMacro, ValueResult};
pub use span::{HirFileId, MacroCallId, MacroFileId};
pub type DeclarativeMacro = ::mbe::DeclarativeMacro<tt::Span>;
pub mod tt {
pub use span::Span;
pub use tt::{DelimiterKind, Spacing};

View file

@ -51,7 +51,7 @@ fn benchmark_expand_macro_rules() {
assert_eq!(hash, 69413);
}
fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro<Span>> {
fn macro_rules_fixtures() -> FxHashMap<String, DeclarativeMacro> {
macro_rules_fixtures_tt()
.into_iter()
.map(|(id, tt)| (id, DeclarativeMacro::parse_macro_rules(&tt, true, true)))
@ -80,7 +80,7 @@ fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree<Span>> {
/// Generate random invocation fixtures from rules
fn invocation_fixtures(
rules: &FxHashMap<String, DeclarativeMacro<Span>>,
rules: &FxHashMap<String, DeclarativeMacro>,
) -> Vec<(String, tt::Subtree<Span>)> {
let mut seed = 123456789;
let mut res = Vec::new();
@ -128,11 +128,7 @@ fn invocation_fixtures(
}
return res;
fn collect_from_op(
op: &Op<Span>,
token_trees: &mut Vec<tt::TokenTree<Span>>,
seed: &mut usize,
) {
fn collect_from_op(op: &Op, token_trees: &mut Vec<tt::TokenTree<Span>>, seed: &mut usize) {
return match op {
Op::Var { kind, .. } => match kind.as_ref() {
Some(MetaVarKind::Ident) => token_trees.push(make_ident("foo")),

View file

@ -6,20 +6,20 @@ mod matcher;
mod transcriber;
use rustc_hash::FxHashMap;
use span::Span;
use syntax::SmolStr;
use tt::Span;
use crate::{parser::MetaVarKind, ExpandError, ExpandResult};
pub(crate) fn expand_rules<S: Span>(
rules: &[crate::Rule<S>],
input: &tt::Subtree<S>,
marker: impl Fn(&mut S) + Copy,
pub(crate) fn expand_rules(
rules: &[crate::Rule],
input: &tt::Subtree<Span>,
marker: impl Fn(&mut Span) + Copy,
is_2021: bool,
new_meta_vars: bool,
call_site: S,
) -> ExpandResult<tt::Subtree<S>> {
let mut match_: Option<(matcher::Match<S>, &crate::Rule<S>)> = None;
call_site: Span,
) -> ExpandResult<tt::Subtree<Span>> {
let mut match_: Option<(matcher::Match, &crate::Rule)> = None;
for rule in rules {
let new_match = matcher::match_(&rule.lhs, input, is_2021);
@ -110,30 +110,24 @@ pub(crate) fn expand_rules<S: Span>(
/// In other words, `Bindings` is a *multi* mapping from `SmolStr` to
/// `tt::TokenTree`, where the index to select a particular `TokenTree` among
/// many is not a plain `usize`, but a `&[usize]`.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Bindings<S> {
inner: FxHashMap<SmolStr, Binding<S>>,
}
impl<S> Default for Bindings<S> {
fn default() -> Self {
Self { inner: Default::default() }
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct Bindings {
inner: FxHashMap<SmolStr, Binding>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Binding<S> {
Fragment(Fragment<S>),
Nested(Vec<Binding<S>>),
enum Binding {
Fragment(Fragment),
Nested(Vec<Binding>),
Empty,
Missing(MetaVarKind),
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Fragment<S> {
enum Fragment {
Empty,
/// token fragments are just copy-pasted into the output
Tokens(tt::TokenTree<S>),
Tokens(tt::TokenTree<Span>),
/// Expr ast fragments are surrounded with `()` on insertion to preserve
/// precedence. Note that this impl is different from the one currently in
/// `rustc` -- `rustc` doesn't translate fragments into token trees at all.
@ -141,7 +135,7 @@ enum Fragment<S> {
/// At one point in time, we tried to use "fake" delimiters here à la
/// proc-macro delimiter=none. As we later discovered, "none" delimiters are
/// tricky to handle in the parser, and rustc doesn't handle those either.
Expr(tt::Subtree<S>),
Expr(tt::Subtree<Span>),
/// There are roughly two types of paths: paths in expression context, where a
/// separator `::` between an identifier and its following generic argument list
/// is mandatory, and paths in type context, where `::` can be omitted.
@ -151,5 +145,5 @@ enum Fragment<S> {
/// and is trasncribed as an expression-context path, verbatim transcription
/// would cause a syntax error. We need to fix it up just before transcribing;
/// see `transcriber::fix_up_and_push_path_tt()`.
Path(tt::Subtree<S>),
Path(tt::Subtree<Span>),
}

View file

@ -62,8 +62,9 @@
use std::rc::Rc;
use smallvec::{smallvec, SmallVec};
use span::Span;
use syntax::SmolStr;
use tt::{DelimSpan, Span};
use tt::DelimSpan;
use crate::{
expander::{Binding, Bindings, ExpandResult, Fragment},
@ -72,7 +73,7 @@ use crate::{
ExpandError, MetaTemplate, ValueResult,
};
impl<S: Span> Bindings<S> {
impl Bindings {
fn push_optional(&mut self, name: &SmolStr) {
self.inner.insert(name.clone(), Binding::Fragment(Fragment::Empty));
}
@ -81,14 +82,14 @@ impl<S: Span> Bindings<S> {
self.inner.insert(name.clone(), Binding::Empty);
}
fn bindings(&self) -> impl Iterator<Item = &Binding<S>> {
fn bindings(&self) -> impl Iterator<Item = &Binding> {
self.inner.values()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct Match<S> {
pub(super) bindings: Bindings<S>,
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub(super) struct Match {
pub(super) bindings: Bindings,
/// We currently just keep the first error and count the rest to compare matches.
pub(super) err: Option<ExpandError>,
pub(super) err_count: usize,
@ -98,19 +99,7 @@ pub(super) struct Match<S> {
pub(super) bound_count: usize,
}
impl<S> Default for Match<S> {
fn default() -> Self {
Self {
bindings: Default::default(),
err: Default::default(),
err_count: Default::default(),
unmatched_tts: Default::default(),
bound_count: Default::default(),
}
}
}
impl<S> Match<S> {
impl Match {
fn add_err(&mut self, err: ExpandError) {
let prev_err = self.err.take();
self.err = prev_err.or(Some(err));
@ -119,16 +108,12 @@ impl<S> Match<S> {
}
/// Matching errors are added to the `Match`.
pub(super) fn match_<S: Span>(
pattern: &MetaTemplate<S>,
input: &tt::Subtree<S>,
is_2021: bool,
) -> Match<S> {
pub(super) fn match_(pattern: &MetaTemplate, input: &tt::Subtree<Span>, is_2021: bool) -> Match {
let mut res = match_loop(pattern, input, is_2021);
res.bound_count = count(res.bindings.bindings());
return res;
fn count<'a, S: 'a>(bindings: impl Iterator<Item = &'a Binding<S>>) -> usize {
fn count<'a>(bindings: impl Iterator<Item = &'a Binding>) -> usize {
bindings
.map(|it| match it {
Binding::Fragment(_) => 1,
@ -141,10 +126,10 @@ pub(super) fn match_<S: Span>(
}
#[derive(Debug, Clone)]
enum BindingKind<S> {
enum BindingKind {
Empty(SmolStr),
Optional(SmolStr),
Fragment(SmolStr, Fragment<S>),
Fragment(SmolStr, Fragment),
Missing(SmolStr, MetaVarKind),
Nested(usize, usize),
}
@ -158,18 +143,13 @@ enum LinkNode<T> {
Parent { idx: usize, len: usize },
}
struct BindingsBuilder<S> {
nodes: Vec<Vec<LinkNode<Rc<BindingKind<S>>>>>,
#[derive(Default)]
struct BindingsBuilder {
nodes: Vec<Vec<LinkNode<Rc<BindingKind>>>>,
nested: Vec<Vec<LinkNode<usize>>>,
}
impl<S> Default for BindingsBuilder<S> {
fn default() -> Self {
Self { nodes: Default::default(), nested: Default::default() }
}
}
impl<S: Span> BindingsBuilder<S> {
impl BindingsBuilder {
fn alloc(&mut self) -> BindingsIdx {
let idx = self.nodes.len();
self.nodes.push(Vec::new());
@ -206,7 +186,7 @@ impl<S: Span> BindingsBuilder<S> {
self.nodes[idx.0].push(LinkNode::Node(Rc::new(BindingKind::Optional(var.clone()))));
}
fn push_fragment(&mut self, idx: &mut BindingsIdx, var: &SmolStr, fragment: Fragment<S>) {
fn push_fragment(&mut self, idx: &mut BindingsIdx, var: &SmolStr, fragment: Fragment) {
self.nodes[idx.0]
.push(LinkNode::Node(Rc::new(BindingKind::Fragment(var.clone(), fragment))));
}
@ -227,11 +207,11 @@ impl<S: Span> BindingsBuilder<S> {
idx.0 = new_idx;
}
fn build(self, idx: &BindingsIdx) -> Bindings<S> {
fn build(self, idx: &BindingsIdx) -> Bindings {
self.build_inner(&self.nodes[idx.0])
}
fn build_inner(&self, link_nodes: &[LinkNode<Rc<BindingKind<S>>>]) -> Bindings<S> {
fn build_inner(&self, link_nodes: &[LinkNode<Rc<BindingKind>>]) -> Bindings {
let mut bindings = Bindings::default();
let mut nodes = Vec::new();
self.collect_nodes(link_nodes, &mut nodes);
@ -281,7 +261,7 @@ impl<S: Span> BindingsBuilder<S> {
&'a self,
id: usize,
len: usize,
nested_refs: &mut Vec<&'a [LinkNode<Rc<BindingKind<S>>>]>,
nested_refs: &mut Vec<&'a [LinkNode<Rc<BindingKind>>]>,
) {
self.nested[id].iter().take(len).for_each(|it| match it {
LinkNode::Node(id) => nested_refs.push(&self.nodes[*id]),
@ -289,7 +269,7 @@ impl<S: Span> BindingsBuilder<S> {
});
}
fn collect_nested(&self, idx: usize, nested_idx: usize, nested: &mut Vec<Bindings<S>>) {
fn collect_nested(&self, idx: usize, nested_idx: usize, nested: &mut Vec<Bindings>) {
let last = &self.nodes[idx];
let mut nested_refs: Vec<&[_]> = Vec::new();
self.nested[nested_idx].iter().for_each(|it| match *it {
@ -300,7 +280,7 @@ impl<S: Span> BindingsBuilder<S> {
nested.extend(nested_refs.into_iter().map(|iter| self.build_inner(iter)));
}
fn collect_nodes_ref<'a>(&'a self, id: usize, len: usize, nodes: &mut Vec<&'a BindingKind<S>>) {
fn collect_nodes_ref<'a>(&'a self, id: usize, len: usize, nodes: &mut Vec<&'a BindingKind>) {
self.nodes[id].iter().take(len).for_each(|it| match it {
LinkNode::Node(it) => nodes.push(it),
LinkNode::Parent { idx, len } => self.collect_nodes_ref(*idx, *len, nodes),
@ -309,8 +289,8 @@ impl<S: Span> BindingsBuilder<S> {
fn collect_nodes<'a>(
&'a self,
link_nodes: &'a [LinkNode<Rc<BindingKind<S>>>],
nodes: &mut Vec<&'a BindingKind<S>>,
link_nodes: &'a [LinkNode<Rc<BindingKind>>],
nodes: &mut Vec<&'a BindingKind>,
) {
link_nodes.iter().for_each(|it| match it {
LinkNode::Node(it) => nodes.push(it),
@ -320,22 +300,22 @@ impl<S: Span> BindingsBuilder<S> {
}
#[derive(Debug, Clone)]
struct MatchState<'t, S> {
struct MatchState<'t> {
/// The position of the "dot" in this matcher
dot: OpDelimitedIter<'t, S>,
dot: OpDelimitedIter<'t>,
/// Token subtree stack
/// When matching against matchers with nested delimited submatchers (e.g., `pat ( pat ( .. )
/// pat ) pat`), we need to keep track of the matchers we are descending into. This stack does
/// that where the bottom of the stack is the outermost matcher.
stack: SmallVec<[OpDelimitedIter<'t, S>; 4]>,
stack: SmallVec<[OpDelimitedIter<'t>; 4]>,
/// The "parent" matcher position if we are in a repetition. That is, the matcher position just
/// before we enter the repetition.
up: Option<Box<MatchState<'t, S>>>,
up: Option<Box<MatchState<'t>>>,
/// The separator if we are in a repetition.
sep: Option<Separator<S>>,
sep: Option<Separator>,
/// The KleeneOp of this sequence if we are in a repetition.
sep_kind: Option<RepeatKind>,
@ -347,7 +327,7 @@ struct MatchState<'t, S> {
bindings: BindingsIdx,
/// Cached result of meta variable parsing
meta_result: Option<(TtIter<'t, S>, ExpandResult<Option<Fragment<S>>>)>,
meta_result: Option<(TtIter<'t, Span>, ExpandResult<Option<Fragment>>)>,
/// Is error occurred in this state, will `poised` to "parent"
is_error: bool,
@ -372,18 +352,18 @@ struct MatchState<'t, S> {
/// - `bb_items`: the set of items that are waiting for the black-box parser.
/// - `error_items`: the set of items in errors, used for error-resilient parsing
#[inline]
fn match_loop_inner<'t, S: Span>(
src: TtIter<'t, S>,
stack: &[TtIter<'t, S>],
res: &mut Match<S>,
bindings_builder: &mut BindingsBuilder<S>,
cur_items: &mut SmallVec<[MatchState<'t, S>; 1]>,
bb_items: &mut SmallVec<[MatchState<'t, S>; 1]>,
next_items: &mut Vec<MatchState<'t, S>>,
eof_items: &mut SmallVec<[MatchState<'t, S>; 1]>,
error_items: &mut SmallVec<[MatchState<'t, S>; 1]>,
fn match_loop_inner<'t>(
src: TtIter<'t, Span>,
stack: &[TtIter<'t, Span>],
res: &mut Match,
bindings_builder: &mut BindingsBuilder,
cur_items: &mut SmallVec<[MatchState<'t>; 1]>,
bb_items: &mut SmallVec<[MatchState<'t>; 1]>,
next_items: &mut Vec<MatchState<'t>>,
eof_items: &mut SmallVec<[MatchState<'t>; 1]>,
error_items: &mut SmallVec<[MatchState<'t>; 1]>,
is_2021: bool,
delim_span: tt::DelimSpan<S>,
delim_span: tt::DelimSpan<Span>,
) {
macro_rules! try_push {
($items: expr, $it:expr) => {
@ -607,10 +587,10 @@ fn match_loop_inner<'t, S: Span>(
}
}
fn match_loop<S: Span>(pattern: &MetaTemplate<S>, src: &tt::Subtree<S>, is_2021: bool) -> Match<S> {
fn match_loop(pattern: &MetaTemplate, src: &tt::Subtree<Span>, is_2021: bool) -> Match {
let span = src.delimiter.delim_span();
let mut src = TtIter::new(src);
let mut stack: SmallVec<[TtIter<'_, S>; 1]> = SmallVec::new();
let mut stack: SmallVec<[TtIter<'_, Span>; 1]> = SmallVec::new();
let mut res = Match::default();
let mut error_recover_item = None;
@ -758,12 +738,12 @@ fn match_loop<S: Span>(pattern: &MetaTemplate<S>, src: &tt::Subtree<S>, is_2021:
}
}
fn match_meta_var<S: Span>(
fn match_meta_var(
kind: MetaVarKind,
input: &mut TtIter<'_, S>,
input: &mut TtIter<'_, Span>,
is_2021: bool,
delim_span: DelimSpan<S>,
) -> ExpandResult<Option<Fragment<S>>> {
delim_span: DelimSpan<Span>,
) -> ExpandResult<Option<Fragment>> {
let fragment = match kind {
MetaVarKind::Path => {
return input.expect_fragment(parser::PrefixEntryPoint::Path).map(|it| {
@ -846,7 +826,7 @@ fn match_meta_var<S: Span>(
input.expect_fragment(fragment).map(|it| it.map(Fragment::Tokens))
}
fn collect_vars<S: Span>(collector_fun: &mut impl FnMut(SmolStr), pattern: &MetaTemplate<S>) {
fn collect_vars(collector_fun: &mut impl FnMut(SmolStr), pattern: &MetaTemplate) {
for op in pattern.iter() {
match op {
Op::Var { name, .. } => collector_fun(name.clone()),
@ -859,11 +839,11 @@ fn collect_vars<S: Span>(collector_fun: &mut impl FnMut(SmolStr), pattern: &Meta
}
}
}
impl<S: Span> MetaTemplate<S> {
fn iter_delimited_with(&self, delimiter: tt::Delimiter<S>) -> OpDelimitedIter<'_, S> {
impl MetaTemplate {
fn iter_delimited_with(&self, delimiter: tt::Delimiter<Span>) -> OpDelimitedIter<'_> {
OpDelimitedIter { inner: &self.0, idx: 0, delimited: delimiter }
}
fn iter_delimited(&self, span: tt::DelimSpan<S>) -> OpDelimitedIter<'_, S> {
fn iter_delimited(&self, span: tt::DelimSpan<Span>) -> OpDelimitedIter<'_> {
OpDelimitedIter {
inner: &self.0,
idx: 0,
@ -873,27 +853,27 @@ impl<S: Span> MetaTemplate<S> {
}
#[derive(Debug, Clone, Copy)]
enum OpDelimited<'a, S> {
Op(&'a Op<S>),
enum OpDelimited<'a> {
Op(&'a Op),
Open,
Close,
}
#[derive(Debug, Clone, Copy)]
struct OpDelimitedIter<'a, S> {
inner: &'a [Op<S>],
delimited: tt::Delimiter<S>,
struct OpDelimitedIter<'a> {
inner: &'a [Op],
delimited: tt::Delimiter<Span>,
idx: usize,
}
impl<'a, S: Span> OpDelimitedIter<'a, S> {
impl<'a> OpDelimitedIter<'a> {
fn is_eof(&self) -> bool {
let len = self.inner.len()
+ if self.delimited.kind != tt::DelimiterKind::Invisible { 2 } else { 0 };
self.idx >= len
}
fn peek(&self) -> Option<OpDelimited<'a, S>> {
fn peek(&self) -> Option<OpDelimited<'a>> {
match self.delimited.kind {
tt::DelimiterKind::Invisible => self.inner.get(self.idx).map(OpDelimited::Op),
_ => match self.idx {
@ -909,8 +889,8 @@ impl<'a, S: Span> OpDelimitedIter<'a, S> {
}
}
impl<'a, S: Span> Iterator for OpDelimitedIter<'a, S> {
type Item = OpDelimited<'a, S>;
impl<'a> Iterator for OpDelimitedIter<'a> {
type Item = OpDelimited<'a>;
fn next(&mut self) -> Option<Self::Item> {
let res = self.peek();
@ -926,8 +906,8 @@ impl<'a, S: Span> Iterator for OpDelimitedIter<'a, S> {
}
}
impl<S: Span> TtIter<'_, S> {
fn expect_separator(&mut self, separator: &Separator<S>) -> bool {
impl TtIter<'_, Span> {
fn expect_separator(&mut self, separator: &Separator) -> bool {
let mut fork = self.clone();
let ok = match separator {
Separator::Ident(lhs) => match fork.expect_ident_or_underscore() {
@ -957,7 +937,7 @@ impl<S: Span> TtIter<'_, S> {
ok
}
fn expect_tt(&mut self) -> Result<tt::TokenTree<S>, ()> {
fn expect_tt(&mut self) -> Result<tt::TokenTree<Span>, ()> {
if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = self.peek_n(0) {
if punct.char == '\'' {
self.expect_lifetime()
@ -976,7 +956,7 @@ impl<S: Span> TtIter<'_, S> {
}
}
fn expect_lifetime(&mut self) -> Result<tt::TokenTree<S>, ()> {
fn expect_lifetime(&mut self) -> Result<tt::TokenTree<Span>, ()> {
let punct = self.expect_single_punct()?;
if punct.char != '\'' {
return Err(());
@ -997,7 +977,7 @@ impl<S: Span> TtIter<'_, S> {
.into())
}
fn eat_char(&mut self, c: char) -> Option<tt::TokenTree<S>> {
fn eat_char(&mut self, c: char) -> Option<tt::TokenTree<Span>> {
let mut fork = self.clone();
match fork.expect_char(c) {
Ok(_) => {

View file

@ -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> {

View file

@ -17,8 +17,8 @@ mod tt_iter;
#[cfg(test)]
mod benchmark;
use span::Span;
use stdx::impl_from;
use tt::Span;
use std::fmt;
@ -127,8 +127,8 @@ impl fmt::Display for CountError {
/// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident`
/// and `$()*` have special meaning (see `Var` and `Repeat` data structures)
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DeclarativeMacro<S> {
rules: Box<[Rule<S>]>,
pub struct DeclarativeMacro {
rules: Box<[Rule]>,
// This is used for correctly determining the behavior of the pat fragment
// FIXME: This should be tracked by hygiene of the fragment identifier!
is_2021: bool,
@ -136,23 +136,23 @@ pub struct DeclarativeMacro<S> {
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct Rule<S> {
lhs: MetaTemplate<S>,
rhs: MetaTemplate<S>,
struct Rule {
lhs: MetaTemplate,
rhs: MetaTemplate,
}
impl<S: Span> DeclarativeMacro<S> {
pub fn from_err(err: ParseError, is_2021: bool) -> DeclarativeMacro<S> {
impl DeclarativeMacro {
pub fn from_err(err: ParseError, is_2021: bool) -> DeclarativeMacro {
DeclarativeMacro { rules: Box::default(), is_2021, err: Some(Box::new(err)) }
}
/// The old, `macro_rules! m {}` flavor.
pub fn parse_macro_rules(
tt: &tt::Subtree<S>,
tt: &tt::Subtree<Span>,
is_2021: bool,
// FIXME: Remove this once we drop support for rust 1.76 (defaults to true then)
new_meta_vars: bool,
) -> DeclarativeMacro<S> {
) -> DeclarativeMacro {
// Note: this parsing can be implemented using mbe machinery itself, by
// matching against `$($lhs:tt => $rhs:tt);*` pattern, but implementing
// manually seems easier.
@ -189,11 +189,11 @@ impl<S: Span> DeclarativeMacro<S> {
/// The new, unstable `macro m {}` flavor.
pub fn parse_macro2(
tt: &tt::Subtree<S>,
tt: &tt::Subtree<Span>,
is_2021: bool,
// FIXME: Remove this once we drop support for rust 1.76 (defaults to true then)
new_meta_vars: bool,
) -> DeclarativeMacro<S> {
) -> DeclarativeMacro {
let mut src = TtIter::new(tt);
let mut rules = Vec::new();
let mut err = None;
@ -249,18 +249,18 @@ impl<S: Span> DeclarativeMacro<S> {
pub fn expand(
&self,
tt: &tt::Subtree<S>,
marker: impl Fn(&mut S) + Copy,
tt: &tt::Subtree<Span>,
marker: impl Fn(&mut Span) + Copy,
new_meta_vars: bool,
call_site: S,
) -> ExpandResult<tt::Subtree<S>> {
call_site: Span,
) -> ExpandResult<tt::Subtree<Span>> {
expander::expand_rules(&self.rules, tt, marker, self.is_2021, new_meta_vars, call_site)
}
}
impl<S: Span> Rule<S> {
impl Rule {
fn parse(
src: &mut TtIter<'_, S>,
src: &mut TtIter<'_, Span>,
expect_arrow: bool,
new_meta_vars: bool,
) -> Result<Self, ParseError> {
@ -278,7 +278,7 @@ impl<S: Span> Rule<S> {
}
}
fn validate<S: Span>(pattern: &MetaTemplate<S>) -> Result<(), ParseError> {
fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> {
for op in pattern.iter() {
match op {
Op::Subtree { tokens, .. } => validate(tokens)?,

View file

@ -2,8 +2,8 @@
//! trees.
use smallvec::{smallvec, SmallVec};
use span::Span;
use syntax::SmolStr;
use tt::Span;
use crate::{tt_iter::TtIter, ParseError};
@ -21,25 +21,25 @@ use crate::{tt_iter::TtIter, ParseError};
/// Stuff to the right is a [`MetaTemplate`] template which is used to produce
/// output.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct MetaTemplate<S>(pub(crate) Box<[Op<S>]>);
pub(crate) struct MetaTemplate(pub(crate) Box<[Op]>);
impl<S: Span> MetaTemplate<S> {
pub(crate) fn parse_pattern(pattern: &tt::Subtree<S>) -> Result<Self, ParseError> {
impl MetaTemplate {
pub(crate) fn parse_pattern(pattern: &tt::Subtree<Span>) -> Result<Self, ParseError> {
MetaTemplate::parse(pattern, Mode::Pattern, false)
}
pub(crate) fn parse_template(
template: &tt::Subtree<S>,
template: &tt::Subtree<Span>,
new_meta_vars: bool,
) -> Result<Self, ParseError> {
MetaTemplate::parse(template, Mode::Template, new_meta_vars)
}
pub(crate) fn iter(&self) -> impl Iterator<Item = &Op<S>> {
pub(crate) fn iter(&self) -> impl Iterator<Item = &Op> {
self.0.iter()
}
fn parse(tt: &tt::Subtree<S>, mode: Mode, new_meta_vars: bool) -> Result<Self, ParseError> {
fn parse(tt: &tt::Subtree<Span>, mode: Mode, new_meta_vars: bool) -> Result<Self, ParseError> {
let mut src = TtIter::new(tt);
let mut res = Vec::new();
@ -53,15 +53,15 @@ impl<S: Span> MetaTemplate<S> {
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Op<S> {
pub(crate) enum Op {
Var {
name: SmolStr,
kind: Option<MetaVarKind>,
id: S,
id: Span,
},
Ignore {
name: SmolStr,
id: S,
id: Span,
},
Index {
depth: usize,
@ -75,17 +75,17 @@ pub(crate) enum Op<S> {
depth: Option<usize>,
},
Repeat {
tokens: MetaTemplate<S>,
tokens: MetaTemplate,
kind: RepeatKind,
separator: Option<Separator<S>>,
separator: Option<Separator>,
},
Subtree {
tokens: MetaTemplate<S>,
delimiter: tt::Delimiter<S>,
tokens: MetaTemplate,
delimiter: tt::Delimiter<Span>,
},
Literal(tt::Literal<S>),
Punct(SmallVec<[tt::Punct<S>; 3]>),
Ident(tt::Ident<S>),
Literal(tt::Literal<Span>),
Punct(SmallVec<[tt::Punct<Span>; 3]>),
Ident(tt::Ident<Span>),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@ -114,15 +114,15 @@ pub(crate) enum MetaVarKind {
}
#[derive(Clone, Debug, Eq)]
pub(crate) enum Separator<S> {
Literal(tt::Literal<S>),
Ident(tt::Ident<S>),
Puncts(SmallVec<[tt::Punct<S>; 3]>),
pub(crate) enum Separator {
Literal(tt::Literal<Span>),
Ident(tt::Ident<Span>),
Puncts(SmallVec<[tt::Punct<Span>; 3]>),
}
// Note that when we compare a Separator, we just care about its textual value.
impl<S> PartialEq for Separator<S> {
fn eq(&self, other: &Separator<S>) -> bool {
impl PartialEq for Separator {
fn eq(&self, other: &Separator) -> bool {
use Separator::*;
match (self, other) {
@ -144,12 +144,12 @@ enum Mode {
Template,
}
fn next_op<S: Span>(
first_peeked: &tt::TokenTree<S>,
src: &mut TtIter<'_, S>,
fn next_op(
first_peeked: &tt::TokenTree<Span>,
src: &mut TtIter<'_, Span>,
mode: Mode,
new_meta_vars: bool,
) -> Result<Op<S>, ParseError> {
) -> Result<Op, ParseError> {
let res = match first_peeked {
tt::TokenTree::Leaf(tt::Leaf::Punct(p @ tt::Punct { char: '$', .. })) => {
src.next().expect("first token already peeked");
@ -240,8 +240,8 @@ fn next_op<S: Span>(
Ok(res)
}
fn eat_fragment_kind<S: Span>(
src: &mut TtIter<'_, S>,
fn eat_fragment_kind(
src: &mut TtIter<'_, Span>,
mode: Mode,
) -> Result<Option<MetaVarKind>, ParseError> {
if let Mode::Pattern = mode {
@ -271,13 +271,11 @@ fn eat_fragment_kind<S: Span>(
Ok(None)
}
fn is_boolean_literal<S>(lit: &tt::Literal<S>) -> bool {
fn is_boolean_literal(lit: &tt::Literal<Span>) -> bool {
matches!(lit.text.as_str(), "true" | "false")
}
fn parse_repeat<S: Span>(
src: &mut TtIter<'_, S>,
) -> Result<(Option<Separator<S>>, RepeatKind), ParseError> {
fn parse_repeat(src: &mut TtIter<'_, Span>) -> Result<(Option<Separator>, RepeatKind), ParseError> {
let mut separator = Separator::Puncts(SmallVec::new());
for tt in src {
let tt = match tt {
@ -314,7 +312,7 @@ fn parse_repeat<S: Span>(
Err(ParseError::InvalidRepeat)
}
fn parse_metavar_expr<S: Span>(new_meta_vars: bool, src: &mut TtIter<'_, S>) -> Result<Op<S>, ()> {
fn parse_metavar_expr(new_meta_vars: bool, src: &mut TtIter<'_, Span>) -> Result<Op, ()> {
let func = src.expect_ident()?;
let args = src.expect_subtree()?;
@ -352,7 +350,7 @@ fn parse_metavar_expr<S: Span>(new_meta_vars: bool, src: &mut TtIter<'_, S>) ->
Ok(op)
}
fn parse_depth<S: Span>(src: &mut TtIter<'_, S>) -> Result<usize, ()> {
fn parse_depth(src: &mut TtIter<'_, Span>) -> Result<usize, ()> {
if src.len() == 0 {
Ok(0)
} else if let tt::Leaf::Literal(lit) = src.expect_literal()? {
@ -363,7 +361,7 @@ fn parse_depth<S: Span>(src: &mut TtIter<'_, S>) -> Result<usize, ()> {
}
}
fn try_eat_comma<S: Span>(src: &mut TtIter<'_, S>) -> bool {
fn try_eat_comma(src: &mut TtIter<'_, Span>) -> bool {
if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: ',', .. }))) = src.peek_n(0) {
let _ = src.next();
return true;