Skip to content

Commit 5651331

Browse files
committed
macros: Allow parsing :tt fragments
:tt fragments stand for token trees, and are composed of either a token, or a delimited token tree, which is a token tree surrounded by delimiters (parentheses, curly brackets or square brackets). This should allow us to handle a lot more macros, including extremely powerful macro patterns such as TT munchers
1 parent 8283724 commit 5651331

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

gcc/rust/expand/rust-macro-expand.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,8 @@ MacroExpander::match_fragment (Parser<MacroInvocLexer> &parser,
497497
gcc_unreachable ();
498498
break;
499499

500-
// what is TT?
501500
case AST::MacroFragSpec::TT:
502-
// parser.parse_token_tree() ?
503-
gcc_unreachable ();
501+
parser.parse_token_tree ();
504502
break;
505503

506504
// i guess we just ignore invalid and just error out

gcc/rust/parse/rust-parse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ template <typename ManagedTokenSource> class Parser
142142
std::vector<std::unique_ptr<AST::LifetimeParam> > parse_lifetime_params ();
143143
AST::Visibility parse_visibility ();
144144
std::unique_ptr<AST::IdentifierPattern> parse_identifier_pattern ();
145+
std::unique_ptr<AST::TokenTree> parse_token_tree ();
145146

146147
private:
147148
void skip_after_semicolon ();
@@ -188,7 +189,6 @@ template <typename ManagedTokenSource> class Parser
188189

189190
// Token tree or macro related
190191
AST::DelimTokenTree parse_delim_token_tree ();
191-
std::unique_ptr<AST::TokenTree> parse_token_tree ();
192192
std::unique_ptr<AST::MacroRulesDefinition>
193193
parse_macro_rules_def (AST::AttrVec outer_attrs);
194194
std::unique_ptr<AST::MacroInvocation>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
macro_rules! t {
2+
($t:tt) => {
3+
$t
4+
};
5+
}
6+
7+
fn frob() -> i32 {
8+
t!(15) + t!((14))
9+
}
10+
11+
fn main() -> i32 {
12+
frob() - 29
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
macro_rules! count_tt {
2+
($t:tt) => { 1 };
3+
($t:tt $($ts:tt)*) => { 1 + count_tt!($($ts)*) };
4+
}
5+
6+
fn main() -> i32 {
7+
let count = count_tt!(1 2 let a = 15) + count_tt!(1 2 (let a = 15));
8+
// ^ ^ ^^^ ^ ^ ^^ ^ ^ ^^^^^^^^^^^^
9+
// 6 token-trees 3 token-trees
10+
11+
count - 9
12+
}

0 commit comments

Comments
 (0)