diff options
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 22 | ||||
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 23 | ||||
-rw-r--r-- | crates/ra_parser/src/grammar.rs | 6 | ||||
-rw-r--r-- | crates/ra_parser/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_parser/src/syntax_kind/generated.rs | 4 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 66 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar.ron | 15 |
7 files changed, 132 insertions, 8 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 2f47e32d3..4cfa1f955 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs | |||
@@ -37,9 +37,15 @@ pub enum ExpandError { | |||
37 | NoMatchingRule, | 37 | NoMatchingRule, |
38 | UnexpectedToken, | 38 | UnexpectedToken, |
39 | BindingError(String), | 39 | BindingError(String), |
40 | ConversionError, | ||
40 | } | 41 | } |
41 | 42 | ||
42 | pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list, syntax_node_to_token_tree}; | 43 | pub use crate::syntax_bridge::{ |
44 | ast_to_token_tree, | ||
45 | token_tree_to_ast_item_list, | ||
46 | syntax_node_to_token_tree, | ||
47 | token_tree_to_macro_items, | ||
48 | }; | ||
43 | 49 | ||
44 | /// This struct contains AST for a single `macro_rules` definition. What might | 50 | /// This struct contains AST for a single `macro_rules` definition. What might |
45 | /// be very confusing is that AST has almost exactly the same shape as | 51 | /// be very confusing is that AST has almost exactly the same shape as |
@@ -192,21 +198,21 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
192 | pub(crate) fn expand_to_syntax( | 198 | pub(crate) fn expand_to_syntax( |
193 | rules: &MacroRules, | 199 | rules: &MacroRules, |
194 | invocation: &str, | 200 | invocation: &str, |
195 | ) -> ra_syntax::TreeArc<ast::SourceFile> { | 201 | ) -> ra_syntax::TreeArc<ast::MacroItems> { |
196 | let expanded = expand(rules, invocation); | 202 | let expanded = expand(rules, invocation); |
197 | token_tree_to_ast_item_list(&expanded) | 203 | token_tree_to_macro_items(&expanded) |
198 | } | 204 | } |
199 | 205 | ||
200 | pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { | 206 | pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { |
201 | let expanded = expand(rules, invocation); | 207 | let expanded = expand(rules, invocation); |
202 | assert_eq!(expanded.to_string(), expansion); | 208 | assert_eq!(expanded.to_string(), expansion); |
203 | 209 | ||
204 | let tree = token_tree_to_ast_item_list(&expanded); | 210 | let tree = token_tree_to_macro_items(&expanded); |
205 | 211 | ||
206 | // Eat all white space by parse it back and forth | 212 | // Eat all white space by parse it back and forth |
207 | let expansion = ast::SourceFile::parse(expansion); | 213 | let expansion = ast::SourceFile::parse(expansion); |
208 | let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0; | 214 | let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0; |
209 | let file = token_tree_to_ast_item_list(&expansion); | 215 | let file = token_tree_to_macro_items(&expansion); |
210 | 216 | ||
211 | assert_eq!(tree.syntax().debug_dump().trim(), file.syntax().debug_dump().trim()); | 217 | assert_eq!(tree.syntax().debug_dump().trim(), file.syntax().debug_dump().trim()); |
212 | } | 218 | } |
@@ -346,11 +352,11 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
346 | ", | 352 | ", |
347 | ); | 353 | ); |
348 | let expansion = expand(&rules, "structs!(Foo, Bar)"); | 354 | let expansion = expand(&rules, "structs!(Foo, Bar)"); |
349 | let tree = token_tree_to_ast_item_list(&expansion); | 355 | let tree = token_tree_to_macro_items(&expansion); |
350 | assert_eq!( | 356 | assert_eq!( |
351 | tree.syntax().debug_dump().trim(), | 357 | tree.syntax().debug_dump().trim(), |
352 | r#" | 358 | r#" |
353 | SOURCE_FILE@[0; 40) | 359 | MACRO_ITEMS@[0; 40) |
354 | STRUCT_DEF@[0; 20) | 360 | STRUCT_DEF@[0; 20) |
355 | STRUCT_KW@[0; 6) "struct" | 361 | STRUCT_KW@[0; 6) "struct" |
356 | NAME@[6; 9) | 362 | NAME@[6; 9) |
@@ -527,7 +533,7 @@ SOURCE_FILE@[0; 40) | |||
527 | 533 | ||
528 | assert_eq!( | 534 | assert_eq!( |
529 | expand_to_syntax(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(), | 535 | expand_to_syntax(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(), |
530 | r#"SOURCE_FILE@[0; 15) | 536 | r#"MACRO_ITEMS@[0; 15) |
531 | FN_DEF@[0; 15) | 537 | FN_DEF@[0; 15) |
532 | FN_KW@[0; 2) "fn" | 538 | FN_KW@[0; 2) "fn" |
533 | NAME@[2; 5) | 539 | NAME@[2; 5) |
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 28ded7870..19e09be80 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -30,6 +30,29 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke | |||
30 | Some((tt, token_map)) | 30 | Some((tt, token_map)) |
31 | } | 31 | } |
32 | 32 | ||
33 | // The following items are what `rustc` macro can be parsed into : | ||
34 | // link: https://github.com/rust-lang/rust/blob/9ebf47851a357faa4cd97f4b1dc7835f6376e639/src/libsyntax/ext/expand.rs#L141 | ||
35 | // * Expr(P<ast::Expr>) | ||
36 | // * Pat(P<ast::Pat>) | ||
37 | // * Ty(P<ast::Ty>) | ||
38 | // * Stmts(SmallVec<[ast::Stmt; 1]>) | ||
39 | // * Items(SmallVec<[P<ast::Item>; 1]>) | ||
40 | // | ||
41 | // * TraitItems(SmallVec<[ast::TraitItem; 1]>) | ||
42 | // * ImplItems(SmallVec<[ast::ImplItem; 1]>) | ||
43 | // * ForeignItems(SmallVec<[ast::ForeignItem; 1]> | ||
44 | // | ||
45 | // | ||
46 | |||
47 | /// Parses the token tree (result of macro expansion) as a sequence of items | ||
48 | pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> TreeArc<ast::MacroItems> { | ||
49 | let token_source = SubtreeTokenSource::new(tt); | ||
50 | let mut tree_sink = TtTreeSink::new(token_source.querier()); | ||
51 | ra_parser::parse_macro_items(&token_source, &mut tree_sink); | ||
52 | let syntax = tree_sink.inner.finish(); | ||
53 | ast::MacroItems::cast(&syntax).unwrap().to_owned() | ||
54 | } | ||
55 | |||
33 | /// Parses the token tree (result of macro expansion) as a sequence of items | 56 | /// Parses the token tree (result of macro expansion) as a sequence of items |
34 | pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> TreeArc<ast::SourceFile> { | 57 | pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> TreeArc<ast::SourceFile> { |
35 | let token_source = SubtreeTokenSource::new(tt); | 58 | let token_source = SubtreeTokenSource::new(tt); |
diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs index f8ed1299a..1adc27b80 100644 --- a/crates/ra_parser/src/grammar.rs +++ b/crates/ra_parser/src/grammar.rs | |||
@@ -49,6 +49,12 @@ pub(crate) fn root(p: &mut Parser) { | |||
49 | m.complete(p, SOURCE_FILE); | 49 | m.complete(p, SOURCE_FILE); |
50 | } | 50 | } |
51 | 51 | ||
52 | pub(crate) fn macro_items(p: &mut Parser) { | ||
53 | let m = p.start(); | ||
54 | items::mod_contents(p, false); | ||
55 | m.complete(p, MACRO_ITEMS); | ||
56 | } | ||
57 | |||
52 | pub(crate) fn path(p: &mut Parser) { | 58 | pub(crate) fn path(p: &mut Parser) { |
53 | paths::type_path(p); | 59 | paths::type_path(p); |
54 | } | 60 | } |
diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs index 11b5b9a75..d6bcc4d8c 100644 --- a/crates/ra_parser/src/lib.rs +++ b/crates/ra_parser/src/lib.rs | |||
@@ -98,6 +98,10 @@ pub fn parse_item(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) | |||
98 | parse_from_tokens(token_source, tree_sink, grammar::item); | 98 | parse_from_tokens(token_source, tree_sink, grammar::item); |
99 | } | 99 | } |
100 | 100 | ||
101 | pub fn parse_macro_items(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { | ||
102 | parse_from_tokens(token_source, tree_sink, grammar::macro_items); | ||
103 | } | ||
104 | |||
101 | /// A parsing function for a specific braced-block. | 105 | /// A parsing function for a specific braced-block. |
102 | pub struct Reparser(fn(&mut parser::Parser)); | 106 | pub struct Reparser(fn(&mut parser::Parser)); |
103 | 107 | ||
diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs index 498b0e164..6f984aea1 100644 --- a/crates/ra_parser/src/syntax_kind/generated.rs +++ b/crates/ra_parser/src/syntax_kind/generated.rs | |||
@@ -233,6 +233,8 @@ pub enum SyntaxKind { | |||
233 | ARG_LIST, | 233 | ARG_LIST, |
234 | TYPE_BOUND, | 234 | TYPE_BOUND, |
235 | TYPE_BOUND_LIST, | 235 | TYPE_BOUND_LIST, |
236 | MACRO_ITEMS, | ||
237 | MACRO_STMTS, | ||
236 | // Technical kind so that we can cast from u16 safely | 238 | // Technical kind so that we can cast from u16 safely |
237 | #[doc(hidden)] | 239 | #[doc(hidden)] |
238 | __LAST, | 240 | __LAST, |
@@ -592,6 +594,8 @@ impl SyntaxKind { | |||
592 | ARG_LIST => &SyntaxInfo { name: "ARG_LIST" }, | 594 | ARG_LIST => &SyntaxInfo { name: "ARG_LIST" }, |
593 | TYPE_BOUND => &SyntaxInfo { name: "TYPE_BOUND" }, | 595 | TYPE_BOUND => &SyntaxInfo { name: "TYPE_BOUND" }, |
594 | TYPE_BOUND_LIST => &SyntaxInfo { name: "TYPE_BOUND_LIST" }, | 596 | TYPE_BOUND_LIST => &SyntaxInfo { name: "TYPE_BOUND_LIST" }, |
597 | MACRO_ITEMS => &SyntaxInfo { name: "MACRO_ITEMS" }, | ||
598 | MACRO_STMTS => &SyntaxInfo { name: "MACRO_STMTS" }, | ||
595 | TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" }, | 599 | TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" }, |
596 | EOF => &SyntaxInfo { name: "EOF" }, | 600 | EOF => &SyntaxInfo { name: "EOF" }, |
597 | __LAST => &SyntaxInfo { name: "__LAST" }, | 601 | __LAST => &SyntaxInfo { name: "__LAST" }, |
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)] | ||
1776 | pub struct MacroItems { | ||
1777 | pub(crate) syntax: SyntaxNode, | ||
1778 | } | ||
1779 | unsafe impl TransparentNewType for MacroItems { | ||
1780 | type Repr = rowan::SyntaxNode; | ||
1781 | } | ||
1782 | |||
1783 | impl 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 | |||
1793 | impl ToOwned for MacroItems { | ||
1794 | type Owned = TreeArc<MacroItems>; | ||
1795 | fn to_owned(&self) -> TreeArc<MacroItems> { TreeArc::cast(self.syntax.to_owned()) } | ||
1796 | } | ||
1797 | |||
1798 | |||
1799 | impl ast::ModuleItemOwner for MacroItems {} | ||
1800 | impl ast::FnDefOwner for MacroItems {} | ||
1801 | impl MacroItems {} | ||
1802 | |||
1803 | // MacroStmts | ||
1804 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
1805 | #[repr(transparent)] | ||
1806 | pub struct MacroStmts { | ||
1807 | pub(crate) syntax: SyntaxNode, | ||
1808 | } | ||
1809 | unsafe impl TransparentNewType for MacroStmts { | ||
1810 | type Repr = rowan::SyntaxNode; | ||
1811 | } | ||
1812 | |||
1813 | impl 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 | |||
1823 | impl ToOwned for MacroStmts { | ||
1824 | type Owned = TreeArc<MacroStmts>; | ||
1825 | fn to_owned(&self) -> TreeArc<MacroStmts> { TreeArc::cast(self.syntax.to_owned()) } | ||
1826 | } | ||
1827 | |||
1828 | |||
1829 | impl 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 | ) |