Skip to content

Commit

Permalink
Add information about the syntax used in ranges
Browse files Browse the repository at this point in the history
... or ..=
  • Loading branch information
Badel2 committed Sep 22, 2017
1 parent 4737c5a commit 7aabf57
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/lowering.rs
Expand Up @@ -1869,7 +1869,7 @@ impl<'a> LoweringContext<'a> {

fn lower_range_end(&mut self, e: &RangeEnd) -> hir::RangeEnd {
match *e {
RangeEnd::Included => hir::RangeEnd::Included,
RangeEnd::Included(_) => hir::RangeEnd::Included,
RangeEnd::Excluded => hir::RangeEnd::Excluded,
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/libsyntax/ast.rs
Expand Up @@ -538,10 +538,16 @@ pub enum BindingMode {

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum RangeEnd {
Included,
Included(RangeSyntax),
Excluded,
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum RangeSyntax {
DotDotDot,
DotDotEq,
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum PatKind {
/// Represents a wildcard pattern (`_`)
Expand Down Expand Up @@ -578,7 +584,7 @@ pub enum PatKind {
Ref(P<Pat>, Mutability),
/// A literal
Lit(P<Expr>),
/// A range pattern, e.g. `1...2` or `1..2`
/// A range pattern, e.g. `1...2`, `1..=2` or `1..2`
Range(P<Expr>, P<Expr>, RangeEnd),
/// `[a, b, ..i, y, z]` is represented as:
/// `PatKind::Slice(box [a, b], Some(i), box [y, z])`
Expand Down
11 changes: 7 additions & 4 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -38,7 +38,7 @@ use ast::{Ty, TyKind, TypeBinding, TyParam, TyParamBounds};
use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple};
use ast::{Visibility, WhereClause};
use ast::{BinOpKind, UnOp};
use ast::RangeEnd;
use ast::{RangeEnd, RangeSyntax};
use {ast, attr};
use codemap::{self, CodeMap, Spanned, respan};
use syntax_pos::{self, Span, BytePos};
Expand Down Expand Up @@ -3557,7 +3557,8 @@ impl<'a> Parser<'a> {
token::DotDotDot | token::DotDotEq | token::DotDot => {
let end_kind = match self.token {
token::DotDot => RangeEnd::Excluded,
token::DotDotDot | token::DotDotEq => RangeEnd::Included,
token::DotDotDot => RangeEnd::Included(RangeSyntax::DotDotDot),
token::DotDotEq => RangeEnd::Included(RangeSyntax::DotDotEq),
_ => panic!("can only parse `..`/`...`/`..=` for ranges \
(checked above)"),
};
Expand Down Expand Up @@ -3600,10 +3601,12 @@ impl<'a> Parser<'a> {
Ok(begin) => {
if self.eat(&token::DotDotDot) {
let end = self.parse_pat_range_end()?;
pat = PatKind::Range(begin, end, RangeEnd::Included);
pat = PatKind::Range(begin, end,
RangeEnd::Included(RangeSyntax::DotDotDot));
} else if self.eat(&token::DotDotEq) {
let end = self.parse_pat_range_end()?;
pat = PatKind::Range(begin, end, RangeEnd::Included);
pat = PatKind::Range(begin, end,
RangeEnd::Included(RangeSyntax::DotDotEq));
} else if self.eat(&token::DotDot) {
let end = self.parse_pat_range_end()?;
pat = PatKind::Range(begin, end, RangeEnd::Excluded);
Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax/print/pprust.rs
Expand Up @@ -11,7 +11,7 @@
pub use self::AnnNode::*;

use abi::{self, Abi};
use ast::{self, BlockCheckMode, PatKind, RangeEnd};
use ast::{self, BlockCheckMode, PatKind, RangeEnd, RangeSyntax};
use ast::{SelfKind, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
use ast::Attribute;
use util::parser::{self, AssocOp, Fixity};
Expand Down Expand Up @@ -2590,7 +2590,8 @@ impl<'a> State<'a> {
self.print_expr(begin)?;
self.s.space()?;
match *end_kind {
RangeEnd::Included => self.s.word("...")?,
RangeEnd::Included(RangeSyntax::DotDotDot) => self.s.word("...")?,
RangeEnd::Included(RangeSyntax::DotDotEq) => self.s.word("..=")?,
RangeEnd::Excluded => self.s.word("..")?,
}
self.print_expr(end)?;
Expand Down

0 comments on commit 7aabf57

Please sign in to comment.