---input---
IMPLEMENTATION MODULE Sorting;

(* J. Andrea, Dec.16/91 *)
(* This code may be freely used and distributed, it may not be sold. *)

(* Adapted to ISO Module-2 by Frank Schoonjans  Feb 2004 *)

FROM Storage IMPORT ALLOCATE;

CONST
   max_stack = 20;
   n_small   = 6; (* use a simple sort for this size and smaller *)

VAR
  rtemp :REAL;
  ctemp :CARDINAL;

  L, R, n               :INTEGER;
  top, bottom, lastflip :INTEGER;

  tos            :CARDINAL;
  Lstack, Rstack :ARRAY [1..max_stack] OF INTEGER;

      (* --------------------------------------------------- *)
      PROCEDURE CardQSortIndex( x :ARRAY OF CARDINAL; array_len :CARDINAL;
                                VAR index :ARRAY OF CARDINAL );

      VAR
        median : CARDINAL;
        i,j    : INTEGER;
      BEGIN

        n := VAL(INTEGER,array_len) - 1; (* back to zero offset *)

        (* initialize the index *)
        FOR i := 0 TO n DO
          index[i] := VAL(CARDINAL,i);
        END;

        tos := 0;

        L := 0;  R := n;

        (* PUSH very first set *)
        tos := tos + 1;  Lstack[tos] := L;  Rstack[tos] := R;

        REPEAT

          (* POP *)
          L := Lstack[tos];  R := Rstack[tos];  tos := tos - 1;

          IF R - L + 1 > n_small THEN

            REPEAT
              i := L;  j := R;    median := x[index[( L + R ) DIV 2]];

              REPEAT
                WHILE x[index[i]] < median DO
                  i := i + 1;
                END;
                WHILE median < x[index[j]] DO
                  j := j - 1;
                END;

                IF i <= j THEN (* swap *)
                  ctemp := index[i];  index[i] := index[j];  index[j] := ctemp;
                  i := i + 1;  j := j - 1;
                END;
              UNTIL i > j;

              IF j - L < R - i THEN
                IF i < R THEN (* PUSH *)
                  tos := tos + 1;  Lstack[tos] := i;  Rstack[tos] := R;
                END;
                R := j;
              ELSE
                IF L < j THEN (* push *)
                  tos := tos + 1;  Lstack[tos] := L;  Rstack[tos] := j;
                END;
                L := i;
              END;

            UNTIL L >= R;

         ELSE

           (* small sort for small number of values *)
           FOR i := L TO R - 1 DO
             FOR j := i TO R DO
               IF x[index[i]] > x[index[j]] THEN
                  ctemp    := index[i];
                  index[i] := index[j];
                  index[j] := ctemp
               END;
             END;
           END;

         END; (* check for small *)

       UNTIL tos = 0;

      END CardQSortIndex;

      (* --------------------------------------------------- *)
      PROCEDURE RealQSortIndex( x :ARRAY OF REAL; array_len :CARDINAL;
                                VAR index :ARRAY OF CARDINAL );

      VAR
        median :REAL;
        i,j    :INTEGER;
      BEGIN

        n := VAL(INTEGER,array_len) - 1; (* back to zero offset *)

        (* initialize the index *)
        FOR i := 0 TO n DO
          index[i] := VAL(CARDINAL,i);
        END;

        tos := 0;

        L := 0;  R := n;

        (* PUSH very first set *)
        tos := tos + 1;  Lstack[tos] := L;  Rstack[tos] := R;

        REPEAT

          (* POP *)
          L := Lstack[tos];  R := Rstack[tos];  tos := tos - 1;

          IF R - L + 1 > n_small THEN

            REPEAT
              i := L;  j := R;    median := x[index[( L + R ) DIV 2]];

              REPEAT
                WHILE x[index[i]] < median DO
                  i := i + 1;
                END;
                WHILE median < x[index[j]] DO
                  j := j - 1;
                END;

                IF i <= j THEN (* swap *)
                  ctemp := index[i];  index[i] := index[j];  index[j] := ctemp;
                  i := i + 1;  j := j - 1;
                END;
              UNTIL i > j;

              IF j - L < R - i THEN
                IF i < R THEN (* PUSH *)
                  tos := tos + 1;  Lstack[tos] := i;  Rstack[tos] := R;
                END;
                R := j;
              ELSE
                IF L < j THEN (* push *)
                  tos := tos + 1;  Lstack[tos] := L;  Rstack[tos] := j;
                END;
                L := i;
              END;

            UNTIL L >= R;

         ELSE

           (* small sort for small number of values *)
           FOR i := L TO R - 1 DO
             FOR j := i TO R DO
               IF x[index[i]] > x[index[j]] THEN
                  ctemp    := index[i];
                  index[i] := index[j];
                  index[j] := ctemp
               END;
             END;
           END;

         END; (* check for small *)

       UNTIL tos = 0;

      END RealQSortIndex;

      (* --------------------------------------------------- *)
      PROCEDURE CardQSort( VAR x :ARRAY OF CARDINAL; array_len :CARDINAL );

      VAR
        median : CARDINAL;
        n,i,j  : INTEGER;
      BEGIN

        n := VAL(INTEGER,array_len) - 1; (* back to zero offset *)

        tos := 0;

        L := 0;  R := n;

        (* PUSH very first set *)
        tos := tos + 1;  Lstack[tos] := L;  Rstack[tos] := R;

        REPEAT

          (* POP *)
          L := Lstack[tos];  R := Rstack[tos];  tos := tos - 1;

          IF R - L + 1 > n_small THEN

            REPEAT
              i := L;  j := R;    median := x[( L + R ) DIV 2];

              REPEAT
                WHILE x[i] < median DO
                  i := i + 1;
                END;
                WHILE median < x[j] DO
                  j := j - 1;
                END;

                IF i <= j THEN (* swap *)
                  ctemp := x[i];  x[i] := x[j];  x[j] := ctemp;
                  i := i + 1;  j := j - 1;
                END;
              UNTIL i > j;

              IF j - L < R - i THEN
                IF i < R THEN (* PUSH *)
                  tos := tos + 1;  Lstack[tos] := i;  Rstack[tos] := R;
                END;
                R := j;
              ELSE
                IF L < j THEN (* push *)
                  tos := tos + 1;  Lstack[tos] := L;  Rstack[tos] := j;
                END;
                L := i;
              END;

            UNTIL L >= R;

         ELSE

           (* small sort for small number of values *)
           FOR i := L TO R - 1 DO
             FOR j := i TO R DO
               IF x[i] > x[j] THEN
                  ctemp := x[i];
                  x[i]  := x[j];
                  x[j]  := ctemp
               END;
             END;
           END;

         END; (* check for small *)

       UNTIL tos = 0;

      END CardQSort;

      (* ----------------------------------------------------- *)
      PROCEDURE CardBSort( VAR x :ARRAY OF CARDINAL; array_len :CARDINAL );
      VAR i,j : INTEGER;
      BEGIN
        top    := 0;      (* open arrays are zero offset *)
        bottom := VAL(INTEGER,array_len) - 1;

        WHILE top < bottom DO

          lastflip := top;

          FOR i := top TO bottom-1 DO
             IF x[i] > x[i+1] THEN    (* flip *)
               ctemp  := x[i];
               x[i]   := x[i+1];
               x[i+1] := ctemp;
               lastflip := i;
             END;
          END;

          bottom := lastflip;

          IF bottom > top THEN

             i := bottom - 1;
             FOR j := top TO bottom-1 DO
               IF x[i] > x[i+1] THEN    (* flip *)
                 ctemp  := x[i];
                 x[i]   := x[i+1];
                 x[i+1] := ctemp;
                 lastflip := i;
               END;
               i := i - 1;
             END;

             top := lastflip + 1;

          ELSE
             (* force a loop failure *)
             top := bottom + 1;
          END;

       END;

      END CardBSort;


      (* ----------------------------------------------------- *)
      PROCEDURE RealBSort( VAR x :ARRAY OF REAL; array_len :CARDINAL );
      VAR bottom,top : INTEGER;
          i,j        : INTEGER;
      BEGIN
        top    := 0;      (* open arrays are zero offset *)
        bottom := VAL(INTEGER,array_len) - 1;

        WHILE top < bottom DO

          lastflip := top;

          FOR i := top TO bottom-1 DO
             IF x[i] > x[i+1] THEN    (* flip *)
               rtemp  := x[i];
               x[i]   := x[i+1];
               x[i+1] := rtemp;
               lastflip := i;
             END;
          END;

          bottom := lastflip;

          IF bottom > top THEN

             i := bottom - 1;
             FOR j := top TO bottom-1 DO
               IF x[i] > x[i+1] THEN    (* flip *)
                 rtemp  := x[i];
                 x[i]   := x[i+1];
                 x[i+1] := rtemp;
                 lastflip := i;
               END;
               i := i - 1;
             END;

             top := lastflip + 1;

          ELSE
             (* force a loop failure *)
             top := bottom + 1;
          END;

       END;

      END RealBSort;


    (* ----------------------------------------------------- *)
    PROCEDURE TopoSort( x, y :ARRAY OF CARDINAL; n_pairs :CARDINAL;
                       VAR solution :ARRAY OF CARDINAL; VAR n_solution :CARDINAL;
                       VAR error, sorted :BOOLEAN );
    (*
     This procedure needs some garbage collection added, i've tried but
     will little success. J. Andrea, Dec.18/91
    *)

    TYPE
      LPtr = POINTER TO Leader;
      TPtr = POINTER TO Trailer;

      Leader = RECORD
                  key   :CARDINAL;
                  count :INTEGER;
                  trail :TPtr;
                  next  :LPtr;
               END;

      Trailer = RECORD
                  id   :LPtr;
                  next :TPtr;
                END;

    VAR
      p, q, head, tail :LPtr;
      t                :TPtr;
      i, max_solutions :CARDINAL;

      (* -------------------------------------------- *)
      PROCEDURE Find( w :CARDINAL ) :LPtr;
      VAR h :LPtr;
      BEGIN
        h := head;   tail^.key := w;  (* sentinel *)
        WHILE h^.key # w DO
           h := h^.next;
        END;
        IF h = tail THEN
          NEW( tail );
          n := n + 1;
          h^.count := 0;
          h^.trail := NIL;
          h^.next  := tail;
        END;
        RETURN h;
      END Find;

    BEGIN

        error      := FALSE;
        n_solution := 0;

        IF n_pairs < 2 THEN
          error := TRUE;
        ELSE

          max_solutions := HIGH( solution ) + 1;

          NEW( head );  tail := head;  n := 0;

          (* add all of the given pairs *)

          FOR i := 0 TO n_pairs - 1 DO
            p := Find( x[i] );   q := Find( y[i] );
            NEW(t);
            t^.id    := q;
            t^.next  := p^.trail;
            p^.trail := t;
            q^.count := q^.count + 1;
          END;

          (* search for leaders without predecessors *)

           p := head;  head := NIL;
           WHILE p # tail DO
            q := p;  p := q^.next;
            IF q^.count = 0 THEN
              (* insert q^ in new chain *)
              q^.next := head;   head := q;
            END;
          END;

          (* output phase *)

          q := head;
          WHILE ( NOT error ) & ( q # NIL ) DO
            n_solution := n_solution + 1;
            IF n_solution > max_solutions THEN
              error := TRUE;
            ELSE

              solution[n_solution-1] := q^.key;
              n := n - 1;
              t := q^.trail;  q := q^.next;
              WHILE t # NIL DO
                p := t^.id;  p^.count := p^.count - 1;
                IF p^.count = 0 THEN
                  (* insert p^ in leader list *)
                  p^.next := q;  q := p;
                END;
                t := t^.next;
              END;
            END;
          END;

          IF n # 0 THEN
            sorted := FALSE;
          ELSE
            sorted := TRUE;
          END;

       END;

    END TopoSort;

BEGIN
END Sorting.

---tokens---
'IMPLEMENTATION' Keyword.Reserved
' '           Text
'MODULE'      Keyword.Reserved
' '           Text
'Sorting'     Name
';'           Punctuation
'\n\n'        Text

'(* J. Andrea, Dec.16/91 *)' Comment.Multiline
'\n'          Text

'(* This code may be freely used and distributed, it may not be sold. *)' Comment.Multiline
'\n\n'        Text

'(* Adapted to ISO Module-2 by Frank Schoonjans  Feb 2004 *)' Comment.Multiline
'\n\n'        Text

'FROM'        Keyword.Reserved
' '           Text
'Storage'     Name
' '           Text
'IMPORT'      Keyword.Reserved
' '           Text
'ALLOCATE'    Name
';'           Punctuation
'\n\n'        Text

'CONST'       Keyword.Reserved
'\n'          Text

'   '         Text
'max_stack'   Name
' '           Text
'='           Operator
' '           Text
'20'          Literal.Number.Integer
';'           Punctuation
'\n'          Text

'   '         Text
'n_small'     Name
'   '         Text
'='           Operator
' '           Text
'6'           Literal.Number.Integer
';'           Punctuation
' '           Text
'(* use a simple sort for this size and smaller *)' Comment.Multiline
'\n\n'        Text

'VAR'         Keyword.Reserved
'\n'          Text

'  '          Text
'rtemp'       Name
' '           Text
':'           Punctuation
'REAL'        Name.Builtin
';'           Punctuation
'\n'          Text

'  '          Text
'ctemp'       Name
' '           Text
':'           Punctuation
'CARDINAL'    Name.Builtin
';'           Punctuation
'\n\n'        Text

'  '          Text
'L'           Name
','           Punctuation
' '           Text
'R'           Name
','           Punctuation
' '           Text
'n'           Name
'               ' Text
':'           Punctuation
'INTEGER'     Name.Builtin
';'           Punctuation
'\n'          Text

'  '          Text
'top'         Name
','           Punctuation
' '           Text
'bottom'      Name
','           Punctuation
' '           Text
'lastflip'    Name
' '           Text
':'           Punctuation
'INTEGER'     Name.Builtin
';'           Punctuation
'\n\n'        Text

'  '          Text
'tos'         Name
'            ' Text
':'           Punctuation
'CARDINAL'    Name.Builtin
';'           Punctuation
'\n'          Text

'  '          Text
'Lstack'      Name
','           Punctuation
' '           Text
'Rstack'      Name
' '           Text
':'           Punctuation
'ARRAY'       Keyword.Reserved
' '           Text
'['           Punctuation
'1'           Literal.Number.Integer
'..'          Punctuation
'max_stack'   Name
']'           Punctuation
' '           Text
'OF'          Keyword.Reserved
' '           Text
'INTEGER'     Name.Builtin
';'           Punctuation
'\n\n'        Text

'      '      Text
'(* --------------------------------------------------- *)' Comment.Multiline
'\n'          Text

'      '      Text
'PROCEDURE'   Keyword.Reserved
' '           Text
'CardQSortIndex' Name
'('           Punctuation
' '           Text
'x'           Name
' '           Text
':'           Punctuation
'ARRAY'       Keyword.Reserved
' '           Text
'OF'          Keyword.Reserved
' '           Text
'CARDINAL'    Name.Builtin
';'           Punctuation
' '           Text
'array_len'   Name
' '           Text
':'           Punctuation
'CARDINAL'    Name.Builtin
';'           Punctuation
'\n'          Text

'                                ' Text
'VAR'         Keyword.Reserved
' '           Text
'index'       Name
' '           Text
':'           Punctuation
'ARRAY'       Keyword.Reserved
' '           Text
'OF'          Keyword.Reserved
' '           Text
'CARDINAL'    Name.Builtin
' '           Text
')'           Punctuation
';'           Punctuation
'\n\n'        Text

'      '      Text
'VAR'         Keyword.Reserved
'\n'          Text

'        '    Text
'median'      Name
' '           Text
':'           Punctuation
' '           Text
'CARDINAL'    Name.Builtin
';'           Punctuation
'\n'          Text

'        '    Text
'i'           Name
','           Punctuation
'j'           Name
'    '        Text
':'           Punctuation
' '           Text
'INTEGER'     Name.Builtin
';'           Punctuation
'\n'          Text

'      '      Text
'BEGIN'       Keyword.Reserved
'\n\n'        Text

'        '    Text
'n'           Name
' '           Text
':='          Operator
' '           Text
'VAL'         Name.Builtin
'('           Punctuation
'INTEGER'     Name.Builtin
','           Punctuation
'array_len'   Name
')'           Punctuation
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
' '           Text
'(* back to zero offset *)' Comment.Multiline
'\n\n'        Text

'        '    Text
'(* initialize the index *)' Comment.Multiline
'\n'          Text

'        '    Text
'FOR'         Keyword.Reserved
' '           Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
' '           Text
'TO'          Keyword.Reserved
' '           Text
'n'           Name
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'          '  Text
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'VAL'         Name.Builtin
'('           Punctuation
'CARDINAL'    Name.Builtin
','           Punctuation
'i'           Name
')'           Punctuation
';'           Punctuation
'\n'          Text

'        '    Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'        '    Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'        '    Text
'L'           Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'R'           Name
' '           Text
':='          Operator
' '           Text
'n'           Name
';'           Punctuation
'\n\n'        Text

'        '    Text
'(* PUSH very first set *)' Comment.Multiline
'\n'          Text

'        '    Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'L'           Name
';'           Punctuation
'  '          Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'R'           Name
';'           Punctuation
'\n\n'        Text

'        '    Text
'REPEAT'      Keyword.Reserved
'\n\n'        Text

'          '  Text
'(* POP *)'   Comment.Multiline
'\n'          Text

'          '  Text
'L'           Name
' '           Text
':='          Operator
' '           Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
';'           Punctuation
'  '          Text
'R'           Name
' '           Text
':='          Operator
' '           Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
';'           Punctuation
'  '          Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'          '  Text
'IF'          Keyword.Reserved
' '           Text
'R'           Name
' '           Text
'-'           Operator
' '           Text
'L'           Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
' '           Text
'>'           Operator
' '           Text
'n_small'     Name
' '           Text
'THEN'        Keyword.Reserved
'\n\n'        Text

'            ' Text
'REPEAT'      Keyword.Reserved
'\n'          Text

'              ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'L'           Name
';'           Punctuation
'  '          Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'R'           Name
';'           Punctuation
'    '        Text
'median'      Name
' '           Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'index'       Name
'['           Punctuation
'('           Punctuation
' '           Text
'L'           Name
' '           Text
'+'           Operator
' '           Text
'R'           Name
' '           Text
')'           Punctuation
' '           Text
'DIV'         Keyword.Reserved
' '           Text
'2'           Literal.Number.Integer
']'           Punctuation
']'           Punctuation
';'           Punctuation
'\n\n'        Text

'              ' Text
'REPEAT'      Keyword.Reserved
'\n'          Text

'                ' Text
'WHILE'       Keyword.Reserved
' '           Text
'x'           Name
'['           Punctuation
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
']'           Punctuation
' '           Text
'<'           Operator
' '           Text
'median'      Name
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'                  ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'                ' Text
'WHILE'       Keyword.Reserved
' '           Text
'median'      Name
' '           Text
'<'           Operator
' '           Text
'x'           Name
'['           Punctuation
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
']'           Punctuation
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'                  ' Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'j'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'                ' Text
'IF'          Keyword.Reserved
' '           Text
'i'           Name
' '           Text
'<='          Operator
' '           Text
'j'           Name
' '           Text
'THEN'        Keyword.Reserved
' '           Text
'(* swap *)'  Comment.Multiline
'\n'          Text

'                  ' Text
'ctemp'       Name
' '           Text
':='          Operator
' '           Text
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
';'           Punctuation
'  '          Text
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
';'           Punctuation
'  '          Text
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'ctemp'       Name
';'           Punctuation
'\n'          Text

'                  ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'j'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'              ' Text
'UNTIL'       Keyword.Reserved
' '           Text
'i'           Name
' '           Text
'>'           Operator
' '           Text
'j'           Name
';'           Punctuation
'\n\n'        Text

'              ' Text
'IF'          Keyword.Reserved
' '           Text
'j'           Name
' '           Text
'-'           Operator
' '           Text
'L'           Name
' '           Text
'<'           Operator
' '           Text
'R'           Name
' '           Text
'-'           Operator
' '           Text
'i'           Name
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'                ' Text
'IF'          Keyword.Reserved
' '           Text
'i'           Name
' '           Text
'<'           Operator
' '           Text
'R'           Name
' '           Text
'THEN'        Keyword.Reserved
' '           Text
'(* PUSH *)'  Comment.Multiline
'\n'          Text

'                  ' Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'i'           Name
';'           Punctuation
'  '          Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'R'           Name
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'                ' Text
'R'           Name
' '           Text
':='          Operator
' '           Text
'j'           Name
';'           Punctuation
'\n'          Text

'              ' Text
'ELSE'        Keyword.Reserved
'\n'          Text

'                ' Text
'IF'          Keyword.Reserved
' '           Text
'L'           Name
' '           Text
'<'           Operator
' '           Text
'j'           Name
' '           Text
'THEN'        Keyword.Reserved
' '           Text
'(* push *)'  Comment.Multiline
'\n'          Text

'                  ' Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'L'           Name
';'           Punctuation
'  '          Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'j'           Name
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'                ' Text
'L'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
';'           Punctuation
'\n'          Text

'              ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'            ' Text
'UNTIL'       Keyword.Reserved
' '           Text
'L'           Name
' '           Text
'>='          Operator
' '           Text
'R'           Name
';'           Punctuation
'\n\n'        Text

'         '   Text
'ELSE'        Keyword.Reserved
'\n\n'        Text

'           ' Text
'(* small sort for small number of values *)' Comment.Multiline
'\n'          Text

'           ' Text
'FOR'         Keyword.Reserved
' '           Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'L'           Name
' '           Text
'TO'          Keyword.Reserved
' '           Text
'R'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'             ' Text
'FOR'         Keyword.Reserved
' '           Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'TO'          Keyword.Reserved
' '           Text
'R'           Name
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'               ' Text
'IF'          Keyword.Reserved
' '           Text
'x'           Name
'['           Punctuation
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
']'           Punctuation
' '           Text
'>'           Operator
' '           Text
'x'           Name
'['           Punctuation
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
']'           Punctuation
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'                  ' Text
'ctemp'       Name
'    '        Text
':='          Operator
' '           Text
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
';'           Punctuation
'\n'          Text

'                  ' Text
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
';'           Punctuation
'\n'          Text

'                  ' Text
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'ctemp'       Name
'\n'          Text

'               ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'             ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'           ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'         '   Text
'END'         Keyword.Reserved
';'           Punctuation
' '           Text
'(* check for small *)' Comment.Multiline
'\n\n'        Text

'       '     Text
'UNTIL'       Keyword.Reserved
' '           Text
'tos'         Name
' '           Text
'='           Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'      '      Text
'END'         Keyword.Reserved
' '           Text
'CardQSortIndex' Name
';'           Punctuation
'\n\n'        Text

'      '      Text
'(* --------------------------------------------------- *)' Comment.Multiline
'\n'          Text

'      '      Text
'PROCEDURE'   Keyword.Reserved
' '           Text
'RealQSortIndex' Name
'('           Punctuation
' '           Text
'x'           Name
' '           Text
':'           Punctuation
'ARRAY'       Keyword.Reserved
' '           Text
'OF'          Keyword.Reserved
' '           Text
'REAL'        Name.Builtin
';'           Punctuation
' '           Text
'array_len'   Name
' '           Text
':'           Punctuation
'CARDINAL'    Name.Builtin
';'           Punctuation
'\n'          Text

'                                ' Text
'VAR'         Keyword.Reserved
' '           Text
'index'       Name
' '           Text
':'           Punctuation
'ARRAY'       Keyword.Reserved
' '           Text
'OF'          Keyword.Reserved
' '           Text
'CARDINAL'    Name.Builtin
' '           Text
')'           Punctuation
';'           Punctuation
'\n\n'        Text

'      '      Text
'VAR'         Keyword.Reserved
'\n'          Text

'        '    Text
'median'      Name
' '           Text
':'           Punctuation
'REAL'        Name.Builtin
';'           Punctuation
'\n'          Text

'        '    Text
'i'           Name
','           Punctuation
'j'           Name
'    '        Text
':'           Punctuation
'INTEGER'     Name.Builtin
';'           Punctuation
'\n'          Text

'      '      Text
'BEGIN'       Keyword.Reserved
'\n\n'        Text

'        '    Text
'n'           Name
' '           Text
':='          Operator
' '           Text
'VAL'         Name.Builtin
'('           Punctuation
'INTEGER'     Name.Builtin
','           Punctuation
'array_len'   Name
')'           Punctuation
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
' '           Text
'(* back to zero offset *)' Comment.Multiline
'\n\n'        Text

'        '    Text
'(* initialize the index *)' Comment.Multiline
'\n'          Text

'        '    Text
'FOR'         Keyword.Reserved
' '           Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
' '           Text
'TO'          Keyword.Reserved
' '           Text
'n'           Name
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'          '  Text
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'VAL'         Name.Builtin
'('           Punctuation
'CARDINAL'    Name.Builtin
','           Punctuation
'i'           Name
')'           Punctuation
';'           Punctuation
'\n'          Text

'        '    Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'        '    Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'        '    Text
'L'           Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'R'           Name
' '           Text
':='          Operator
' '           Text
'n'           Name
';'           Punctuation
'\n\n'        Text

'        '    Text
'(* PUSH very first set *)' Comment.Multiline
'\n'          Text

'        '    Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'L'           Name
';'           Punctuation
'  '          Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'R'           Name
';'           Punctuation
'\n\n'        Text

'        '    Text
'REPEAT'      Keyword.Reserved
'\n\n'        Text

'          '  Text
'(* POP *)'   Comment.Multiline
'\n'          Text

'          '  Text
'L'           Name
' '           Text
':='          Operator
' '           Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
';'           Punctuation
'  '          Text
'R'           Name
' '           Text
':='          Operator
' '           Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
';'           Punctuation
'  '          Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'          '  Text
'IF'          Keyword.Reserved
' '           Text
'R'           Name
' '           Text
'-'           Operator
' '           Text
'L'           Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
' '           Text
'>'           Operator
' '           Text
'n_small'     Name
' '           Text
'THEN'        Keyword.Reserved
'\n\n'        Text

'            ' Text
'REPEAT'      Keyword.Reserved
'\n'          Text

'              ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'L'           Name
';'           Punctuation
'  '          Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'R'           Name
';'           Punctuation
'    '        Text
'median'      Name
' '           Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'index'       Name
'['           Punctuation
'('           Punctuation
' '           Text
'L'           Name
' '           Text
'+'           Operator
' '           Text
'R'           Name
' '           Text
')'           Punctuation
' '           Text
'DIV'         Keyword.Reserved
' '           Text
'2'           Literal.Number.Integer
']'           Punctuation
']'           Punctuation
';'           Punctuation
'\n\n'        Text

'              ' Text
'REPEAT'      Keyword.Reserved
'\n'          Text

'                ' Text
'WHILE'       Keyword.Reserved
' '           Text
'x'           Name
'['           Punctuation
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
']'           Punctuation
' '           Text
'<'           Operator
' '           Text
'median'      Name
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'                  ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'                ' Text
'WHILE'       Keyword.Reserved
' '           Text
'median'      Name
' '           Text
'<'           Operator
' '           Text
'x'           Name
'['           Punctuation
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
']'           Punctuation
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'                  ' Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'j'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'                ' Text
'IF'          Keyword.Reserved
' '           Text
'i'           Name
' '           Text
'<='          Operator
' '           Text
'j'           Name
' '           Text
'THEN'        Keyword.Reserved
' '           Text
'(* swap *)'  Comment.Multiline
'\n'          Text

'                  ' Text
'ctemp'       Name
' '           Text
':='          Operator
' '           Text
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
';'           Punctuation
'  '          Text
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
';'           Punctuation
'  '          Text
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'ctemp'       Name
';'           Punctuation
'\n'          Text

'                  ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'j'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'              ' Text
'UNTIL'       Keyword.Reserved
' '           Text
'i'           Name
' '           Text
'>'           Operator
' '           Text
'j'           Name
';'           Punctuation
'\n\n'        Text

'              ' Text
'IF'          Keyword.Reserved
' '           Text
'j'           Name
' '           Text
'-'           Operator
' '           Text
'L'           Name
' '           Text
'<'           Operator
' '           Text
'R'           Name
' '           Text
'-'           Operator
' '           Text
'i'           Name
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'                ' Text
'IF'          Keyword.Reserved
' '           Text
'i'           Name
' '           Text
'<'           Operator
' '           Text
'R'           Name
' '           Text
'THEN'        Keyword.Reserved
' '           Text
'(* PUSH *)'  Comment.Multiline
'\n'          Text

'                  ' Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'i'           Name
';'           Punctuation
'  '          Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'R'           Name
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'                ' Text
'R'           Name
' '           Text
':='          Operator
' '           Text
'j'           Name
';'           Punctuation
'\n'          Text

'              ' Text
'ELSE'        Keyword.Reserved
'\n'          Text

'                ' Text
'IF'          Keyword.Reserved
' '           Text
'L'           Name
' '           Text
'<'           Operator
' '           Text
'j'           Name
' '           Text
'THEN'        Keyword.Reserved
' '           Text
'(* push *)'  Comment.Multiline
'\n'          Text

'                  ' Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'L'           Name
';'           Punctuation
'  '          Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'j'           Name
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'                ' Text
'L'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
';'           Punctuation
'\n'          Text

'              ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'            ' Text
'UNTIL'       Keyword.Reserved
' '           Text
'L'           Name
' '           Text
'>='          Operator
' '           Text
'R'           Name
';'           Punctuation
'\n\n'        Text

'         '   Text
'ELSE'        Keyword.Reserved
'\n\n'        Text

'           ' Text
'(* small sort for small number of values *)' Comment.Multiline
'\n'          Text

'           ' Text
'FOR'         Keyword.Reserved
' '           Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'L'           Name
' '           Text
'TO'          Keyword.Reserved
' '           Text
'R'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'             ' Text
'FOR'         Keyword.Reserved
' '           Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'TO'          Keyword.Reserved
' '           Text
'R'           Name
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'               ' Text
'IF'          Keyword.Reserved
' '           Text
'x'           Name
'['           Punctuation
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
']'           Punctuation
' '           Text
'>'           Operator
' '           Text
'x'           Name
'['           Punctuation
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
']'           Punctuation
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'                  ' Text
'ctemp'       Name
'    '        Text
':='          Operator
' '           Text
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
';'           Punctuation
'\n'          Text

'                  ' Text
'index'       Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
';'           Punctuation
'\n'          Text

'                  ' Text
'index'       Name
'['           Punctuation
'j'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'ctemp'       Name
'\n'          Text

'               ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'             ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'           ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'         '   Text
'END'         Keyword.Reserved
';'           Punctuation
' '           Text
'(* check for small *)' Comment.Multiline
'\n\n'        Text

'       '     Text
'UNTIL'       Keyword.Reserved
' '           Text
'tos'         Name
' '           Text
'='           Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'      '      Text
'END'         Keyword.Reserved
' '           Text
'RealQSortIndex' Name
';'           Punctuation
'\n\n'        Text

'      '      Text
'(* --------------------------------------------------- *)' Comment.Multiline
'\n'          Text

'      '      Text
'PROCEDURE'   Keyword.Reserved
' '           Text
'CardQSort'   Name
'('           Punctuation
' '           Text
'VAR'         Keyword.Reserved
' '           Text
'x'           Name
' '           Text
':'           Punctuation
'ARRAY'       Keyword.Reserved
' '           Text
'OF'          Keyword.Reserved
' '           Text
'CARDINAL'    Name.Builtin
';'           Punctuation
' '           Text
'array_len'   Name
' '           Text
':'           Punctuation
'CARDINAL'    Name.Builtin
' '           Text
')'           Punctuation
';'           Punctuation
'\n\n'        Text

'      '      Text
'VAR'         Keyword.Reserved
'\n'          Text

'        '    Text
'median'      Name
' '           Text
':'           Punctuation
' '           Text
'CARDINAL'    Name.Builtin
';'           Punctuation
'\n'          Text

'        '    Text
'n'           Name
','           Punctuation
'i'           Name
','           Punctuation
'j'           Name
'  '          Text
':'           Punctuation
' '           Text
'INTEGER'     Name.Builtin
';'           Punctuation
'\n'          Text

'      '      Text
'BEGIN'       Keyword.Reserved
'\n\n'        Text

'        '    Text
'n'           Name
' '           Text
':='          Operator
' '           Text
'VAL'         Name.Builtin
'('           Punctuation
'INTEGER'     Name.Builtin
','           Punctuation
'array_len'   Name
')'           Punctuation
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
' '           Text
'(* back to zero offset *)' Comment.Multiline
'\n\n'        Text

'        '    Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'        '    Text
'L'           Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'R'           Name
' '           Text
':='          Operator
' '           Text
'n'           Name
';'           Punctuation
'\n\n'        Text

'        '    Text
'(* PUSH very first set *)' Comment.Multiline
'\n'          Text

'        '    Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'L'           Name
';'           Punctuation
'  '          Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'R'           Name
';'           Punctuation
'\n\n'        Text

'        '    Text
'REPEAT'      Keyword.Reserved
'\n\n'        Text

'          '  Text
'(* POP *)'   Comment.Multiline
'\n'          Text

'          '  Text
'L'           Name
' '           Text
':='          Operator
' '           Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
';'           Punctuation
'  '          Text
'R'           Name
' '           Text
':='          Operator
' '           Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
';'           Punctuation
'  '          Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'          '  Text
'IF'          Keyword.Reserved
' '           Text
'R'           Name
' '           Text
'-'           Operator
' '           Text
'L'           Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
' '           Text
'>'           Operator
' '           Text
'n_small'     Name
' '           Text
'THEN'        Keyword.Reserved
'\n\n'        Text

'            ' Text
'REPEAT'      Keyword.Reserved
'\n'          Text

'              ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'L'           Name
';'           Punctuation
'  '          Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'R'           Name
';'           Punctuation
'    '        Text
'median'      Name
' '           Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'('           Punctuation
' '           Text
'L'           Name
' '           Text
'+'           Operator
' '           Text
'R'           Name
' '           Text
')'           Punctuation
' '           Text
'DIV'         Keyword.Reserved
' '           Text
'2'           Literal.Number.Integer
']'           Punctuation
';'           Punctuation
'\n\n'        Text

'              ' Text
'REPEAT'      Keyword.Reserved
'\n'          Text

'                ' Text
'WHILE'       Keyword.Reserved
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
'<'           Operator
' '           Text
'median'      Name
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'                  ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'                ' Text
'WHILE'       Keyword.Reserved
' '           Text
'median'      Name
' '           Text
'<'           Operator
' '           Text
'x'           Name
'['           Punctuation
'j'           Name
']'           Punctuation
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'                  ' Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'j'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'                ' Text
'IF'          Keyword.Reserved
' '           Text
'i'           Name
' '           Text
'<='          Operator
' '           Text
'j'           Name
' '           Text
'THEN'        Keyword.Reserved
' '           Text
'(* swap *)'  Comment.Multiline
'\n'          Text

'                  ' Text
'ctemp'       Name
' '           Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
';'           Punctuation
'  '          Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'j'           Name
']'           Punctuation
';'           Punctuation
'  '          Text
'x'           Name
'['           Punctuation
'j'           Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'ctemp'       Name
';'           Punctuation
'\n'          Text

'                  ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'j'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'              ' Text
'UNTIL'       Keyword.Reserved
' '           Text
'i'           Name
' '           Text
'>'           Operator
' '           Text
'j'           Name
';'           Punctuation
'\n\n'        Text

'              ' Text
'IF'          Keyword.Reserved
' '           Text
'j'           Name
' '           Text
'-'           Operator
' '           Text
'L'           Name
' '           Text
'<'           Operator
' '           Text
'R'           Name
' '           Text
'-'           Operator
' '           Text
'i'           Name
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'                ' Text
'IF'          Keyword.Reserved
' '           Text
'i'           Name
' '           Text
'<'           Operator
' '           Text
'R'           Name
' '           Text
'THEN'        Keyword.Reserved
' '           Text
'(* PUSH *)'  Comment.Multiline
'\n'          Text

'                  ' Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'i'           Name
';'           Punctuation
'  '          Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'R'           Name
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'                ' Text
'R'           Name
' '           Text
':='          Operator
' '           Text
'j'           Name
';'           Punctuation
'\n'          Text

'              ' Text
'ELSE'        Keyword.Reserved
'\n'          Text

'                ' Text
'IF'          Keyword.Reserved
' '           Text
'L'           Name
' '           Text
'<'           Operator
' '           Text
'j'           Name
' '           Text
'THEN'        Keyword.Reserved
' '           Text
'(* push *)'  Comment.Multiline
'\n'          Text

'                  ' Text
'tos'         Name
' '           Text
':='          Operator
' '           Text
'tos'         Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'  '          Text
'Lstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'L'           Name
';'           Punctuation
'  '          Text
'Rstack'      Name
'['           Punctuation
'tos'         Name
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'j'           Name
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'                ' Text
'L'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
';'           Punctuation
'\n'          Text

'              ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'            ' Text
'UNTIL'       Keyword.Reserved
' '           Text
'L'           Name
' '           Text
'>='          Operator
' '           Text
'R'           Name
';'           Punctuation
'\n\n'        Text

'         '   Text
'ELSE'        Keyword.Reserved
'\n\n'        Text

'           ' Text
'(* small sort for small number of values *)' Comment.Multiline
'\n'          Text

'           ' Text
'FOR'         Keyword.Reserved
' '           Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'L'           Name
' '           Text
'TO'          Keyword.Reserved
' '           Text
'R'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'             ' Text
'FOR'         Keyword.Reserved
' '           Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'TO'          Keyword.Reserved
' '           Text
'R'           Name
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'               ' Text
'IF'          Keyword.Reserved
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
'>'           Operator
' '           Text
'x'           Name
'['           Punctuation
'j'           Name
']'           Punctuation
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'                  ' Text
'ctemp'       Name
' '           Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
';'           Punctuation
'\n'          Text

'                  ' Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
'  '          Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'j'           Name
']'           Punctuation
';'           Punctuation
'\n'          Text

'                  ' Text
'x'           Name
'['           Punctuation
'j'           Name
']'           Punctuation
'  '          Text
':='          Operator
' '           Text
'ctemp'       Name
'\n'          Text

'               ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'             ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'           ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'         '   Text
'END'         Keyword.Reserved
';'           Punctuation
' '           Text
'(* check for small *)' Comment.Multiline
'\n\n'        Text

'       '     Text
'UNTIL'       Keyword.Reserved
' '           Text
'tos'         Name
' '           Text
'='           Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'      '      Text
'END'         Keyword.Reserved
' '           Text
'CardQSort'   Name
';'           Punctuation
'\n\n'        Text

'      '      Text
'(* ----------------------------------------------------- *)' Comment.Multiline
'\n'          Text

'      '      Text
'PROCEDURE'   Keyword.Reserved
' '           Text
'CardBSort'   Name
'('           Punctuation
' '           Text
'VAR'         Keyword.Reserved
' '           Text
'x'           Name
' '           Text
':'           Punctuation
'ARRAY'       Keyword.Reserved
' '           Text
'OF'          Keyword.Reserved
' '           Text
'CARDINAL'    Name.Builtin
';'           Punctuation
' '           Text
'array_len'   Name
' '           Text
':'           Punctuation
'CARDINAL'    Name.Builtin
' '           Text
')'           Punctuation
';'           Punctuation
'\n'          Text

'      '      Text
'VAR'         Keyword.Reserved
' '           Text
'i'           Name
','           Punctuation
'j'           Name
' '           Text
':'           Punctuation
' '           Text
'INTEGER'     Name.Builtin
';'           Punctuation
'\n'          Text

'      '      Text
'BEGIN'       Keyword.Reserved
'\n'          Text

'        '    Text
'top'         Name
'    '        Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'      '      Text
'(* open arrays are zero offset *)' Comment.Multiline
'\n'          Text

'        '    Text
'bottom'      Name
' '           Text
':='          Operator
' '           Text
'VAL'         Name.Builtin
'('           Punctuation
'INTEGER'     Name.Builtin
','           Punctuation
'array_len'   Name
')'           Punctuation
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'        '    Text
'WHILE'       Keyword.Reserved
' '           Text
'top'         Name
' '           Text
'<'           Operator
' '           Text
'bottom'      Name
' '           Text
'DO'          Keyword.Reserved
'\n\n'        Text

'          '  Text
'lastflip'    Name
' '           Text
':='          Operator
' '           Text
'top'         Name
';'           Punctuation
'\n\n'        Text

'          '  Text
'FOR'         Keyword.Reserved
' '           Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'top'         Name
' '           Text
'TO'          Keyword.Reserved
' '           Text
'bottom'      Name
'-'           Operator
'1'           Literal.Number.Integer
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'             ' Text
'IF'          Keyword.Reserved
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
'>'           Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
' '           Text
'THEN'        Keyword.Reserved
'    '        Text
'(* flip *)'  Comment.Multiline
'\n'          Text

'               ' Text
'ctemp'       Name
'  '          Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
';'           Punctuation
'\n'          Text

'               ' Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
'   '         Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
';'           Punctuation
'\n'          Text

'               ' Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'ctemp'       Name
';'           Punctuation
'\n'          Text

'               ' Text
'lastflip'    Name
' '           Text
':='          Operator
' '           Text
'i'           Name
';'           Punctuation
'\n'          Text

'             ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'          '  Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'          '  Text
'bottom'      Name
' '           Text
':='          Operator
' '           Text
'lastflip'    Name
';'           Punctuation
'\n\n'        Text

'          '  Text
'IF'          Keyword.Reserved
' '           Text
'bottom'      Name
' '           Text
'>'           Operator
' '           Text
'top'         Name
' '           Text
'THEN'        Keyword.Reserved
'\n\n'        Text

'             ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'bottom'      Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'             ' Text
'FOR'         Keyword.Reserved
' '           Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'top'         Name
' '           Text
'TO'          Keyword.Reserved
' '           Text
'bottom'      Name
'-'           Operator
'1'           Literal.Number.Integer
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'               ' Text
'IF'          Keyword.Reserved
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
'>'           Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
' '           Text
'THEN'        Keyword.Reserved
'    '        Text
'(* flip *)'  Comment.Multiline
'\n'          Text

'                 ' Text
'ctemp'       Name
'  '          Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
';'           Punctuation
'\n'          Text

'                 ' Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
'   '         Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
';'           Punctuation
'\n'          Text

'                 ' Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'ctemp'       Name
';'           Punctuation
'\n'          Text

'                 ' Text
'lastflip'    Name
' '           Text
':='          Operator
' '           Text
'i'           Name
';'           Punctuation
'\n'          Text

'               ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'               ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'             ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'             ' Text
'top'         Name
' '           Text
':='          Operator
' '           Text
'lastflip'    Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'          '  Text
'ELSE'        Keyword.Reserved
'\n'          Text

'             ' Text
'(* force a loop failure *)' Comment.Multiline
'\n'          Text

'             ' Text
'top'         Name
' '           Text
':='          Operator
' '           Text
'bottom'      Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'          '  Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'       '     Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'      '      Text
'END'         Keyword.Reserved
' '           Text
'CardBSort'   Name
';'           Punctuation
'\n\n\n'      Text

'      '      Text
'(* ----------------------------------------------------- *)' Comment.Multiline
'\n'          Text

'      '      Text
'PROCEDURE'   Keyword.Reserved
' '           Text
'RealBSort'   Name
'('           Punctuation
' '           Text
'VAR'         Keyword.Reserved
' '           Text
'x'           Name
' '           Text
':'           Punctuation
'ARRAY'       Keyword.Reserved
' '           Text
'OF'          Keyword.Reserved
' '           Text
'REAL'        Name.Builtin
';'           Punctuation
' '           Text
'array_len'   Name
' '           Text
':'           Punctuation
'CARDINAL'    Name.Builtin
' '           Text
')'           Punctuation
';'           Punctuation
'\n'          Text

'      '      Text
'VAR'         Keyword.Reserved
' '           Text
'bottom'      Name
','           Punctuation
'top'         Name
' '           Text
':'           Punctuation
' '           Text
'INTEGER'     Name.Builtin
';'           Punctuation
'\n'          Text

'          '  Text
'i'           Name
','           Punctuation
'j'           Name
'        '    Text
':'           Punctuation
' '           Text
'INTEGER'     Name.Builtin
';'           Punctuation
'\n'          Text

'      '      Text
'BEGIN'       Keyword.Reserved
'\n'          Text

'        '    Text
'top'         Name
'    '        Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'      '      Text
'(* open arrays are zero offset *)' Comment.Multiline
'\n'          Text

'        '    Text
'bottom'      Name
' '           Text
':='          Operator
' '           Text
'VAL'         Name.Builtin
'('           Punctuation
'INTEGER'     Name.Builtin
','           Punctuation
'array_len'   Name
')'           Punctuation
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'        '    Text
'WHILE'       Keyword.Reserved
' '           Text
'top'         Name
' '           Text
'<'           Operator
' '           Text
'bottom'      Name
' '           Text
'DO'          Keyword.Reserved
'\n\n'        Text

'          '  Text
'lastflip'    Name
' '           Text
':='          Operator
' '           Text
'top'         Name
';'           Punctuation
'\n\n'        Text

'          '  Text
'FOR'         Keyword.Reserved
' '           Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'top'         Name
' '           Text
'TO'          Keyword.Reserved
' '           Text
'bottom'      Name
'-'           Operator
'1'           Literal.Number.Integer
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'             ' Text
'IF'          Keyword.Reserved
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
'>'           Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
' '           Text
'THEN'        Keyword.Reserved
'    '        Text
'(* flip *)'  Comment.Multiline
'\n'          Text

'               ' Text
'rtemp'       Name
'  '          Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
';'           Punctuation
'\n'          Text

'               ' Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
'   '         Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
';'           Punctuation
'\n'          Text

'               ' Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'rtemp'       Name
';'           Punctuation
'\n'          Text

'               ' Text
'lastflip'    Name
' '           Text
':='          Operator
' '           Text
'i'           Name
';'           Punctuation
'\n'          Text

'             ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'          '  Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'          '  Text
'bottom'      Name
' '           Text
':='          Operator
' '           Text
'lastflip'    Name
';'           Punctuation
'\n\n'        Text

'          '  Text
'IF'          Keyword.Reserved
' '           Text
'bottom'      Name
' '           Text
'>'           Operator
' '           Text
'top'         Name
' '           Text
'THEN'        Keyword.Reserved
'\n\n'        Text

'             ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'bottom'      Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'             ' Text
'FOR'         Keyword.Reserved
' '           Text
'j'           Name
' '           Text
':='          Operator
' '           Text
'top'         Name
' '           Text
'TO'          Keyword.Reserved
' '           Text
'bottom'      Name
'-'           Operator
'1'           Literal.Number.Integer
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'               ' Text
'IF'          Keyword.Reserved
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
'>'           Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
' '           Text
'THEN'        Keyword.Reserved
'    '        Text
'(* flip *)'  Comment.Multiline
'\n'          Text

'                 ' Text
'rtemp'       Name
'  '          Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
';'           Punctuation
'\n'          Text

'                 ' Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
'   '         Text
':='          Operator
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
';'           Punctuation
'\n'          Text

'                 ' Text
'x'           Name
'['           Punctuation
'i'           Name
'+'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'rtemp'       Name
';'           Punctuation
'\n'          Text

'                 ' Text
'lastflip'    Name
' '           Text
':='          Operator
' '           Text
'i'           Name
';'           Punctuation
'\n'          Text

'               ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'               ' Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'i'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'             ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'             ' Text
'top'         Name
' '           Text
':='          Operator
' '           Text
'lastflip'    Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'          '  Text
'ELSE'        Keyword.Reserved
'\n'          Text

'             ' Text
'(* force a loop failure *)' Comment.Multiline
'\n'          Text

'             ' Text
'top'         Name
' '           Text
':='          Operator
' '           Text
'bottom'      Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'          '  Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'       '     Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'      '      Text
'END'         Keyword.Reserved
' '           Text
'RealBSort'   Name
';'           Punctuation
'\n\n\n'      Text

'    '        Text
'(* ----------------------------------------------------- *)' Comment.Multiline
'\n'          Text

'    '        Text
'PROCEDURE'   Keyword.Reserved
' '           Text
'TopoSort'    Name
'('           Punctuation
' '           Text
'x'           Name
','           Punctuation
' '           Text
'y'           Name
' '           Text
':'           Punctuation
'ARRAY'       Keyword.Reserved
' '           Text
'OF'          Keyword.Reserved
' '           Text
'CARDINAL'    Name.Builtin
';'           Punctuation
' '           Text
'n_pairs'     Name
' '           Text
':'           Punctuation
'CARDINAL'    Name.Builtin
';'           Punctuation
'\n'          Text

'                       ' Text
'VAR'         Keyword.Reserved
' '           Text
'solution'    Name
' '           Text
':'           Punctuation
'ARRAY'       Keyword.Reserved
' '           Text
'OF'          Keyword.Reserved
' '           Text
'CARDINAL'    Name.Builtin
';'           Punctuation
' '           Text
'VAR'         Keyword.Reserved
' '           Text
'n_solution'  Name
' '           Text
':'           Punctuation
'CARDINAL'    Name.Builtin
';'           Punctuation
'\n'          Text

'                       ' Text
'VAR'         Keyword.Reserved
' '           Text
'error'       Name
','           Punctuation
' '           Text
'sorted'      Name
' '           Text
':'           Punctuation
'BOOLEAN'     Name.Builtin
' '           Text
')'           Punctuation
';'           Punctuation
'\n'          Text

'    '        Text
"(*\n     This procedure needs some garbage collection added, i've tried but\n     will little success. J. Andrea, Dec.18/91\n    *)" Comment.Multiline
'\n\n'        Text

'    '        Text
'TYPE'        Keyword.Reserved
'\n'          Text

'      '      Text
'LPtr'        Name
' '           Text
'='           Operator
' '           Text
'POINTER'     Keyword.Reserved
' '           Text
'TO'          Keyword.Reserved
' '           Text
'Leader'      Name
';'           Punctuation
'\n'          Text

'      '      Text
'TPtr'        Name
' '           Text
'='           Operator
' '           Text
'POINTER'     Keyword.Reserved
' '           Text
'TO'          Keyword.Reserved
' '           Text
'Trailer'     Name
';'           Punctuation
'\n\n'        Text

'      '      Text
'Leader'      Name
' '           Text
'='           Operator
' '           Text
'RECORD'      Keyword.Reserved
'\n'          Text

'                  ' Text
'key'         Name
'   '         Text
':'           Punctuation
'CARDINAL'    Name.Builtin
';'           Punctuation
'\n'          Text

'                  ' Text
'count'       Name
' '           Text
':'           Punctuation
'INTEGER'     Name.Builtin
';'           Punctuation
'\n'          Text

'                  ' Text
'trail'       Name
' '           Text
':'           Punctuation
'TPtr'        Name
';'           Punctuation
'\n'          Text

'                  ' Text
'next'        Name
'  '          Text
':'           Punctuation
'LPtr'        Name
';'           Punctuation
'\n'          Text

'               ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'      '      Text
'Trailer'     Name
' '           Text
'='           Operator
' '           Text
'RECORD'      Keyword.Reserved
'\n'          Text

'                  ' Text
'id'          Name
'   '         Text
':'           Punctuation
'LPtr'        Name
';'           Punctuation
'\n'          Text

'                  ' Text
'next'        Name
' '           Text
':'           Punctuation
'TPtr'        Name
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'    '        Text
'VAR'         Keyword.Reserved
'\n'          Text

'      '      Text
'p'           Name
','           Punctuation
' '           Text
'q'           Name
','           Punctuation
' '           Text
'head'        Name
','           Punctuation
' '           Text
'tail'        Name
' '           Text
':'           Punctuation
'LPtr'        Name
';'           Punctuation
'\n'          Text

'      '      Text
't'           Name
'                ' Text
':'           Punctuation
'TPtr'        Name
';'           Punctuation
'\n'          Text

'      '      Text
'i'           Name
','           Punctuation
' '           Text
'max_solutions' Name
' '           Text
':'           Punctuation
'CARDINAL'    Name.Builtin
';'           Punctuation
'\n\n'        Text

'      '      Text
'(* -------------------------------------------- *)' Comment.Multiline
'\n'          Text

'      '      Text
'PROCEDURE'   Keyword.Reserved
' '           Text
'Find'        Name
'('           Punctuation
' '           Text
'w'           Name
' '           Text
':'           Punctuation
'CARDINAL'    Name.Builtin
' '           Text
')'           Punctuation
' '           Text
':'           Punctuation
'LPtr'        Name
';'           Punctuation
'\n'          Text

'      '      Text
'VAR'         Keyword.Reserved
' '           Text
'h'           Name
' '           Text
':'           Punctuation
'LPtr'        Name
';'           Punctuation
'\n'          Text

'      '      Text
'BEGIN'       Keyword.Reserved
'\n'          Text

'        '    Text
'h'           Name
' '           Text
':='          Operator
' '           Text
'head'        Name
';'           Punctuation
'   '         Text
'tail'        Name
'^'           Operator
'.'           Punctuation
'key'         Name
' '           Text
':='          Operator
' '           Text
'w'           Name
';'           Punctuation
'  '          Text
'(* sentinel *)' Comment.Multiline
'\n'          Text

'        '    Text
'WHILE'       Keyword.Reserved
' '           Text
'h'           Name
'^'           Operator
'.'           Punctuation
'key'         Name
' '           Text
'#'           Operator
' '           Text
'w'           Name
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'           ' Text
'h'           Name
' '           Text
':='          Operator
' '           Text
'h'           Name
'^'           Operator
'.'           Punctuation
'next'        Name
';'           Punctuation
'\n'          Text

'        '    Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'        '    Text
'IF'          Keyword.Reserved
' '           Text
'h'           Name
' '           Text
'='           Operator
' '           Text
'tail'        Name
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'          '  Text
'NEW'         Keyword.Reserved
'('           Punctuation
' '           Text
'tail'        Name
' '           Text
')'           Punctuation
';'           Punctuation
'\n'          Text

'          '  Text
'n'           Name
' '           Text
':='          Operator
' '           Text
'n'           Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'          '  Text
'h'           Name
'^'           Operator
'.'           Punctuation
'count'       Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'          '  Text
'h'           Name
'^'           Operator
'.'           Punctuation
'trail'       Name
' '           Text
':='          Operator
' '           Text
'NIL'         Name.Builtin
';'           Punctuation
'\n'          Text

'          '  Text
'h'           Name
'^'           Operator
'.'           Punctuation
'next'        Name
'  '          Text
':='          Operator
' '           Text
'tail'        Name
';'           Punctuation
'\n'          Text

'        '    Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'        '    Text
'RETURN'      Keyword.Reserved
' '           Text
'h'           Name
';'           Punctuation
'\n'          Text

'      '      Text
'END'         Keyword.Reserved
' '           Text
'Find'        Name
';'           Punctuation
'\n\n'        Text

'    '        Text
'BEGIN'       Keyword.Reserved
'\n\n'        Text

'        '    Text
'error'       Name
'      '      Text
':='          Operator
' '           Text
'FALSE'       Name.Builtin
';'           Punctuation
'\n'          Text

'        '    Text
'n_solution'  Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'        '    Text
'IF'          Keyword.Reserved
' '           Text
'n_pairs'     Name
' '           Text
'<'           Operator
' '           Text
'2'           Literal.Number.Integer
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'          '  Text
'error'       Name
' '           Text
':='          Operator
' '           Text
'TRUE'        Name.Builtin
';'           Punctuation
'\n'          Text

'        '    Text
'ELSE'        Keyword.Reserved
'\n\n'        Text

'          '  Text
'max_solutions' Name
' '           Text
':='          Operator
' '           Text
'HIGH'        Name.Builtin
'('           Punctuation
' '           Text
'solution'    Name
' '           Text
')'           Punctuation
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'          '  Text
'NEW'         Keyword.Reserved
'('           Punctuation
' '           Text
'head'        Name
' '           Text
')'           Punctuation
';'           Punctuation
'  '          Text
'tail'        Name
' '           Text
':='          Operator
' '           Text
'head'        Name
';'           Punctuation
'  '          Text
'n'           Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
';'           Punctuation
'\n\n'        Text

'          '  Text
'(* add all of the given pairs *)' Comment.Multiline
'\n\n'        Text

'          '  Text
'FOR'         Keyword.Reserved
' '           Text
'i'           Name
' '           Text
':='          Operator
' '           Text
'0'           Literal.Number.Integer
' '           Text
'TO'          Keyword.Reserved
' '           Text
'n_pairs'     Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'            ' Text
'p'           Name
' '           Text
':='          Operator
' '           Text
'Find'        Name
'('           Punctuation
' '           Text
'x'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
')'           Punctuation
';'           Punctuation
'   '         Text
'q'           Name
' '           Text
':='          Operator
' '           Text
'Find'        Name
'('           Punctuation
' '           Text
'y'           Name
'['           Punctuation
'i'           Name
']'           Punctuation
' '           Text
')'           Punctuation
';'           Punctuation
'\n'          Text

'            ' Text
'NEW'         Keyword.Reserved
'('           Punctuation
't'           Name
')'           Punctuation
';'           Punctuation
'\n'          Text

'            ' Text
't'           Name
'^'           Operator
'.'           Punctuation
'id'          Name
'    '        Text
':='          Operator
' '           Text
'q'           Name
';'           Punctuation
'\n'          Text

'            ' Text
't'           Name
'^'           Operator
'.'           Punctuation
'next'        Name
'  '          Text
':='          Operator
' '           Text
'p'           Name
'^'           Operator
'.'           Punctuation
'trail'       Name
';'           Punctuation
'\n'          Text

'            ' Text
'p'           Name
'^'           Operator
'.'           Punctuation
'trail'       Name
' '           Text
':='          Operator
' '           Text
't'           Name
';'           Punctuation
'\n'          Text

'            ' Text
'q'           Name
'^'           Operator
'.'           Punctuation
'count'       Name
' '           Text
':='          Operator
' '           Text
'q'           Name
'^'           Operator
'.'           Punctuation
'count'       Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'          '  Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'          '  Text
'(* search for leaders without predecessors *)' Comment.Multiline
'\n\n'        Text

'           ' Text
'p'           Name
' '           Text
':='          Operator
' '           Text
'head'        Name
';'           Punctuation
'  '          Text
'head'        Name
' '           Text
':='          Operator
' '           Text
'NIL'         Name.Builtin
';'           Punctuation
'\n'          Text

'           ' Text
'WHILE'       Keyword.Reserved
' '           Text
'p'           Name
' '           Text
'#'           Operator
' '           Text
'tail'        Name
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'            ' Text
'q'           Name
' '           Text
':='          Operator
' '           Text
'p'           Name
';'           Punctuation
'  '          Text
'p'           Name
' '           Text
':='          Operator
' '           Text
'q'           Name
'^'           Operator
'.'           Punctuation
'next'        Name
';'           Punctuation
'\n'          Text

'            ' Text
'IF'          Keyword.Reserved
' '           Text
'q'           Name
'^'           Operator
'.'           Punctuation
'count'       Name
' '           Text
'='           Operator
' '           Text
'0'           Literal.Number.Integer
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'              ' Text
'(* insert q^ in new chain *)' Comment.Multiline
'\n'          Text

'              ' Text
'q'           Name
'^'           Operator
'.'           Punctuation
'next'        Name
' '           Text
':='          Operator
' '           Text
'head'        Name
';'           Punctuation
'   '         Text
'head'        Name
' '           Text
':='          Operator
' '           Text
'q'           Name
';'           Punctuation
'\n'          Text

'            ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'          '  Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'          '  Text
'(* output phase *)' Comment.Multiline
'\n\n'        Text

'          '  Text
'q'           Name
' '           Text
':='          Operator
' '           Text
'head'        Name
';'           Punctuation
'\n'          Text

'          '  Text
'WHILE'       Keyword.Reserved
' '           Text
'('           Punctuation
' '           Text
'NOT'         Keyword.Reserved
' '           Text
'error'       Name
' '           Text
')'           Punctuation
' '           Text
'&'           Operator
' '           Text
'('           Punctuation
' '           Text
'q'           Name
' '           Text
'#'           Operator
' '           Text
'NIL'         Name.Builtin
' '           Text
')'           Punctuation
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'            ' Text
'n_solution'  Name
' '           Text
':='          Operator
' '           Text
'n_solution'  Name
' '           Text
'+'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'            ' Text
'IF'          Keyword.Reserved
' '           Text
'n_solution'  Name
' '           Text
'>'           Operator
' '           Text
'max_solutions' Name
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'              ' Text
'error'       Name
' '           Text
':='          Operator
' '           Text
'TRUE'        Name.Builtin
';'           Punctuation
'\n'          Text

'            ' Text
'ELSE'        Keyword.Reserved
'\n\n'        Text

'              ' Text
'solution'    Name
'['           Punctuation
'n_solution'  Name
'-'           Operator
'1'           Literal.Number.Integer
']'           Punctuation
' '           Text
':='          Operator
' '           Text
'q'           Name
'^'           Operator
'.'           Punctuation
'key'         Name
';'           Punctuation
'\n'          Text

'              ' Text
'n'           Name
' '           Text
':='          Operator
' '           Text
'n'           Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'              ' Text
't'           Name
' '           Text
':='          Operator
' '           Text
'q'           Name
'^'           Operator
'.'           Punctuation
'trail'       Name
';'           Punctuation
'  '          Text
'q'           Name
' '           Text
':='          Operator
' '           Text
'q'           Name
'^'           Operator
'.'           Punctuation
'next'        Name
';'           Punctuation
'\n'          Text

'              ' Text
'WHILE'       Keyword.Reserved
' '           Text
't'           Name
' '           Text
'#'           Operator
' '           Text
'NIL'         Name.Builtin
' '           Text
'DO'          Keyword.Reserved
'\n'          Text

'                ' Text
'p'           Name
' '           Text
':='          Operator
' '           Text
't'           Name
'^'           Operator
'.'           Punctuation
'id'          Name
';'           Punctuation
'  '          Text
'p'           Name
'^'           Operator
'.'           Punctuation
'count'       Name
' '           Text
':='          Operator
' '           Text
'p'           Name
'^'           Operator
'.'           Punctuation
'count'       Name
' '           Text
'-'           Operator
' '           Text
'1'           Literal.Number.Integer
';'           Punctuation
'\n'          Text

'                ' Text
'IF'          Keyword.Reserved
' '           Text
'p'           Name
'^'           Operator
'.'           Punctuation
'count'       Name
' '           Text
'='           Operator
' '           Text
'0'           Literal.Number.Integer
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'                  ' Text
'(* insert p^ in leader list *)' Comment.Multiline
'\n'          Text

'                  ' Text
'p'           Name
'^'           Operator
'.'           Punctuation
'next'        Name
' '           Text
':='          Operator
' '           Text
'q'           Name
';'           Punctuation
'  '          Text
'q'           Name
' '           Text
':='          Operator
' '           Text
'p'           Name
';'           Punctuation
'\n'          Text

'                ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'                ' Text
't'           Name
' '           Text
':='          Operator
' '           Text
't'           Name
'^'           Operator
'.'           Punctuation
'next'        Name
';'           Punctuation
'\n'          Text

'              ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'            ' Text
'END'         Keyword.Reserved
';'           Punctuation
'\n'          Text

'          '  Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'          '  Text
'IF'          Keyword.Reserved
' '           Text
'n'           Name
' '           Text
'#'           Operator
' '           Text
'0'           Literal.Number.Integer
' '           Text
'THEN'        Keyword.Reserved
'\n'          Text

'            ' Text
'sorted'      Name
' '           Text
':='          Operator
' '           Text
'FALSE'       Name.Builtin
';'           Punctuation
'\n'          Text

'          '  Text
'ELSE'        Keyword.Reserved
'\n'          Text

'            ' Text
'sorted'      Name
' '           Text
':='          Operator
' '           Text
'TRUE'        Name.Builtin
';'           Punctuation
'\n'          Text

'          '  Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'       '     Text
'END'         Keyword.Reserved
';'           Punctuation
'\n\n'        Text

'    '        Text
'END'         Keyword.Reserved
' '           Text
'TopoSort'    Name
';'           Punctuation
'\n\n'        Text

'BEGIN'       Keyword.Reserved
'\n'          Text

'END'         Keyword.Reserved
' '           Text
'Sorting'     Name
'.'           Punctuation
'\n'          Text
