nu-parser: fix parsing comments with unclosed ' " [] {} in functions (#3053)

* nu-parser: fix parsing comments with unclosed ' " [] {} in functions

* add tests for last commit
This commit is contained in:
Saeed Rasooli 2021-02-14 12:10:28 +03:30 committed by GitHub
parent d7b707939f
commit c3d2c61729
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

View File

@ -68,6 +68,8 @@ pub fn baseline(src: &mut Input, span_offset: usize) -> (Spanned<String>, Option
// closing quote.
let mut quote_start: Option<char> = None;
let mut in_comment = false;
// This Vec tracks paired delimiters
let mut block_level: Vec<BlockKind> = vec![];
@ -98,6 +100,20 @@ pub fn baseline(src: &mut Input, span_offset: usize) -> (Spanned<String>, 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);

View File

@ -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";