diff --git a/crates/nu-parser/src/lex.rs b/crates/nu-parser/src/lex.rs index 42f0fa85da..399afb428e 100644 --- a/crates/nu-parser/src/lex.rs +++ b/crates/nu-parser/src/lex.rs @@ -207,6 +207,23 @@ pub fn lex_item( // We encountered a closing `)` delimiter. Pop off the opening `(`. if let Some(BlockKind::Paren) = block_level.last() { let _ = block_level.pop(); + } else { + // We encountered a closing `)` delimiter, but the last opening + // delimiter was not a `(`. This is an error. + let span = Span::new(span_offset + token_start, span_offset + *curr_offset); + + *curr_offset += 1; + return ( + Token { + contents: TokenContents::Item, + span, + }, + Some(ParseError::Unbalanced( + "(".to_string(), + ")".to_string(), + Span::new(span.end, span.end + 1), + )), + ); } } else if is_item_terminator(&block_level, c, additional_whitespace, special_tokens) { break; diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index 6f1224a031..83fc7aa339 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -550,6 +550,16 @@ fn unbalanced_delimiter4() -> TestResult { fail_test(r#"}"#, "unbalanced { and }") } +#[test] +fn unbalanced_parens1() -> TestResult { + fail_test(r#")"#, "unbalanced ( and )") +} + +#[test] +fn unbalanced_parens2() -> TestResult { + fail_test(r#"("("))"#, "unbalanced ( and )") +} + #[test] fn register_with_string_literal() -> TestResult { fail_test(r#"register 'nu-plugin-math'"#, "File not found")