From 883b0c804e160cf06caba98390e6523f17d3f1df Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Sun, 27 Oct 2024 16:57:48 +0100 Subject: [PATCH] add implicit root node to avoid crash on multiple root nodes --- crates/pawarser/src/parser.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/pawarser/src/parser.rs b/crates/pawarser/src/parser.rs index 3782f0f..c592cdb 100644 --- a/crates/pawarser/src/parser.rs +++ b/crates/pawarser/src/parser.rs @@ -120,6 +120,9 @@ impl<'src, 'toks, SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError> let mut errors: Vec = Vec::new(); 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() { match mem::replace(&mut events[i], Event::tombstone()) { Event::Start { @@ -185,6 +188,9 @@ impl<'src, 'toks, SyntaxKind: SyntaxElement, SyntaxErr: SyntaxError> } } + // finish SYNTAX_ROOT + builder.finish_node(); + ParserOutput { green_node: builder.finish(), errors,