Merge pull request #45 from kubouch/left-unbounded-ranges

Allow parsing left-unbounded range (..10)
This commit is contained in:
JT 2021-09-13 05:01:07 +12:00 committed by GitHub
commit d88e46d2d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View file

@ -629,6 +629,14 @@ pub fn parse_call(
err,
)
} else {
// 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);
}
}
parse_external_call(working_set, spans)
}
}

View file

@ -371,6 +371,38 @@ 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,
None,
Some(_),
RangeOperator {
inclusion: RangeInclusion::Inclusive,
..
}
),
..
}
))
}
_ => panic!("No match"),
}
}
#[test]
fn parse_negative_range() {
let engine_state = EngineState::new();