mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
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:
parent
d7b707939f
commit
c3d2c61729
@ -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);
|
||||
|
@ -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";
|
||||
|
Loading…
Reference in New Issue
Block a user