From 7053b6010aa35580c051b334920a4bba3ffbec1a Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 19 Aug 2011 12:01:01 -0700 Subject: [PATCH] Parse x[y] as indexes --- src/comp/syntax/parse/parser.rs | 7 +++++++ src/test/run-pass/integral-indexing.rs | 24 ++++++++++++------------ src/test/run-pass/str-idx.rs | 2 +- src/test/run-pass/vec.rs | 12 ++++++------ 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 5361a686814..2d11ae974d3 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -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() { diff --git a/src/test/run-pass/integral-indexing.rs b/src/test/run-pass/integral-indexing.rs index 7b95cfcda67..d03963df2ed 100644 --- a/src/test/run-pass/integral-indexing.rs +++ b/src/test/run-pass/integral-indexing.rs @@ -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]; } diff --git a/src/test/run-pass/str-idx.rs b/src/test/run-pass/str-idx.rs index 40366edeebb..0c619a213fa 100644 --- a/src/test/run-pass/str-idx.rs +++ b/src/test/run-pass/str-idx.rs @@ -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); } \ No newline at end of file diff --git a/src/test/run-pass/vec.rs b/src/test/run-pass/vec.rs index afc452342c8..eef478a372e 100644 --- a/src/test/run-pass/vec.rs +++ b/src/test/run-pass/vec.rs @@ -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); }