Skip to content

Commit c849983

Browse files
committed
macro-substitute: Do not substitute non-repetition fragments in sub-maps
When creating a sub-map for repetitions, we need to be weary of not accessing matched-fragments beyond the vector's size. For example, with the following *fragments* { "e": [1], "es": [2, 3, 10]}, the sub-maps we want to create are the following: { "e": [1], "es": [2]}, { "e": [1], "es": [3]}, { "e": [1], "es": [10]}, Up until this point however, we were trying to access the second index for the "e" metavar when creating the second submap, then the third, and so on... which is obviously outside of the vector's bounds. We can simply check if the metavar only has one match and expand that one in that case. We still need to work on checking that multiple metavars in the same transcriber repetition pattern have the same amount of matched fragments (ie the original vectors of matches in the original map have the same size)
1 parent e82b59d commit c849983

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ SubstituteCtx::substitute_metavar (std::unique_ptr<AST::Token> &metavar)
1717
else
1818
{
1919
// Replace
20-
// We only care about the vector when expanding repetitions. Just access
21-
// the first element of the vector.
20+
// We only care about the vector when expanding repetitions.
21+
// Just access the first element of the vector.
2222
// FIXME: Clean this up so it makes more sense
2323
auto &frag = it->second[0];
2424
for (size_t offs = frag.token_offset_begin; offs < frag.token_offset_end;
@@ -103,7 +103,13 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end)
103103
for (auto &kv_match : fragments)
104104
{
105105
std::vector<MatchedFragment> sub_vec;
106-
sub_vec.emplace_back (kv_match.second[i]);
106+
107+
// FIXME: Hack: If a fragment is not repeated, how does it fit in the
108+
// submap? Do we really want to expand it? Is this normal behavior?
109+
if (kv_match.second.size () == 1)
110+
sub_vec.emplace_back (kv_match.second[0]);
111+
else
112+
sub_vec.emplace_back (kv_match.second[i]);
107113

108114
sub_map.insert ({kv_match.first, sub_vec});
109115
}
@@ -177,6 +183,7 @@ std::vector<std::unique_ptr<AST::Token>>
177183
SubstituteCtx::substitute_tokens ()
178184
{
179185
std::vector<std::unique_ptr<AST::Token>> replaced_tokens;
186+
rust_debug ("expanding tokens");
180187

181188
for (size_t i = 0; i < macro.size (); i++)
182189
{

0 commit comments

Comments
 (0)