aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/expand/rust-macro-expand.cc
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-03-07 08:47:19 +0000
committerGitHub <noreply@github.com>2022-03-07 08:47:19 +0000
commit366c53371ad40025984a98e01b02d452d49816aa (patch)
treec43d12ae5fa1058e4a944682720a5100c9111eb9 /gcc/rust/expand/rust-macro-expand.cc
parentb82408fd6a17762fcb6fc7b5efe9307a3e59f8b7 (diff)
parent0c7e16e1258b67ba8efa1b019802bee4d802d4a5 (diff)
downloadgcc-366c53371ad40025984a98e01b02d452d49816aa.zip
gcc-366c53371ad40025984a98e01b02d452d49816aa.tar.gz
gcc-366c53371ad40025984a98e01b02d452d49816aa.tar.bz2
Merge #991
991: Match and expand macro separators properly r=CohenArthur a=CohenArthur More nice recursive macros: ```rust macro_rules! add { ($e:expr | $($es:expr) | *) => { $e + add!($($es) | *) }; ($e:expr) => { $e }; } add!(1 | 2 | 3 | 4 | 5 | 6); ``` Closes #968 This PR needs #986 to be merged first, as it depends on it for the test cases. You can skip reviewing the first two commits which are just from #986 Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc/rust/expand/rust-macro-expand.cc')
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc21
1 files changed, 14 insertions, 7 deletions
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 26f584d..b15e783 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -3598,12 +3598,13 @@ MacroExpander::match_token (Parser<MacroInvocLexer> &parser, AST::Token &token)
}
bool
-MacroExpander::match_n_matches (
- Parser<MacroInvocLexer> &parser,
- std::vector<std::unique_ptr<AST::MacroMatch>> &matches, size_t &match_amount,
- size_t lo_bound, size_t hi_bound)
+MacroExpander::match_n_matches (Parser<MacroInvocLexer> &parser,
+ AST::MacroMatchRepetition &rep,
+ size_t &match_amount, size_t lo_bound,
+ size_t hi_bound)
{
match_amount = 0;
+ auto &matches = rep.get_matches ();
const MacroInvocLexer &source = parser.get_token_source ();
while (true)
@@ -3614,6 +3615,12 @@ MacroExpander::match_n_matches (
if (t_id == RIGHT_PAREN || t_id == RIGHT_SQUARE || t_id == RIGHT_CURLY)
break;
+ // Skip parsing a separator on the first match, otherwise consume it.
+ // If it isn't present, this is an error
+ if (rep.has_sep () && match_amount > 0)
+ if (!match_token (parser, *rep.get_sep ()))
+ break;
+
bool valid_current_match = false;
for (auto &match : matches)
{
@@ -3688,17 +3695,17 @@ MacroExpander::match_repetition (Parser<MacroInvocLexer> &parser,
case AST::MacroMatchRepetition::MacroRepOp::ANY:
lo_str = "0";
hi_str = "+inf";
- res = match_n_matches (parser, rep.get_matches (), match_amount);
+ res = match_n_matches (parser, rep, match_amount);
break;
case AST::MacroMatchRepetition::MacroRepOp::ONE_OR_MORE:
lo_str = "1";
hi_str = "+inf";
- res = match_n_matches (parser, rep.get_matches (), match_amount, 1);
+ res = match_n_matches (parser, rep, match_amount, 1);
break;
case AST::MacroMatchRepetition::MacroRepOp::ZERO_OR_ONE:
lo_str = "0";
hi_str = "1";
- res = match_n_matches (parser, rep.get_matches (), match_amount, 0, 1);
+ res = match_n_matches (parser, rep, match_amount, 0, 1);
break;
default:
gcc_unreachable ();