XTRAN Example — Link Parent Statements with Ending Child Statements, Using HTML Tags
We characterize two main types of computer languages that have statement blocks under parent statements:
- "End statement" languages, in which a statement block is
ended by a statement designed for the purpose. Some such languages
dedicate a statement type to end each parent statement type; examples include
COBOL's
END-PERFORM
, C's preprocessor#endif
and BASH'sfi
. Others use a more general ending statement such asEND
. - "Compound statement" languages, in which syntactically a
parent statement takes a "single statement" as its child statement,
but it can be a compound statement started and ended with statements
designed for the purpose. Examples are C's
{}
and Pascal'sBEGIN
/END
.
In "compound statement" computer languages that use special
characters to delimit statement blocks, such as C-style
languages with their {}
, most code editors provide a command
that will move you from the start of such a block to its end or
vice versa, accounting for blocks nested within it. But some code editors
don't have that capability. And for other computer languages, such a
capability is not available in any editor.
XTRAN to the rescue!
The following example uses an XTRAN rules file comprising 44 non-comment lines of XTRAN's rules language ("meta-code") to link each parent statement with its last child statement and vice versa, using HTML tags.
This example uses C / C++ / Java / C#, but the rules are language-independent; they can be used, untouched, for any language that has parent and child statements.
The rules took about one hour to create and debug. (That's right, just one hour!) They use an XTRAN feature called unconditional decoration to add the HTML links. XTRAN also provides conditional decoration and code nesting depth decoration .
The rules do both data declarations (e.g., structures) and executable statements.
The rules don't bother to create links if the parent statement's block contains only one statement (counting more deeply nested statements as well).
The example below is small, for the sake of brevity, so you can easily see the end of a parent statement's block. However, in real-world code, such blocks are often long and complexly nested, so the ability to jump back and forth is very useful.
The following is an English paraphrase of the XTRAN rules used for this example:
Specify HTML as module rendering "decoration" prefix and suffix For each statement If it is a parent statement with children Add HTML tags as unconditional "decorations" to link this statement with its last child statement, and vice versa Render code, including added HTML tag "decorations"
How can such powerful and generalized code visualization be automated in one hour and 44 lines of rules? 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:
- Text manipulation
- Text formatting
- "Per statement" recursive iterators
- Access to XTRAN's Internal Representation (XIR)
- XIR's language-independent properties
- Navigation in XIR
- Module decoration
- Unconditional statement and/or expression decoration
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:
- BLUE for XTRAN versions (runnable programs)
- ORANGE for XTRAN rules (text files)
- RED for
code
Input to XTRAN:
void prc(void) { short i, j; typedef struct str1 { short k, l; long m, n; struct str1a { short o, p; long q, r; }; short s; } Str1; i = 0; if (i != 0 && i != 1) { i = 1; if (i <= 1) i = 2; i = 3; } i = 4; i = 5; for (i = 0; i < 3; ++i) { i = 1; j = i + 1; j = i + 2; i += 3; if (i < 10) { i = 5; j = 3; } j = i + 3; } return; }
Output from XTRAN (rendered HTML):
Code with parent statements and their block end statements linked
void prc(void) { short i, j; typedef struct str1 { short k, l; long m, n; struct str1a { short o, p; long q, r; }; short s; } Str1; i = 0; if (i != 0 && i != 1) { i = 1; if (i <= 1) i = 2; i = 3; } i = 4; i = 5; for (i = 0; i < 3; ++i) { i = 1; j = i + 1; j = i + 2; i += 3; if (i < 10) { i = 5; j = 3; } j = i + 3; } return; }