aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-04-19 09:56:39 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-04-19 09:56:39 +0100
commitab0a96586fd54858106cb6ac112d61eb657426f6 (patch)
tree86bbf2601069f6b0007dc181c824ef706d08c60d /crates/ra_syntax
parentd55f1136d6444b1f50b9092c36a976d0e1c26202 (diff)
parenta6d51e09610989821aaf79871bcab0661c9b0f74 (diff)
Merge #1148
1148: Add token_tree_to_xxx functions r=matklad a=edwin0cheng <del>As discus in PR #1147 , this PR added a `mbe::MacroKind` . Currently only 2 kind of macro are supported, `SourceFile` and `Block`.</del> Added following functions for `tt::TokenTree` and `ast::Node` conversion: * token_tree_to_expr * token_tree_to_pat * token_tree_to_ty * token_tree_to_macro_stmts * token_tree_to_macro_items And added two new syntax kind: * MACRO_ITEMS * MACRO_STMTS Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast/generated.rs66
-rw-r--r--crates/ra_syntax/src/grammar.ron15
2 files changed, 81 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 774d9bcc8..17de4f058 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -1770,6 +1770,72 @@ impl MacroCall {
1770 } 1770 }
1771} 1771}
1772 1772
1773// MacroItems
1774#[derive(Debug, PartialEq, Eq, Hash)]
1775#[repr(transparent)]
1776pub struct MacroItems {
1777 pub(crate) syntax: SyntaxNode,
1778}
1779unsafe impl TransparentNewType for MacroItems {
1780 type Repr = rowan::SyntaxNode;
1781}
1782
1783impl AstNode for MacroItems {
1784 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
1785 match syntax.kind() {
1786 MACRO_ITEMS => Some(MacroItems::from_repr(syntax.into_repr())),
1787 _ => None,
1788 }
1789 }
1790 fn syntax(&self) -> &SyntaxNode { &self.syntax }
1791}
1792
1793impl ToOwned for MacroItems {
1794 type Owned = TreeArc<MacroItems>;
1795 fn to_owned(&self) -> TreeArc<MacroItems> { TreeArc::cast(self.syntax.to_owned()) }
1796}
1797
1798
1799impl ast::ModuleItemOwner for MacroItems {}
1800impl ast::FnDefOwner for MacroItems {}
1801impl MacroItems {}
1802
1803// MacroStmts
1804#[derive(Debug, PartialEq, Eq, Hash)]
1805#[repr(transparent)]
1806pub struct MacroStmts {
1807 pub(crate) syntax: SyntaxNode,
1808}
1809unsafe impl TransparentNewType for MacroStmts {
1810 type Repr = rowan::SyntaxNode;
1811}
1812
1813impl AstNode for MacroStmts {
1814 fn cast(syntax: &SyntaxNode) -> Option<&Self> {
1815 match syntax.kind() {
1816 MACRO_STMTS => Some(MacroStmts::from_repr(syntax.into_repr())),
1817 _ => None,
1818 }
1819 }
1820 fn syntax(&self) -> &SyntaxNode { &self.syntax }
1821}
1822
1823impl ToOwned for MacroStmts {
1824 type Owned = TreeArc<MacroStmts>;
1825 fn to_owned(&self) -> TreeArc<MacroStmts> { TreeArc::cast(self.syntax.to_owned()) }
1826}
1827
1828
1829impl MacroStmts {
1830 pub fn statements(&self) -> impl Iterator<Item = &Stmt> {
1831 super::children(self)
1832 }
1833
1834 pub fn expr(&self) -> Option<&Expr> {
1835 super::child_opt(self)
1836 }
1837}
1838
1773// MatchArm 1839// MatchArm
1774#[derive(Debug, PartialEq, Eq, Hash)] 1840#[derive(Debug, PartialEq, Eq, Hash)]
1775#[repr(transparent)] 1841#[repr(transparent)]
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index b41241287..663e3a2f9 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -247,6 +247,10 @@ Grammar(
247 "ARG_LIST", 247 "ARG_LIST",
248 "TYPE_BOUND", 248 "TYPE_BOUND",
249 "TYPE_BOUND_LIST", 249 "TYPE_BOUND_LIST",
250
251 // macro related
252 "MACRO_ITEMS",
253 "MACRO_STMTS",
250 ], 254 ],
251 ast: { 255 ast: {
252 "SourceFile": ( 256 "SourceFile": (
@@ -668,5 +672,16 @@ Grammar(
668 "TypeArg": (options: ["TypeRef"]), 672 "TypeArg": (options: ["TypeRef"]),
669 "AssocTypeArg": (options: ["NameRef", "TypeRef"]), 673 "AssocTypeArg": (options: ["NameRef", "TypeRef"]),
670 "LifetimeArg": (), 674 "LifetimeArg": (),
675
676 "MacroItems": (
677 traits: [ "ModuleItemOwner", "FnDefOwner" ],
678 ),
679
680 "MacroStmts" : (
681 options: [ "Expr" ],
682 collections: [
683 ["statements", "Stmt"],
684 ],
685 )
671 }, 686 },
672) 687)