mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +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 std::rc::Rc;
|
||||||
|
|
||||||
use parser::ParserEntryPoint;
|
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
use syntax::SmolStr;
|
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>> {
|
fn match_meta_var(kind: &str, input: &mut TtIter) -> ExpandResult<Option<Fragment>> {
|
||||||
let fragment = match kind {
|
let fragment = match kind {
|
||||||
"path" => {
|
"path" => parser::PrefixEntryPoint::Path,
|
||||||
return input
|
"ty" => parser::PrefixEntryPoint::Ty,
|
||||||
.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));
|
|
||||||
}
|
|
||||||
// FIXME: These two should actually behave differently depending on the edition.
|
// 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
|
// 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
|
return input
|
||||||
.expect_fragment2(parser::PrefixEntryPoint::Pat)
|
.expect_fragment(parser::PrefixEntryPoint::Expr)
|
||||||
.map(|tt| tt.map(Fragment::Tokens));
|
.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 {
|
let tt_result = match kind {
|
||||||
"ident" => input
|
"ident" => input
|
||||||
|
@ -752,14 +731,13 @@ fn match_meta_var(kind: &str, input: &mut TtIter) -> ExpandResult<Option<Fragmen
|
||||||
.map_err(|()| err!())
|
.map_err(|()| err!())
|
||||||
}
|
}
|
||||||
// `vis` is optional
|
// `vis` is optional
|
||||||
"vis" => Ok(input.expect_fragment2(parser::PrefixEntryPoint::Vis).value),
|
"vis" => Ok(input.expect_fragment(parser::PrefixEntryPoint::Vis).value),
|
||||||
_ => Err(ExpandError::UnexpectedToken),
|
_ => Err(ExpandError::UnexpectedToken),
|
||||||
};
|
};
|
||||||
return tt_result.map(|it| it.map(Fragment::Tokens)).into();
|
return tt_result.map(|it| it.map(Fragment::Tokens)).into();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let result = input.expect_fragment(fragment);
|
input.expect_fragment(fragment).map(|it| it.map(Fragment::Tokens))
|
||||||
result.map(|tt| if kind == "expr" { tt.map(Fragment::Expr) } else { tt.map(Fragment::Tokens) })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_vars(buf: &mut Vec<SmolStr>, pattern: &MetaTemplate) {
|
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();
|
let mut res = Vec::new();
|
||||||
|
|
||||||
while iter.peek_n(0).is_some() {
|
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 {
|
res.push(match expanded.value {
|
||||||
None => break,
|
None => break,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
//! A "Parser" structure for token trees. We use this when parsing a declarative
|
//! A "Parser" structure for token trees. We use this when parsing a declarative
|
||||||
//! macro definition into a list of patterns and templates.
|
//! 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 syntax::SyntaxKind;
|
||||||
use tt::buffer::TokenBuffer;
|
use tt::buffer::TokenBuffer;
|
||||||
|
@ -90,63 +90,6 @@ impl<'a> TtIter<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn expect_fragment(
|
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,
|
&mut self,
|
||||||
entry_point: parser::PrefixEntryPoint,
|
entry_point: parser::PrefixEntryPoint,
|
||||||
) -> ExpandResult<Option<tt::TokenTree>> {
|
) -> ExpandResult<Option<tt::TokenTree>> {
|
||||||
|
|
|
@ -75,6 +75,13 @@ pub(crate) mod entry {
|
||||||
pub(crate) fn path(p: &mut Parser) {
|
pub(crate) fn path(p: &mut Parser) {
|
||||||
let _ = paths::type_path(p);
|
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);
|
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) {
|
pub(crate) fn macro_items(p: &mut Parser) {
|
||||||
let m = p.start();
|
let m = p.start();
|
||||||
items::mod_contents(p, false);
|
items::mod_contents(p, false);
|
||||||
|
|
|
@ -59,6 +59,8 @@ pub enum PrefixEntryPoint {
|
||||||
Ty,
|
Ty,
|
||||||
Expr,
|
Expr,
|
||||||
Path,
|
Path,
|
||||||
|
Item,
|
||||||
|
MetaItem,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrefixEntryPoint {
|
impl PrefixEntryPoint {
|
||||||
|
@ -71,6 +73,8 @@ impl PrefixEntryPoint {
|
||||||
PrefixEntryPoint::Ty => grammar::entry::prefix::ty,
|
PrefixEntryPoint::Ty => grammar::entry::prefix::ty,
|
||||||
PrefixEntryPoint::Expr => grammar::entry::prefix::expr,
|
PrefixEntryPoint::Expr => grammar::entry::prefix::expr,
|
||||||
PrefixEntryPoint::Path => grammar::entry::prefix::path,
|
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);
|
let mut p = parser::Parser::new(input);
|
||||||
entry_point(&mut p);
|
entry_point(&mut p);
|
||||||
|
@ -118,8 +122,8 @@ pub fn parse(inp: &Input, entry_point: ParserEntryPoint) -> Output {
|
||||||
ParserEntryPoint::Expr => grammar::entry::prefix::expr,
|
ParserEntryPoint::Expr => grammar::entry::prefix::expr,
|
||||||
ParserEntryPoint::Type => grammar::entry::prefix::ty,
|
ParserEntryPoint::Type => grammar::entry::prefix::ty,
|
||||||
ParserEntryPoint::Pattern => grammar::entry::prefix::pat,
|
ParserEntryPoint::Pattern => grammar::entry::prefix::pat,
|
||||||
ParserEntryPoint::Item => grammar::entry_points::item,
|
ParserEntryPoint::Item => grammar::entry::prefix::item,
|
||||||
ParserEntryPoint::MetaItem => grammar::entry_points::meta_item,
|
ParserEntryPoint::MetaItem => grammar::entry::prefix::meta_item,
|
||||||
ParserEntryPoint::StatementOptionalSemi => grammar::entry_points::stmt_optional_semi,
|
ParserEntryPoint::StatementOptionalSemi => grammar::entry_points::stmt_optional_semi,
|
||||||
ParserEntryPoint::Items => grammar::entry_points::macro_items,
|
ParserEntryPoint::Items => grammar::entry_points::macro_items,
|
||||||
ParserEntryPoint::Statements => grammar::entry_points::macro_stmts,
|
ParserEntryPoint::Statements => grammar::entry_points::macro_stmts,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue