diff options
author | Owen Avery <powerboat9.gamer@gmail.com> | 2023-06-26 13:06:10 -0400 |
---|---|---|
committer | CohenArthur <arthur.cohen@embecosm.com> | 2023-06-27 07:50:44 +0000 |
commit | 96ba341e0fd4799bcd22535fc2166a9ac96628ca (patch) | |
tree | 499e9808e9954aa1f00603443f6a24c822d4e12b /gcc/rust/parse/rust-parse-impl.h | |
parent | 4c07c94212e2cc43bf4c01eb195347821791659b (diff) | |
download | gcc-96ba341e0fd4799bcd22535fc2166a9ac96628ca.zip gcc-96ba341e0fd4799bcd22535fc2166a9ac96628ca.tar.gz gcc-96ba341e0fd4799bcd22535fc2166a9ac96628ca.tar.bz2 |
Match tokens in macros more closely
gcc/rust/ChangeLog:
* expand/rust-macro-expand.cc
(MacroExpander::match_token): Match token instead of token id.
* parse/rust-parse-impl.h
(Parser::skip_token): Add token-skipping variant.
(Parser::expect_token): Likewise.
* parse/rust-parse.h
(Parser::skip_token): Likewise.
(Parser::expect_token): Likewise.
gcc/testsuite/ChangeLog:
* rust/compile/macro-issue2264.rs: New test.
Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
Diffstat (limited to 'gcc/rust/parse/rust-parse-impl.h')
-rw-r--r-- | gcc/rust/parse/rust-parse-impl.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 627b4cc..efbd625 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -11936,6 +11936,15 @@ Parser<ManagedTokenSource>::skip_token (TokenId token_id) return expect_token (token_id) != const_TokenPtr (); } +/* Checks if current token is similar to inputted token - skips it and returns + * true if so, diagnoses an error and returns false otherwise. */ +template <typename ManagedTokenSource> +bool +Parser<ManagedTokenSource>::skip_token (const_TokenPtr token) +{ + return expect_token (token) != const_TokenPtr (); +} + /* Checks if current token has inputted id - skips it and returns true if so, * returns false otherwise without diagnosing an error */ template <typename ManagedTokenSource> @@ -11971,6 +11980,30 @@ Parser<ManagedTokenSource>::expect_token (TokenId token_id) } } +/* Checks the current token - if same as expected, skips and returns it, + * otherwise diagnoses error and returns null. */ +template <typename ManagedTokenSource> +const_TokenPtr +Parser<ManagedTokenSource>::expect_token (const_TokenPtr token_expect) +{ + const_TokenPtr t = lexer.peek_token (); + if (t->get_id () == token_expect->get_id () + && (!t->should_have_str () || t->get_str () == token_expect->get_str ())) + { + lexer.skip_token (); + return t; + } + else + { + Error error (t->get_locus (), "expecting %qs but %qs found", + token_expect->get_token_description (), + t->get_token_description ()); + add_error (std::move (error)); + + return const_TokenPtr (); + } +} + // Skips all tokens until EOF or }. Don't use. template <typename ManagedTokenSource> void |