XTRAN Example — Filter "Natural" Code on Statement Type, Display as HTML


We have created a set of XTRAN rules that filter a body of code based on one or more statement types and display the results as HTML, indented according to each statement's code nesting level.

The rules comprise 163 non-comment code lines of XTRAN meta-code (rules language).  They read a text file of statement verbs and corresponding filter names, so the filtering process can be customized by editing that text file.

The rules are language-independent, so they can be used to filter and display any language XTRAN can analyze.

After reading the filter descriptions, the rules repeatedly prompt the user for a filter name and add the results of that filter to the output HTML file.

This example filters Natural code to display data declaration statements only.

The input to and output from XTRAN are untouched, except that line numbers have been added to the Natural code for reference.



Process Flowchart

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

process flowchart

Input to XTRAN:

  1 0010 DEFINE DATA
  2 0020 LOCAL
  3 0030 1 #CATCH (A9)
  4 0040 1 #TEST  (A9)
  5 0050 1 REDEFINE #TEST
  6 0060   2 #T-A (A1/9)
  7 0070 1 #NUM   (N6.2)
  8 0080 1 #PIDX  (N2)
  9 0090 1 #SHOW  (A9)
 10 0100 END-DEFINE
 11 0110 *
 12 0120 SET KEY ALL
 13 0130 *
 14 0140 PROG.
 15 0150 REPEAT
 16 0160 INPUT (AD=MDLT'_') #CATCH #TEST (AD=OI) #NUM(AD=OI) #SHOW(AD=OI)
 17 0170 IF *PF-KEY = 'PF3'
 18 0180   ESCAPE BOTTOM(PROG.)
 19 0190 END-IF
 20 0200 *
 21 0210 MOVE RIGHT #CATCH TO #TEST
 22 0220 EXAMINE #TEST FOR ' ' REPLACE WITH '0'
 23 0230 *
 24 0240 REPEAT
 25 0250   EXAMINE #TEST FOR '.' GIVING POSITION IN #PIDX
 26 0260   DECIDE ON FIRST VALUE #PIDX
 27 0270     VALUE 0
 28 0280       IF SUBSTR(#TEST,1,3) = '000'
 29 0290         MOVE '   ' TO SUBSTR(#TEST,1,3)
 30 0300         MOVE LEFT #TEST TO #TEST
 31 0310         COMPRESS #TEST '.00' INTO #TEST LEAVING NO SPACE
 32 0320         ESCAPE TOP
 33 0330       END-IF
 34 0340       REINPUT 'Value too large'
 35 0350     VALUES 1:6
 36 0360       MOVE ' ' TO SUBSTR(#TEST,9,1)
 37 0370       MOVE RIGHT #TEST TO #TEST
 38 0380       EXAMINE #TEST FOR ' ' REPLACE WITH '0'
 40 0390     VALUES 8:9
 41 0400       IF SUBSTR(#TEST,1,1) NE '0'
 42 0410         REINPUT 'Value out of range'
 43 0420       END-IF
 44 0430       MOVE ' ' TO SUBSTR(#TEST,1,1)
 45 0440       MOVE LEFT #TEST TO #TEST
 46 0450       COMPRESS #TEST '0' INTO #TEST LEAVING NO SPACE
 47 0460       MOVE RIGHT #TEST TO #TEST
 48 0470     NONE
 49 0480       ESCAPE BOTTOM
 50 0490   END-DECIDE
 51 0500 END-REPEAT
 52 0510 *
 53 0520 IF #TEST = MASK(NNNNNN.NN)
 54 0530   COMPUTE #NUM = VAL(#TEST)
 55 0540 ELSE
 56 0550   RESET #NUM
 57 0560 END-IF
 58 0570 MOVE EDITED #NUM (EM=ZZZZZ9.99) TO #SHOW
 59 0580 END-REPEAT /* (PROG.)
 60 0590 END



Output from XTRAN (HTML):

    1  DEFINE DATA
    2      LOCAL
    3          <decl>
    4          <decl>
    5          <decl>
    6              <decl>
    7          <decl>
    8          <decl>
    9          <decl>