Perhaps you don't even need to pretty-print? If you're just echoing parts of the input, then have the parser add source range annotations to the AST. (Example: see Section A.2 of https://arxiv.org/abs/2009.01326) Of course, if you are generating code, then this does not work. - J.