Allow parsing left-unbounded range (..10)

It is implemented as a preliminary check when parsing a call and relies
on a fact that a token that successfully parses as a range is unlikely
to be a valid path or command name.
This commit is contained in:
Jakub Žádník 2021-09-12 15:29:27 +03:00
parent aa7ebdc9ce
commit 78054a5352
2 changed files with 40 additions and 0 deletions

View File

@ -469,6 +469,15 @@ pub fn parse_call(
spans: &[Span],
expand_aliases: bool,
) -> (Expression, Option<ParseError>) {
// We might be parsing left-unbounded range ("..10")
let bytes = working_set.get_span_contents(spans[0]);
if let (Some(b'.'), Some(b'.')) = (bytes.get(0), bytes.get(1)) {
let (range_expr, range_err) = parse_range(working_set, spans[0]);
if range_err.is_none() {
return (range_expr, range_err);
}
}
// assume spans.len() > 0?
let mut pos = 0;
let mut shorthand = vec![];

View File

@ -371,6 +371,37 @@ mod range {
}
}
#[test]
fn parse_left_unbounded_range() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
let (block, err) = parse(&mut working_set, None, b"..10", true);
assert!(err.is_none());
assert!(block.len() == 1);
match &block[0] {
Statement::Pipeline(Pipeline { expressions }) => {
assert!(expressions.len() == 1);
assert!(matches!(
expressions[0],
Expression {
expr: Expr::Range(
None,
Some(_),
RangeOperator {
inclusion: RangeInclusion::Inclusive,
..
}
),
..
}
))
}
_ => panic!("No match"),
}
}
#[test]
fn parse_negative_range() {
let engine_state = EngineState::new();