aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2022-03-03 14:16:02 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2022-03-03 15:20:55 +0100
commitab4533dab7a5be7c900012f40dbeb3fc0cce0456 (patch)
tree63362d5c7259b8f0bae058588d46023872cee80d /gcc
parent25a33b0739d549c4c2937bb7035a769c0170ba65 (diff)
downloadgcc-ab4533dab7a5be7c900012f40dbeb3fc0cce0456.zip
gcc-ab4533dab7a5be7c900012f40dbeb3fc0cce0456.tar.gz
gcc-ab4533dab7a5be7c900012f40dbeb3fc0cce0456.tar.bz2
macros: Match repetition separator properly
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/ast/rust-macro.h1
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc21
-rw-r--r--gcc/rust/expand/rust-macro-expand.h7
3 files changed, 18 insertions, 11 deletions
diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h
index 9f8f19c..91107ff 100644
--- a/gcc/rust/ast/rust-macro.h
+++ b/gcc/rust/ast/rust-macro.h
@@ -207,6 +207,7 @@ public:
}
MacroRepOp get_op () const { return op; }
+ const std::unique_ptr<MacroRepSep> &get_sep () const { return sep; }
std::vector<std::unique_ptr<MacroMatch> > &get_matches () { return matches; }
protected:
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index a4ed36b..14d68a1 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -3615,12 +3615,13 @@ MacroExpander::match_token (Parser<MacroInvocLexer> &parser, AST::Token &token)
}
bool
-MacroExpander::match_n_matches (
- Parser<MacroInvocLexer> &parser,
- std::vector<std::unique_ptr<AST::MacroMatch>> &matches, size_t &match_amount,
- size_t lo_bound, size_t hi_bound)
+MacroExpander::match_n_matches (Parser<MacroInvocLexer> &parser,
+ AST::MacroMatchRepetition &rep,
+ size_t &match_amount, size_t lo_bound,
+ size_t hi_bound)
{
match_amount = 0;
+ auto &matches = rep.get_matches ();
const MacroInvocLexer &source = parser.get_token_source ();
while (true)
@@ -3631,6 +3632,12 @@ MacroExpander::match_n_matches (
if (t_id == RIGHT_PAREN || t_id == RIGHT_SQUARE || t_id == RIGHT_CURLY)
break;
+ // Skip parsing a separator on the first match, otherwise consume it.
+ // If it isn't present, this is an error
+ if (rep.has_sep () && match_amount > 0)
+ if (!match_token (parser, *rep.get_sep ()))
+ break;
+
bool valid_current_match = false;
for (auto &match : matches)
{
@@ -3705,17 +3712,17 @@ MacroExpander::match_repetition (Parser<MacroInvocLexer> &parser,
case AST::MacroMatchRepetition::MacroRepOp::ANY:
lo_str = "0";
hi_str = "+inf";
- res = match_n_matches (parser, rep.get_matches (), match_amount);
+ res = match_n_matches (parser, rep, match_amount);
break;
case AST::MacroMatchRepetition::MacroRepOp::ONE_OR_MORE:
lo_str = "1";
hi_str = "+inf";
- res = match_n_matches (parser, rep.get_matches (), match_amount, 1);
+ res = match_n_matches (parser, rep, match_amount, 1);
break;
case AST::MacroMatchRepetition::MacroRepOp::ZERO_OR_ONE:
lo_str = "0";
hi_str = "1";
- res = match_n_matches (parser, rep.get_matches (), match_amount, 0, 1);
+ res = match_n_matches (parser, rep, match_amount, 0, 1);
break;
default:
gcc_unreachable ();
diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h
index 9309323..617d16b 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -192,7 +192,7 @@ struct MacroExpander
* Match any amount of matches
*
* @param parser Parser to use for matching
- * @param matches All consecutive matches to identify
+ * @param rep Repetition to try and match
* @param match_amount Reference in which to store the ammount of succesful
* and valid matches
*
@@ -209,9 +209,8 @@ struct MacroExpander
* otherwise
*/
bool match_n_matches (Parser<MacroInvocLexer> &parser,
- std::vector<std::unique_ptr<AST::MacroMatch>> &matches,
- size_t &match_amount, size_t lo_bound = 0,
- size_t hi_bound = 0);
+ AST::MacroMatchRepetition &rep, size_t &match_amount,
+ size_t lo_bound = 0, size_t hi_bound = 0);
void push_context (ContextType t) { context.push_back (t); }