diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index 3137bd91cf..bcda2381a4 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs @@ -61,7 +61,6 @@ use std::rc::Rc; -use parser::ParserEntryPoint; use smallvec::{smallvec, SmallVec}; use syntax::SmolStr; @@ -690,41 +689,21 @@ fn match_leaf(lhs: &tt::Leaf, src: &mut TtIter) -> Result<(), ExpandError> { fn match_meta_var(kind: &str, input: &mut TtIter) -> ExpandResult> { let fragment = match kind { - "path" => { - return input - .expect_fragment2(parser::PrefixEntryPoint::Path) - .map(|tt| tt.map(Fragment::Tokens)); - } - "expr" => { - return input - .expect_fragment2(parser::PrefixEntryPoint::Expr) - .map(|tt| tt.map(Fragment::Expr)); - } - "ty" => { - return input - .expect_fragment2(parser::PrefixEntryPoint::Ty) - .map(|tt| tt.map(Fragment::Tokens)); - } + "path" => parser::PrefixEntryPoint::Path, + "ty" => parser::PrefixEntryPoint::Ty, // FIXME: These two should actually behave differently depending on the edition. // // https://doc.rust-lang.org/edition-guide/rust-2021/or-patterns-macro-rules.html - "pat" | "pat_param" => { + "pat" | "pat_param" => parser::PrefixEntryPoint::Pat, + "stmt" => parser::PrefixEntryPoint::Stmt, + "block" => parser::PrefixEntryPoint::Block, + "meta" => parser::PrefixEntryPoint::MetaItem, + "item" => parser::PrefixEntryPoint::Item, + "expr" => { return input - .expect_fragment2(parser::PrefixEntryPoint::Pat) - .map(|tt| tt.map(Fragment::Tokens)); + .expect_fragment(parser::PrefixEntryPoint::Expr) + .map(|tt| tt.map(Fragment::Expr)) } - "stmt" => { - return input - .expect_fragment2(parser::PrefixEntryPoint::Stmt) - .map(|tt| tt.map(Fragment::Tokens)); - } - "block" => { - return input - .expect_fragment2(parser::PrefixEntryPoint::Block) - .map(|tt| tt.map(Fragment::Tokens)); - } - "meta" => ParserEntryPoint::MetaItem, - "item" => ParserEntryPoint::Item, _ => { let tt_result = match kind { "ident" => input @@ -752,14 +731,13 @@ fn match_meta_var(kind: &str, input: &mut TtIter) -> ExpandResult Ok(input.expect_fragment2(parser::PrefixEntryPoint::Vis).value), + "vis" => Ok(input.expect_fragment(parser::PrefixEntryPoint::Vis).value), _ => Err(ExpandError::UnexpectedToken), }; return tt_result.map(|it| it.map(Fragment::Tokens)).into(); } }; - let result = input.expect_fragment(fragment); - result.map(|tt| if kind == "expr" { tt.map(Fragment::Expr) } else { tt.map(Fragment::Tokens) }) + input.expect_fragment(fragment).map(|it| it.map(Fragment::Tokens)) } fn collect_vars(buf: &mut Vec, pattern: &MetaTemplate) { diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index f0c1f806ff..896e33663e 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -106,7 +106,7 @@ pub fn parse_exprs_with_sep(tt: &tt::Subtree, sep: char) -> Vec { let mut res = Vec::new(); while iter.peek_n(0).is_some() { - let expanded = iter.expect_fragment(ParserEntryPoint::Expr); + let expanded = iter.expect_fragment(parser::PrefixEntryPoint::Expr); res.push(match expanded.value { None => break, diff --git a/crates/mbe/src/tt_iter.rs b/crates/mbe/src/tt_iter.rs index 03d0cbee8b..6c9f615c7a 100644 --- a/crates/mbe/src/tt_iter.rs +++ b/crates/mbe/src/tt_iter.rs @@ -1,7 +1,7 @@ //! A "Parser" structure for token trees. We use this when parsing a declarative //! macro definition into a list of patterns and templates. -use crate::{to_parser_input::to_parser_input, ExpandError, ExpandResult, ParserEntryPoint}; +use crate::{to_parser_input::to_parser_input, ExpandError, ExpandResult}; use syntax::SyntaxKind; use tt::buffer::TokenBuffer; @@ -90,63 +90,6 @@ impl<'a> TtIter<'a> { } pub(crate) fn expect_fragment( - &mut self, - entry_point: ParserEntryPoint, - ) -> ExpandResult> { - let buffer = TokenBuffer::from_tokens(self.inner.as_slice()); - let parser_input = to_parser_input(&buffer); - let tree_traversal = parser::parse(&parser_input, entry_point); - - let mut cursor = buffer.begin(); - let mut error = false; - for step in tree_traversal.iter() { - match step { - parser::Step::Token { kind, mut n_input_tokens } => { - if kind == SyntaxKind::LIFETIME_IDENT { - n_input_tokens = 2; - } - for _ in 0..n_input_tokens { - cursor = cursor.bump_subtree(); - } - } - parser::Step::Enter { .. } | parser::Step::Exit => (), - parser::Step::Error { .. } => error = true, - } - } - - let mut err = if !cursor.is_root() || error { - Some(err!("expected {:?}", entry_point)) - } else { - None - }; - - let mut curr = buffer.begin(); - let mut res = vec![]; - - if cursor.is_root() { - while curr != cursor { - if let Some(token) = curr.token_tree() { - res.push(token); - } - curr = curr.bump(); - } - } - self.inner = self.inner.as_slice()[res.len()..].iter(); - if res.is_empty() && err.is_none() { - err = Some(err!("no tokens consumed")); - } - let res = match res.len() { - 1 => Some(res[0].cloned()), - 0 => None, - _ => Some(tt::TokenTree::Subtree(tt::Subtree { - delimiter: None, - token_trees: res.into_iter().map(|it| it.cloned()).collect(), - })), - }; - ExpandResult { value: res, err } - } - - pub(crate) fn expect_fragment2( &mut self, entry_point: parser::PrefixEntryPoint, ) -> ExpandResult> { diff --git a/crates/parser/src/grammar.rs b/crates/parser/src/grammar.rs index 8310b38b92..0e663b9ac0 100644 --- a/crates/parser/src/grammar.rs +++ b/crates/parser/src/grammar.rs @@ -75,6 +75,13 @@ pub(crate) mod entry { pub(crate) fn path(p: &mut Parser) { let _ = paths::type_path(p); } + pub(crate) fn item(p: &mut Parser) { + items::item_or_macro(p, true); + } + // Parse a meta item , which excluded [], e.g : #[ MetaItem ] + pub(crate) fn meta_item(p: &mut Parser) { + attributes::meta(p); + } } } @@ -92,15 +99,6 @@ pub(crate) mod entry_points { expressions::stmt(p, expressions::StmtWithSemi::Optional, false); } - // Parse a meta item , which excluded [], e.g : #[ MetaItem ] - pub(crate) fn meta_item(p: &mut Parser) { - attributes::meta(p); - } - - pub(crate) fn item(p: &mut Parser) { - items::item_or_macro(p, true); - } - pub(crate) fn macro_items(p: &mut Parser) { let m = p.start(); items::mod_contents(p, false); diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 867acc45f3..9039985da2 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -59,6 +59,8 @@ pub enum PrefixEntryPoint { Ty, Expr, Path, + Item, + MetaItem, } impl PrefixEntryPoint { @@ -71,6 +73,8 @@ impl PrefixEntryPoint { PrefixEntryPoint::Ty => grammar::entry::prefix::ty, PrefixEntryPoint::Expr => grammar::entry::prefix::expr, PrefixEntryPoint::Path => grammar::entry::prefix::path, + PrefixEntryPoint::Item => grammar::entry::prefix::item, + PrefixEntryPoint::MetaItem => grammar::entry::prefix::meta_item, }; let mut p = parser::Parser::new(input); entry_point(&mut p); @@ -118,8 +122,8 @@ pub fn parse(inp: &Input, entry_point: ParserEntryPoint) -> Output { ParserEntryPoint::Expr => grammar::entry::prefix::expr, ParserEntryPoint::Type => grammar::entry::prefix::ty, ParserEntryPoint::Pattern => grammar::entry::prefix::pat, - ParserEntryPoint::Item => grammar::entry_points::item, - ParserEntryPoint::MetaItem => grammar::entry_points::meta_item, + ParserEntryPoint::Item => grammar::entry::prefix::item, + ParserEntryPoint::MetaItem => grammar::entry::prefix::meta_item, ParserEntryPoint::StatementOptionalSemi => grammar::entry_points::stmt_optional_semi, ParserEntryPoint::Items => grammar::entry_points::macro_items, ParserEntryPoint::Statements => grammar::entry_points::macro_stmts,