@@ -33,7 +33,9 @@ SubstituteCtx::substitute_metavar (std::unique_ptr<AST::Token> &metavar)
33
33
}
34
34
35
35
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)
37
39
{
38
40
rust_assert (pattern_end < macro.size ());
39
41
@@ -117,6 +119,11 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end)
117
119
auto substitute_context = SubstituteCtx (input, new_macro, sub_map);
118
120
auto new_tokens = substitute_context.substitute_tokens ();
119
121
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
+
120
127
for (auto &new_token : new_tokens)
121
128
expanded.emplace_back (new_token->clone_token ());
122
129
}
@@ -127,6 +134,13 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end)
127
134
return expanded;
128
135
}
129
136
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
+
130
144
std::pair<std::vector<std::unique_ptr<AST::Token>>, size_t >
131
145
SubstituteCtx::substitute_token (size_t token_idx)
132
146
{
@@ -148,20 +162,34 @@ SubstituteCtx::substitute_token (size_t token_idx)
148
162
pattern_end++)
149
163
;
150
164
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
+
151
181
// 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?
154
184
155
185
// FIXME: Add error handling in the case we haven't found a matching
156
186
// closing delimiter
157
187
158
188
// FIXME: We need to parse the repetition token now
159
189
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};
165
193
}
166
194
// TODO: We need to check if the $ was alone. In that case, do
167
195
// not error out: Simply act as if there was an empty identifier
0 commit comments