Skip to content

Commit

Permalink
Merge pull request #128 from Arnavion/half-open-pat
Browse files Browse the repository at this point in the history
Support parsing half-open ranges in patterns.
  • Loading branch information
dtolnay authored Apr 30, 2017
2 parents f935825 + 1992e2f commit 0e94f0e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
24 changes: 15 additions & 9 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub enum Pat {
/// A literal
Lit(Box<Expr>),
/// A range pattern, e.g. `1...2`
Range(Box<Expr>, Box<Expr>),
Range(Box<Expr>, Box<Expr>, RangeLimits),
/// `[a, b, ..i, y, z]` is represented as:
/// `Pat::Slice(box [a, b], Some(i), box [y, z])`
Slice(Vec<Pat>, Option<Box<Pat>>, Vec<Pat>),
Expand Down Expand Up @@ -1155,9 +1155,9 @@ pub mod parsing {

named!(pat_range -> Pat, do_parse!(
lo: pat_lit_expr >>
punct!("...") >>
limits: range_limits >>
hi: pat_lit_expr >>
(Pat::Range(Box::new(lo), Box::new(hi)))
(Pat::Range(Box::new(lo), Box::new(hi), limits))
));

named!(pat_lit_expr -> Expr, do_parse!(
Expand Down Expand Up @@ -1412,10 +1412,7 @@ mod printing {
}
ExprKind::Range(ref from, ref to, limits) => {
from.to_tokens(tokens);
match limits {
RangeLimits::HalfOpen => tokens.append(".."),
RangeLimits::Closed => tokens.append("..."),
}
limits.to_tokens(tokens);
to.to_tokens(tokens);
}
ExprKind::Path(None, ref path) => path.to_tokens(tokens),
Expand Down Expand Up @@ -1621,9 +1618,9 @@ mod printing {
target.to_tokens(tokens);
}
Pat::Lit(ref lit) => lit.to_tokens(tokens),
Pat::Range(ref lo, ref hi) => {
Pat::Range(ref lo, ref hi, ref limits) => {
lo.to_tokens(tokens);
tokens.append("...");
limits.to_tokens(tokens);
hi.to_tokens(tokens);
}
Pat::Slice(ref before, ref rest, ref after) => {
Expand All @@ -1649,6 +1646,15 @@ mod printing {
}
}

impl ToTokens for RangeLimits {
fn to_tokens(&self, tokens: &mut Tokens) {
match *self {
RangeLimits::HalfOpen => tokens.append(".."),
RangeLimits::Closed => tokens.append("..."),
}
}
}

impl ToTokens for FieldPat {
fn to_tokens(&self, tokens: &mut Tokens) {
if !self.is_shorthand {
Expand Down
5 changes: 3 additions & 2 deletions src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,10 @@ pub fn noop_fold_pat<F: ?Sized + Folder>(folder: &mut F, pat: Pat) -> Pat {
Box(b) => Box(b.lift(|p| folder.fold_pat(p))),
Ref(b, mutability) => Ref(b.lift(|p| folder.fold_pat(p)), mutability),
Lit(expr) => Lit(expr.lift(|e| folder.fold_expr(e))),
Range(l, r) => {
Range(l, r, limits) => {
Range(l.lift(|e| folder.fold_expr(e)),
r.lift(|e| folder.fold_expr(e)))
r.lift(|e| folder.fold_expr(e)),
limits)
}
Slice(lefts, pat, rights) => {
Slice(lefts.lift(|p| folder.fold_pat(p)),
Expand Down
2 changes: 1 addition & 1 deletion src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ pub fn walk_pat<V: Visitor>(visitor: &mut V, pat: &Pat) {
Pat::Lit(ref expr) => {
visitor.visit_expr(expr);
}
Pat::Range(ref start, ref end) => {
Pat::Range(ref start, ref end, _) => {
visitor.visit_expr(start);
visitor.visit_expr(end);
}
Expand Down
2 changes: 0 additions & 2 deletions tests/test_round_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ fn filter(entry: &DirEntry) -> bool {
"tests/rust/src/test/run-pass/issue-38987.rs" |
// TODO better support for attributes
"tests/rust/src/test/run-pass/item-attributes.rs" |
// TODO exclusive range syntax
"tests/rust/src/test/run-pass/match-range.rs" |
// TODO precedence issue with binop vs poly trait ref
"tests/rust/src/test/run-pass/try-macro.rs" |
// TODO 128-bit integer literals
Expand Down

0 comments on commit 0e94f0e

Please sign in to comment.