2 ms·
This AST-based programmatic source code transformations is a very useful approach to have in your software engineering toolbox. The authors correctly identify
by neilv 5y ago
This AST-based programmatic source code transformations is a very useful approach to have in your software engineering toolbox.
The authors correctly identify in section "Limitations" that you want to `unparse` only minimal changes, and avoid losing comments and formatting.
I wrote a library to support doing similar things, with this in mind:
https://www.neilvandyke.org/racket/progedit/ https://www.neilvandyke.org/racket/progedit/
Not clear from the documentation, but, although this particular library runs in Racket, it uses Racket's "syntax objects", so it can process not only Racket source code, but pretty much any language with a parser implemented in Racket. Also not clear from the documentation is that (although the example doesn't use it), syntax objects can also represent comments, and exact positional information, and can be annotated with things like type information and identifier resolution, plus there's always pretty printers... so you can in theory do very nice programmatic source code manipulations.
I originally wrote `progedit` in 2012, for updating a package metadata file that's updated both manually and automatically. (And then made a point to factor out this functionality into a separate generalized open source library, and release it, as good reuse practice.) I later got a nice email from someone who said they'd used `progedit` to refactor millions of lines of code, which they thought would've been nonviable using regexps.