about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2012-09-09 17:35:56 -0700
committerBrian Anderson <banderson@mozilla.com>2012-09-09 17:35:56 -0700
commite7a01b7383df092c9b5fc9367541cd9f90a07c40 (patch)
treec3b1afef2fd960bff295773084a85317acd772e9 /src/libsyntax/parse
parente0c232025c2a175069de3dd30b52b0ac2dbc2f65 (diff)
downloadrust-e7a01b7383df092c9b5fc9367541cd9f90a07c40.tar.gz
rust-e7a01b7383df092c9b5fc9367541cd9f90a07c40.zip
Introduce 'strict' keywords, that may not be used as idents anywhere
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/common.rs21
-rw-r--r--src/libsyntax/parse/parser.rs2
-rw-r--r--src/libsyntax/parse/token.rs23
3 files changed, 42 insertions, 4 deletions
diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs
index 478288ba4cd..c2ed8a7d9d6 100644
--- a/src/libsyntax/parse/common.rs
+++ b/src/libsyntax/parse/common.rs
@@ -84,6 +84,7 @@ impl parser: parser_common {
     }
 
     fn parse_ident() -> ast::ident {
+        self.check_strict_keywords();
         match copy self.token {
           token::IDENT(i, _) => { self.bump(); return i; }
           token::INTERPOLATED(token::nt_ident(*)) => { self.bug(
@@ -183,6 +184,26 @@ impl parser: parser_common {
         }
     }
 
+    fn is_strict_keyword(word: ~str) -> bool {
+        self.strict_keywords.contains_key_ref(&word)
+    }
+
+    fn check_strict_keywords() {
+        match self.token {
+          token::IDENT(_, false) => {
+            let w = token_to_str(self.reader, self.token);
+            self.check_strict_keywords_(w);
+          }
+          _ => ()
+        }
+    }
+
+    fn check_strict_keywords_(w: ~str) {
+        if self.is_strict_keyword(w) {
+            self.fatal(~"found `" + w + ~"` in ident position");
+        }
+    }
+
     fn expect_gt() {
         if self.token == token::GT {
             self.bump();
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 73ff35481d4..138b83f69f0 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -215,6 +215,7 @@ fn parser(sess: parse_sess, cfg: ast::crate_cfg,
         quote_depth: 0u,
         keywords: token::keyword_table(),
         restricted_keywords: token::restricted_keyword_table(),
+        strict_keywords: token::strict_keyword_table(),
         obsolete_set: std::map::hashmap(),
     }
 }
@@ -235,6 +236,7 @@ struct parser {
     interner: interner<@~str>,
     keywords: hashmap<~str, ()>,
     restricted_keywords: hashmap<~str, ()>,
+    strict_keywords: hashmap<~str, ()>,
     /// The set of seen errors about obsolete syntax. Used to suppress
     /// extra detail when the same error is seen twice
     obsolete_set: hashmap<ObsoleteSyntax, ()>,
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index aa2c9ab3a5e..5c719af41cd 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -362,10 +362,11 @@ fn mk_fake_ident_interner() -> ident_interner {
 /**
  * All the valid words that have meaning in the Rust language.
  *
- * Rust keywords are either 'contextual' or 'restricted'. Contextual
- * keywords may be used as identifiers because their appearance in
- * the grammar is unambiguous. Restricted keywords may not appear
- * in positions that might otherwise contain _value identifiers_.
+ * Rust keywords are either 'contextual', 'restricted', or 'strict, Contextual
+ * keywords may be used as identifiers because their appearance in the grammar
+ * is unambiguous. Restricted keywords may not appear in positions that might
+ * otherwise contain _value identifiers_.  Strict keywords may not appear as
+ * identifiers.
  */
 fn keyword_table() -> hashmap<~str, ()> {
     let keywords = str_hash();
@@ -375,6 +376,9 @@ fn keyword_table() -> hashmap<~str, ()> {
     for restricted_keyword_table().each_key |word| {
         keywords.insert(word, ());
     }
+    for strict_keyword_table().each_key |word| {
+        keywords.insert(word, ());
+    }
     keywords
 }
 
@@ -430,6 +434,17 @@ fn restricted_keyword_table() -> hashmap<~str, ()> {
     words
 }
 
+/// Full keywords. May not appear anywhere else.
+fn strict_keyword_table() -> hashmap<~str, ()> {
+    let words = str_hash();
+    let keys = ~[
+    ];
+    for keys.each |word| {
+        words.insert(word, ());
+    }
+    words
+}
+
 impl binop : cmp::Eq {
     pure fn eq(&&other: binop) -> bool {
         (self as uint) == (other as uint)