mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
simplify
This commit is contained in:
parent
369001615f
commit
8e7fc7be65
5 changed files with 27 additions and 104 deletions
|
@ -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<Option<Fragment>> {
|
||||
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<Option<Fragmen
|
|||
.map_err(|()| err!())
|
||||
}
|
||||
// `vis` is optional
|
||||
"vis" => 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<SmolStr>, pattern: &MetaTemplate) {
|
||||
|
|
|
@ -106,7 +106,7 @@ pub fn parse_exprs_with_sep(tt: &tt::Subtree, sep: char) -> Vec<tt::Subtree> {
|
|||
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,
|
||||
|
|
|
@ -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<Option<tt::TokenTree>> {
|
||||
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<Option<tt::TokenTree>> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue