libsyntax: Accept "1..3" as the preferred form of "1 to 3" in patterns

This commit is contained in:
Patrick Walton 2012-08-13 17:37:08 -07:00
parent 36d8682269
commit 55ee06b8ce
4 changed files with 11 additions and 10 deletions

View file

@ -377,7 +377,8 @@ fn scan_number(c: char, rdr: string_reader) -> token::token {
}
}
let mut is_float = false;
if rdr.curr == '.' && !(is_alpha(nextch(rdr)) || nextch(rdr) == '_') {
if rdr.curr == '.' && !(is_alpha(nextch(rdr)) || nextch(rdr) == '_' ||
nextch(rdr) == '.') {
is_float = true;
bump(rdr);
let dec_part = scan_digits(rdr, 10u);

View file

@ -1797,7 +1797,7 @@ class parser {
|| self.is_keyword(~"false")
{
let val = self.parse_expr_res(RESTRICT_NO_BAR_OP);
if self.eat_keyword(~"to") {
if self.eat_keyword(~"to") || self.eat(token::DOTDOT) {
let end = self.parse_expr_res(RESTRICT_NO_BAR_OP);
pat = pat_range(val, end);
} else {

View file

@ -1512,7 +1512,7 @@ fn print_pat(s: ps, &&pat: @ast::pat) {
ast::pat_range(begin, end) => {
print_expr(s, begin);
space(s.s);
word_space(s, ~"to");
word(s.s, ~"..");
print_expr(s, end);
}
}

View file

@ -1,30 +1,30 @@
fn main() {
match 5u {
1u to 5u => {}
1u..5u => {}
_ => fail ~"should match range",
}
match 5u {
6u to 7u => fail ~"shouldn't match range",
6u..7u => fail ~"shouldn't match range",
_ => {}
}
match check 5u {
1u => fail ~"should match non-first range",
2u to 6u => {}
2u..6u => {}
}
match 'c' {
'a' to 'z' => {}
'a'..'z' => {}
_ => fail ~"should suppport char ranges"
}
match -3 {
-7 to 5 => {}
-7..5 => {}
_ => fail ~"should match signed range"
}
match 3.0 {
1.0 to 5.0 => {}
1.0..5.0 => {}
_ => fail ~"should match float range"
}
match -1.5 {
-3.6 to 3.6 => {}
-3.6..3.6 => {}
_ => fail ~"should match negative float range"
}
}