Skip to content

Commit c9e3f38

Browse files
committed
syntax: Divide keywords into contextual/restricted. No bad words
1 parent 08d0707 commit c9e3f38

File tree

4 files changed

+62
-31
lines changed

4 files changed

+62
-31
lines changed

src/librustsyntax/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: lexer::reader,
132132
reader: rdr,
133133
binop_precs: prec::binop_prec_table(),
134134
keywords: token::keyword_table(),
135-
bad_expr_words: token::bad_expr_word_table()}
135+
restricted_keywords: token::restricted_keyword_table()}
136136
}
137137

138138
fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg,

src/librustsyntax/parse/common.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn parse_path_list_ident(p: parser) -> ast::path_list_ident {
4343
}
4444

4545
fn parse_value_ident(p: parser) -> ast::ident {
46-
check_bad_expr_word(p);
46+
check_restricted_keywords(p);
4747
ret parse_ident(p);
4848
}
4949

@@ -87,15 +87,15 @@ fn expect_keyword(p: parser, word: str) {
8787
}
8888
}
8989

90-
fn is_bad_expr_word(p: parser, word: str) -> bool {
91-
p.bad_expr_words.contains_key(word)
90+
fn is_restricted_keyword(p: parser, word: str) -> bool {
91+
p.restricted_keywords.contains_key(word)
9292
}
9393

94-
fn check_bad_expr_word(p: parser) {
94+
fn check_restricted_keywords(p: parser) {
9595
alt p.token {
9696
token::IDENT(_, false) {
9797
let w = token_to_str(p.reader, p.token);
98-
if is_bad_expr_word(p, w) {
98+
if is_restricted_keyword(p, w) {
9999
p.fatal("found `" + w + "` in expression position");
100100
}
101101
}

src/librustsyntax/parse/parser.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type parser = @{
5959
reader: reader,
6060
binop_precs: @[op_spec],
6161
keywords: hashmap<str, ()>,
62-
bad_expr_words: hashmap<str, ()>
62+
restricted_keywords: hashmap<str, ()>
6363
};
6464

6565
impl parser for parser {
@@ -543,7 +543,7 @@ fn parse_path(p: parser) -> @ast::path {
543543
fn parse_value_path(p: parser) -> @ast::path {
544544
let pt = parse_path(p);
545545
let last_word = vec::last(pt.idents);
546-
if is_bad_expr_word(p, last_word) {
546+
if is_restricted_keyword(p, last_word) {
547547
p.fatal("found " + last_word + " in expression position");
548548
}
549549
pt
@@ -802,7 +802,7 @@ fn parse_bottom_expr(p: parser) -> pexpr {
802802
} else if p.token == token::MOD_SEP ||
803803
is_ident(p.token) && !is_keyword(p, "true") &&
804804
!is_keyword(p, "false") {
805-
check_bad_expr_word(p);
805+
check_restricted_keywords(p);
806806
let pth = parse_path_and_ty_param_substs(p, true);
807807
hi = pth.span.hi;
808808
ex = ast::expr_path(pth);
@@ -1370,7 +1370,7 @@ fn parse_pat(p: parser) -> @ast::pat {
13701370
p.bump();
13711371
subpat = parse_pat(p);
13721372
} else {
1373-
if is_bad_expr_word(p, fieldname) {
1373+
if is_restricted_keyword(p, fieldname) {
13741374
p.fatal("found " + fieldname + " in binding position");
13751375
}
13761376
subpat = @{id: p.get_id(),
@@ -2098,7 +2098,7 @@ fn parse_item_enum(p: parser, attrs: [ast::attribute]) -> @ast::item {
20982098
let mut variants: [ast::variant] = [];
20992099
// Newtype syntax
21002100
if p.token == token::EQ {
2101-
if is_bad_expr_word(p, id) {
2101+
if is_restricted_keyword(p, id) {
21022102
p.fatal("found " + id + " in enum constructor position");
21032103
}
21042104
p.bump();

src/librustsyntax/parse/token.rs

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -201,48 +201,79 @@ fn is_bar(t: token::token) -> bool {
201201
}
202202

203203
#[doc = "
204-
All the valid words that have meaning in the Rust language. Some of these are
205-
nonetheless valid as identifiers becasue they are unambiguous.
204+
All the valid words that have meaning in the Rust language.
205+
206+
Rust keywords are either 'contextual' or 'restricted'. Contextual
207+
keywords may be used as identifiers because their appearance in
208+
the grammar is unambiguous. Restricted keywords may not appear
209+
in positions that might otherwise contain _value identifiers_.
206210
"]
207211
fn keyword_table() -> hashmap<str, ()> {
208212
let keywords = str_hash();
209-
for bad_expr_word_table().each_key {|word|
213+
for contextual_keyword_table().each_key {|word|
214+
keywords.insert(word, ());
215+
}
216+
for restricted_keyword_table().each_key {|word|
210217
keywords.insert(word, ());
211218
}
212-
let other_keywords = [
219+
ret keywords;
220+
}
221+
222+
#[doc = "Keywords that may be used as identifiers"]
223+
fn contextual_keyword_table() -> hashmap<str, ()> {
224+
let words = str_hash();
225+
let keys = [
213226
"as",
214227
"bind",
215228
"else",
216229
"implements",
217230
"move",
218231
"of",
219232
"priv",
220-
"self",
221-
"send",
222-
"static",
233+
"self", "send", "static",
223234
"to",
224235
"use",
225236
"with"
226237
];
227-
for other_keywords.each {|word|
228-
keywords.insert(word, ());
238+
for keys.each {|word|
239+
words.insert(word, ());
229240
}
230-
ret keywords;
241+
words
231242
}
232243

233244
#[doc = "
234-
These are the words that shouldn't be allowed as value identifiers,
235-
because, if used at the start of a line, they will cause the line to be
236-
interpreted as a specific kind of statement, which would be confusing.
245+
Keywords that may not appear in any position that might otherwise contain a
246+
_value identifier_. Restricted keywords may still be used as other types of
247+
identifiers.
248+
249+
Reasons:
250+
251+
* For some (most?), if used at the start of a line, they will cause the line
252+
to be interpreted as a specific kind of statement, which would be confusing.
253+
254+
* `true` or `false` as identifiers would always be shadowed by
255+
the boolean constants
237256
"]
238-
fn bad_expr_word_table() -> hashmap<str, ()> {
257+
fn restricted_keyword_table() -> hashmap<str, ()> {
239258
let words = str_hash();
240-
let keys = ["alt", "assert", "be", "break", "check", "claim",
241-
"class", "const", "cont", "copy", "crust", "do", "else",
242-
"enum", "export", "fail", "false", "fn", "for", "if",
243-
"iface", "impl", "import", "let", "log", "loop", "mod",
244-
"mut", "native", "new", "pure", "resource", "true",
245-
"ret", "trait", "type", "unchecked", "unsafe", "while"];
259+
let keys = [
260+
"alt",
261+
"assert",
262+
"be", "break",
263+
"check", "claim", "class", "const", "cont", "copy", "crust",
264+
"do",
265+
"else", "enum", "export",
266+
"fail", "false", "fn", "for",
267+
"if", "iface", "impl", "import",
268+
"let", "log", "loop",
269+
"mod", "mut",
270+
"native", "new",
271+
"pure",
272+
"resource", "ret",
273+
"true", "trait", "type",
274+
"unchecked", "unsafe",
275+
"while"
276+
];
246277
for keys.each {|word|
247278
words.insert(word, ());
248279
}

0 commit comments

Comments
 (0)