XTRAN Example — Vertically Align DSV Data

Scenario — you've inherited some code containing massive data initialization tables, and they're a mess.  You want to clean them up so they're readable, but it's a huge editing job, because:

XTRAN to the rescue!

The following example uses an XTRAN rules file comprising 177 non-comment lines of "meta-code" (XTRAN's rules language) to vertically align DSV items, with many options.  The rules took 2¾ hours to write and 1¾ hours to debug.  (That's right, only 4½ hours total!)

The rules support the following options, which you can specify via environment variables:

To make it easier to specify regexps, the rules replace all underscores in them with spaces before using them.

In the example below, we extracted the relevant lines from some code (highlighted in red) and used these rules to clean them up, using the following options:

We then used the treated lines to replace the messy table initializations in the code.

We have, in fact, used these rules here at XTRAN, LLC to clean up some very large data initialization tables in XTRAN's own code"Physician, heal thyself?"  XTRAN did!

How can such powerful and generalized text formating be automated in only 4½ hours and 177 lines of XTRAN rules?  Because there is so much capability already available as part of XTRAN's rules language.  These rules take advantage of the following functionality:

The input to and output from XTRAN are untouched, except for highlighting of treated code.



Process Flowchart

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

data flowchart

Input to XTRAN:

int arr[100][4] =
    {
    {ITEM1, ITM2,3,I4}, /*first line*/
    {I5,Item6, 77, ITEM8},
    …
    {ITM9,   10, 11},     /*no 4th value*/
    {IT12,I13,014,ITEM015},
    { 0 }
    };


Output from XTRAN:

int arr[100][4] =
    {
    { ITEM1, ITM2,    3, I4       },  //first line
    { I5,    Item6,  77, ITEM8    },
      …
    { ITM9,  10,     11 },            //no 4th value
    { IT12,  I13,   014, ITEM015  },
    { 0 }
    };

Note that, because all values of the 3rd item are numeric, the rules automatically right-justified that item, left-justifying everything else.