Here's some exploratory code I did for work, to generate GLSL shaders dynamically:
https://observablehq.com/@thor/glsl-code-generator
After I learned that GLSL doesn't allow you to chain shaders of the same type together, and copy-pasting shared code from shader to shader seemed like a horrible solution, I decided to make a code generator.
I ended up writing a code generator based on token trees structured as a kind of prefix code.
Take a look. It's a carefully documented live demo.