Introduce 'strict' keywords, that may not be used as idents anywhere

This commit is contained in:
Brian Anderson 2012-09-09 17:35:56 -07:00
parent e0c232025c
commit e7a01b7383
3 changed files with 42 additions and 4 deletions

View file

@ -84,6 +84,7 @@ impl parser: parser_common {
} }
fn parse_ident() -> ast::ident { fn parse_ident() -> ast::ident {
self.check_strict_keywords();
match copy self.token { match copy self.token {
token::IDENT(i, _) => { self.bump(); return i; } token::IDENT(i, _) => { self.bump(); return i; }
token::INTERPOLATED(token::nt_ident(*)) => { self.bug( 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() { fn expect_gt() {
if self.token == token::GT { if self.token == token::GT {
self.bump(); self.bump();

View file

@ -215,6 +215,7 @@ fn parser(sess: parse_sess, cfg: ast::crate_cfg,
quote_depth: 0u, quote_depth: 0u,
keywords: token::keyword_table(), keywords: token::keyword_table(),
restricted_keywords: token::restricted_keyword_table(), restricted_keywords: token::restricted_keyword_table(),
strict_keywords: token::strict_keyword_table(),
obsolete_set: std::map::hashmap(), obsolete_set: std::map::hashmap(),
} }
} }
@ -235,6 +236,7 @@ struct parser {
interner: interner<@~str>, interner: interner<@~str>,
keywords: hashmap<~str, ()>, keywords: hashmap<~str, ()>,
restricted_keywords: hashmap<~str, ()>, restricted_keywords: hashmap<~str, ()>,
strict_keywords: hashmap<~str, ()>,
/// The set of seen errors about obsolete syntax. Used to suppress /// The set of seen errors about obsolete syntax. Used to suppress
/// extra detail when the same error is seen twice /// extra detail when the same error is seen twice
obsolete_set: hashmap<ObsoleteSyntax, ()>, obsolete_set: hashmap<ObsoleteSyntax, ()>,

View file

@ -362,10 +362,11 @@ fn mk_fake_ident_interner() -> ident_interner {
/** /**
* All the valid words that have meaning in the Rust language. * All the valid words that have meaning in the Rust language.
* *
* Rust keywords are either 'contextual' or 'restricted'. Contextual * Rust keywords are either 'contextual', 'restricted', or 'strict, Contextual
* keywords may be used as identifiers because their appearance in * keywords may be used as identifiers because their appearance in the grammar
* the grammar is unambiguous. Restricted keywords may not appear * is unambiguous. Restricted keywords may not appear in positions that might
* in positions that might otherwise contain _value identifiers_. * otherwise contain _value identifiers_. Strict keywords may not appear as
* identifiers.
*/ */
fn keyword_table() -> hashmap<~str, ()> { fn keyword_table() -> hashmap<~str, ()> {
let keywords = str_hash(); let keywords = str_hash();
@ -375,6 +376,9 @@ fn keyword_table() -> hashmap<~str, ()> {
for restricted_keyword_table().each_key |word| { for restricted_keyword_table().each_key |word| {
keywords.insert(word, ()); keywords.insert(word, ());
} }
for strict_keyword_table().each_key |word| {
keywords.insert(word, ());
}
keywords keywords
} }
@ -430,6 +434,17 @@ fn restricted_keyword_table() -> hashmap<~str, ()> {
words 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 { impl binop : cmp::Eq {
pure fn eq(&&other: binop) -> bool { pure fn eq(&&other: binop) -> bool {
(self as uint) == (other as uint) (self as uint) == (other as uint)