From c3d2c61729de40c367f95b10111427f7371f1860 Mon Sep 17 00:00:00 2001 From: Saeed Rasooli Date: Sun, 14 Feb 2021 12:10:28 +0330 Subject: [PATCH] nu-parser: fix parsing comments with unclosed ' " [] {} in functions (#3053) * nu-parser: fix parsing comments with unclosed ' " [] {} in functions * add tests for last commit --- crates/nu-parser/src/lex/lexer.rs | 16 +++++++++++++ crates/nu-parser/src/lex/tests.rs | 40 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) 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";