diff options
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 28ded7870..5844d3f12 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -5,6 +5,7 @@ use ra_syntax::{ | |||
5 | }; | 5 | }; |
6 | 6 | ||
7 | use crate::subtree_source::{SubtreeTokenSource, Querier}; | 7 | use crate::subtree_source::{SubtreeTokenSource, Querier}; |
8 | use crate::ExpandError; | ||
8 | 9 | ||
9 | /// Maps `tt::TokenId` to the relative range of the original token. | 10 | /// Maps `tt::TokenId` to the relative range of the original token. |
10 | #[derive(Default)] | 11 | #[derive(Default)] |
@@ -30,6 +31,71 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke | |||
30 | Some((tt, token_map)) | 31 | Some((tt, token_map)) |
31 | } | 32 | } |
32 | 33 | ||
34 | // The following items are what `rustc` macro can be parsed into : | ||
35 | // link: https://github.com/rust-lang/rust/blob/9ebf47851a357faa4cd97f4b1dc7835f6376e639/src/libsyntax/ext/expand.rs#L141 | ||
36 | // * Expr(P<ast::Expr>) -> token_tree_to_expr | ||
37 | // * Pat(P<ast::Pat>) -> token_tree_to_pat | ||
38 | // * Ty(P<ast::Ty>) -> token_tree_to_ty | ||
39 | // * Stmts(SmallVec<[ast::Stmt; 1]>) -> token_tree_to_stmts | ||
40 | // * Items(SmallVec<[P<ast::Item>; 1]>) -> token_tree_to_items | ||
41 | // | ||
42 | // * TraitItems(SmallVec<[ast::TraitItem; 1]>) | ||
43 | // * ImplItems(SmallVec<[ast::ImplItem; 1]>) | ||
44 | // * ForeignItems(SmallVec<[ast::ForeignItem; 1]> | ||
45 | // | ||
46 | // | ||
47 | |||
48 | /// Parses the token tree (result of macro expansion) to an expression | ||
49 | pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<TreeArc<ast::Expr>, ExpandError> { | ||
50 | let token_source = SubtreeTokenSource::new(tt); | ||
51 | let mut tree_sink = TtTreeSink::new(token_source.querier()); | ||
52 | ra_parser::parse_expr(&token_source, &mut tree_sink); | ||
53 | let syntax = tree_sink.inner.finish(); | ||
54 | ast::Expr::cast(&syntax) | ||
55 | .map(|m| m.to_owned()) | ||
56 | .ok_or_else(|| crate::ExpandError::ConversionError) | ||
57 | } | ||
58 | |||
59 | /// Parses the token tree (result of macro expansion) to a Pattern | ||
60 | pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<TreeArc<ast::Pat>, ExpandError> { | ||
61 | let token_source = SubtreeTokenSource::new(tt); | ||
62 | let mut tree_sink = TtTreeSink::new(token_source.querier()); | ||
63 | ra_parser::parse_pat(&token_source, &mut tree_sink); | ||
64 | let syntax = tree_sink.inner.finish(); | ||
65 | ast::Pat::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) | ||
66 | } | ||
67 | |||
68 | /// Parses the token tree (result of macro expansion) to a Type | ||
69 | pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<TreeArc<ast::TypeRef>, ExpandError> { | ||
70 | let token_source = SubtreeTokenSource::new(tt); | ||
71 | let mut tree_sink = TtTreeSink::new(token_source.querier()); | ||
72 | ra_parser::parse_ty(&token_source, &mut tree_sink); | ||
73 | let syntax = tree_sink.inner.finish(); | ||
74 | ast::TypeRef::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) | ||
75 | } | ||
76 | |||
77 | /// Parses the token tree (result of macro expansion) as a sequence of stmts | ||
78 | pub fn token_tree_to_macro_stmts( | ||
79 | tt: &tt::Subtree, | ||
80 | ) -> Result<TreeArc<ast::MacroStmts>, ExpandError> { | ||
81 | let token_source = SubtreeTokenSource::new(tt); | ||
82 | let mut tree_sink = TtTreeSink::new(token_source.querier()); | ||
83 | ra_parser::parse_macro_stmts(&token_source, &mut tree_sink); | ||
84 | let syntax = tree_sink.inner.finish(); | ||
85 | ast::MacroStmts::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) | ||
86 | } | ||
87 | |||
88 | /// Parses the token tree (result of macro expansion) as a sequence of items | ||
89 | pub fn token_tree_to_macro_items( | ||
90 | tt: &tt::Subtree, | ||
91 | ) -> Result<TreeArc<ast::MacroItems>, ExpandError> { | ||
92 | let token_source = SubtreeTokenSource::new(tt); | ||
93 | let mut tree_sink = TtTreeSink::new(token_source.querier()); | ||
94 | ra_parser::parse_macro_items(&token_source, &mut tree_sink); | ||
95 | let syntax = tree_sink.inner.finish(); | ||
96 | ast::MacroItems::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) | ||
97 | } | ||
98 | |||
33 | /// Parses the token tree (result of macro expansion) as a sequence of items | 99 | /// 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> { | 100 | pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> TreeArc<ast::SourceFile> { |
35 | let token_source = SubtreeTokenSource::new(tt); | 101 | let token_source = SubtreeTokenSource::new(tt); |