Many metaprogramming tasks, such as refactorings, automated bug fixing, or large-scale software renovation, require high-fidelity source code transformations-transformations which preserve comments and layout as much as possible. Abstract syntax trees (ASTs) typically abstract from such details, and hence would require pretty printing, destroying the original program layout. Concrete syntax trees (CSTs) preserve all layout information, but transformation systems or parsers that support CSTs are rare and can be cumbersome to use. In this paper we present separator syntax trees (SSTs), a lightweight syntax tree format, that sits between AST and CSTs, in terms of the amount of information they preserve. SSTs extend ASTs by recording textual layout information separating AST nodes. This information can be used to reconstruct the textual code after parsing, but can largely be ignored when implementing high-fidelity transformations. We have implemented SSTs in Rascal, and show how it enables the concise definition of high-fidelity source code transformations using a simple refactoring for C++.

Workshop on Partial Evaluation and Program Manipulation
Centrum Wiskunde & Informatica, Amsterdam (CWI), The Netherlands

Aarssen, R., & van der Storm, T. (2020). High-fidelity metaprogramming with separator syntax trees. In Proceedings of the ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation (pp. 27–37). doi:10.1145/3372884.3373162