Skip to content

Commit ab4533d

Browse files
committed
macros: Match repetition separator properly
1 parent 25a33b0 commit ab4533d

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

gcc/rust/ast/rust-macro.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ class MacroMatchRepetition : public MacroMatch
207207
}
208208

209209
MacroRepOp get_op () const { return op; }
210+
const std::unique_ptr<MacroRepSep> &get_sep () const { return sep; }
210211
std::vector<std::unique_ptr<MacroMatch> > &get_matches () { return matches; }
211212

212213
protected:

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3615,12 +3615,13 @@ MacroExpander::match_token (Parser<MacroInvocLexer> &parser, AST::Token &token)
36153615
}
36163616

36173617
bool
3618-
MacroExpander::match_n_matches (
3619-
Parser<MacroInvocLexer> &parser,
3620-
std::vector<std::unique_ptr<AST::MacroMatch>> &matches, size_t &match_amount,
3621-
size_t lo_bound, size_t hi_bound)
3618+
MacroExpander::match_n_matches (Parser<MacroInvocLexer> &parser,
3619+
AST::MacroMatchRepetition &rep,
3620+
size_t &match_amount, size_t lo_bound,
3621+
size_t hi_bound)
36223622
{
36233623
match_amount = 0;
3624+
auto &matches = rep.get_matches ();
36243625

36253626
const MacroInvocLexer &source = parser.get_token_source ();
36263627
while (true)
@@ -3631,6 +3632,12 @@ MacroExpander::match_n_matches (
36313632
if (t_id == RIGHT_PAREN || t_id == RIGHT_SQUARE || t_id == RIGHT_CURLY)
36323633
break;
36333634

3635+
// Skip parsing a separator on the first match, otherwise consume it.
3636+
// If it isn't present, this is an error
3637+
if (rep.has_sep () && match_amount > 0)
3638+
if (!match_token (parser, *rep.get_sep ()))
3639+
break;
3640+
36343641
bool valid_current_match = false;
36353642
for (auto &match : matches)
36363643
{
@@ -3705,17 +3712,17 @@ MacroExpander::match_repetition (Parser<MacroInvocLexer> &parser,
37053712
case AST::MacroMatchRepetition::MacroRepOp::ANY:
37063713
lo_str = "0";
37073714
hi_str = "+inf";
3708-
res = match_n_matches (parser, rep.get_matches (), match_amount);
3715+
res = match_n_matches (parser, rep, match_amount);
37093716
break;
37103717
case AST::MacroMatchRepetition::MacroRepOp::ONE_OR_MORE:
37113718
lo_str = "1";
37123719
hi_str = "+inf";
3713-
res = match_n_matches (parser, rep.get_matches (), match_amount, 1);
3720+
res = match_n_matches (parser, rep, match_amount, 1);
37143721
break;
37153722
case AST::MacroMatchRepetition::MacroRepOp::ZERO_OR_ONE:
37163723
lo_str = "0";
37173724
hi_str = "1";
3718-
res = match_n_matches (parser, rep.get_matches (), match_amount, 0, 1);
3725+
res = match_n_matches (parser, rep, match_amount, 0, 1);
37193726
break;
37203727
default:
37213728
gcc_unreachable ();

gcc/rust/expand/rust-macro-expand.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ struct MacroExpander
192192
* Match any amount of matches
193193
*
194194
* @param parser Parser to use for matching
195-
* @param matches All consecutive matches to identify
195+
* @param rep Repetition to try and match
196196
* @param match_amount Reference in which to store the ammount of succesful
197197
* and valid matches
198198
*
@@ -209,9 +209,8 @@ struct MacroExpander
209209
* otherwise
210210
*/
211211
bool match_n_matches (Parser<MacroInvocLexer> &parser,
212-
std::vector<std::unique_ptr<AST::MacroMatch>> &matches,
213-
size_t &match_amount, size_t lo_bound = 0,
214-
size_t hi_bound = 0);
212+
AST::MacroMatchRepetition &rep, size_t &match_amount,
213+
size_t lo_bound = 0, size_t hi_bound = 0);
215214

216215
void push_context (ContextType t) { context.push_back (t); }
217216

0 commit comments

Comments
 (0)