From ce0f054f9d56df4e60291fc2e1b89ce979cf374f Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Wed, 21 Sep 2011 03:00:32 -0400 Subject: Implement pattern ranges for all numeric types. --- src/comp/syntax/ast.rs | 1 + src/comp/syntax/ast_util.rs | 3 ++- src/comp/syntax/fold.rs | 1 + src/comp/syntax/parse/parser.rs | 10 ++++++++-- src/comp/syntax/print/pprust.rs | 6 ++++++ 5 files changed, 18 insertions(+), 3 deletions(-) (limited to 'src/comp/syntax') diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index ad6aacb1549..a409f7c2437 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -92,6 +92,7 @@ tag pat_ { pat_rec([field_pat], bool); pat_tup([@pat]); pat_box(@pat); + pat_range(@lit, @lit); } tag mutability { mut; imm; maybe_mut; } diff --git a/src/comp/syntax/ast_util.rs b/src/comp/syntax/ast_util.rs index 8350ce9a8bb..d94258a6ac7 100644 --- a/src/comp/syntax/ast_util.rs +++ b/src/comp/syntax/ast_util.rs @@ -69,7 +69,7 @@ iter pat_bindings(pat: @pat) -> @pat { for elt in elts { for each b in pat_bindings(elt) { put b; } } } pat_box(sub) { for each b in pat_bindings(sub) { put b; } } - pat_wild. | pat_lit(_) { } + pat_wild. | pat_lit(_) | pat_range(_, _) { } } } @@ -229,3 +229,4 @@ fn ret_by_ref(style: ret_style) -> bool { // buffer-file-coding-system: utf-8-unix // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'"; // End: + diff --git a/src/comp/syntax/fold.rs b/src/comp/syntax/fold.rs index c53176e24fb..68c12153873 100644 --- a/src/comp/syntax/fold.rs +++ b/src/comp/syntax/fold.rs @@ -291,6 +291,7 @@ fn noop_fold_pat(p: pat_, fld: ast_fold) -> pat_ { } pat_tup(elts) { pat_tup(vec::map(fld.fold_pat, elts)) } pat_box(inner) { pat_box(fld.fold_pat(inner)) } + pat_range(_, _) { p } }; } diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 6869dc6501d..efed1380a23 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -1494,8 +1494,14 @@ fn parse_pat(p: parser) -> @ast::pat { tok { if !is_ident(tok) || is_word(p, "true") || is_word(p, "false") { let lit = parse_lit(p); - hi = lit.span.hi; - pat = ast::pat_lit(@lit); + if eat_word(p, "to") { + let end = parse_lit(p); + hi = end.span.hi; + pat = ast::pat_range(@lit, @end); + } else { + hi = lit.span.hi; + pat = ast::pat_lit(@lit); + } } else if is_plain_ident(p) && alt p.look_ahead(1u) { token::DOT. | token::LPAREN. | token::LBRACKET. { diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index 48031a295db..3bf5b0310c9 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -1113,6 +1113,12 @@ fn print_pat(s: ps, pat: @ast::pat) { pclose(s); } ast::pat_box(inner) { word(s.s, "@"); print_pat(s, inner); } + ast::pat_range(begin, end) { + print_literal(s, begin); + space(s.s); + word_space(s, "to"); + print_literal(s, end); + } } s.ann.post(ann_node); } -- cgit 1.4.1-3-g733a5