XTRAN Example — Colorize C, C++, Java, or C# Code by Nesting Depth

The following example uses an XTRAN rules file comprising 78 non-comment lines of XTRAN's rules language ("meta-code") to colorize code based on nesting depth, rendering the code with embedded HTML tags to provide the coloring.  This example uses C code, but the rules are language-independent; they can be use unchanged on any language XTRAN supports.  They took less than three hours to create and debug.  (That's right, less than three hours!)

These rules use an XTRAN feature called code nesting level decoration to add the appropriate HTML tags to the code at rendering time.  XTRAN also provides conditional decoration and unconditional decoration .

The XTRAN rules read, from a text file, information about how to colorize the code based on nesting depth, by specifying, for each depth range, the lower and upper bounds of the range and the color to use for it, in delimiter separated values (DSV) form.  This example used the following input file:

0,0,black
1,1,red
2,2,purple
3,3,blue
4,4,green

The following is an English paraphrase of the XTRAN rules used for this example:

        Read colorizing information from text file, store in XTRAN data base by range
        Specify HTML as module rendering "decoration" prefix and suffix
        Specify HTML tags as statement rendering "decoration" prefix and suffix,
          according to statement's code nesting level, using colorizing
          information in data base
        Render code; XTRAN will decorate it as specified       

Note that the XTRAN rules don't even need to scan the parsed code; they just condition XTRAN's C, C++, Java, or C# renderer to add the decorations (HTML tags) as it renders the code as text from the XTRAN Internal Representation (XIR) created by XTRAN's parser.

How can such powerful and generalized code manipulation be automated in less than 3 hours and 78 lines of meta-code?  Because there is so much capability already available as part of XTRAN's rules language. The rules used for this example take advantage of the following functionality provided by that rules language:

NOTE that the rendered code shown as XTRAN's output below was done with default conditions; XTRAN provides many options for controlling the way it renders code for output.

The input to and output from XTRAN are untouched.



Process Flowchart

Here is a flowchart for this process, in which the elements are color coded:

data flowchart

Input to XTRAN:

void prc(void)
{
        long v1, v2, v3, v4;

v1ex:   v1 = v2;                        /*v1 = v2;*/
        if (v1 == v2)                   /*if (v1 == v2)*/
            {
            v1 = v2;
            for (v3 = 1; v3 < v4; ++v3) /*for (v3 = 1; v3 < v4; ++v3)*/
                {
                v1 = v2;                /*v1 = v2;*/
                if (v1 == v2)           /*if (v1 == v2)*/
                    {
                    v2 = v1;            /*v2 = v1;*/
                    v1 = v2;            /*v1 = v2;*/
                    }
                }
            v2 = v1;
            }
        v1 += v3;                       /*v1 += v3;*/
        v1 *= v4;                       /*v1 *= v4;*/
        return;
}

Output from XTRAN (HTML):


Code colorized by nesting depth

void prc(void)
{
	long v1, v2, v3, v4;

v1ex:
	v1 = v2;			/*v1 = v2;*/
	if (v1 == v2)			/*if (v1 == v2)*/
	    {
	    v1 = v2;
	    for (v3 = 1; v3 < v4; ++v3) /*for (v3 = 1; v3 < v4; ++v3)*/
		{
		v1 = v2;		/*v1 = v2;*/
		if (v1 == v2)		/*if (v1 == v2)*/
		    {
		    v2 = v1;		/*v2 = v1;*/
		    v1 = v2;		/*v1 = v2;*/
		    }
		}
	    v2 = v1;
	    }
	v1 += v3;			/*v1 += v3;*/
	v1 *= v4;			/*v1 *= v4;*/
	return;
}