Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parsing half-open ranges in patterns. #128

Merged
merged 1 commit into from
Apr 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,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 @@ -1141,9 +1141,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 @@ -1393,10 +1393,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 @@ -1602,9 +1599,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 @@ -1630,6 +1627,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 @@ -802,9 +802,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 @@ -632,7 +632,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 @@ -90,8 +90,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