parser: More refactoring of restricted value name checking

This commit is contained in:
Brian Anderson 2012-04-27 15:44:40 -07:00
parent 21dc41649b
commit 5eca3c2210
6 changed files with 17 additions and 14 deletions

View File

@ -95,14 +95,18 @@ fn check_restricted_keywords(p: parser) {
alt p.token {
token::IDENT(_, false) {
let w = token_to_str(p.reader, p.token);
if is_restricted_keyword(p, w) {
p.fatal("found `" + w + "` in expression position");
}
check_restricted_keywords_(p, w);
}
_ { }
}
}
fn check_restricted_keywords_(p: parser, w: ast::ident) {
if is_restricted_keyword(p, w) {
p.fatal("found `" + w + "` in restricted position");
}
}
fn expect_gt(p: parser) {
if p.token == token::GT {
p.bump();

View File

@ -1384,7 +1384,11 @@ fn parse_pat(p: parser) -> @ast::pat {
}
let lo1 = p.last_span.lo;
let fieldname = parse_ident(p);
let fieldname = if p.look_ahead(1u) == token::COLON {
parse_ident(p)
} else {
parse_value_ident(p)
};
let hi1 = p.last_span.lo;
let fieldpath = ast_util::ident_to_path(mk_sp(lo1, hi1),
fieldname);
@ -1393,9 +1397,6 @@ fn parse_pat(p: parser) -> @ast::pat {
p.bump();
subpat = parse_pat(p);
} else {
if is_restricted_keyword(p, fieldname) {
p.fatal("found `" + fieldname + "` in binding position");
}
subpat = @{id: p.get_id(),
node: ast::pat_ident(fieldpath, none),
span: mk_sp(lo, hi)};
@ -2147,9 +2148,7 @@ fn parse_item_enum(p: parser, attrs: [ast::attribute]) -> @ast::item {
let mut variants: [ast::variant] = [];
// Newtype syntax
if p.token == token::EQ {
if is_restricted_keyword(p, id) {
p.fatal("found `" + id + "` in enum constructor position");
}
check_restricted_keywords_(p, id);
p.bump();
let ty = parse_ty(p, false);
expect(p, token::SEMI);

View File

@ -1,2 +1,2 @@
fn false() { } //! ERROR found `false` in expression position
fn false() { } //! ERROR found `false` in restricted position
fn main() { }

View File

@ -1,2 +1,2 @@
fn true() { } //! ERROR found `true` in expression position
fn true() { } //! ERROR found `true` in restricted position
fn main() { }

View File

@ -1,4 +1,4 @@
// error-pattern:found `let` in binding position
// error-pattern:found `let` in restricted position
fn main() {
alt true {

View File

@ -1,4 +1,4 @@
// error-pattern:found `let` in enum constructor position
// error-pattern:found `let` in restricted position
fn main() {
enum let = int;