home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_se / fmt.e < prev    next >
Text File  |  1999-06-05  |  9KB  |  387 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
  4. --                       http://SmallEiffel.loria.fr
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License
  11. -- for  more  details.  You  should  have  received a copy of the GNU General
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class FMT
  17.    --
  18.    -- Driver for Pretty Printing Eiffel code.
  19.    --
  20.  
  21. creation make
  22.  
  23. feature {NONE}
  24.  
  25.    sfw: STD_FILE_WRITE;
  26.          -- Where printing is done.
  27.  
  28.    C_default: INTEGER is 0;
  29.    C_zen:     INTEGER is 1;
  30.    C_end:     INTEGER is 2;
  31.    C_parano:  INTEGER is 3;
  32.  
  33.    indent_increment: INTEGER is 3;
  34.          -- Basic standard increment.
  35.  
  36.    make is
  37.       do
  38.       end;
  39.  
  40. feature
  41.  
  42.    mode: INTEGER;
  43.          -- Internal code to memorize the mode : "-zen|-default|-end|-parano"
  44.  
  45.    valid_mode(m: INTEGER): BOOLEAN is
  46.          -- Is the mode previously obtained using `mode' ?
  47.       do
  48.          inspect
  49.             m
  50.          when C_zen, C_default, C_end, C_parano then
  51.             Result := true;
  52.          else
  53.          end;
  54.       end;
  55.  
  56.    set_mode(m: INTEGER) is
  57.          -- Where `m' is a valid mode previously obtained using `mode'.
  58.       require
  59.          valid_mode(m)
  60.       do
  61.          mode := m;
  62.       ensure
  63.          mode = m
  64.       end;
  65.  
  66.    column: INTEGER;
  67.          -- Current column in output file. Left most
  68.          -- column is number 1;
  69.  
  70.    line: INTEGER;
  71.          -- Current column in output file.
  72.  
  73.    blank_lines: INTEGER;
  74.          -- Number of blank lines at current position.
  75.  
  76.  
  77.    last_character: CHARACTER;
  78.          -- Last printed one.
  79.  
  80.    indent_level: INTEGER;
  81.          -- Current `indent_level'.
  82.  
  83.    semi_colon_flag: BOOLEAN;
  84.          -- When Current instruction must add a following semi_colon.
  85.  
  86.    set_semi_colon_flag(v: like semi_colon_flag) is
  87.       do
  88.          semi_colon_flag := v;
  89.       end;
  90.  
  91.    set_indent_level(il: INTEGER) is
  92.       require
  93.          il >= 0;
  94.       do
  95.          indent_level := il;
  96.       ensure
  97.          indent_level = il;
  98.       end;
  99.  
  100.    level_incr is
  101.       do
  102.          indent_level := indent_level + 1;
  103.       end;
  104.  
  105.    level_decr is
  106.       require
  107.          indent_level > 0;
  108.       do
  109.          indent_level := indent_level - 1;
  110.       end;
  111.  
  112. feature -- Initial mode setting :
  113.  
  114.    set_zen is
  115.          -- The less you can print, no Current when not necessary,
  116.          -- no end of constructs, no ends of routine and
  117.          -- compact printing.
  118.       do
  119.          mode := C_zen;
  120.       end;
  121.  
  122.    set_default is
  123.          -- Default pretty printing mode.
  124.       do
  125.          mode := C_default;
  126.       end;
  127.  
  128.    set_end is
  129.          -- Print ends of all constructs.
  130.       do
  131.          mode := C_end;
  132.       end;
  133.  
  134.    set_parano is
  135.          -- The more you can print (parano mode).
  136.       do
  137.          mode := C_parano;
  138.       end;
  139.  
  140. feature -- Printing Features :
  141.  
  142.    put_end(what: STRING) is
  143.       do
  144.          put_string("-- ");
  145.          put_string(what);
  146.          put_character('%N');
  147.       end;
  148.  
  149.    keyword(k: STRING) is
  150.          -- Print keyword `k'.
  151.          -- If needed, a space is added before `k'.
  152.          -- Always add a ' ' after `k'.
  153.       require
  154.          not k.has('%N');
  155.          not k.has('%T');
  156.       do
  157.          inspect
  158.             last_character
  159.          when ' ','%N','%U' then
  160.          else
  161.             put_character(' ');
  162.          end;
  163.          put_string(k);
  164.          if last_character /= ' ' then
  165.             put_character(' ');
  166.          end;
  167.       ensure
  168.          last_character = ' ';
  169.       end;
  170.  
  171.    skip(line_count: INTEGER) is
  172.       -- Add if needed `line_count' blanks lines and do `indent'.
  173.       require
  174.          line_count >= 0;
  175.       do
  176.          from
  177.          until
  178.             blank_lines >= line_count
  179.          loop
  180.             put_character('%N');
  181.          end;
  182.          indent;
  183.       ensure
  184.          blank_lines >= line_count;
  185.       end;
  186.  
  187.    put_integer(i: INTEGER) is
  188.       -- Print `i' using `put_string'.
  189.       do
  190.          tmp_string.clear;
  191.          i.append_in(tmp_string);
  192.          put_string(tmp_string);
  193.       end;
  194.  
  195.    put_string(s: STRING) is
  196.       require
  197.          s /= Void;
  198.       local
  199.          i: INTEGER;
  200.       do
  201.          from
  202.             i := 1;
  203.          until
  204.             i > s.count
  205.          loop
  206.             put_character(s.item(i));
  207.             i := i + 1;
  208.          end;
  209.       end;
  210.  
  211.    indent is
  212.          -- Go if needed to the `column' according to
  213.          -- the current `indent_level'.
  214.          -- Ensure that the last printed character is ' ' or '%N';
  215.       local
  216.          goal: INTEGER;
  217.       do
  218.          goal := 1 + indent_level * indent_increment;
  219.          if column > goal then
  220.             put_character('%N');
  221.          end;
  222.          from
  223.          until
  224.             goal = column
  225.          loop
  226.             put_character(' ');
  227.          end;
  228.          inspect
  229.             last_character
  230.          when ' ','%N' then
  231.          else
  232.             put_character('%N');
  233.             indent;
  234.          end;
  235.       ensure
  236.          column = indent_level * indent_increment + 1;
  237.          last_character = ' ' or last_character = '%N';
  238.       end;
  239.  
  240.    put_character(c: CHARACTER) is
  241.       do
  242.          sfw.put_character(c);
  243.          last_character := c;
  244.          inspect
  245.             c
  246.          when '%N' then
  247.             line := line + 1;
  248.             column := 1;
  249.             blank_lines := blank_lines + 1;
  250.          when ' ','%T' then
  251.             column := column + 1;
  252.          else
  253.             column := column + 1;
  254.             blank_lines := -1;
  255.          end;
  256.       end;
  257.  
  258. feature {PRETTY}
  259.  
  260.    connect_to(s: like sfw) is
  261.       require
  262.          s /= Void;
  263.       do
  264.          sfw := s;
  265.          line := 1;
  266.          column := 1;
  267.          blank_lines := 0;
  268.          last_character := '%U';
  269.       ensure
  270.          sfw = s;
  271.       end;
  272.  
  273. feature {NONE}
  274.  
  275.    tmp_string: STRING is
  276.       once
  277.          !!Result.blank(256);
  278.       end;
  279.  
  280. feature -- Computed flags :
  281.  
  282.    zen_mode: BOOLEAN is
  283.       do
  284.          Result := mode = C_zen;
  285.       end;
  286.  
  287.    print_end_check: BOOLEAN is
  288.       do
  289.          inspect
  290.             mode
  291.          when C_zen then
  292.          when C_default then
  293.          when C_end then
  294.             Result := true;
  295.          when C_parano then
  296.             Result := true;
  297.          end;
  298.       end;
  299.  
  300.    print_end_loop: BOOLEAN is
  301.       do
  302.          inspect
  303.             mode
  304.          when C_zen then
  305.          when C_default then
  306.          when C_end then
  307.             Result := true;
  308.          when C_parano then
  309.             Result := true;
  310.          end;
  311.       end;
  312.  
  313.    print_end_if: BOOLEAN is
  314.       do
  315.          inspect
  316.             mode
  317.          when C_zen then
  318.          when C_default then
  319.          when C_end then
  320.             Result := true;
  321.          when C_parano then
  322.             Result := true;
  323.          end;
  324.       end;
  325.  
  326.    print_end_inspect: BOOLEAN is
  327.       do
  328.          inspect
  329.             mode
  330.          when C_zen then
  331.          when C_default then
  332.          when C_end then
  333.             Result := true;
  334.          when C_parano then
  335.             Result := true;
  336.          end;
  337.       end;
  338.  
  339.    print_end_debug: BOOLEAN is
  340.       do
  341.          inspect
  342.             mode
  343.          when C_zen then
  344.          when C_default then
  345.          when C_end then
  346.             Result := true;
  347.          when C_parano then
  348.             Result := true;
  349.          end;
  350.       end;
  351.  
  352.    print_end_routine: BOOLEAN is
  353.       do
  354.          inspect
  355.             mode
  356.          when C_zen then
  357.          when C_default then
  358.             Result := true;
  359.          when C_end then
  360.             Result := true;
  361.          when C_parano then
  362.             Result := true;
  363.          end;
  364.       end;
  365.  
  366.    print_current: BOOLEAN is
  367.       do
  368.          inspect
  369.             mode
  370.          when C_zen then
  371.          when C_default then
  372.          when C_end then
  373.          when C_parano then
  374.             Result := true;
  375.          end;
  376.       end;
  377.  
  378. invariant
  379.  
  380.    indent_level >= 0;
  381.  
  382.    valid_mode(mode)
  383.  
  384. end -- FMT
  385.  
  386.  
  387.