1
Fork 0

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 { alt p.token {
token::IDENT(_, false) { token::IDENT(_, false) {
let w = token_to_str(p.reader, p.token); let w = token_to_str(p.reader, p.token);
if is_restricted_keyword(p, w) { check_restricted_keywords_(p, w);
p.fatal("found `" + w + "` in expression position");
}
} }
_ { } _ { }
} }
} }
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) { fn expect_gt(p: parser) {
if p.token == token::GT { if p.token == token::GT {
p.bump(); p.bump();

View file

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

View file

@ -1,4 +1,4 @@
// error-pattern:found `let` in binding position // error-pattern:found `let` in restricted position
fn main() { fn main() {
alt true { 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() { fn main() {
enum let = int; enum let = int;