@@ -3611,7 +3611,6 @@ MacroExpander::match_n_matches (
3611
3611
match_amount = 0 ;
3612
3612
3613
3613
const MacroInvocLexer &source = parser.get_token_source ();
3614
- std::vector<std::string> fragment_identifiers;
3615
3614
while (true )
3616
3615
{
3617
3616
// If the current token is a closing macro delimiter, break away.
@@ -3637,8 +3636,6 @@ MacroExpander::match_n_matches (
3637
3636
{fragment->get_ident (),
3638
3637
MatchedFragment (fragment->get_ident (), offs_begin,
3639
3638
offs_end)});
3640
-
3641
- fragment_identifiers.emplace_back (fragment->get_ident ());
3642
3639
}
3643
3640
break ;
3644
3641
@@ -3677,21 +3674,10 @@ MacroExpander::match_n_matches (
3677
3674
3678
3675
// Check if the amount of matches we got is valid: Is it more than the lower
3679
3676
// bound and less than the higher bound?
3680
- auto result = hi_bound ? match_amount >= lo_bound && match_amount <= hi_bound
3681
- : match_amount >= lo_bound;
3682
-
3683
- // We can now set the amount to each fragment we matched in the substack
3684
- auto &stack_map = sub_stack.peek ();
3685
- for (auto &fragment_id : fragment_identifiers)
3686
- {
3687
- auto it = stack_map.find (fragment_id);
3688
-
3689
- rust_assert (it != stack_map.end ());
3677
+ bool did_meet_lo_bound = match_amount >= lo_bound;
3678
+ bool did_meet_hi_bound = hi_bound ? match_amount <= hi_bound : true ;
3690
3679
3691
- it->second .set_match_amount (match_amount);
3692
- }
3693
-
3694
- return result;
3680
+ return did_meet_lo_bound && did_meet_hi_bound;
3695
3681
}
3696
3682
3697
3683
bool
@@ -3733,6 +3719,31 @@ MacroExpander::match_repetition (Parser<MacroInvocLexer> &parser,
3733
3719
rust_debug_loc (rep.get_match_locus (), " %s matched %lu times" ,
3734
3720
res ? " successfully" : " unsuccessfully" , match_amount);
3735
3721
3722
+ // We can now set the amount to each fragment we matched in the substack
3723
+ auto &stack_map = sub_stack.peek ();
3724
+ for (auto &match : rep.get_matches ())
3725
+ {
3726
+ if (match->get_macro_match_type ()
3727
+ == AST::MacroMatch::MacroMatchType::Fragment)
3728
+ {
3729
+ auto fragment = static_cast <AST::MacroMatchFragment *> (match.get ());
3730
+ auto it = stack_map.find (fragment->get_ident ());
3731
+
3732
+ // If we can't find the fragment, but the result was valid, then it's
3733
+ // a zero-matched fragment and we can insert it
3734
+ if (it == stack_map.end ())
3735
+ {
3736
+ stack_map.insert (
3737
+ {fragment->get_ident (),
3738
+ MatchedFragment::zero (fragment->get_ident ())});
3739
+ }
3740
+ else
3741
+ {
3742
+ it->second .set_match_amount (match_amount);
3743
+ }
3744
+ }
3745
+ }
3746
+
3736
3747
return res;
3737
3748
}
3738
3749
@@ -4037,14 +4048,6 @@ MacroExpander::substitute_tokens (
4037
4048
{
4038
4049
std::vector<std::unique_ptr<AST::Token>> replaced_tokens;
4039
4050
4040
- // for token in macro
4041
- // if token == ?:
4042
- // // That's not always true: If it's a left paren, it's repetition
4043
- // // We probably want to store the matched amount in the fragment so
4044
- // // we can expand it here
4045
- // id = next_token();
4046
- // frag = fragment.find(id);
4047
-
4048
4051
for (size_t i = 0 ; i < macro.size (); i++)
4049
4052
{
4050
4053
auto &tok = macro.at (i);
@@ -4065,54 +4068,6 @@ MacroExpander::substitute_tokens (
4065
4068
{
4066
4069
replaced_tokens.emplace_back (tok->clone_token ());
4067
4070
}
4068
-
4069
- // std::vector<std::unique_ptr<AST::Token>> parsed_toks;
4070
-
4071
- // std::string ident;
4072
- // for (size_t offs = i; i < macro.size (); offs++)
4073
- // {
4074
- // auto &tok = macro.at (offs);
4075
- // if (tok->get_id () == DOLLAR_SIGN && offs == i)
4076
- // {
4077
- // parsed_toks.push_back (tok->clone_token ());
4078
- // }
4079
- // else if (tok->get_id () == IDENTIFIER)
4080
- // {
4081
- // rust_assert (tok->as_string ().size () == 1);
4082
- // ident.push_back (tok->as_string ().at (0));
4083
- // parsed_toks.push_back (tok->clone_token ());
4084
- // }
4085
- // else
4086
- // {
4087
- // break;
4088
- // }
4089
- // }
4090
-
4091
- // // lookup the ident
4092
- // auto it = fragments.find (ident);
4093
- // if (it == fragments.end ())
4094
- // {
4095
- // // just leave the tokens in
4096
- // for (auto &tok : parsed_toks)
4097
- // {
4098
- // replaced_tokens.push_back (tok->clone_token ());
4099
- // }
4100
- // }
4101
- // else
4102
- // {
4103
- // // replace
4104
- // MatchedFragment &frag = it->second;
4105
- // for (size_t offs = frag.token_offset_begin;
4106
- // offs < frag.token_offset_end; offs++)
4107
- // {
4108
- // auto &tok = input.at (offs);
4109
- // replaced_tokens.push_back (tok->clone_token ());
4110
- // }
4111
- // }
4112
- // i += parsed_toks.size () - 1;
4113
- //
4114
- // }
4115
- // else { replaced_tokens.push_back (tok->clone_token ()); }
4116
4071
}
4117
4072
4118
4073
return replaced_tokens;
0 commit comments