XTRAN Example — Highlight Expressions in C, C++, Java, or C#

The following example uses an XTRAN rules file comprising 58 non-comment lines of "meta-code" (XTRAN's rules language) to highlight C, C++, Java, or C# expressions, rendering the code as HTML to provide the title, legend, and highlighting.

These rules took less than two hours to create and debug.  (That's right, less than two hours!)

The highlighting rules are not specific to C / C++ / Java / C#; they can be used without modification for any language XTRAN handles that has condition, comparison, and assignment operators.  They use an XTRAN feature called unconditional decoration, which can be set for any statement or expression, to add the appropriate HTML tags to the code at rendering time.  XTRAN also provides conditional decoration and code nesting depth decoration .

The XTRAN rules scan the code and decorate expressions using unconditional decoration; XTRAN's code renderer then adds the decoration as it renders the code as text from the XTRAN Internal Representation (XIR) created by XTRAN's parser.

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

        Specify HTML as module rendering "decoration" prefix and suffix,
          including title and legend
        For each statement
            If statement's first expression is a comparison operator
                Set expression's decoration to highlight the whole expression
            Else if it's a condition operator
                Set expression's decoration to highlight only the operator
            Else if it's an assignment operator
                Set expression's decoration to highlight only the operator
        Render code; XTRAN will decorate expressions as set

How can such powerful code visualization be automated in less than two hours and 58 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 proc(void)
{
	short sh;

	if (sh == 1)
	    sh = 2;
	else if (sh >= 3 && sh < 8)
	    sh += 3;
	return;
}


Output from XTRAN (HTML):


Expression highlighting using XTRAN

Legend:

void proc(void)
{
	short sh;

	if (sh == 1)
	    sh = 2;
	else if (sh >= 3 && sh < 8)
	    sh += 3;
	return;
}