Implement exclusive and inclusive ranges with ..< and .. (#2541)

* Implement exclusive and inclusive ranges with .. and ..=

This commit adds right-exclusive ranges.

The original a..b inclusive syntax was changed to reflect the Rust notation.
New a..=b syntax was introduced to have the old behavior.

Currently, both a.. and b..= is valid, and it is unclear whether it's valid
to impose restrictions.

The original issue suggests .. for inclusive and ..< for exclusive ranges,
this can be implemented by making simple changes to this commit.

* Fix collect tests by changing ranges to ..=

* Fix clippy lints in exclusive range matching

* Implement exclusive ranges using `..<`
This commit is contained in:
Radek Vít
2020-09-13 23:53:08 +02:00
committed by GitHub
parent c355585112
commit 599bb9797d
8 changed files with 108 additions and 19 deletions

View File

@ -906,7 +906,7 @@ impl ShellTypeName for Synthetic {
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)]
pub struct Range {
pub left: Option<SpannedExpression>,
pub dotdot: Span,
pub operator: Spanned<RangeOperator>,
pub right: Option<SpannedExpression>,
}
@ -919,7 +919,7 @@ impl PrettyDebugWithSource for Range {
} else {
DebugDocBuilder::blank()
}) + b::space()
+ b::keyword(self.dotdot.slice(source))
+ b::keyword(self.operator.span().slice(source))
+ b::space()
+ (if let Some(right) = &self.right {
right.pretty_debug(source)
@ -932,6 +932,12 @@ impl PrettyDebugWithSource for Range {
}
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)]
pub enum RangeOperator {
Inclusive,
RightExclusive,
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)]
pub enum Literal {
Number(Number),
@ -1107,12 +1113,12 @@ impl Expression {
pub fn range(
left: Option<SpannedExpression>,
dotdot: Span,
operator: Spanned<RangeOperator>,
right: Option<SpannedExpression>,
) -> Expression {
Expression::Range(Box::new(Range {
left,
dotdot,
operator,
right,
}))
}
@ -1291,6 +1297,7 @@ pub enum FlatShape {
Operator,
Dot,
DotDot,
DotDotLeftAngleBracket,
InternalCommand,
ExternalCommand,
ExternalWord,

View File

@ -1,6 +1,6 @@
use crate::type_name::ShellTypeName;
use crate::value::column_path::ColumnPath;
use crate::value::range::Range;
use crate::value::range::{Range, RangeInclusion};
use crate::value::{serde_bigdecimal, serde_bigint};
use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
@ -251,8 +251,13 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S
Primitive::Int(i) => i.to_string(),
Primitive::Decimal(decimal) => format!("{:.4}", decimal),
Primitive::Range(range) => format!(
"{}..{}",
"{}..{}{}",
format_primitive(&range.from.0.item, None),
if range.to.1 == RangeInclusion::Exclusive {
"<"
} else {
""
},
format_primitive(&range.to.0.item, None)
),
Primitive::Pattern(s) => s.to_string(),