mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-20 03:49:51 +00:00
Move to upstream macro_rules! model
This commit is contained in:
parent
39aae835fd
commit
c1cb595382
36 changed files with 325 additions and 275 deletions
|
|
@ -128,7 +128,6 @@ pub struct MacroCall {
|
|||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
impl ast::AttrsOwner for MacroCall {}
|
||||
impl ast::NameOwner for MacroCall {}
|
||||
impl MacroCall {
|
||||
pub fn path(&self) -> Option<Path> { support::child(&self.syntax) }
|
||||
pub fn excl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![!]) }
|
||||
|
|
@ -273,6 +272,20 @@ impl Impl {
|
|||
pub fn assoc_item_list(&self) -> Option<AssocItemList> { support::child(&self.syntax) }
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct MacroRules {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
impl ast::AttrsOwner for MacroRules {}
|
||||
impl ast::NameOwner for MacroRules {}
|
||||
impl ast::VisibilityOwner for MacroRules {}
|
||||
impl MacroRules {
|
||||
pub fn macro_rules_token(&self) -> Option<SyntaxToken> {
|
||||
support::token(&self.syntax, T![macro_rules])
|
||||
}
|
||||
pub fn excl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![!]) }
|
||||
pub fn token_tree(&self) -> Option<TokenTree> { support::child(&self.syntax) }
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Module {
|
||||
pub(crate) syntax: SyntaxNode,
|
||||
}
|
||||
|
|
@ -1318,6 +1331,7 @@ pub enum Item {
|
|||
Fn(Fn),
|
||||
Impl(Impl),
|
||||
MacroCall(MacroCall),
|
||||
MacroRules(MacroRules),
|
||||
Module(Module),
|
||||
Static(Static),
|
||||
Struct(Struct),
|
||||
|
|
@ -1374,7 +1388,6 @@ pub enum AssocItem {
|
|||
TypeAlias(TypeAlias),
|
||||
}
|
||||
impl ast::AttrsOwner for AssocItem {}
|
||||
impl ast::NameOwner for AssocItem {}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum ExternItem {
|
||||
Fn(Fn),
|
||||
|
|
@ -1383,7 +1396,6 @@ pub enum ExternItem {
|
|||
TypeAlias(TypeAlias),
|
||||
}
|
||||
impl ast::AttrsOwner for ExternItem {}
|
||||
impl ast::NameOwner for ExternItem {}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum GenericParam {
|
||||
ConstParam(ConstParam),
|
||||
|
|
@ -1666,6 +1678,17 @@ impl AstNode for Impl {
|
|||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
impl AstNode for MacroRules {
|
||||
fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_RULES }
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
if Self::can_cast(syntax.kind()) {
|
||||
Some(Self { syntax })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn syntax(&self) -> &SyntaxNode { &self.syntax }
|
||||
}
|
||||
impl AstNode for Module {
|
||||
fn can_cast(kind: SyntaxKind) -> bool { kind == MODULE }
|
||||
fn cast(syntax: SyntaxNode) -> Option<Self> {
|
||||
|
|
@ -3060,6 +3083,9 @@ impl From<Impl> for Item {
|
|||
impl From<MacroCall> for Item {
|
||||
fn from(node: MacroCall) -> Item { Item::MacroCall(node) }
|
||||
}
|
||||
impl From<MacroRules> for Item {
|
||||
fn from(node: MacroRules) -> Item { Item::MacroRules(node) }
|
||||
}
|
||||
impl From<Module> for Item {
|
||||
fn from(node: Module) -> Item { Item::Module(node) }
|
||||
}
|
||||
|
|
@ -3084,8 +3110,8 @@ impl From<Use> for Item {
|
|||
impl AstNode for Item {
|
||||
fn can_cast(kind: SyntaxKind) -> bool {
|
||||
match kind {
|
||||
CONST | ENUM | EXTERN_BLOCK | EXTERN_CRATE | FN | IMPL | MACRO_CALL | MODULE
|
||||
| STATIC | STRUCT | TRAIT | TYPE_ALIAS | UNION | USE => true,
|
||||
CONST | ENUM | EXTERN_BLOCK | EXTERN_CRATE | FN | IMPL | MACRO_CALL | MACRO_RULES
|
||||
| MODULE | STATIC | STRUCT | TRAIT | TYPE_ALIAS | UNION | USE => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
@ -3098,6 +3124,7 @@ impl AstNode for Item {
|
|||
FN => Item::Fn(Fn { syntax }),
|
||||
IMPL => Item::Impl(Impl { syntax }),
|
||||
MACRO_CALL => Item::MacroCall(MacroCall { syntax }),
|
||||
MACRO_RULES => Item::MacroRules(MacroRules { syntax }),
|
||||
MODULE => Item::Module(Module { syntax }),
|
||||
STATIC => Item::Static(Static { syntax }),
|
||||
STRUCT => Item::Struct(Struct { syntax }),
|
||||
|
|
@ -3118,6 +3145,7 @@ impl AstNode for Item {
|
|||
Item::Fn(it) => &it.syntax,
|
||||
Item::Impl(it) => &it.syntax,
|
||||
Item::MacroCall(it) => &it.syntax,
|
||||
Item::MacroRules(it) => &it.syntax,
|
||||
Item::Module(it) => &it.syntax,
|
||||
Item::Static(it) => &it.syntax,
|
||||
Item::Struct(it) => &it.syntax,
|
||||
|
|
@ -3582,6 +3610,11 @@ impl std::fmt::Display for Impl {
|
|||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
}
|
||||
}
|
||||
impl std::fmt::Display for MacroRules {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
}
|
||||
}
|
||||
impl std::fmt::Display for Module {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(self.syntax(), f)
|
||||
|
|
|
|||
|
|
@ -382,21 +382,6 @@ impl ast::Visibility {
|
|||
}
|
||||
}
|
||||
|
||||
impl ast::MacroCall {
|
||||
pub fn is_macro_rules(&self) -> Option<ast::Name> {
|
||||
let name_ref = self.path()?.segment()?.name_ref()?;
|
||||
if name_ref.text() == "macro_rules" {
|
||||
self.name()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_bang(&self) -> bool {
|
||||
self.is_macro_rules().is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::LifetimeParam {
|
||||
pub fn lifetime_bounds(&self) -> impl Iterator<Item = SyntaxToken> {
|
||||
self.syntax()
|
||||
|
|
@ -476,5 +461,5 @@ impl ast::DocCommentsOwner for ast::Static {}
|
|||
impl ast::DocCommentsOwner for ast::Const {}
|
||||
impl ast::DocCommentsOwner for ast::TypeAlias {}
|
||||
impl ast::DocCommentsOwner for ast::Impl {}
|
||||
impl ast::DocCommentsOwner for ast::MacroCall {}
|
||||
impl ast::DocCommentsOwner for ast::MacroRules {}
|
||||
impl ast::DocCommentsOwner for ast::Use {}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ pub fn type_label(node: &ast::TypeAlias) -> String {
|
|||
label.trim().to_owned()
|
||||
}
|
||||
|
||||
pub fn macro_label(node: &ast::MacroCall) -> String {
|
||||
pub fn macro_label(node: &ast::MacroRules) -> String {
|
||||
let name = node.name().map(|name| name.syntax().text().to_string()).unwrap_or_default();
|
||||
let vis = if node.has_atom_attr("macro_export") { "#[macro_export]\n" } else { "" };
|
||||
format!("{}macro_rules! {}", vis, name)
|
||||
|
|
|
|||
|
|
@ -147,8 +147,8 @@ fn n_attached_trivias<'a>(
|
|||
trivias: impl Iterator<Item = (SyntaxKind, &'a str)>,
|
||||
) -> usize {
|
||||
match kind {
|
||||
MACRO_CALL | CONST | TYPE_ALIAS | STRUCT | ENUM | VARIANT | FN | TRAIT | MODULE
|
||||
| RECORD_FIELD | STATIC | USE => {
|
||||
MACRO_CALL | MACRO_RULES | CONST | TYPE_ALIAS | STRUCT | ENUM | VARIANT | FN | TRAIT
|
||||
| MODULE | RECORD_FIELD | STATIC | USE => {
|
||||
let mut res = 0;
|
||||
let mut trivias = trivias.enumerate().peekable();
|
||||
|
||||
|
|
|
|||
|
|
@ -17,14 +17,17 @@ SOURCE_FILE@0..42
|
|||
IDENT@28..31 "bin"
|
||||
ERROR@31..32
|
||||
SLASH@31..32 "/"
|
||||
MACRO_CALL@32..41
|
||||
MACRO_CALL@32..35
|
||||
PATH@32..35
|
||||
PATH_SEGMENT@32..35
|
||||
NAME_REF@32..35
|
||||
IDENT@32..35 "env"
|
||||
WHITESPACE@35..36 " "
|
||||
NAME@36..41
|
||||
IDENT@36..41 "rusti"
|
||||
WHITESPACE@35..36 " "
|
||||
MACRO_CALL@36..41
|
||||
PATH@36..41
|
||||
PATH_SEGMENT@36..41
|
||||
NAME_REF@36..41
|
||||
IDENT@36..41 "rusti"
|
||||
WHITESPACE@41..42 "\n"
|
||||
error 23..23: expected `[`
|
||||
error 23..23: expected an item
|
||||
|
|
@ -35,5 +38,8 @@ error 31..31: expected `{`, `[`, `(`
|
|||
error 31..31: expected SEMICOLON
|
||||
error 31..31: expected an item
|
||||
error 35..35: expected BANG
|
||||
error 35..35: expected `{`, `[`, `(`
|
||||
error 35..35: expected SEMICOLON
|
||||
error 41..41: expected BANG
|
||||
error 41..41: expected `{`, `[`, `(`
|
||||
error 41..41: expected SEMICOLON
|
||||
|
|
|
|||
|
|
@ -12,11 +12,8 @@ SOURCE_FILE@0..70
|
|||
L_CURLY@9..10 "{"
|
||||
R_CURLY@10..11 "}"
|
||||
WHITESPACE@11..12 "\n"
|
||||
MACRO_CALL@12..31
|
||||
PATH@12..23
|
||||
PATH_SEGMENT@12..23
|
||||
NAME_REF@12..23
|
||||
IDENT@12..23 "macro_rules"
|
||||
MACRO_RULES@12..31
|
||||
MACRO_RULES_KW@12..23 "macro_rules"
|
||||
BANG@23..24 "!"
|
||||
WHITESPACE@24..25 " "
|
||||
NAME@25..28
|
||||
|
|
|
|||
|
|
@ -82,32 +82,28 @@ SOURCE_FILE@0..167
|
|||
L_CURLY@102..103 "{"
|
||||
R_CURLY@103..104 "}"
|
||||
WHITESPACE@104..109 "\n "
|
||||
EXPR_STMT@109..152
|
||||
MACRO_CALL@109..152
|
||||
PATH@109..120
|
||||
PATH_SEGMENT@109..120
|
||||
NAME_REF@109..120
|
||||
IDENT@109..120 "macro_rules"
|
||||
BANG@120..121 "!"
|
||||
WHITESPACE@121..122 " "
|
||||
NAME@122..126
|
||||
IDENT@122..126 "test"
|
||||
WHITESPACE@126..127 " "
|
||||
TOKEN_TREE@127..152
|
||||
L_CURLY@127..128 "{"
|
||||
WHITESPACE@128..138 "\n "
|
||||
TOKEN_TREE@138..140
|
||||
L_PAREN@138..139 "("
|
||||
R_PAREN@139..140 ")"
|
||||
WHITESPACE@140..141 " "
|
||||
EQ@141..142 "="
|
||||
R_ANGLE@142..143 ">"
|
||||
WHITESPACE@143..144 " "
|
||||
TOKEN_TREE@144..146
|
||||
L_CURLY@144..145 "{"
|
||||
R_CURLY@145..146 "}"
|
||||
WHITESPACE@146..151 "\n "
|
||||
R_CURLY@151..152 "}"
|
||||
MACRO_RULES@109..152
|
||||
MACRO_RULES_KW@109..120 "macro_rules"
|
||||
BANG@120..121 "!"
|
||||
WHITESPACE@121..122 " "
|
||||
NAME@122..126
|
||||
IDENT@122..126 "test"
|
||||
WHITESPACE@126..127 " "
|
||||
TOKEN_TREE@127..152
|
||||
L_CURLY@127..128 "{"
|
||||
WHITESPACE@128..138 "\n "
|
||||
TOKEN_TREE@138..140
|
||||
L_PAREN@138..139 "("
|
||||
R_PAREN@139..140 ")"
|
||||
WHITESPACE@140..141 " "
|
||||
EQ@141..142 "="
|
||||
R_ANGLE@142..143 ">"
|
||||
WHITESPACE@143..144 " "
|
||||
TOKEN_TREE@144..146
|
||||
L_CURLY@144..145 "{"
|
||||
R_CURLY@145..146 "}"
|
||||
WHITESPACE@146..151 "\n "
|
||||
R_CURLY@151..152 "}"
|
||||
WHITESPACE@152..157 "\n "
|
||||
MACRO_CALL@157..164
|
||||
PATH@157..161
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
SOURCE_FILE@0..30
|
||||
MACRO_CALL@0..29
|
||||
PATH@0..11
|
||||
PATH_SEGMENT@0..11
|
||||
NAME_REF@0..11
|
||||
IDENT@0..11 "macro_rules"
|
||||
MACRO_RULES@0..29
|
||||
MACRO_RULES_KW@0..11 "macro_rules"
|
||||
BANG@11..12 "!"
|
||||
WHITESPACE@12..13 " "
|
||||
NAME@13..16
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
SOURCE_FILE@0..65
|
||||
MACRO_CALL@0..64
|
||||
MACRO_RULES@0..64
|
||||
COMMENT@0..13 "/// Some docs"
|
||||
WHITESPACE@13..14 "\n"
|
||||
ATTR@14..29
|
||||
|
|
@ -11,10 +11,7 @@ SOURCE_FILE@0..65
|
|||
IDENT@16..28 "macro_export"
|
||||
R_BRACK@28..29 "]"
|
||||
WHITESPACE@29..30 "\n"
|
||||
PATH@30..41
|
||||
PATH_SEGMENT@30..41
|
||||
NAME_REF@30..41
|
||||
IDENT@30..41 "macro_rules"
|
||||
MACRO_RULES_KW@30..41 "macro_rules"
|
||||
BANG@41..42 "!"
|
||||
WHITESPACE@42..43 " "
|
||||
NAME@43..46
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue