summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2016-04-18 22:53:50 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2016-04-24 20:59:44 +0300
commitb32d7b592306a2784585e842193db1546a2f9587 (patch)
tree4d12769084adfda5aad64a7cb699155f9ee751cc /src/libsyntax
parent923001ebb7980ec425e6df561575bccaf5f90240 (diff)
downloadrust-b32d7b592306a2784585e842193db1546a2f9587.tar.gz
rust-b32d7b592306a2784585e842193db1546a2f9587.zip
syntax: Merge keywords and remaining special idents in one list
Simplify the macro used for generation of keywords
Make `Keyword::ident` private
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ext/build.rs8
-rw-r--r--src/libsyntax/ext/expand.rs12
-rw-r--r--src/libsyntax/fold.rs4
-rw-r--r--src/libsyntax/parse/token.rs226
-rw-r--r--src/libsyntax/print/pprust.rs7
-rw-r--r--src/libsyntax/std_inject.rs4
-rw-r--r--src/libsyntax/test.rs12
7 files changed, 111 insertions, 162 deletions
diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs
index c234ea3afeb..b589eacb46d 100644
--- a/src/libsyntax/ext/build.rs
+++ b/src/libsyntax/ext/build.rs
@@ -13,9 +13,7 @@ use ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp, PatKind};
 use attr;
 use codemap::{Span, respan, Spanned, DUMMY_SP, Pos};
 use ext::base::ExtCtxt;
-use parse::token::{keywords, special_idents};
-use parse::token::InternedString;
-use parse::token;
+use parse::token::{self, keywords, InternedString};
 use ptr::P;
 
 // Transitional reexports so qquote can find the paths it is looking for
@@ -602,7 +600,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
         self.expr_path(self.path_ident(span, id))
     }
     fn expr_self(&self, span: Span) -> P<ast::Expr> {
-        self.expr_ident(span, keywords::SelfValue.ident)
+        self.expr_ident(span, keywords::SelfValue.ident())
     }
 
     fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
