Skip to content

Commit c6ee9ec

Browse files
authored
Fix to parse array with comments when no trailing comma (#7994)
* Fix to parse array with comments when no trailing comma * Fix parsing array with block comment and no whitespace
1 parent fbe0166 commit c6ee9ec

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

rust/kcl-lib/src/parsing/parser.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ fn array_separator(i: &mut TokenSlice) -> ModalResult<()> {
787787
// Normally you need a comma.
788788
comma_sep,
789789
// But, if the array is ending, no need for a comma.
790-
peek(preceded(opt(whitespace), close_bracket)).void(),
790+
peek(preceded((opt(non_code_node), opt(whitespace)), close_bracket)).void(),
791791
))
792792
.parse_next(i)
793793
}
@@ -800,7 +800,7 @@ pub(crate) fn array_elem_by_elem(i: &mut TokenSlice) -> ModalResult<Node<ArrayEx
800800
0..,
801801
alt((
802802
terminated(expression.map(NonCodeOr::Code), array_separator),
803-
terminated(non_code_node.map(NonCodeOr::NonCode), whitespace),
803+
terminated(non_code_node.map(NonCodeOr::NonCode), opt(whitespace)),
804804
)),
805805
)
806806
.context(expected("array contents, a list of elements (like [1, 2, 3])"))
@@ -4841,6 +4841,26 @@ export fn cos(num: number(rad)): number(_) {}"#;
48414841
let _arr = array_elem_by_elem(&mut tokens.as_slice()).unwrap();
48424842
}
48434843

4844+
#[test]
4845+
fn array_no_trailing_comma_with_comment() {
4846+
let program = r#"[
4847+
1, // one
4848+
2, // two
4849+
3 // three
4850+
]"#;
4851+
let module_id = ModuleId::default();
4852+
let tokens = crate::parsing::token::lex(program, module_id).unwrap();
4853+
let _arr = array_elem_by_elem(&mut tokens.as_slice()).unwrap();
4854+
}
4855+
4856+
#[test]
4857+
fn array_block_comment_no_whitespace() {
4858+
let program = r#"[1/* comment*/]"#;
4859+
let module_id = ModuleId::default();
4860+
let tokens = crate::parsing::token::lex(program, module_id).unwrap();
4861+
let _arr = array_elem_by_elem(&mut tokens.as_slice()).unwrap();
4862+
}
4863+
48444864
#[test]
48454865
fn basic_if_else() {
48464866
let some_program_string = "if true {

rust/kcl-lib/src/unparser.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,30 @@ fn ghi(part001) {
24512451
);
24522452
}
24532453

2454+
#[test]
2455+
fn test_recast_array_no_trailing_comma_with_comments() {
2456+
let some_program_string = r#"[
2457+
1, // one
2458+
2, // two
2459+
3 // three
2460+
]"#;
2461+
let program = crate::parsing::top_level_parse(some_program_string).unwrap();
2462+
2463+
let recasted = program.recast_top(&Default::default(), 0);
2464+
assert_eq!(
2465+
recasted,
2466+
r#"[
2467+
1,
2468+
// one
2469+
2,
2470+
// two
2471+
3,
2472+
// three
2473+
]
2474+
"#
2475+
);
2476+
}
2477+
24542478
#[test]
24552479
fn test_recast_negative_var() {
24562480
let some_program_string = r#"w = 20

0 commit comments

Comments
 (0)