Skip to content

Commit 9ad096e

Browse files
ribru17amaanq
authored andcommitted
fix(lib): prevent finished_tree assertion failure
**Problem:** When resetting the parser during subtree balancing, an error is thrown: ``` parser.c:2198: ts_parser_parse: Assertion `self->finished_tree.ptr' failed. ``` **Solution:** Reset `canceled_balancing` to false in `ts_parser_reset()`.
1 parent ac8a4ba commit 9ad096e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

cli/src/tests/parser_test.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,31 @@ fn test_parsing_with_timeout_during_balancing() {
10041004
assert!(tree.is_none());
10051005
assert!(in_balancing);
10061006

1007+
// This should not cause an assertion failure.
1008+
parser.reset();
1009+
let tree = parser.parse_with_options(
1010+
&mut |offset, _| {
1011+
if offset >= code.len() {
1012+
&[]
1013+
} else {
1014+
&code.as_bytes()[offset..]
1015+
}
1016+
},
1017+
None,
1018+
Some(ParseOptions::new().progress_callback(&mut |state| {
1019+
if state.current_byte_offset() != current_byte_offset {
1020+
current_byte_offset = state.current_byte_offset();
1021+
false
1022+
} else {
1023+
in_balancing = true;
1024+
true
1025+
}
1026+
})),
1027+
);
1028+
1029+
assert!(tree.is_none());
1030+
assert!(in_balancing);
1031+
10071032
// If we resume parsing (implying we didn't call `parser.reset()`), we should be able to
10081033
// finish parsing the tree, continuing from where we left off.
10091034
let tree = parser

lib/src/parser.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,6 +2080,7 @@ void ts_parser_reset(TSParser *self) {
20802080
self->accept_count = 0;
20812081
self->has_scanner_error = false;
20822082
self->has_error = false;
2083+
self->canceled_balancing = false;
20832084
self->parse_options = (TSParseOptions) {0};
20842085
self->parse_state = (TSParseState) {0};
20852086
}

0 commit comments

Comments
 (0)