diff --git a/crates/nu-parser/src/lex/lexer.rs b/crates/nu-parser/src/lex/lexer.rs index 02ae5c8e12..2ff8a2c27d 100644 --- a/crates/nu-parser/src/lex/lexer.rs +++ b/crates/nu-parser/src/lex/lexer.rs @@ -68,6 +68,8 @@ pub fn baseline(src: &mut Input, span_offset: usize) -> (Spanned, Option // closing quote. let mut quote_start: Option = None; + let mut in_comment = false; + // This Vec tracks paired delimiters let mut block_level: Vec = vec![]; @@ -98,6 +100,20 @@ pub fn baseline(src: &mut Input, span_offset: usize) -> (Spanned, Option if Some(c) == quote_start { quote_start = None; } + } else if c == '#' { + if is_termination(&block_level, c) { + break; + } + in_comment = true; + } else if c == '\n' { + in_comment = false; + if is_termination(&block_level, c) { + break; + } + } else if in_comment { + if is_termination(&block_level, c) { + break; + } } else if c == '\'' || c == '"' || c == '`' { // We encountered the opening quote of a string literal. quote_start = Some(c); diff --git a/crates/nu-parser/src/lex/tests.rs b/crates/nu-parser/src/lex/tests.rs index edd0f007d6..7492fef18c 100644 --- a/crates/nu-parser/src/lex/tests.rs +++ b/crates/nu-parser/src/lex/tests.rs @@ -131,6 +131,46 @@ def e [] {echo hi} ); } + #[test] + fn def_comment_with_sinqle_quote() { + let input = r#"def f [] { + # shouldn't return error + echo hi + }"#; + let (_result, err) = lex(input, 0); + assert!(err.is_none()); + } + + #[test] + fn def_comment_with_double_quote() { + let input = r#"def f [] { + # should "not return error + echo hi + }"#; + let (_result, err) = lex(input, 0); + assert!(err.is_none()); + } + + #[test] + fn def_comment_with_bracks() { + let input = r#"def f [] { + # should not [return error + echo hi + }"#; + let (_result, err) = lex(input, 0); + assert!(err.is_none()); + } + + #[test] + fn def_comment_with_curly() { + let input = r#"def f [] { + # should not return {error + echo hi + }"#; + let (_result, err) = lex(input, 0); + assert!(err.is_none()); + } + #[test] fn ignore_future() { let input = "foo 'bar";