]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Introduce a proper keyword list
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Thu, 16 Nov 2023 15:20:49 +0000 (16:20 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 30 Jan 2024 11:36:45 +0000 (12:36 +0100)
The old "keyword" list was used for the lexer, and could therefore not
be used with keyword spanning over multiple tokens as those tokens should
remain lexed as is. Hence the introduction of a new list macro for
keyword exclusive tasks. This also means we can no longer match a token
id for each keyword. The token id map has been renamed to keep it's
properties.

gcc/rust/ChangeLog:

* lex/rust-lex.cc (Lexer::classify_keyword): Update keyword map name.
* lex/rust-token.h (enum PrimitiveCoreType): Remove some deprecated
comments.
* util/rust-keyword-values.cc (get_keywords): Update the keyword map
name.
(RS_TOKEN): Define as empty
(RS_TOKEN_KEYWORD_2015): Add the emission value.
(RS_TOKEN_KEYWORD_2018): Likewise.
* util/rust-keyword-values.h (RS_KEYWORD_LIST): Introduce the keyword
list.
(RS_TOKEN_KEYWORD_2018): Define multiple new keywords.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/lex/rust-lex.cc
gcc/rust/lex/rust-token.h
gcc/rust/util/rust-keyword-values.cc
gcc/rust/util/rust-keyword-values.h

index 2d41c114f73e166f5c2cc18ffd7f5dae7bc24558..5bff2d9125c4bb95226034973abc798161ec1b4d 100644 (file)
@@ -260,7 +260,7 @@ Lexer::replace_current_token (TokenPtr replacement)
 TokenId
 Lexer::classify_keyword (const std::string &str)
 {
-  auto &keywords = Rust::Values::Keywords::keywords;
+  auto &keywords = Rust::Values::Keywords::keywords_tokens;
   auto keyword = keywords.find (str);
 
   if (keyword == keywords.end ())
index e38c3cf99433f3e0580bc0c07ed91ca7b79750cd..438b29b495724e852505bf41f132e1d84f145e0b 100644 (file)
@@ -145,7 +145,6 @@ enum PrimitiveCoreType
   /* Doc Comments */                                                           \
   RS_TOKEN (INNER_DOC_COMMENT, "#![doc]")                                      \
   RS_TOKEN (OUTER_DOC_COMMENT, "#[doc]")                                       \
-  /* have "weak" union and 'static keywords? */                                \
   RS_TOKEN_KEYWORD_2015 (ABSTRACT, "abstract") /* unused */                    \
   RS_TOKEN_KEYWORD_2015 (AS, "as")                                             \
   RS_TOKEN_KEYWORD_2018 (ASYNC, "async") /* unused */                          \
@@ -157,7 +156,6 @@ enum PrimitiveCoreType
   RS_TOKEN_KEYWORD_2015 (CONST, "const")                                       \
   RS_TOKEN_KEYWORD_2015 (CONTINUE, "continue")                                 \
   RS_TOKEN_KEYWORD_2015 (CRATE, "crate")                                       \
-  /* FIXME: Do we need to add $crate (DOLLAR_CRATE) as a reserved kw? */       \
   RS_TOKEN_KEYWORD_2015 (DO, "do") /* unused */                                \
   RS_TOKEN_KEYWORD_2018 (DYN, "dyn")                                           \
   RS_TOKEN_KEYWORD_2015 (ELSE, "else")                                         \
index 8aa5ef1371ab7fa63b29e27f34f3777b7361d5f9..9e1d2bcdef6b962406a32242b73e31108cdb784f 100644 (file)
@@ -38,7 +38,18 @@ get_keywords ()
   return m;
 }
 
-const std::map<std::string, TokenId> Keywords::keywords = get_keywords ();
+const std::map<std::string, TokenId> Keywords::keywords_tokens
+  = get_keywords ();
+
+const std::set<std::string> Keywords::keywords = {
+#define RS_TOKEN(x, y)
+#define RS_TOKEN_KEYWORD_2015(tok, key) {key},
+#define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
+  RS_KEYWORD_LIST
+#undef RS_TOKEN_KEYWORD_2015
+#undef RS_TOKEN_KEYWORD_2018
+#undef RS_TOKEN
+};
 
 } // namespace Values
 } // namespace Rust
index 4a6f1df525d0cc9e04e08452f28dfd7fb9bbfbb6..01c98a2cde4534c21b5f2928b7efc80480277fb8 100644 (file)
 
 #include "rust-token.h"
 
+// Append keywords made from multiple tokens to the existing token-keyword list
+#define RS_KEYWORD_LIST                                                        \
+  RS_TOKEN_LIST                                                                \
+  RS_TOKEN_KEYWORD_2015 (DOLLAR_CRATE, "$crate")                               \
+  RS_TOKEN_KEYWORD_2015 (PATH_ROOT, "{{root}}")                                \
+  RS_TOKEN_KEYWORD_2015 (STATIC_LIFETIME, "'static")                           \
+  RS_TOKEN_KEYWORD_2015 (UNDERSCORE_LIFETIME, "'_")
+
 namespace Rust {
 namespace Values {
 
@@ -28,14 +36,15 @@ namespace Values {
 class Keywords
 {
 public:
-  const static std::map<std::string, TokenId> keywords;
+  const static std::map<std::string, TokenId> keywords_tokens;
 
+  const static std::set<std::string> keywords;
   // Rust keyword values
 public:
 #define RS_TOKEN(x, y)
 #define RS_TOKEN_KEYWORD_2015(tok, key) static constexpr auto &tok = key;
 #define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
-  RS_TOKEN_LIST
+  RS_KEYWORD_LIST
 #undef RS_TOKEN_KEYWORD_2015
 #undef RS_TOKEN_KEYWORD_2018
 #undef RS_TOKEN