Hixie's Natural Log

2002-10-05 18:18 UTC #include web-log-entry.txt

Last night I wrote a preprocessor. It will be used by the phoenix guys to preprocess their XUL files so that they can do platform-specific stuff without using overlays, and so that they can embed scripts straight into the XUL files for distribution while keeping them separate for development.

I implemented all the obvious stuff, #ifdef, #else, #endif, #define, etc. It also supports #include and #error, and I invented a new instruction #expand to do variable substitution (they didn't want normal lines to be unexpectedly affected).

The code is quite neat, if I do say so myself. I used some of Perl's niftier features, such as using method lookup instead of a switch statement for the preprocessing instruction dispatching, and exceptions instead of passing error codes back and forth, and using the syntactic sugar local instead of explicit temporary variables. The core is implemented using a simple stack approach, and includes are done by recursing the parser into the new file. (To be honest the includes were an afterthought.)

I was really happy about how easy this code was to write — it basically worked first time, and I had no bugs except a couple of typos and one minor oversight (originally preprecessor instructions like #define were still executed even in blocks being skipped... oops!). It will be interesting to see how it is used.