@@ -1132,7 +1130,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
                 vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item> {
         P(ast::Item {
             id: ast::DUMMY_NODE_ID,
-            ident: special_idents::Invalid,
+            ident: keywords::Invalid.ident(),
             attrs: vec![],
             node: ast::ItemKind::Use(vp),
             vis: vis,
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs
index 3fe4913b5bb..dd6e9a1e4a6 100644
--- a/src/libsyntax/ext/expand.rs
+++ b/src/libsyntax/ext/expand.rs
@@ -25,7 +25,7 @@ use fold;
 use fold::*;
 use util::move_map::MoveMap;
 use parse;
-use parse::token::{fresh_mark, fresh_name, intern};
+use parse::token::{fresh_mark, fresh_name, intern, keywords};
 use ptr::P;
 use util::small_vector::SmallVector;
 use visit;
@@ -380,7 +380,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
 
             Some(rc) => match *rc {
                 NormalTT(ref expander, tt_span, allow_internal_unstable) => {
-                    if ident.name != parse::token::special_idents::Invalid.name {
+                    if ident.name != keywords::Invalid.name() {
                         fld.cx
                             .span_err(path_span,
                                       &format!("macro {}! expects no ident argument, given '{}'",
@@ -401,7 +401,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
                     expander.expand(fld.cx, span, &marked_before[..])
                 }
                 IdentTT(ref expander, tt_span, allow_internal_unstable) => {
-                    if ident.name == parse::token::special_idents::Invalid.name {
+                    if ident.name == keywords::Invalid.name() {
                         fld.cx.span_err(path_span,
                                         &format!("macro {}! expects an ident argument",
                                                 extname));
@@ -420,7 +420,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
                     expander.expand(fld.cx, span, ident, marked_tts)
                 }
                 MacroRulesTT => {
-                    if ident.name == parse::token::special_idents::Invalid.name {
+                    if ident.name == keywords::Invalid.name() {
                         fld.cx.span_err(path_span, "macro_rules! expects an ident argument");
                         return SmallVector::zero();
                     }
@@ -893,7 +893,7 @@ fn expand_annotatable(a: Annotatable,
             }
             ast::ItemKind::Mod(_) | ast::ItemKind::ForeignMod(_) => {
                 let valid_ident =
-                    it.ident.name != parse::token::special_idents::Invalid.name;
+                    it.ident.name != keywords::Invalid.name();
 
                 if valid_ident {
                     fld.cx.mod_push(it.ident);
@@ -1807,7 +1807,7 @@ mod tests {
 
     // run one of the renaming tests
     fn run_renaming_test(t: &RenamingTest, test_idx: usize) {
-        let invalid_name = token::special_idents::Invalid.name;
+        let invalid_name = keywords::Invalid.name();
         let (teststr, bound_connections, bound_ident_check) = match *t {
             (ref str,ref conns, bic) => (str.to_string(), conns.clone(), bic)
         };
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index ad95b96363e..8bf7d839467 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -22,7 +22,7 @@ use ast::*;
 use ast;
 use attr::{ThinAttributes, ThinAttributesExt};
 use codemap::{respan, Span, Spanned};
-use parse::token;
+use parse::token::{self, keywords};
 use ptr::P;
 use util::small_vector::SmallVector;
 use util::move_map::MoveMap;
@@ -1015,7 +1015,7 @@ pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, config, mut exported_mac
     let config = folder.fold_meta_items(config);
 
     let mut items = folder.fold_item(P(ast::Item {
-        ident: token::special_idents::Invalid,
+        ident: keywords::Invalid.ident(),
         attrs: attrs,
         id: ast::DUMMY_NODE_ID,
         vis: ast::Visibility::Public,
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index 449a2268740..32078c875be 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -141,7 +141,7 @@ pub enum Token {
     /// Doc comment
     DocComment(ast::Name),
     // In left-hand-sides of MBE macros:
-    /// Parse a nonterminal (name to bind, name of NT, styles of their idents)
+    /// Parse a nonterminal (name to bind, name of NT)
     MatchNt(ast::Ident, ast::Ident),
     // In right-hand-sides of MBE macros:
     /// A syntactic variable that will be filled in by macro expansion.
@@ -271,34 +271,30 @@ 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.ident.name,
+            Ident(id) => id.name == kw.name(),
             _ => false,
         }
     }
 
     pub fn is_path_segment_keyword(&self) -> bool {
         match *self {
-            Ident(id) => id.name == keywords::Super.ident.name ||
-                         id.name == keywords::SelfValue.ident.name ||
-                         id.name == keywords::SelfType.ident.name,
+            Ident(id) => id.name == keywords::Super.name() ||
+                         id.name == keywords::SelfValue.name() ||
+                         id.name == keywords::SelfType.name(),
             _ => false,
         }
     }
 
-    /// Returns `true` if the token is either a used or reserved keyword.
+    /// Returns `true` if the token is either a strict or reserved keyword.
     pub fn is_any_keyword(&self) -> bool {
-        match *self {
-            Ident(id) => id.name >= USED_KEYWORD_START &&
-                         id.name <= RESERVED_KEYWORD_FINAL,
-            _ => false
-        }
+        self.is_strict_keyword() || self.is_reserved_keyword()
     }
 
-    /// Returns `true` if the token is a used keyword.
-    pub fn is_used_keyword(&self) -> bool {
+    /// Returns `true` if the token is a strict keyword.
+    pub fn is_strict_keyword(&self) -> bool {
         match *self {
-            Ident(id) => id.name >= USED_KEYWORD_START &&
-                         id.name <= USED_KEYWORD_FINAL,
+            Ident(id) => id.name >= keywords::As.name() &&
+                         id.name <= keywords::While.name(),
             _ => false,
         }
     }
@@ -306,8 +302,8 @@ impl Token {
     /// 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 &&
-                         id.name <= RESERVED_KEYWORD_FINAL,
+            Ident(id) => id.name >= keywords::Abstract.name() &&
+                         id.name <= keywords::Yield.name(),
             _ => false,
         }
     }
@@ -370,148 +366,104 @@ impl fmt::Debug for Nonterminal {
     }
 }
 
-// Get the first "argument"
-macro_rules! first {
-    ( $first:expr, $( $remainder:expr, )* ) => ( $first )
-}
-
-// Get the last "argument" (has to be done recursively to avoid phoney local ambiguity error)
-macro_rules! last {
-    ( $first:expr, $( $remainder:expr, )+ ) => ( last!( $( $remainder, )+ ) );
-    ( $first:expr, ) => ( $first )
-}
-
 // 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.
-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_index: expr, $si_const: ident, $si_str: expr); )*
-    }
-
-    pub mod keywords {
-        'used:
-        $( ($ukw_index: expr, $ukw_const: ident, $ukw_str: expr); )*
-        'reserved:
-        $( ($rkw_index: expr, $rkw_const: ident, $rkw_str: expr); )*
-    }
+macro_rules! declare_keywords {(
+    $( ($index: expr, $konst: ident, $string: expr) )*
 ) => {
-    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_const: ast::Ident = ast::Ident::with_empty_ctxt(ast::Name($si_index));
-        )*
-    }
-
-    /// Rust keywords are either 'used' in the language or 'reserved' for future use.
     pub mod keywords {
         use ast;
         #[derive(Clone, Copy, PartialEq, Eq)]
         pub struct Keyword {
-            pub ident: ast::Ident,
+            ident: ast::Ident,
+        }
+        impl Keyword {
+            #[inline] pub fn ident(self) -> ast::Ident { self.ident }
+            #[inline] pub fn name(self) -> ast::Name { self.ident.name }
         }
         $(
             #[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))
+            pub const $konst: Keyword = Keyword {
+                ident: ast::Ident::with_empty_ctxt(ast::Name($index))
             };
         )*
     }
 
     fn mk_fresh_ident_interner() -> IdentInterner {
-        interner::StrInterner::prefill(&[$($si_str,)* $($ukw_str,)* $($rkw_str,)*])
+        interner::StrInterner::prefill(&[$($string,)*])
     }
 }}
 
 // 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 {
-        // 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 {
-        // Keywords
-        'used:
-        (9,                          Static,     "static");
-        (10,                         Super,      "super");
-        (11,                         SelfValue,  "self");
-        (12,                         SelfType,   "Self");
-        (13,                         As,         "as");
-        (14,                         Break,      "break");
-        (15,                         Crate,      "crate");
-        (16,                         Else,       "else");
-        (17,                         Enum,       "enum");
-        (18,                         Extern,     "extern");
-        (19,                         False,      "false");
-        (20,                         Fn,         "fn");
-        (21,                         For,        "for");
-        (22,                         If,         "if");
-        (23,                         Impl,       "impl");
-        (24,                         In,         "in");
-        (25,                         Let,        "let");
-        (26,                         Loop,       "loop");
-        (27,                         Match,      "match");
-        (28,                         Mod,        "mod");
-        (29,                         Move,       "move");
-        (30,                         Mut,        "mut");
-        (31,                         Pub,        "pub");
-        (32,                         Ref,        "ref");
-        (33,                         Return,     "return");
-        (34,                         Struct,     "struct");
-        (35,                         True,       "true");
-        (36,                         Trait,      "trait");
-        (37,                         Type,       "type");
-        (38,                         Unsafe,     "unsafe");
-        (39,                         Use,        "use");
-        (40,                         While,      "while");
-        (41,                         Continue,   "continue");
-        (42,                         Box,        "box");
-        (43,                         Const,      "const");
-        (44,                         Where,      "where");
-        'reserved:
-        (45,                         Virtual,    "virtual");
-        (46,                         Proc,       "proc");
-        (47,                         Alignof,    "alignof");
-        (48,                         Become,     "become");
-        (49,                         Offsetof,   "offsetof");
-        (50,                         Priv,       "priv");
-        (51,                         Pure,       "pure");
-        (52,                         Sizeof,     "sizeof");
-        (53,                         Typeof,     "typeof");
-        (54,                         Unsized,    "unsized");
-        (55,                         Yield,      "yield");
-        (56,                         Do,         "do");
-        (57,                         Abstract,   "abstract");
-        (58,                         Final,      "final");
-        (59,                         Override,   "override");
-        (60,                         Macro,      "macro");
-    }
+// After modifying this list adjust `is_strict_keyword`/`is_reserved_keyword`,
+// this should be rarely necessary though if the keywords are kept in alphabetic order.
+declare_keywords! {
+    // Invalid identifier
+    (0,  Invalid,        "")
+
+    // Strict keywords used in the language.
+    (1,  As,             "as")
+    (2,  Box,            "box")
+    (3,  Break,          "break")
+    (4,  Const,          "const")
+    (5,  Continue,       "continue")
+    (6,  Crate,          "crate")
+    (7,  Else,           "else")
+    (8,  Enum,           "enum")
+    (9,  Extern,         "extern")
+    (10, False,          "false")
+    (11, Fn,             "fn")
+    (12, For,            "for")
+    (13, If,             "if")
+    (14, Impl,           "impl")
+    (15, In,             "in")
+    (16, Let,            "let")
+    (17, Loop,           "loop")
+    (18, Match,          "match")
+    (19, Mod,            "mod")
+    (20, Move,           "move")
+    (21, Mut,            "mut")
+    (22, Pub,            "pub")
+    (23, Ref,            "ref")
+    (24, Return,         "return")
+    (25, SelfValue,      "self")
+    (26, SelfType,       "Self")
+    (27, Static,         "static")
+    (28, Struct,         "struct")
+    (29, Super,          "super")
+    (30, Trait,          "trait")
+    (31, True,           "true")
+    (32, Type,           "type")
+    (33, Unsafe,         "unsafe")
+    (34, Use,            "use")
+    (35, Where,          "where")
+    (36, While,          "while")
+
+    // Keywords reserved for future use.
+    (37, Abstract,       "abstract")
+    (38, Alignof,        "alignof")
+    (39, Become,         "become")
+    (40, Do,             "do")
+    (41, Final,          "final")
+    (42, Macro,          "macro")
+    (43, Offsetof,       "offsetof")
+    (44, Override,       "override")
+    (45, Priv,           "priv")
+    (46, Proc,           "proc")
+    (47, Pure,           "pure")
+    (48, Sizeof,         "sizeof")
+    (49, Typeof,         "typeof")
+    (50, Unsized,        "unsized")
+    (51, Virtual,        "virtual")
+    (52, Yield,          "yield")
+
+    // Weak keywords, have special meaning only in specific contexts.
+    (53, Default,        "default")
+    (54, StaticLifetime, "'static")
+    (55, Union,          "union")
 }
 
 // looks like we can get rid of this completely...
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index d5318e32aa8..798477d8fe5 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -20,7 +20,7 @@ use attr;
 use attr::{AttrMetaMethods, AttributeMethods};
 use codemap::{self, CodeMap, BytePos};
 use errors;
-use parse::token::{self, BinOpToken, Token, InternedString};
+use parse::token::{self, keywords, BinOpToken, Token, InternedString};
 use parse::lexer::comments;
 use parse;
 use print::pp::{self, break_offset, word, space, zerobreak, hardbreak};
@@ -2957,9 +2957,8 @@ impl<'a> State<'a> {
             ast::TyKind::Infer if is_closure => self.print_pat(&input.pat)?,
             _ => {
                 match input.pat.node {
-                    PatKind::Ident(_, ref path1, _) if
-                        path1.node.name ==
-                            parse::token::special_idents::Invalid.name => {
+                    PatKind::Ident(_, ref path1, _)
+                            if path1.node.name == keywords::Invalid.name() => {
                         // Do nothing.
                     }
                     _ => {
diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax/std_inject.rs
index 1d640d74353..84a7b144848 100644
--- a/src/libsyntax/std_inject.rs
+++ b/src/libsyntax/std_inject.rs
@@ -14,7 +14,7 @@ use codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
 use codemap;
 use fold::Folder;
 use fold;
-use parse::token::{intern, InternedString, special_idents};
+use parse::token::{intern, InternedString, keywords};
 use parse::{token, ParseSess};
 use ptr::P;
 use util::small_vector::SmallVector;
@@ -148,7 +148,7 @@ impl fold::Folder for PreludeInjector {
         let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path)));
         mod_.items.insert(0, P(ast::Item {
             id: ast::DUMMY_NODE_ID,
-            ident: special_idents::Invalid,
+            ident: keywords::Invalid.ident(),
             node: ast::ItemKind::Use(vp),
             attrs: vec![ast::Attribute {
                 span: self.span,
diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs
index f464eb9bf97..8eeb61e0de4 100644
--- a/src/libsyntax/test.rs
+++ b/src/libsyntax/test.rs
@@ -31,7 +31,7 @@ use ext::expand::ExpansionConfig;
 use fold::Folder;
 use util::move_map::MoveMap;
 use fold;
-use parse::token::{intern, InternedString};
+use parse::token::{intern, keywords, InternedString};
 use parse::{token, ParseSess};
 use print::pprust;
 use ast;
@@ -116,7 +116,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
 
     fn fold_item(&mut self, i: P<ast::Item>) -> SmallVector<P<ast::Item>> {
         let ident = i.ident;
-        if ident.name != token::special_idents::Invalid.name {
+        if ident.name != keywords::Invalid.name() {
             self.cx.path.push(ident);
         }
         debug!("current path: {}", path_name_i(&self.cx.path));
@@ -160,7 +160,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
             ast::ItemKind::Mod(..) => fold::noop_fold_item(i, self),
             _ => SmallVector::one(i),
         };
-        if ident.name != token::special_idents::Invalid.name {
+        if ident.name != keywords::Invalid.name() {
             self.cx.path.pop();
         }
         res
@@ -453,7 +453,7 @@ fn mk_std(cx: &TestCtxt) -> P<ast::Item> {
         (ast::ItemKind::Use(
             P(nospan(ast::ViewPathSimple(id_test,
                                          path_node(vec!(id_test)))))),
-         ast::Visibility::Public, token::special_idents::Invalid)
+         ast::Visibility::Public, keywords::Invalid.ident())
     } else {
         (ast::ItemKind::ExternCrate(None), ast::Visibility::Inherited, id_test)
     };
@@ -545,7 +545,7 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
 
         P(ast::Item {
             id: ast::DUMMY_NODE_ID,
-            ident: token::special_idents::Invalid,
+            ident: keywords::Invalid.ident(),
             attrs: vec![],
             node: ast::ItemKind::Use(P(use_path)),
             vis: ast::Visibility::Inherited,
@@ -590,7 +590,7 @@ fn mk_tests(cx: &TestCtxt) -> P<ast::Item> {
     let struct_type = ecx.ty_path(ecx.path(sp, vec![ecx.ident_of("self"),
                                                     ecx.ident_of("test"),
                                                     ecx.ident_of("TestDescAndFn")]));
-    let static_lt = ecx.lifetime(sp, token::special_idents::StaticLifetime.name);
+    let static_lt = ecx.lifetime(sp, keywords::StaticLifetime.name());
     // &'static [self::test::TestDescAndFn]
     let static_type = ecx.ty_rptr(sp,
                                   ecx.ty(sp, ast::TyKind::Vec(struct_type)),