Skip to content

Commit e7a01b7

Browse files
committed
Introduce 'strict' keywords, that may not be used as idents anywhere
1 parent e0c2320 commit e7a01b7

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/libsyntax/parse/common.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl parser: parser_common {
8484
}
8585

8686
fn parse_ident() -> ast::ident {
87+
self.check_strict_keywords();
8788
match copy self.token {
8889
token::IDENT(i, _) => { self.bump(); return i; }
8990
token::INTERPOLATED(token::nt_ident(*)) => { self.bug(
@@ -183,6 +184,26 @@ impl parser: parser_common {
183184
}
184185
}
185186

187+
fn is_strict_keyword(word: ~str) -> bool {
188+
self.strict_keywords.contains_key_ref(&word)
189+
}
190+
191+
fn check_strict_keywords() {
192+
match self.token {
193+
token::IDENT(_, false) => {
194+
let w = token_to_str(self.reader, self.token);
195+
self.check_strict_keywords_(w);
196+
}
197+
_ => ()
198+
}
199+
}
200+
201+
fn check_strict_keywords_(w: ~str) {
202+
if self.is_strict_keyword(w) {
203+
self.fatal(~"found `" + w + ~"` in ident position");
204+
}
205+
}
206+
186207
fn expect_gt() {
187208
if self.token == token::GT {
188209
self.bump();

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ fn parser(sess: parse_sess, cfg: ast::crate_cfg,
215215
quote_depth: 0u,
216216
keywords: token::keyword_table(),
217217
restricted_keywords: token::restricted_keyword_table(),
218+
strict_keywords: token::strict_keyword_table(),
218219
obsolete_set: std::map::hashmap(),
219220
}
220221
}
@@ -235,6 +236,7 @@ struct parser {
235236
interner: interner<@~str>,
236237
keywords: hashmap<~str, ()>,
237238
restricted_keywords: hashmap<~str, ()>,
239+
strict_keywords: hashmap<~str, ()>,
238240
/// The set of seen errors about obsolete syntax. Used to suppress
239241
/// extra detail when the same error is seen twice
240242
obsolete_set: hashmap<ObsoleteSyntax, ()>,

src/libsyntax/parse/token.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,11 @@ fn mk_fake_ident_interner() -> ident_interner {
362362
/**
363363
* All the valid words that have meaning in the Rust language.
364364
*
365-
* Rust keywords are either 'contextual' or 'restricted'. Contextual
366-
* keywords may be used as identifiers because their appearance in
367-
* the grammar is unambiguous. Restricted keywords may not appear
368-
* in positions that might otherwise contain _value identifiers_.
365+
* Rust keywords are either 'contextual', 'restricted', or 'strict, Contextual
366+
* keywords may be used as identifiers because their appearance in the grammar
367+
* is unambiguous. Restricted keywords may not appear in positions that might
368+
* otherwise contain _value identifiers_. Strict keywords may not appear as
369+
* identifiers.
369370
*/
370371
fn keyword_table() -> hashmap<~str, ()> {
371372
let keywords = str_hash();
@@ -375,6 +376,9 @@ fn keyword_table() -> hashmap<~str, ()> {
375376
for restricted_keyword_table().each_key |word| {
376377
keywords.insert(word, ());
377378
}
379+
for strict_keyword_table().each_key |word| {
380+
keywords.insert(word, ());
381+
}
378382
keywords
379383
}
380384

@@ -430,6 +434,17 @@ fn restricted_keyword_table() -> hashmap<~str, ()> {
430434
words
431435
}
432436

437+
/// Full keywords. May not appear anywhere else.
438+
fn strict_keyword_table() -> hashmap<~str, ()> {
439+
let words = str_hash();
440+
let keys = ~[
441+
];
442+
for keys.each |word| {
443+
words.insert(word, ());
444+
}
445+
words
446+
}
447+
433448
impl binop : cmp::Eq {
434449
pure fn eq(&&other: binop) -> bool {
435450
(self as uint) == (other as uint)

0 commit comments

Comments
 (0)