diff options
author | Owen Avery <powerboat9.gamer@gmail.com> | 2023-05-08 20:58:59 -0400 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-01-16 18:37:18 +0100 |
commit | 2aeff9ce08576ad33a29ed6fc6a87e9516b0641f (patch) | |
tree | 13baedb8833902eb1e320327ceef66f9d9844ec6 /gcc/rust | |
parent | 8769b4900fd0a91cc5021a4f86679820b17d4c41 (diff) | |
download | gcc-2aeff9ce08576ad33a29ed6fc6a87e9516b0641f.zip gcc-2aeff9ce08576ad33a29ed6fc6a87e9516b0641f.tar.gz gcc-2aeff9ce08576ad33a29ed6fc6a87e9516b0641f.tar.bz2 |
gccrs: Handle keywords in macro fragments
gcc/rust/ChangeLog:
* lex/rust-token.cc
(token_id_is_keyword): New.
(token_id_keyword_string): New.
* lex/rust-token.h
(token_id_is_keyword): New.
(token_id_keyword_string): New.
* expand/rust-macro-expand.cc
(MacroExpander::match_fragment): Match keywords for ident fragment.
* parse/rust-parse-impl.h
(Parser::parse_identifier_or_keyword_token): Add.
* parse/rust-parse.h
(Parser::parse_identifier_or_keyword_token): Add.
gcc/testsuite/ChangeLog:
* rust/compile/macro-issue2192.rs: New test.
Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r-- | gcc/rust/expand/rust-macro-expand.cc | 2 | ||||
-rw-r--r-- | gcc/rust/lex/rust-token.cc | 34 | ||||
-rw-r--r-- | gcc/rust/lex/rust-token.h | 6 | ||||
-rw-r--r-- | gcc/rust/parse/rust-parse-impl.h | 18 | ||||
-rw-r--r-- | gcc/rust/parse/rust-parse.h | 1 |
5 files changed, 60 insertions, 1 deletions
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 09a4849..5f6c50e 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -378,7 +378,7 @@ MacroExpander::match_fragment (Parser<MacroInvocLexer> &parser, break; case AST::MacroFragSpec::IDENT: - parser.parse_identifier_pattern (); + parser.parse_identifier_or_keyword_token (); break; case AST::MacroFragSpec::LITERAL: diff --git a/gcc/rust/lex/rust-token.cc b/gcc/rust/lex/rust-token.cc index 8b7cdc0..2956d92 100644 --- a/gcc/rust/lex/rust-token.cc +++ b/gcc/rust/lex/rust-token.cc @@ -57,6 +57,40 @@ token_id_to_str (TokenId id) } } +/* checks if a token is a keyword */ +bool +token_id_is_keyword (TokenId id) +{ + switch (id) + { +#define RS_TOKEN_KEYWORD(name, _) case name: +#define RS_TOKEN(a, b) + RS_TOKEN_LIST return true; +#undef RS_TOKEN_KEYWORD +#undef RS_TOKEN + default: + return false; + } +} + +/* gets the string associated with a keyword */ +const char * +token_id_keyword_string (TokenId id) +{ + switch (id) + { +#define RS_TOKEN_KEYWORD(id, str) \ + case id: \ + return str; +#define RS_TOKEN(a, b) + RS_TOKEN_LIST +#undef RS_TOKEN_KEYWORD +#undef RS_TOKEN + default: + return nullptr; + } +} + const char * get_type_hint_string (PrimitiveCoreType type) { diff --git a/gcc/rust/lex/rust-token.h b/gcc/rust/lex/rust-token.h index 65a37fd..48640f5 100644 --- a/gcc/rust/lex/rust-token.h +++ b/gcc/rust/lex/rust-token.h @@ -226,6 +226,12 @@ get_token_description (TokenId id); * x-macros */ const char * token_id_to_str (TokenId id); +/* checks if a token is a keyword */ +bool +token_id_is_keyword (TokenId id); +/* gets the string associated with a keyword */ +const char * +token_id_keyword_string (TokenId id); // Get type hint description as a string. const char * get_type_hint_string (PrimitiveCoreType type); diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 1addbe9..7fefb16 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -999,6 +999,24 @@ Parser<ManagedTokenSource>::parse_delim_token_tree () } } +// Parses an identifier/keyword as a Token +template <typename ManagedTokenSource> +std::unique_ptr<AST::Token> +Parser<ManagedTokenSource>::parse_identifier_or_keyword_token () +{ + const_TokenPtr t = lexer.peek_token (); + + if (t->get_id () == IDENTIFIER || token_id_is_keyword (t->get_id ())) + { + lexer.skip_token (); + return std::unique_ptr<AST::Token> (new AST::Token (std::move (t))); + } + else + { + return nullptr; + } +} + /* Parses a TokenTree syntactical production. This is either a delimited token * tree or a non-delimiter token. */ template <typename ManagedTokenSource> diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 6957b66..71f0ff1 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -148,6 +148,7 @@ public: std::vector<std::unique_ptr<AST::LifetimeParam> > parse_lifetime_params (); AST::Visibility parse_visibility (); std::unique_ptr<AST::IdentifierPattern> parse_identifier_pattern (); + std::unique_ptr<AST::Token> parse_identifier_or_keyword_token (); std::unique_ptr<AST::TokenTree> parse_token_tree (); std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, Location> parse_attribute_body (); |