Skip to content

Commit

Permalink
Add edition keywords, use for async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Apr 10, 2018
1 parent 4607cc6 commit d55832f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
8 changes: 8 additions & 0 deletions src/libsyntax/edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use std::fmt;
use std::str::FromStr;
use symbol::{self, Symbol};

/// The edition of the compiler (RFC 2052)
#[derive(Clone, Copy, Hash, PartialOrd, Ord, Eq, PartialEq, Debug)]
Expand Down Expand Up @@ -56,6 +57,13 @@ impl Edition {
}
}

pub fn is_future_edition_keyword(&self, sym: Symbol) -> bool {
match *self {
Edition::Edition2015 => symbol::is_future_edition_keyword_2015(sym),
Edition::Edition2018 => symbol::is_future_edition_keyword_2018(sym),
}
}

pub fn feature_name(&self) -> &'static str {
match *self {
Edition::Edition2015 => "rust_2015_preview",
Expand Down
6 changes: 5 additions & 1 deletion src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ impl<'a> StringReader<'a> {
let c = self.ch;

if ident_start(c) {
let (is_ident_start, is_raw_ident) =
let (is_ident_start, mut is_raw_ident) =
match (c.unwrap(), self.nextch(), self.nextnextch()) {
// r# followed by an identifier starter is a raw identifier.
// This is an exception to the r# case below.
Expand Down Expand Up @@ -1155,9 +1155,13 @@ impl<'a> StringReader<'a> {
&format!("`r#{}` is not currently supported.", ident.name)
).raise();
}

if is_raw_ident {
let span = self.mk_sp(raw_start, self.pos);
self.sess.raw_identifier_spans.borrow_mut().push(span);
} else if self.sess.edition.is_future_edition_keyword(ident.name) {
// we pretend these are raw even if they aren't
is_raw_ident = true;
}
token::Ident(ident, is_raw_ident)
}));
Expand Down
16 changes: 1 addition & 15 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,7 @@ pub fn is_raw_guess(ident: ast::Ident) -> bool {
is_reserved_ident(ident) && !is_path_segment_keyword(ident)
}

// Returns true for reserved identifiers used internally for elided lifetimes,
// unnamed method parameters, crate root module, error recovery etc.
pub fn is_special_ident(id: ast::Ident) -> bool {
id.name <= keywords::Underscore.name()
}

/// Returns `true` if the token is a keyword used in the language.
pub fn is_used_keyword(id: ast::Ident) -> bool {
id.name >= keywords::As.name() && id.name <= keywords::While.name()
}

/// Returns `true` if the token is a keyword reserved for possible future use.
pub fn is_unused_keyword(id: ast::Ident) -> bool {
id.name >= keywords::Abstract.name() && id.name <= keywords::Yield.name()
}
pub use syntax_pos::symbol::{is_special_ident, is_used_keyword, is_unused_keyword};

/// Returns `true` if the token is either a special identifier or a keyword.
pub fn is_reserved_ident(id: ast::Ident) -> bool {
Expand Down
45 changes: 38 additions & 7 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ declare_keywords! {
(37, Use, "use")
(38, Where, "where")
(39, While, "while")
// edition-gated used keywords go here
// be sure to update is_used_keyword and
// is_future_edition_keyword_* below

// Keywords reserved for future use.
(40, Abstract, "abstract")
Expand All @@ -343,17 +346,45 @@ declare_keywords! {
(53, Unsized, "unsized")
(54, Virtual, "virtual")
(55, Yield, "yield")
// edition-gated reserved keywords
// be sure to update is_unused_keyword and
// is_future_edition_keyword_* below
(56, Async, "async") // Rust 2018+ only

// Special lifetime names
(56, UnderscoreLifetime, "'_")
(57, StaticLifetime, "'static")
(57, UnderscoreLifetime, "'_")
(58, StaticLifetime, "'static")

// Weak keywords, have special meaning only in specific contexts.
(58, Auto, "auto")
(59, Catch, "catch")
(60, Default, "default")
(61, Dyn, "dyn")
(62, Union, "union")
(59, Auto, "auto")
(60, Catch, "catch")
(61, Default, "default")
(62, Dyn, "dyn")
(63, Union, "union")
}

// Returns true for reserved identifiers used internally for elided lifetimes,
// unnamed method parameters, crate root module, error recovery etc.
pub fn is_special_ident(id: Ident) -> bool {
id.name <= self::keywords::Underscore.name()
}

/// Returns `true` if the token is a keyword used in the language.
pub fn is_used_keyword(id: Ident) -> bool {
id.name >= self::keywords::As.name() && id.name <= self::keywords::While.name()
}

/// Returns `true` if the token is a keyword reserved for possible future use.
pub fn is_unused_keyword(id: Ident) -> bool {
id.name >= self::keywords::Abstract.name() && id.name <= self::keywords::Async.name()
}

pub fn is_future_edition_keyword_2015(sym: Symbol) -> bool {
sym == self::keywords::Async.name()
}

pub fn is_future_edition_keyword_2018(_: Symbol) -> bool {
false
}

// If an interner exists, return it. Otherwise, prepare a fresh one.
Expand Down

0 comments on commit d55832f

Please sign in to comment.