Skip to content

Commit 366c533

Browse files
Merge #991
991: Match and expand macro separators properly r=CohenArthur a=CohenArthur More nice recursive macros: ```rust macro_rules! add { ($e:expr | $($es:expr) | *) => { $e + add!($($es) | *) }; ($e:expr) => { $e }; } add!(1 | 2 | 3 | 4 | 5 | 6); ``` Closes #968 This PR needs #986 to be merged first, as it depends on it for the test cases. You can skip reviewing the first two commits which are just from #986 Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2 parents b82408f + 0c7e16e commit 366c533

File tree

8 files changed

+103
-21
lines changed

8 files changed

+103
-21
lines changed

gcc/rust/ast/rust-macro.h

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

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

211212
protected:

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

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

36003600
bool
3601-
MacroExpander::match_n_matches (
3602-
Parser<MacroInvocLexer> &parser,
3603-
std::vector<std::unique_ptr<AST::MacroMatch>> &matches, size_t &match_amount,
3604-
size_t lo_bound, size_t hi_bound)
3601+
MacroExpander::match_n_matches (Parser<MacroInvocLexer> &parser,
3602+
AST::MacroMatchRepetition &rep,
3603+
size_t &match_amount, size_t lo_bound,
3604+
size_t hi_bound)
36053605
{
36063606
match_amount = 0;
3607+
auto &matches = rep.get_matches ();
36073608

36083609
const MacroInvocLexer &source = parser.get_token_source ();
36093610
while (true)
@@ -3614,6 +3615,12 @@ MacroExpander::match_n_matches (
36143615
if (t_id == RIGHT_PAREN || t_id == RIGHT_SQUARE || t_id == RIGHT_CURLY)
36153616
break;
36163617

3618+
// Skip parsing a separator on the first match, otherwise consume it.
3619+
// If it isn't present, this is an error
3620+
if (rep.has_sep () && match_amount > 0)
3621+
if (!match_token (parser, *rep.get_sep ()))
3622+
break;
3623+
36173624
bool valid_current_match = false;
36183625
for (auto &match : matches)
36193626
{
@@ -3688,17 +3695,17 @@ MacroExpander::match_repetition (Parser<MacroInvocLexer> &parser,
36883695
case AST::MacroMatchRepetition::MacroRepOp::ANY:
36893696
lo_str = "0";
36903697
hi_str = "+inf";
3691-
res = match_n_matches (parser, rep.get_matches (), match_amount);
3698+
res = match_n_matches (parser, rep, match_amount);
36923699
break;
36933700
case AST::MacroMatchRepetition::MacroRepOp::ONE_OR_MORE:
36943701
lo_str = "1";
36953702
hi_str = "+inf";
3696-
res = match_n_matches (parser, rep.get_matches (), match_amount, 1);
3703+
res = match_n_matches (parser, rep, match_amount, 1);
36973704
break;
36983705
case AST::MacroMatchRepetition::MacroRepOp::ZERO_OR_ONE:
36993706
lo_str = "0";
37003707
hi_str = "1";
3701-
res = match_n_matches (parser, rep.get_matches (), match_amount, 0, 1);
3708+
res = match_n_matches (parser, rep, match_amount, 0, 1);
37023709
break;
37033710
default:
37043711
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

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

gcc/testsuite/rust/compile/macro9.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
macro_rules! add {
2+
($e:expr, $($es:expr),*) => {
3+
$e + add!($($es),*)
4+
};
5+
($e:expr) => {
6+
$e
7+
};
8+
}
9+
10+
fn main() -> i32 {
11+
let a = add!(15 2 9); // { dg-error "Failed to match any rule within macro" }
12+
let b = add!(15);
13+
let b = add!(15 14); // { dg-error "Failed to match any rule within macro" }
14+
let b = add!(15, 14,); // { dg-error "Failed to match any rule within macro" }
15+
16+
0
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
macro_rules! add {
2+
($e:expr, $($es:expr),*) => {
3+
$e + add!($($es),*)
4+
};
5+
($e:expr) => {
6+
$e
7+
};
8+
}
9+
10+
fn main() -> i32 {
11+
let a = add!(15, 2, 9); // 26
12+
13+
a - 26
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
macro_rules! add {
2+
($e:expr big_tok $($es:expr) big_tok *) => {
3+
$e + add!($($es) big_tok *)
4+
};
5+
($e:expr) => {
6+
$e
7+
};
8+
}
9+
10+
fn main() -> i32 {
11+
let a = add!(15 big_tok 2 big_tok 9); // 26
12+
13+
a - 26
14+
}

0 commit comments

Comments
 (0)