Skip to content

Commit 4fde21b

Browse files
committed
macros: Substitute separator if necessary when expanding repetitions
1 parent ab4533d commit 4fde21b

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ SubstituteCtx::substitute_metavar (std::unique_ptr<AST::Token> &metavar)
3333
}
3434

3535
std::vector<std::unique_ptr<AST::Token>>
36-
SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end)
36+
SubstituteCtx::substitute_repetition (
37+
size_t pattern_start, size_t pattern_end,
38+
std::unique_ptr<AST::Token> separator_token)
3739
{
3840
rust_assert (pattern_end < macro.size ());
3941

@@ -117,6 +119,11 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end)
117119
auto substitute_context = SubstituteCtx (input, new_macro, sub_map);
118120
auto new_tokens = substitute_context.substitute_tokens ();
119121

122+
// Skip the first repetition, but add the separator to the expanded
123+
// tokens if it is present
124+
if (i != 0 && separator_token)
125+
expanded.emplace_back (separator_token->clone_token ());
126+
120127
for (auto &new_token : new_tokens)
121128
expanded.emplace_back (new_token->clone_token ());
122129
}
@@ -127,6 +134,13 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end)
127134
return expanded;
128135
}
129136

137+
static bool
138+
is_rep_op (std::unique_ptr<AST::Token> &tok)
139+
{
140+
auto id = tok->get_id ();
141+
return id == QUESTION_MARK || id == ASTERISK || id == PLUS;
142+
}
143+
130144
std::pair<std::vector<std::unique_ptr<AST::Token>>, size_t>
131145
SubstituteCtx::substitute_token (size_t token_idx)
132146
{
@@ -148,20 +162,34 @@ SubstituteCtx::substitute_token (size_t token_idx)
148162
pattern_end++)
149163
;
150164

165+
std::unique_ptr<AST::Token> separator_token = nullptr;
166+
// FIXME: Can this go out of bounds?
167+
auto &post_pattern_token = macro.at (pattern_end + 1);
168+
if (!is_rep_op (post_pattern_token))
169+
separator_token = post_pattern_token->clone_token ();
170+
171+
// Amount of tokens to skip
172+
auto to_skip = 0;
173+
// Parentheses
174+
to_skip += 2;
175+
// Repetition operator
176+
to_skip += 1;
177+
// Separator
178+
if (separator_token)
179+
to_skip += 1;
180+
151181
// FIXME: This skips whitespaces... Is that okay??
152-
// FIXME: Is there any existing parsing function that allows us to parse
153-
// a macro pattern?
182+
// FIXME: Is there any existing parsing function that allows us to
183+
// parse a macro pattern?
154184

155185
// FIXME: Add error handling in the case we haven't found a matching
156186
// closing delimiter
157187

158188
// FIXME: We need to parse the repetition token now
159189

160-
return {
161-
substitute_repetition (pattern_start, pattern_end),
162-
// + 2 for the opening and closing parentheses which are mandatory
163-
// + 1 for the repetitor (+, *, ?)
164-
pattern_end - pattern_start + 3};
190+
return {substitute_repetition (pattern_start, pattern_end,
191+
std::move (separator_token)),
192+
pattern_end - pattern_start + to_skip};
165193
}
166194
// TODO: We need to check if the $ was alone. In that case, do
167195
// not error out: Simply act as if there was an empty identifier

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ class SubstituteCtx
4949
* Substitute a macro repetition by its given fragments
5050
*
5151
* @param pattern_start Start index of the pattern tokens
52-
* @param pattern_end Index Amount of tokens in the pattern
52+
* @param pattern_end End index of the patterns tokens
53+
* @param separator Optional separator to include when expanding tokens
5354
*
5455
* @return A vector containing the repeated pattern
5556
*/
5657
std::vector<std::unique_ptr<AST::Token>>
57-
substitute_repetition (size_t pattern_start, size_t pattern_end);
58+
substitute_repetition (size_t pattern_start, size_t pattern_end,
59+
std::unique_ptr<AST::Token> separator);
5860

5961
/**
6062
* Substitute a given token by its appropriate representation

0 commit comments

Comments
 (0)