add implicit root node to avoid crash on multiple root nodes

This commit is contained in:
Schrottkatze 2024-10-27 16:57:48 +01:00
parent f7d05ead2c
commit 883b0c804e
No known key found for this signature in database

View file

@ -120,6 +120,9 @@ impl<'src, 'toks, SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError>
let mut errors: Vec<SyntaxErr> = Vec::new(); let mut errors: Vec<SyntaxErr> = Vec::new();
raw_toks.reverse(); raw_toks.reverse();
// always have an implicit root node to avoid [`GreenNodeBuilder::finish()`] panicking due to multiple root elements.
builder.start_node(SyntaxKind::SYNTAX_ROOT.into());
for i in 0..events.len() { for i in 0..events.len() {
match mem::replace(&mut events[i], Event::tombstone()) { match mem::replace(&mut events[i], Event::tombstone()) {
Event::Start { Event::Start {
@ -185,6 +188,9 @@ impl<'src, 'toks, SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError>
} }
} }
// finish SYNTAX_ROOT
builder.finish_node();
ParserOutput { ParserOutput {
green_node: builder.finish(), green_node: builder.finish(),
errors, errors,