Compose files with macros

This article is about a program I wrote to compose text files via macros. The macros are based on file names, and they are parsed by a compiled regular expression. I wrote this program because I wanted the capability of a templating engine, but with the simplicity of $ cat.

Macros

Macros are a set of symbols, that (when called) will expand to a certain output. This definition applies very well to fcmpose. In fcmpose, macros expand to the contents of their files. So the macro $foo$ will expand to the content of ./foo. Do note that this expansion is recursive, and infinite recursion will result in a stack overflow.

Don’t let the content of ./foo be Hello, %foo%!.

Regular expressions

To search for our macros I make use of two regular expressions:

Major finds the actual macro, while minor finds the file path inside the macro. To compile these regular expressions I use the std/regexp package, which implements the RE2 engine by Russ Cox.

I could talk about regular expressions all day, but that is not the point of this article. And I will end up writing an article about formal grammers and automata anyway.

Conclusion

I enjoyed writing this program, because it made me understand some foundational concepts even better. And the program ended up the way I wanted: Simple and expressive.

Sources