aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/lex
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2022-07-19 13:09:17 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2022-07-19 13:09:17 +0200
commit66af31d5bc5a1ea76be381dc9246fbe4fe034fbb (patch)
tree9541637ba71200d98109f7bc3f698842deb4848e /gcc/rust/lex
parentfb1b2a6bf5eed9dd9405dca9b9c895f48509875e (diff)
downloadgcc-66af31d5bc5a1ea76be381dc9246fbe4fe034fbb.zip
gcc-66af31d5bc5a1ea76be381dc9246fbe4fe034fbb.tar.gz
gcc-66af31d5bc5a1ea76be381dc9246fbe4fe034fbb.tar.bz2
lexer: Allow specifiying tokens as reserved in certain editions
Some tokens such as `try` only became reserved keywords in certain editions. This behavior might happen again in the future and we should be able to handle it.
Diffstat (limited to 'gcc/rust/lex')
-rw-r--r--gcc/rust/lex/rust-lex.cc19
1 files changed, 17 insertions, 2 deletions
diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc
index 93d96d3..ecf151d 100644
--- a/gcc/rust/lex/rust-lex.cc
+++ b/gcc/rust/lex/rust-lex.cc
@@ -21,6 +21,7 @@
#include "rust-system.h" // for rust_assert and rust_unreachable
#include "rust-diagnostics.h" // for rust_error_at
#include "rust-linemap.h"
+#include "rust-session-manager.h"
#include "safe-ctype.h"
namespace Rust {
@@ -228,10 +229,24 @@ Lexer::classify_keyword (const std::string &str)
if (idx == last || str != *idx)
return IDENTIFIER;
- else
- return keyword_keys[idx - keyword_index];
// TODO: possibly replace this x-macro system with something like hash map?
+
+ // We now have the expected token ID of the reserved keyword. However, some
+ // keywords are reserved starting in certain editions. For example, `try` is
+ // only a reserved keyword in editions >=2018. The language might gain new
+ // reserved keywords in the future.
+ //
+ // https://doc.rust-lang.org/reference/keywords.html#reserved-keywords
+ auto id = keyword_keys[idx - keyword_index];
+
+ // `try` is not a reserved keyword before 2018
+ if (Session::get_instance ().options.get_edition ()
+ == CompileOptions::Edition::E2015
+ && id == TRY)
+ return IDENTIFIER;
+
+ return id;
}
TokenPtr