diff options
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 177 | ||||
-rw-r--r-- | crates/ra_mbe/src/mbe_expander.rs | 36 | ||||
-rw-r--r-- | crates/ra_mbe/src/subtree_parser.rs | 12 | ||||
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 71 | ||||
-rw-r--r-- | crates/ra_mbe/src/tt_cursor.rs | 40 |
5 files changed, 325 insertions, 11 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 8d5008d20..d2115bd67 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs | |||
@@ -37,9 +37,19 @@ 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_expr, | ||
48 | token_tree_to_pat, | ||
49 | token_tree_to_ty, | ||
50 | token_tree_to_macro_items, | ||
51 | token_tree_to_macro_stmts, | ||
52 | }; | ||
43 | 53 | ||
44 | /// This struct contains AST for a single `macro_rules` definition. What might | 54 | /// 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 | 55 | /// be very confusing is that AST has almost exactly the same shape as |
@@ -192,23 +202,26 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
192 | pub(crate) fn expand_to_syntax( | 202 | pub(crate) fn expand_to_syntax( |
193 | rules: &MacroRules, | 203 | rules: &MacroRules, |
194 | invocation: &str, | 204 | invocation: &str, |
195 | ) -> ra_syntax::TreeArc<ast::SourceFile> { | 205 | ) -> ra_syntax::TreeArc<ast::MacroItems> { |
196 | let expanded = expand(rules, invocation); | 206 | let expanded = expand(rules, invocation); |
197 | token_tree_to_ast_item_list(&expanded) | 207 | token_tree_to_macro_items(&expanded).unwrap() |
198 | } | 208 | } |
199 | 209 | ||
200 | pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { | 210 | pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { |
201 | let expanded = expand(rules, invocation); | 211 | let expanded = expand(rules, invocation); |
202 | assert_eq!(expanded.to_string(), expansion); | 212 | assert_eq!(expanded.to_string(), expansion); |
203 | 213 | ||
204 | let tree = token_tree_to_ast_item_list(&expanded); | 214 | let tree = token_tree_to_macro_items(&expanded); |
205 | 215 | ||
206 | // Eat all white space by parse it back and forth | 216 | // Eat all white space by parse it back and forth |
207 | let expansion = ast::SourceFile::parse(expansion); | 217 | let expansion = ast::SourceFile::parse(expansion); |
208 | let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0; | 218 | let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0; |
209 | let file = token_tree_to_ast_item_list(&expansion); | 219 | let file = token_tree_to_macro_items(&expansion); |
210 | 220 | ||
211 | assert_eq!(tree.syntax().debug_dump().trim(), file.syntax().debug_dump().trim()); | 221 | assert_eq!( |
222 | tree.unwrap().syntax().debug_dump().trim(), | ||
223 | file.unwrap().syntax().debug_dump().trim() | ||
224 | ); | ||
212 | } | 225 | } |
213 | 226 | ||
214 | #[test] | 227 | #[test] |
@@ -346,11 +359,11 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
346 | ", | 359 | ", |
347 | ); | 360 | ); |
348 | let expansion = expand(&rules, "structs!(Foo, Bar)"); | 361 | let expansion = expand(&rules, "structs!(Foo, Bar)"); |
349 | let tree = token_tree_to_ast_item_list(&expansion); | 362 | let tree = token_tree_to_macro_items(&expansion); |
350 | assert_eq!( | 363 | assert_eq!( |
351 | tree.syntax().debug_dump().trim(), | 364 | tree.unwrap().syntax().debug_dump().trim(), |
352 | r#" | 365 | r#" |
353 | SOURCE_FILE@[0; 40) | 366 | MACRO_ITEMS@[0; 40) |
354 | STRUCT_DEF@[0; 20) | 367 | STRUCT_DEF@[0; 20) |
355 | STRUCT_KW@[0; 6) "struct" | 368 | STRUCT_KW@[0; 6) "struct" |
356 | NAME@[6; 9) | 369 | NAME@[6; 9) |
@@ -444,6 +457,59 @@ SOURCE_FILE@[0; 40) | |||
444 | assert_expansion(&rules, "foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}"); | 457 | assert_expansion(&rules, "foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}"); |
445 | } | 458 | } |
446 | 459 | ||
460 | #[test] | ||
461 | fn test_tt_to_stmts() { | ||
462 | let rules = create_rules( | ||
463 | r#" | ||
464 | macro_rules! foo { | ||
465 | () => { | ||
466 | let a = 0; | ||
467 | a = 10 + 1; | ||
468 | a | ||
469 | } | ||
470 | } | ||
471 | "#, | ||
472 | ); | ||
473 | |||
474 | let expanded = expand(&rules, "foo!{}"); | ||
475 | let stmts = token_tree_to_macro_stmts(&expanded); | ||
476 | |||
477 | assert_eq!( | ||
478 | stmts.unwrap().syntax().debug_dump().trim(), | ||
479 | r#"MACRO_STMTS@[0; 15) | ||
480 | LET_STMT@[0; 7) | ||
481 | LET_KW@[0; 3) "let" | ||
482 | BIND_PAT@[3; 4) | ||
483 | NAME@[3; 4) | ||
484 | IDENT@[3; 4) "a" | ||
485 | EQ@[4; 5) "=" | ||
486 | LITERAL@[5; 6) | ||
487 | INT_NUMBER@[5; 6) "0" | ||
488 | SEMI@[6; 7) ";" | ||
489 | EXPR_STMT@[7; 14) | ||
490 | BIN_EXPR@[7; 13) | ||
491 | PATH_EXPR@[7; 8) | ||
492 | PATH@[7; 8) | ||
493 | PATH_SEGMENT@[7; 8) | ||
494 | NAME_REF@[7; 8) | ||
495 | IDENT@[7; 8) "a" | ||
496 | EQ@[8; 9) "=" | ||
497 | BIN_EXPR@[9; 13) | ||
498 | LITERAL@[9; 11) | ||
499 | INT_NUMBER@[9; 11) "10" | ||
500 | PLUS@[11; 12) "+" | ||
501 | LITERAL@[12; 13) | ||
502 | INT_NUMBER@[12; 13) "1" | ||
503 | SEMI@[13; 14) ";" | ||
504 | EXPR_STMT@[14; 15) | ||
505 | PATH_EXPR@[14; 15) | ||
506 | PATH@[14; 15) | ||
507 | PATH_SEGMENT@[14; 15) | ||
508 | NAME_REF@[14; 15) | ||
509 | IDENT@[14; 15) "a""#, | ||
510 | ); | ||
511 | } | ||
512 | |||
447 | // The following tests are port from intellij-rust directly | 513 | // The following tests are port from intellij-rust directly |
448 | // https://github.com/intellij-rust/intellij-rust/blob/c4e9feee4ad46e7953b1948c112533360b6087bb/src/test/kotlin/org/rust/lang/core/macros/RsMacroExpansionTest.kt | 514 | // https://github.com/intellij-rust/intellij-rust/blob/c4e9feee4ad46e7953b1948c112533360b6087bb/src/test/kotlin/org/rust/lang/core/macros/RsMacroExpansionTest.kt |
449 | 515 | ||
@@ -527,7 +593,7 @@ SOURCE_FILE@[0; 40) | |||
527 | 593 | ||
528 | assert_eq!( | 594 | assert_eq!( |
529 | expand_to_syntax(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(), | 595 | expand_to_syntax(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(), |
530 | r#"SOURCE_FILE@[0; 15) | 596 | r#"MACRO_ITEMS@[0; 15) |
531 | FN_DEF@[0; 15) | 597 | FN_DEF@[0; 15) |
532 | FN_KW@[0; 2) "fn" | 598 | FN_KW@[0; 2) "fn" |
533 | NAME@[2; 5) | 599 | NAME@[2; 5) |
@@ -665,4 +731,95 @@ SOURCE_FILE@[0; 40) | |||
665 | } | 731 | } |
666 | "#, r#"extern crate a ; mod b ; mod c {} use d ; const E : i32 = 0 ; static F : i32 = 0 ; impl G {} struct H ; enum I {Foo} trait J {} fn h () {} extern {} type T = u8 ;"#); | 732 | "#, r#"extern crate a ; mod b ; mod c {} use d ; const E : i32 = 0 ; static F : i32 = 0 ; impl G {} struct H ; enum I {Foo} trait J {} fn h () {} extern {} type T = u8 ;"#); |
667 | } | 733 | } |
734 | |||
735 | #[test] | ||
736 | fn test_block() { | ||
737 | let rules = create_rules( | ||
738 | r#" | ||
739 | macro_rules! foo { | ||
740 | ($ i:block) => { fn foo() $ i } | ||
741 | } | ||
742 | "#, | ||
743 | ); | ||
744 | assert_expansion(&rules, "foo! { { 1; } }", "fn foo () {1 ;}"); | ||
745 | } | ||
746 | |||
747 | #[test] | ||
748 | fn test_meta() { | ||
749 | let rules = create_rules( | ||
750 | r#" | ||
751 | macro_rules! foo { | ||
752 | ($ i:meta) => ( | ||
753 | #[$ i] | ||
754 | fn bar() {} | ||
755 | ) | ||
756 | } | ||
757 | "#, | ||
758 | ); | ||
759 | assert_expansion( | ||
760 | &rules, | ||
761 | r#"foo! { cfg(target_os = "windows") }"#, | ||
762 | r#"# [cfg (target_os = "windows")] fn bar () {}"#, | ||
763 | ); | ||
764 | } | ||
765 | |||
766 | // #[test] | ||
767 | // fn test_tt_block() { | ||
768 | // let rules = create_rules( | ||
769 | // r#" | ||
770 | // macro_rules! foo { | ||
771 | // ($ i:tt) => { fn foo() $ i } | ||
772 | // } | ||
773 | // "#, | ||
774 | // ); | ||
775 | // assert_expansion(&rules, r#"foo! { { 1; } }"#, r#"fn foo () {1 ;}"#); | ||
776 | // } | ||
777 | |||
778 | // #[test] | ||
779 | // fn test_tt_group() { | ||
780 | // let rules = create_rules( | ||
781 | // r#" | ||
782 | // macro_rules! foo { | ||
783 | // ($($ i:tt)*) => { $($ i)* } | ||
784 | // } | ||
785 | // "#, | ||
786 | // ); | ||
787 | // assert_expansion(&rules, r#"foo! { fn foo() {} }"#, r#"fn foo () {}"#); | ||
788 | // } | ||
789 | |||
790 | #[test] | ||
791 | fn test_lifetime() { | ||
792 | let rules = create_rules( | ||
793 | r#" | ||
794 | macro_rules! foo { | ||
795 | ($ lt:lifetime) => { struct Ref<$ lt>{ s: &$ lt str } } | ||
796 | } | ||
797 | "#, | ||
798 | ); | ||
799 | assert_expansion(&rules, r#"foo!{'a}"#, r#"struct Ref < 'a > {s : & 'a str}"#); | ||
800 | } | ||
801 | |||
802 | #[test] | ||
803 | fn test_literal() { | ||
804 | let rules = create_rules( | ||
805 | r#" | ||
806 | macro_rules! foo { | ||
807 | ($ type:ty $ lit:literal) => { const VALUE: $ type = $ lit;}; | ||
808 | } | ||
809 | "#, | ||
810 | ); | ||
811 | assert_expansion(&rules, r#"foo!(u8 0)"#, r#"const VALUE : u8 = 0 ;"#); | ||
812 | } | ||
813 | |||
814 | #[test] | ||
815 | fn test_vis() { | ||
816 | let rules = create_rules( | ||
817 | r#" | ||
818 | macro_rules! foo { | ||
819 | ($ vis:vis $ name:ident) => { $ vis fn $ name() {}}; | ||
820 | } | ||
821 | "#, | ||
822 | ); | ||
823 | assert_expansion(&rules, r#"foo!(pub foo);"#, r#"pub fn foo () {}"#); | ||
824 | } | ||
668 | } | 825 | } |
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index acba42809..86867111f 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs | |||
@@ -161,11 +161,47 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result<Bindings, | |||
161 | let pat = input.eat_stmt().ok_or(ExpandError::UnexpectedToken)?.clone(); | 161 | let pat = input.eat_stmt().ok_or(ExpandError::UnexpectedToken)?.clone(); |
162 | res.inner.insert(text.clone(), Binding::Simple(pat.into())); | 162 | res.inner.insert(text.clone(), Binding::Simple(pat.into())); |
163 | } | 163 | } |
164 | "block" => { | ||
165 | let block = | ||
166 | input.eat_block().ok_or(ExpandError::UnexpectedToken)?.clone(); | ||
167 | res.inner.insert(text.clone(), Binding::Simple(block.into())); | ||
168 | } | ||
169 | "meta" => { | ||
170 | let meta = | ||
171 | input.eat_meta().ok_or(ExpandError::UnexpectedToken)?.clone(); | ||
172 | res.inner.insert(text.clone(), Binding::Simple(meta.into())); | ||
173 | } | ||
174 | // FIXME: | ||
175 | // Enable followiing code when everything is fixed | ||
176 | // At least we can dogfood itself to not stackoverflow | ||
177 | // | ||
178 | // "tt" => { | ||
179 | // let token = input.eat().ok_or(ExpandError::UnexpectedToken)?.clone(); | ||
180 | // res.inner.insert(text.clone(), Binding::Simple(token.into())); | ||
181 | // } | ||
164 | "item" => { | 182 | "item" => { |
165 | let item = | 183 | let item = |
166 | input.eat_item().ok_or(ExpandError::UnexpectedToken)?.clone(); | 184 | input.eat_item().ok_or(ExpandError::UnexpectedToken)?.clone(); |
167 | res.inner.insert(text.clone(), Binding::Simple(item.into())); | 185 | res.inner.insert(text.clone(), Binding::Simple(item.into())); |
168 | } | 186 | } |
187 | "lifetime" => { | ||
188 | let lifetime = | ||
189 | input.eat_lifetime().ok_or(ExpandError::UnexpectedToken)?.clone(); | ||
190 | res.inner.insert(text.clone(), Binding::Simple(lifetime.into())); | ||
191 | } | ||
192 | "literal" => { | ||
193 | let literal = | ||
194 | input.eat_literal().ok_or(ExpandError::UnexpectedToken)?.clone(); | ||
195 | res.inner.insert( | ||
196 | text.clone(), | ||
197 | Binding::Simple(tt::Leaf::from(literal).into()), | ||
198 | ); | ||
199 | } | ||
200 | "vis" => { | ||
201 | let vis = input.eat_vis().ok_or(ExpandError::UnexpectedToken)?.clone(); | ||
202 | res.inner.insert(text.clone(), Binding::Simple(vis.into())); | ||
203 | } | ||
204 | |||
169 | _ => return Err(ExpandError::UnexpectedToken), | 205 | _ => return Err(ExpandError::UnexpectedToken), |
170 | } | 206 | } |
171 | } | 207 | } |
diff --git a/crates/ra_mbe/src/subtree_parser.rs b/crates/ra_mbe/src/subtree_parser.rs index 195e4c3ac..528aa0f8a 100644 --- a/crates/ra_mbe/src/subtree_parser.rs +++ b/crates/ra_mbe/src/subtree_parser.rs | |||
@@ -46,10 +46,22 @@ impl<'a> Parser<'a> { | |||
46 | self.parse(|src, sink| ra_parser::parse_stmt(src, sink, false)) | 46 | self.parse(|src, sink| ra_parser::parse_stmt(src, sink, false)) |
47 | } | 47 | } |
48 | 48 | ||
49 | pub fn parse_block(self) -> Option<tt::TokenTree> { | ||
50 | self.parse(ra_parser::parse_block) | ||
51 | } | ||
52 | |||
53 | pub fn parse_meta(self) -> Option<tt::TokenTree> { | ||
54 | self.parse(ra_parser::parse_meta) | ||
55 | } | ||
56 | |||
49 | pub fn parse_item(self) -> Option<tt::TokenTree> { | 57 | pub fn parse_item(self) -> Option<tt::TokenTree> { |
50 | self.parse(ra_parser::parse_item) | 58 | self.parse(ra_parser::parse_item) |
51 | } | 59 | } |
52 | 60 | ||
61 | pub fn parse_vis(self) -> Option<tt::TokenTree> { | ||
62 | self.parse(ra_parser::parse_vis) | ||
63 | } | ||
64 | |||
53 | fn parse<F>(self, f: F) -> Option<tt::TokenTree> | 65 | fn parse<F>(self, f: F) -> Option<tt::TokenTree> |
54 | where | 66 | where |
55 | F: FnOnce(&dyn TokenSource, &mut dyn TreeSink), | 67 | F: FnOnce(&dyn TokenSource, &mut dyn TreeSink), |
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 28ded7870..38a481029 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); |
@@ -91,7 +157,10 @@ fn convert_tt( | |||
91 | ); | 157 | ); |
92 | } | 158 | } |
93 | } else { | 159 | } else { |
94 | let child = if token.kind().is_keyword() || token.kind() == IDENT { | 160 | let child: tt::TokenTree = if token.kind().is_keyword() |
161 | || token.kind() == IDENT | ||
162 | || token.kind() == LIFETIME | ||
163 | { | ||
95 | let relative_range = token.range() - global_offset; | 164 | let relative_range = token.range() - global_offset; |
96 | let id = token_map.alloc(relative_range); | 165 | let id = token_map.alloc(relative_range); |
97 | let text = token.text().clone(); | 166 | let text = token.text().clone(); |
diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs index 484437b0e..741b5ea1c 100644 --- a/crates/ra_mbe/src/tt_cursor.rs +++ b/crates/ra_mbe/src/tt_cursor.rs | |||
@@ -41,6 +41,13 @@ impl<'a> TtCursor<'a> { | |||
41 | } | 41 | } |
42 | } | 42 | } |
43 | 43 | ||
44 | pub(crate) fn at_literal(&mut self) -> Option<&'a tt::Literal> { | ||
45 | match self.current() { | ||
46 | Some(tt::TokenTree::Leaf(tt::Leaf::Literal(i))) => Some(i), | ||
47 | _ => None, | ||
48 | } | ||
49 | } | ||
50 | |||
44 | pub(crate) fn bump(&mut self) { | 51 | pub(crate) fn bump(&mut self) { |
45 | self.pos += 1; | 52 | self.pos += 1; |
46 | } | 53 | } |
@@ -79,6 +86,13 @@ impl<'a> TtCursor<'a> { | |||
79 | }) | 86 | }) |
80 | } | 87 | } |
81 | 88 | ||
89 | pub(crate) fn eat_literal(&mut self) -> Option<&'a tt::Literal> { | ||
90 | self.at_literal().map(|i| { | ||
91 | self.bump(); | ||
92 | i | ||
93 | }) | ||
94 | } | ||
95 | |||
82 | pub(crate) fn eat_path(&mut self) -> Option<tt::TokenTree> { | 96 | pub(crate) fn eat_path(&mut self) -> Option<tt::TokenTree> { |
83 | let parser = Parser::new(&mut self.pos, self.subtree); | 97 | let parser = Parser::new(&mut self.pos, self.subtree); |
84 | parser.parse_path() | 98 | parser.parse_path() |
@@ -104,11 +118,37 @@ impl<'a> TtCursor<'a> { | |||
104 | parser.parse_stmt() | 118 | parser.parse_stmt() |
105 | } | 119 | } |
106 | 120 | ||
121 | pub(crate) fn eat_block(&mut self) -> Option<tt::TokenTree> { | ||
122 | let parser = Parser::new(&mut self.pos, self.subtree); | ||
123 | parser.parse_block() | ||
124 | } | ||
125 | |||
126 | pub(crate) fn eat_meta(&mut self) -> Option<tt::TokenTree> { | ||
127 | let parser = Parser::new(&mut self.pos, self.subtree); | ||
128 | parser.parse_meta() | ||
129 | } | ||
130 | |||
107 | pub(crate) fn eat_item(&mut self) -> Option<tt::TokenTree> { | 131 | pub(crate) fn eat_item(&mut self) -> Option<tt::TokenTree> { |
108 | let parser = Parser::new(&mut self.pos, self.subtree); | 132 | let parser = Parser::new(&mut self.pos, self.subtree); |
109 | parser.parse_item() | 133 | parser.parse_item() |
110 | } | 134 | } |
111 | 135 | ||
136 | pub(crate) fn eat_lifetime(&mut self) -> Option<tt::TokenTree> { | ||
137 | // check if it start from "`" | ||
138 | if let Some(ident) = self.at_ident() { | ||
139 | if ident.text.chars().next()? != '\'' { | ||
140 | return None; | ||
141 | } | ||
142 | } | ||
143 | |||
144 | self.eat_ident().cloned().map(|ident| tt::Leaf::from(ident).into()) | ||
145 | } | ||
146 | |||
147 | pub(crate) fn eat_vis(&mut self) -> Option<tt::TokenTree> { | ||
148 | let parser = Parser::new(&mut self.pos, self.subtree); | ||
149 | parser.parse_vis() | ||
150 | } | ||
151 | |||
112 | pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> { | 152 | pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> { |
113 | if self.at_char(char) { | 153 | if self.at_char(char) { |
114 | self.bump(); | 154 | self.bump(); |