Skip to content

Latest commit

 

History

History
26 lines (20 loc) · 1.15 KB

File metadata and controls

26 lines (20 loc) · 1.15 KB

Compiler Utilities: Code Templates

This directory contains support for inserting textual code fragments with substitutions into generated files. The associated make-fragments tool can be used to generate the fragment strings from source files.

The basic operation is

CodeTemplate.expand substitutions text

which expands the string text by replacing placeholders with their expansion as specified in the list of id-value pairs substitutions. Placeholders in text have the syntax @<id>@ and are replaced with the string associated with <id> in the list substitutions. If <id> is empty, then no substitution is applied, instead the "@@" is replaced by "@".

There are two versions of the CodeTemplate module. One uses simple strings as the substitution text, whereas the other takes a function from a character column to a string. The latter can be used to match the indentation of multi-line substitutions to the position of the insertion point.

I often use this mechanism to handle boilerplate code in compilers that generate source code in a language like SML, C, or C++.