Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiajt committed Sep 5, 2021
1 parent 6f17695 commit 57677a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
args: --all

- uses: actions-rs/cargo@v1
with:
Expand Down
12 changes: 11 additions & 1 deletion crates/nu-cli/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ fn convert_span_to_diag(
}
}

panic!("internal error: can't find span in parser state")
if span.start == working_set.next_span_start() {
// We're trying to highlight the space after the end
if let Some((file_id, (_, _, end))) = working_set.files().enumerate().last() {
return Ok((file_id, *end..(*end + 1)));
}
}

panic!(
"internal error: can't find span in parser state: {:?}",
span
)
}

pub fn report_parsing_error(
Expand Down
39 changes: 9 additions & 30 deletions crates/nu-parser/tests/test_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ mod range {
}

#[test]
fn parse_left_unbounded_range() {
fn parse_right_unbounded_range() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);

let (block, err) = parse_source(&mut working_set, b"..10", true);
let (block, err) = parse_source(&mut working_set, b"0..", true);

assert!(err.is_none());
assert!(block.len() == 1);
Expand All @@ -318,8 +318,8 @@ mod range {
expressions[0],
Expression {
expr: Expr::Range(
None,
Some(_),
None,
RangeOperator {
inclusion: RangeInclusion::Inclusive,
..
Expand All @@ -334,11 +334,11 @@ mod range {
}

#[test]
fn parse_right_unbounded_range() {
fn parse_negative_range() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);

let (block, err) = parse_source(&mut working_set, b"0..", true);
let (block, err) = parse_source(&mut working_set, b"-10..-3", true);

assert!(err.is_none());
assert!(block.len() == 1);
Expand All @@ -350,7 +350,7 @@ mod range {
Expression {
expr: Expr::Range(
Some(_),
None,
Some(_),
RangeOperator {
inclusion: RangeInclusion::Inclusive,
..
Expand All @@ -365,33 +365,12 @@ mod range {
}

#[test]
fn parse_negative_range() {
fn bad_parse_does_crash() {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);

let (block, err) = parse_source(&mut working_set, b"-10..-3", true);
let (_, err) = parse_source(&mut working_set, b"(0)..\"a\"", 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(
Some(_),
Some(_),
RangeOperator {
inclusion: RangeInclusion::Inclusive,
..
}
),
..
}
))
}
_ => panic!("No match"),
}
assert!(err.is_some());
}
}

0 comments on commit 57677a5

Please sign in to comment.