Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

A Macro Preprocessor in C#

Rate me:
Please Sign up or sign in to vote.
4.48/5 (14 votes)
6 Apr 20053 min read 108.7K   1.3K   28   10
This library supplies the same macro substitution facilities as the C/C++ preprocessor.

Introduction

The C preprocessor is a useful tool that allows programmers to work around the limitations of C. It was so abused to do clever and un-maintainable things that C++ programmers now avoid it, and in fact, it has been left out of more modern languages like C#. If you really wanted to, you could put a separate preprocessor step in (using a standalone preprocessor like CPP) but you would probably not be thanked by those who have to read the code later.

The class I'll present here does the lexical substitution part of the C preprocessor. You can add macros which behave like C macros and which can do useful things like 'stringizing' and token-pasting. Then calling the Substitute() method will return your string after all macros have been expanded. It is not intended to replace standalone C preprocessors (that has been done repeatedly) but it is sometimes convenient to have the ability to make complex macro substitutions. I wrote this class as part of an interpreter project, and it makes typing in an interactive session much easier. It's also a cool way to explore C macro substitution interactively, and shows how .NET regular expressions can make complex text substitutions easier. The full implementation is only 130 lines of C#, including comments!

Background

System.Text.RegularExpressions.Regexp is a marvelous class and everyone should be familiar with it. It is very useful when making intelligent, possibly context-sensitive replacements in text. The idea is to find the match, and then depending on the matched value, replace the match with some other text. For instance, the formal parameters of a macro are the dummy variables which were used when defining it; the preprocessor will then find the actual parameters and have to substitute these into the macro text.

Here is an example from MacroSubstitutor where we have to replace the formal parameters of a macro with the actual parameters. The code has been slightly simplified (ignoring '#') to show the match/replace loop more clearly.

C#
public class MacroEntry {
        public string   Subst;
        public string[] Parms;
}

...
    static Regex iden = new Regex(@"[a-zA-Z_]\w*");            
    
    public string ReplaceParms(MacroEntry me, string[] actual_parms) {
        Match m;
        int istart = 0;
        string subst = me.Subst;
        while ((m = iden.Match(subst,istart)) != Match.Empty) {
            int idx = Array.IndexOf(me.Parms,m.Value);
            int len = m.Length;
            if (idx != -1) {
                string actual = actual_parms[idx];
                subst = iden.Replace(subst,actual,1,istart);                
                len = actual.Length;
            }
            istart = m.Index + len;
        }
        return subst;
    }

Please note the overloaded forms of Regex.Match and Regex.Replace that are used here. They allow you to specify the starting position where the matching and replacing begin and how many replacements to actually make (by default, it does a global replace, which is not good for context-sensitive replacements). Obviously, it could be faster, but it's fast enough for what I needed it to do, and (most importantly) it's concise and readable code.

The tricky method is MacroSubstitutor.Substitute, because we have to be careful extracting the arguments passed to the macro. At first, this seems easy; grab the string up to ')' and split using ','. But consider FOR(i,f(x,g(y))); it is necessary to carefully count the bracket level and only pull in arguments at level one. Not everything can be done with regular expressions, and sometimes a loop is more obvious.

C#
int idx = 0, isi = i;
while (parenDepth > 0) {
    if (parenDepth == 1 && (str[i] == ',' || str[i] == ')')) {
        actuals[idx] = str.Substring(isi,i - isi);
        idx++;
        isi = i+1;  // past ',' or ')'
    }
    if (str[i] == '(') parenDepth++;  else
    if (str[i] == ')') parenDepth--;
    i++;
}

The interesting point is that the results of substitutions are themselves examined for any new macros to be substituted. We start our next match at the newly substituted text. It works like this:

C#
FOR(i,N)                        // original string
for(int i = 0; i < (N); i++)    // 'FOR' is substituted
for(int i = 0; i < (20); i++)   // 'N' is substituted

Using the code

The class is straightforward to use:

C#
static void Test() {
    MacroSubstitutor ms = new MacroSubstitutor();
    ms.AddMacro("FOR","for(int i = 0; i < (n); i++)",new string[]{"i"});
   ms.AddMacro("N","20",null);
   Console.WriteLine(ms.Substitute("FOR(k,N)"));
}

There's an even easier method, where you let the class handle the macro definitions. These look just like C preprocessor #defines, except that I prefer #def because it's easier to type in a hurry. Here is the complete source for a simple interactive preprocessor.

C#
class MacroTest {
    static string Read() {
        Console.Write(">> ");
        return Console.In.ReadLine();
   }
   
   static void Main() {
       MacroSubstitutor ms = new MacroSubstitutor();
       string line;
       while ((line = Read()) != null)
           Console.WriteLine(ms.ProcessLine(line)); 
    }      
 }

Here is an example of an interactive session:

>> #def N 20
>> #def write Console.WriteLine
>> #def FOR(i,n) for(int i = 0; i < (n); i++)
>> FOR(k,N) write(k);
for(int k = 0; k < (20); k++) Console.WriteLine(k);
>> #def quote(x) #x
>> #def cat(x,y) x##y
>> quote(cat(dog,mouse))
"dogmouse"

Occasionally, you will need to customize the lookup. The example here is where we want to replace DATE and USER with their values at the time of substitution. MacroSubstitutor provides a CustomReplacement method which can be overridden; it will be called if the macro replacement for a symbol is a special value MacroEntry.Custom:

C#
class MyMacroSubstitutor : MacroSubstitutor {
    public override string CustomReplacement(string s) {
        switch(s) {
        case "DATE": return DateTime.Now.ToString();
        case "USER": return Environment.GetEnvironmentVariable("USERNAME");
        }
        return "";
    }
}

static MacroSubstitutor ms = new MyMacroSubstitutor();
...

    ms.AddMacro("DATE",MacroEntry.Custom,null);
    ms.AddMacro("USER",MacroEntry.Custom,null);

Alternatively, we could have used a delegate here, but a good old-fashioned virtual method does the job nicely. When C# 2.0 becomes widely available, it will be a pleasure to be able to write code using anonymous delegate functions:

C#
ms.AddMacro("DATE",delegate(string s) {
    return DateTime.Now.ToString();
});

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
South Africa South Africa
Steve is the author of 'C++ By Example'
(Que 2002) and developer of the UnderC interactive C++ interpreter.

Comments and Discussions

 
GeneralDoes the job Pin
Guirec5-Aug-15 23:40
professionalGuirec5-Aug-15 23:40 
GeneralMy vote of 1 Pin
hardik antala2-Aug-11 22:58
hardik antala2-Aug-11 22:58 
no use
GeneralRe: My vote of 1 Pin
Thomas Levesque8-Mar-13 10:36
professionalThomas Levesque8-Mar-13 10:36 
GeneralProblem in code Pin
interarticle5-Mar-10 17:52
interarticle5-Mar-10 17:52 
GeneralDoesn't Work Pin
Christopher Stratmann14-Nov-07 6:15
Christopher Stratmann14-Nov-07 6:15 
GeneralRe: Doesn't Work Pin
klinkenbecker11-Aug-18 3:49
klinkenbecker11-Aug-18 3:49 
GeneralThanks Pin
Greg Roberts11-Apr-05 20:12
Greg Roberts11-Apr-05 20:12 
GeneralRe: Thanks Pin
Steve Donovan12-Apr-05 21:19
Steve Donovan12-Apr-05 21:19 
GeneralRe: Thanks Pin
necropower22-Feb-06 10:19
necropower22-Feb-06 10:19 
GeneralRe: Thanks Pin
Greg Roberts17-Apr-05 1:04
Greg Roberts17-Apr-05 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.