about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2016-04-16 18:05:06 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2016-04-24 20:59:44 +0300
commite2c821d35ee5cb5211f92480a53b409b2b2c359e (patch)
treef4abbb16e9b02d26998e05a3566402f92233c852 /src/libsyntax/parse
parent546c052d225d41cd31f610e87a20f15cd0fa8e3c (diff)
downloadrust-e2c821d35ee5cb5211f92480a53b409b2b2c359e.tar.gz
rust-e2c821d35ee5cb5211f92480a53b409b2b2c359e.zip
syntax: Make static/super/self/Self keywords + special ident cleanup
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/parser.rs44
-rw-r--r--src/libsyntax/parse/token.rs159
2 files changed, 76 insertions, 127 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 71f059de041..2c82cbcb6c6 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -292,7 +292,7 @@ impl TokenType {
         match *self {
             TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)),
             TokenType::Operator => "an operator".to_string(),
-            TokenType::Keyword(kw) => format!("`{}`", kw.to_name()),
+            TokenType::Keyword(kw) => format!("`{}`", kw.ident.name),
         }
     }
 }
@@ -562,9 +562,7 @@ impl<'a> Parser<'a> {
     }
 
     pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
-        if !self.restrictions.contains(Restrictions::ALLOW_MODULE_PATHS) {
-            self.check_strict_keywords();
-        }
+        self.check_used_keywords();
         self.check_reserved_keywords();
         match self.token {
             token::Ident(i) => {
@@ -658,8 +656,8 @@ impl<'a> Parser<'a> {
     }
 
     /// Signal an error if the given string is a strict keyword
-    pub fn check_strict_keywords(&mut self) {
-        if self.token.is_strict_keyword() {
+    pub fn check_used_keywords(&mut self) {
+        if self.token.is_used_keyword() {
             let token_str = self.this_token_to_string();
             let span = self.span;
             self.span_err(span,
@@ -1553,7 +1551,7 @@ impl<'a> Parser<'a> {
         } else {
             debug!("parse_arg_general ident_to_pat");
             let sp = self.last_span;
-            let spanned = Spanned { span: sp, node: special_idents::invalid };
+            let spanned = Spanned { span: sp, node: special_idents::Invalid };
             P(Pat {
                 id: ast::DUMMY_NODE_ID,
                 node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable),
@@ -2335,7 +2333,7 @@ impl<'a> Parser<'a> {
                     }
                     hi = self.last_span.hi;
                 } else if self.token.is_keyword(keywords::Let) {
-                    // Catch this syntax error here, instead of in `check_strict_keywords`, so
+                    // Catch this syntax error here, instead of in `check_used_keywords`, so
                     // that we can explicitly mention that let is not to be used as an expression
                     let mut db = self.fatal("expected expression, found statement (`let`)");
                     db.note("variable declaration using `let` is a statement");
@@ -2618,7 +2616,7 @@ impl<'a> Parser<'a> {
                     self.span_err(self.span, &format!("unexpected token: `{}`", actual));
 
                     let dot_pos = self.last_span.hi;
-                    e = self.parse_dot_suffix(special_idents::invalid,
+                    e = self.parse_dot_suffix(special_idents::Invalid,
                                               mk_sp(dot_pos, dot_pos),
                                               e, lo)?;
                   }
@@ -2696,7 +2694,7 @@ impl<'a> Parser<'a> {
         };
         // continue by trying to parse the `:ident` after `$name`
         if self.token == token::Colon && self.look_ahead(1, |t| t.is_ident() &&
-                                                                !t.is_strict_keyword() &&
+                                                                !t.is_used_keyword() &&
                                                                 !t.is_reserved_keyword()) {
             self.bump();
             sp = mk_sp(sp.lo, self.span.hi);
@@ -3942,7 +3940,7 @@ impl<'a> Parser<'a> {
             self.bump();
 
             let id = match self.token {
-                token::OpenDelim(_) => token::special_idents::invalid, // no special identifier
+                token::OpenDelim(_) => token::special_idents::Invalid, // no special identifier
                 _ => self.parse_ident()?,
             };
 
@@ -3954,7 +3952,7 @@ impl<'a> Parser<'a> {
                 _ => {
                     // we only expect an ident if we didn't parse one
                     // above.
-                    let ident_str = if id.name == token::special_idents::invalid.name {
+                    let ident_str = if id.name == token::special_idents::Invalid.name {
                         "identifier, "
                     } else {
                         ""
@@ -3980,7 +3978,7 @@ impl<'a> Parser<'a> {
                 MacStmtStyle::NoBraces
             };
 
-            if id.name == token::special_idents::invalid.name {
+            if id.name == token::special_idents::Invalid.name {
                 let mac = P(spanned(lo, hi, Mac_ { path: pth, tts: tts, ctxt: EMPTY_CTXT }));
                 let stmt = StmtKind::Mac(mac, style, attrs.into_thin_attrs());
                 spanned(lo, hi, stmt)
@@ -4610,8 +4608,10 @@ impl<'a> Parser<'a> {
 
     fn expect_self_ident(&mut self) -> PResult<'a, ast::Ident> {
         match self.token {
-            token::Ident(id) if id.name == special_idents::self_.name => {
+            token::Ident(id) if id.name == keywords::SelfValue.ident.name => {
                 self.bump();
+                // The hygiene context of `id` needs to be preserved here,
+                // so we can't just return `SelfValue.ident`.
                 Ok(id)
             },
             _ => {
@@ -4696,7 +4696,7 @@ impl<'a> Parser<'a> {
                     self.bump();
                 }
                 // error case, making bogus self ident:
-                SelfKind::Value(special_idents::self_)
+                SelfKind::Value(keywords::SelfValue.ident)
             }
             token::Ident(..) => {
                 if self.token.is_keyword(keywords::SelfValue) {
@@ -4971,7 +4971,7 @@ impl<'a> Parser<'a> {
             if delim != token::Brace {
                 self.expect(&token::Semi)?
             }
-            Ok((token::special_idents::invalid, vec![], ast::ImplItemKind::Macro(m)))
+            Ok((token::special_idents::Invalid, vec![], ast::ImplItemKind::Macro(m)))
         } else {
             let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
             let ident = self.parse_ident()?;
@@ -5066,7 +5066,7 @@ impl<'a> Parser<'a> {
 
             self.expect(&token::OpenDelim(token::Brace))?;
             self.expect(&token::CloseDelim(token::Brace))?;
-            Ok((special_idents::invalid,
+            Ok((special_idents::Invalid,
              ItemKind::DefaultImpl(unsafety, opt_trait.unwrap()), None))
         } else {
             if opt_trait.is_some() {
@@ -5082,7 +5082,7 @@ impl<'a> Parser<'a> {
                 impl_items.push(self.parse_impl_item()?);
             }
 
-            Ok((special_idents::invalid,
+            Ok((special_idents::Invalid,
              ItemKind::Impl(unsafety, polarity, generics, opt_trait, ty, impl_items),
              Some(attrs)))
         }
@@ -5260,7 +5260,7 @@ impl<'a> Parser<'a> {
 
     /// Parse defaultness: DEFAULT or nothing
     fn parse_defaultness(&mut self) -> PResult<'a, Defaultness> {
-        if self.eat_contextual_keyword(special_idents::DEFAULT) {
+        if self.eat_contextual_keyword(special_idents::Default) {
             Ok(Defaultness::Default)
         } else {
             Ok(Defaultness::Final)
@@ -5588,7 +5588,7 @@ impl<'a> Parser<'a> {
         };
         Ok(self.mk_item(lo,
                      last_span.hi,
-                     special_idents::invalid,
+                     special_idents::Invalid,
                      ItemKind::ForeignMod(m),
                      visibility,
                      attrs))
@@ -5727,7 +5727,7 @@ impl<'a> Parser<'a> {
             let last_span = self.last_span;
             let item = self.mk_item(lo,
                                     last_span.hi,
-                                    token::special_idents::invalid,
+                                    token::special_idents::Invalid,
                                     item_,
                                     visibility,
                                     attrs);
@@ -6018,7 +6018,7 @@ impl<'a> Parser<'a> {
             let id = if self.token.is_ident() {
                 self.parse_ident()?
             } else {
-                token::special_idents::invalid // no special identifier
+                token::special_idents::Invalid // no special identifier
             };
             // eat a matched-delimiter token tree:
             let delim = self.expect_open_delim()?;
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index 76bd0f66cd8..449a2268740 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -271,47 +271,39 @@ impl Token {
     /// Returns `true` if the token is a given keyword, `kw`.
     pub fn is_keyword(&self, kw: keywords::Keyword) -> bool {
         match *self {
-            Ident(id) => id.name == kw.to_name(),
+            Ident(id) => id.name == kw.ident.name,
             _ => false,
         }
     }
 
     pub fn is_path_segment_keyword(&self) -> bool {
         match *self {
-            Ident(id) => id.name == SUPER_KEYWORD_NAME ||
-                         id.name == SELF_KEYWORD_NAME ||
-                         id.name == SELF_TYPE_KEYWORD_NAME,
+            Ident(id) => id.name == keywords::Super.ident.name ||
+                         id.name == keywords::SelfValue.ident.name ||
+                         id.name == keywords::SelfType.ident.name,
             _ => false,
         }
     }
 
-    /// Returns `true` if the token is either a strict or reserved keyword.
+    /// Returns `true` if the token is either a used or reserved keyword.
     pub fn is_any_keyword(&self) -> bool {
         match *self {
-            Ident(id) => id.name == SELF_KEYWORD_NAME ||
-                         id.name == STATIC_KEYWORD_NAME ||
-                         id.name == SUPER_KEYWORD_NAME ||
-                         id.name == SELF_TYPE_KEYWORD_NAME ||
-                         id.name >= STRICT_KEYWORD_START &&
+            Ident(id) => id.name >= USED_KEYWORD_START &&
                          id.name <= RESERVED_KEYWORD_FINAL,
             _ => false
         }
     }
 
-    /// Returns `true` if the token is either a strict keyword.
-    pub fn is_strict_keyword(&self) -> bool {
+    /// Returns `true` if the token is a used keyword.
+    pub fn is_used_keyword(&self) -> bool {
         match *self {
-            Ident(id) => id.name == SELF_KEYWORD_NAME ||
-                         id.name == STATIC_KEYWORD_NAME ||
-                         id.name == SUPER_KEYWORD_NAME ||
-                         id.name == SELF_TYPE_KEYWORD_NAME ||
-                         id.name >= STRICT_KEYWORD_START &&
-                         id.name <= STRICT_KEYWORD_FINAL,
+            Ident(id) => id.name >= USED_KEYWORD_START &&
+                         id.name <= USED_KEYWORD_FINAL,
             _ => false,
         }
     }
 
-    /// Returns `true` if the token is either a keyword reserved for possible future use.
+    /// Returns `true` if the token is a keyword reserved for possible future use.
     pub fn is_reserved_keyword(&self) -> bool {
         match *self {
             Ident(id) => id.name >= RESERVED_KEYWORD_START &&
@@ -378,7 +370,6 @@ impl fmt::Debug for Nonterminal {
     }
 }
 
-
 // Get the first "argument"
 macro_rules! first {
     ( $first:expr, $( $remainder:expr, )* ) => ( $first )
@@ -392,122 +383,85 @@ macro_rules! last {
 
 // In this macro, there is the requirement that the name (the number) must be monotonically
 // increasing by one in the special identifiers, starting at 0; the same holds for the keywords,
-// except starting from the next number instead of zero, and with the additional exception that
-// special identifiers are *also* allowed (they are deduplicated in the important place, the
-// interner), an exception which is demonstrated by "static" and "self".
+// except starting from the next number instead of zero.
 macro_rules! declare_special_idents_and_keywords {(
     // So now, in these rules, why is each definition parenthesised?
     // Answer: otherwise we get a spurious local ambiguity bug on the "}"
     pub mod special_idents {
-        $( ($si_name:expr, $si_static:ident, $si_str:expr); )*
+        $( ($si_index: expr, $si_const: ident, $si_str: expr); )*
     }
 
     pub mod keywords {
-        'strict:
-        $( ($sk_name:expr, $sk_variant:ident, $sk_str:expr); )*
+        'used:
+        $( ($ukw_index: expr, $ukw_const: ident, $ukw_str: expr); )*
         'reserved:
-        $( ($rk_name:expr, $rk_variant:ident, $rk_str:expr); )*
+        $( ($rkw_index: expr, $rkw_const: ident, $rkw_str: expr); )*
     }
 ) => {
-    const STRICT_KEYWORD_START: ast::Name = first!($( ast::Name($sk_name), )*);
-    const STRICT_KEYWORD_FINAL: ast::Name = last!($( ast::Name($sk_name), )*);
-    const RESERVED_KEYWORD_START: ast::Name = first!($( ast::Name($rk_name), )*);
-    const RESERVED_KEYWORD_FINAL: ast::Name = last!($( ast::Name($rk_name), )*);
+    const USED_KEYWORD_START: ast::Name = first!($( ast::Name($ukw_index), )*);
+    const USED_KEYWORD_FINAL: ast::Name = last!($( ast::Name($ukw_index), )*);
+    const RESERVED_KEYWORD_START: ast::Name = first!($( ast::Name($rkw_index), )*);
+    const RESERVED_KEYWORD_FINAL: ast::Name = last!($( ast::Name($rkw_index), )*);
 
     pub mod special_idents {
         use ast;
         $(
             #[allow(non_upper_case_globals)]
-            pub const $si_static: ast::Ident = ast::Ident {
-                name: ast::Name($si_name),
-                ctxt: ast::EMPTY_CTXT,
-            };
-         )*
-    }
-
-    pub mod special_names {
-        use ast;
-        $(
-            #[allow(non_upper_case_globals)]
-            pub const $si_static: ast::Name = ast::Name($si_name);
+            pub const $si_const: ast::Ident = ast::Ident::with_empty_ctxt(ast::Name($si_index));
         )*
     }
 
-    /// All the valid words that have meaning in the Rust language.
-    ///
-    /// Rust keywords are either 'strict' or 'reserved'.  Strict keywords may not
-    /// appear as identifiers at all. Reserved keywords are not used anywhere in
-    /// the language and may not appear as identifiers.
+    /// Rust keywords are either 'used' in the language or 'reserved' for future use.
     pub mod keywords {
-        pub use self::Keyword::*;
         use ast;
-
-        #[derive(Copy, Clone, PartialEq, Eq)]
-        pub enum Keyword {
-            $( $sk_variant, )*
-            $( $rk_variant, )*
-        }
-
-        impl Keyword {
-            pub fn to_name(&self) -> ast::Name {
-                match *self {
-                    $( $sk_variant => ast::Name($sk_name), )*
-                    $( $rk_variant => ast::Name($rk_name), )*
-                }
-            }
+        #[derive(Clone, Copy, PartialEq, Eq)]
+        pub struct Keyword {
+            pub ident: ast::Ident,
         }
+        $(
+            #[allow(non_upper_case_globals)]
+            pub const $ukw_const: Keyword = Keyword {
+                ident: ast::Ident::with_empty_ctxt(ast::Name($ukw_index))
+            };
+        )*
+        $(
+            #[allow(non_upper_case_globals)]
+            pub const $rkw_const: Keyword = Keyword {
+                ident: ast::Ident::with_empty_ctxt(ast::Name($rkw_index))
+            };
+        )*
     }
 
     fn mk_fresh_ident_interner() -> IdentInterner {
-        let mut init_vec = Vec::new();
-        $(init_vec.push($si_str);)*
-        $(init_vec.push($sk_str);)*
-        $(init_vec.push($rk_str);)*
-        interner::StrInterner::prefill(&init_vec[..])
+        interner::StrInterner::prefill(&[$($si_str,)* $($ukw_str,)* $($rkw_str,)*])
     }
 }}
 
-// If the special idents get renumbered, remember to modify these two as appropriate
-pub const SELF_KEYWORD_NAME: ast::Name = ast::Name(SELF_KEYWORD_NAME_NUM);
-const STATIC_KEYWORD_NAME: ast::Name = ast::Name(STATIC_KEYWORD_NAME_NUM);
-pub const SUPER_KEYWORD_NAME: ast::Name = ast::Name(SUPER_KEYWORD_NAME_NUM);
-const SELF_TYPE_KEYWORD_NAME: ast::Name = ast::Name(SELF_TYPE_KEYWORD_NAME_NUM);
-
-pub const SELF_KEYWORD_NAME_NUM: u32 = 1;
-const STATIC_KEYWORD_NAME_NUM: u32 = 2;
-const SUPER_KEYWORD_NAME_NUM: u32 = 3;
-const SELF_TYPE_KEYWORD_NAME_NUM: u32 = 10;
-
 // NB: leaving holes in the ident table is bad! a different ident will get
 // interned with the id from the hole, but it will be between the min and max
 // of the reserved words, and thus tagged as "reserved".
 
 declare_special_idents_and_keywords! {
     pub mod special_idents {
-        // These ones are statics
-        (0,                          invalid,                "");
-        (super::SELF_KEYWORD_NAME_NUM,   self_,              "self");
-        (super::STATIC_KEYWORD_NAME_NUM, statik,             "static");
-        (super::SUPER_KEYWORD_NAME_NUM, super_,              "super");
-        (4,                          static_lifetime,        "'static");
-
-        // for matcher NTs
-        (5,                          tt,                     "tt");
-        (6,                          matchers,               "matchers");
-
-        // outside of libsyntax
-        (7,                          clownshoe_abi,          "__rust_abi");
-        (8,                          opaque,                 "<opaque>");
-        (9,                          __unused1,              "<__unused1>");
-        (super::SELF_TYPE_KEYWORD_NAME_NUM, type_self,       "Self");
-        (11,                         prelude_import,         "prelude_import");
-        (12,                         DEFAULT,                "default");
+        // Special identifiers
+        (0,                          Invalid,        "");
+        (1,                          __Unused1,      "<__unused1>");
+        (2,                          __Unused2,      "<__unused2>");
+        (3,                          __Unused3,      "<__unused3>");
+        (4,                          __Unused4,      "<__unused4>");
+        (5,                          __Unused5,      "<__unused5>");
+        (6,                          Union,          "union");
+        (7,                          Default,        "default");
+        (8,                          StaticLifetime, "'static");
     }
 
     pub mod keywords {
-        // These ones are variants of the Keyword enum
-
-        'strict:
+        // Keywords
+        'used:
+        (9,                          Static,     "static");
+        (10,                         Super,      "super");
+        (11,                         SelfValue,  "self");
+        (12,                         SelfType,   "Self");
         (13,                         As,         "as");
         (14,                         Break,      "break");
         (15,                         Crate,      "crate");
@@ -529,12 +483,7 @@ declare_special_idents_and_keywords! {
         (31,                         Pub,        "pub");
         (32,                         Ref,        "ref");
         (33,                         Return,     "return");
-        // Static and Self are also special idents (prefill de-dupes)
-        (super::STATIC_KEYWORD_NAME_NUM, Static, "static");
-        (super::SELF_KEYWORD_NAME_NUM, SelfValue, "self");
-        (super::SELF_TYPE_KEYWORD_NAME_NUM, SelfType, "Self");
         (34,                         Struct,     "struct");
-        (super::SUPER_KEYWORD_NAME_NUM, Super,   "super");
         (35,                         True,       "true");
         (36,                         Trait,      "trait");
         (37,                         Type,       "type");