Is it possible to extend .toString() to include parsed text in output?

Hi folks,
I’m writing syntax highlighter for the existing grammar. The existing grammar is PEG and I cannot directly translate it to Lezer.

Fortunately, I have plenty of tests for the existing parser. The test setup is pretty simple: take a bunch of predefined inputs, parse them and compare results to the expected parse tree. The expected parse tree contains not only node names but also corresponding text from the input. So the whole thing is robust.

This is where I am somewhat stuck. Tree.toString() only outputs node names and no content. So I can test that the shape of a tree is correct, but I cannot verify that nodes themselves contain correct text. I can workaround that by running highlightCode and annotating input with tags. But it also means that I would need to duplicate all tests and I really want to avoid that.

Is it possible to extend .toString() to include parsed text in output? I tried looking into toString() implementation but quickly got lost inside TreeBuffer…

No. The text content is not in the tree at all.