Parse x[y] as indexes

This commit is contained in:
Brian Anderson 2011-08-19 12:01:01 -07:00
parent 325ea41a1e
commit 7053b6010a
4 changed files with 26 additions and 19 deletions

View File

@ -1109,6 +1109,13 @@ fn parse_dot_or_call_expr_with(p: &parser, e: @ast::expr) -> @ast::expr {
e = mk_expr(p, lo, hi, ast::expr_call(e, es.node));
}
}
token::LBRACKET. {
p.bump();
let ix = parse_expr(p);
hi = ix.span.hi;
expect(p, token::RBRACKET);
e = mk_expr(p, lo, hi, ast::expr_index(e, ix));
}
token::DOT. {
p.bump();
alt p.peek() {

View File

@ -5,16 +5,16 @@
fn main() {
let v: [int] = ~[0, 1, 2, 3, 4, 5];
let s: str = "abcdef";
assert (v.(3u) == 3);
assert (v.(3u8) == 3);
assert (v.(3i8) == 3);
assert (v.(3u32) == 3);
assert (v.(3i32) == 3);
log v.(3u8);
assert (s.(3u) == 'd' as u8);
assert (s.(3u8) == 'd' as u8);
assert (s.(3i8) == 'd' as u8);
assert (s.(3u32) == 'd' as u8);
assert (s.(3i32) == 'd' as u8);
log s.(3u8);
assert (v[3u] == 3);
assert (v[3u8] == 3);
assert (v[3i8] == 3);
assert (v[3u32] == 3);
assert (v[3i32] == 3);
log v[3u8];
assert (s[3u] == 'd' as u8);
assert (s[3u8] == 'd' as u8);
assert (s[3i8] == 'd' as u8);
assert (s[3u32] == 'd' as u8);
assert (s[3i32] == 'd' as u8);
log s[3u8];
}

View File

@ -2,7 +2,7 @@
fn main() {
let s = "hello";
let c: u8 = s.(4);
let c: u8 = s[4];
log c;
assert (c == 0x6f as u8);
}

View File

@ -4,12 +4,12 @@
// -*- rust -*-
fn main() {
let v: [int] = ~[10, 20];
assert (v.(0) == 10);
assert (v.(1) == 20);
assert (v[0]== 10);
assert (v[1] == 20);
let x: int = 0;
assert (v.(x) == 10);
assert (v.(x + 1) == 20);
assert (v[x] == 10);
assert (v[x + 1] == 20);
x = x + 1;
assert (v.(x) == 20);
assert (v.(x - 1) == 10);
assert (v[x] == 20);
assert (v[x - 1] == 10);
}