home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ppcsmalleiffel / lib_se / fmt.e < prev    next >
Encoding:
Text File  |  1998-01-16  |  7.2 KB  |  398 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://www.loria.fr/SmallEiffel
  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. creation make
  21.    
  22. feature {NONE}
  23.    
  24.    sfw: STD_FILE_WRITE; 
  25.      -- Where printing is done.
  26.    
  27. feature 
  28.  
  29.    mode: INTEGER;
  30.      -- Internal code to memorize the mode : "-zen|-default|-end|-parano"
  31.  
  32. feature {NONE}
  33.    
  34.    C_default: INTEGER is 0;
  35.    C_zen: INTEGER is 1;
  36.    C_end: INTEGER is 2;
  37.    C_parano: INTEGER is 3;
  38.    
  39. feature
  40.  
  41.    valid_mode(m: INTEGER): BOOLEAN is
  42.      -- Is the mode previously obtained using `mode' ?
  43.       do
  44.      inspect
  45.         m
  46.      when C_zen, C_default, C_end, C_parano then
  47.         Result := true;
  48.      else
  49.      end;
  50.       end;
  51.  
  52.    set_mode(m: INTEGER) is
  53.      -- Where `m' is a valid mode previously obtained using `mode'.
  54.       require
  55.      valid_mode(m)
  56.       do
  57.      mode := m;
  58.       ensure
  59.      mode = m
  60.       end;
  61.    
  62. feature
  63.    
  64.    column: INTEGER;
  65.      -- Current column in output file. Left most
  66.      -- column is number 1;
  67.    
  68.    line: INTEGER;
  69.      -- Current column in output file.
  70.    
  71.    blank_lines: INTEGER;
  72.      -- Number of blank lines at current position.
  73.    
  74.    
  75.    last_character: CHARACTER;
  76.      -- Last printed one.
  77.    
  78.    indent_level: INTEGER;
  79.      -- Current `indent_level'.
  80.    
  81.    semi_colon_flag: BOOLEAN;
  82.      -- When Current instruction must add a following semi_colon.
  83.  
  84. feature {NONE}
  85.    
  86.    indent_increment: INTEGER is 3; 
  87.      -- Basic standard increment.
  88.    
  89. feature 
  90.    
  91.    make is
  92.       do
  93.       end;
  94.    
  95. feature
  96.  
  97.    set_semi_colon_flag(v: like semi_colon_flag) is
  98.       do
  99.      semi_colon_flag := v;
  100.       end;
  101.    
  102.    set_indent_level(il: INTEGER) is
  103.       require 
  104.      il >= 0;
  105.       do
  106.      indent_level := il;
  107.       ensure
  108.      indent_level = il;
  109.       end;
  110.  
  111.    level_incr is
  112.       do
  113.      indent_level := indent_level + 1;
  114.       end;
  115.    
  116.    level_decr is
  117.       require      
  118.      indent_level > 0;
  119.       do
  120.      indent_level := indent_level - 1;
  121.       end;
  122.    
  123. feature -- Initial mode setting :
  124.  
  125.    set_zen is
  126.      -- The less you can print, no Current when not necessary, 
  127.      -- no end of constructs, no ends of routine and 
  128.      -- compact printing.
  129.       do
  130.      mode := C_zen;
  131.       end;
  132.  
  133.    set_default is
  134.      -- Default pretty printing mode.
  135.       do
  136.      mode := C_default;
  137.       end;
  138.  
  139.    set_end is
  140.      -- Print ends of all constructs.
  141.       do
  142.      mode := C_end;
  143.       end;
  144.    
  145.    set_parano is
  146.      -- The more you can print (parano mode).
  147.       do
  148.      mode := C_parano;
  149.       end;
  150.    
  151. feature {ANY} -- Printing Features :
  152.    
  153.    put_end(what: STRING) is
  154.       do
  155.      put_string("-- ");
  156.      put_string(what);
  157.      put_character('%N');
  158.       end;
  159.    
  160.    keyword(k: STRING) is
  161.      -- Print keyword `k'.
  162.      -- If needed, a space is added before `k'.
  163.      -- Always add a ' ' after `k'.
  164.       require
  165.      not k.has('%N');
  166.      not k.has('%T');
  167.       do
  168.      inspect 
  169.         last_character
  170.      when ' ','%N','%U' then
  171.      else
  172.         put_character(' ');
  173.      end;
  174.      put_string(k);
  175.      if last_character /= ' ' then
  176.         put_character(' ');
  177.      end;
  178.       ensure
  179.      last_character = ' ';
  180.       end;
  181.    
  182.    skip(line_count: INTEGER) is
  183.       -- Add if needed `line_count' blanks lines and do `indent'.
  184.       require
  185.      line_count >= 0;
  186.       do
  187.      from  
  188.      until
  189.         blank_lines >= line_count
  190.      loop
  191.         put_character('%N');
  192.      end;
  193.      indent;
  194.       ensure 
  195.      blank_lines >= line_count;
  196.       end;
  197.    
  198.    put_integer(i: INTEGER) is
  199.       -- Print `i' using `put_string'.
  200.       do
  201.      tmp_string.clear;
  202.      i.append_in(tmp_string);
  203.      put_string(tmp_string);
  204.       end;
  205.    
  206.    put_string(s: STRING) is
  207.       require
  208.      s /= Void;
  209.       local
  210.      i: INTEGER;
  211.       do
  212.      from  
  213.         i := 1;
  214.      until
  215.         i > s.count
  216.      loop
  217.         put_character(s.item(i));
  218.         i := i + 1;
  219.      end;
  220.       end;
  221.    
  222.    indent is
  223.      -- Go if needed to the `column' according to
  224.      -- the current `indent_level'.
  225.      -- Ensure that the last printed character is ' ' or '%N';
  226.       local
  227.      goal: INTEGER;
  228.       do
  229.      goal := 1 + indent_level * indent_increment;
  230.      if column > goal then
  231.         put_character('%N');
  232.      end;
  233.      from  
  234.      until
  235.         goal = column
  236.      loop
  237.         put_character(' ');
  238.      end;
  239.      inspect 
  240.         last_character
  241.      when ' ','%N' then
  242.      else
  243.         put_character('%N');
  244.         indent;
  245.      end;
  246.       ensure
  247.      column = indent_level * indent_increment + 1;
  248.      last_character = ' ' or last_character = '%N';
  249.       end;
  250.       
  251.    put_character(c: CHARACTER) is
  252.       do
  253.      sfw.put_character(c);
  254.      last_character := c;
  255.      inspect 
  256.         c
  257.      when '%N' then
  258.         line := line + 1;
  259.         column := 1;
  260.         blank_lines := blank_lines + 1;
  261.      when ' ','%T' then
  262.         column := column + 1;
  263.      else
  264.         column := column + 1;
  265.         blank_lines := -1;
  266.      end;
  267.       end;
  268.    
  269. feature {PRETTY}
  270.    
  271.    connect_to(s: like sfw) is
  272.       require
  273.      s /= Void;
  274.       do
  275.      sfw := s;
  276.      line := 1;
  277.      column := 1;
  278.      blank_lines := 0;
  279.      last_character := '%U';
  280.       ensure
  281.      sfw = s;
  282.       end;
  283.    
  284. feature {NONE}
  285.    
  286.    tmp_string: STRING is
  287.       once
  288.      !!Result.blank(256);
  289.       end;
  290.  
  291. feature -- Computed flags :
  292.  
  293.    zen_mode: BOOLEAN is
  294.       do
  295.      Result := mode = C_zen;
  296.       end;
  297.  
  298.    print_end_check: BOOLEAN is
  299.       do
  300.      inspect
  301.         mode
  302.      when C_zen then
  303.      when C_default then
  304.      when C_end then
  305.         Result := true;
  306.      when C_parano then
  307.         Result := true;
  308.      end;
  309.       end;
  310.  
  311.    print_end_loop: BOOLEAN is
  312.       do
  313.      inspect
  314.         mode
  315.      when C_zen then
  316.      when C_default then
  317.      when C_end then
  318.         Result := true;
  319.      when C_parano then
  320.         Result := true;
  321.      end;
  322.       end;
  323.  
  324.    print_end_if: BOOLEAN is
  325.       do
  326.      inspect
  327.         mode
  328.      when C_zen then
  329.      when C_default then
  330.      when C_end then
  331.         Result := true;
  332.      when C_parano then
  333.         Result := true;
  334.      end;
  335.       end;
  336.  
  337.    print_end_inspect: BOOLEAN is
  338.       do
  339.      inspect
  340.         mode
  341.      when C_zen then
  342.      when C_default then
  343.      when C_end then
  344.         Result := true;
  345.      when C_parano then
  346.         Result := true;
  347.      end;
  348.       end;
  349.  
  350.    print_end_debug: BOOLEAN is
  351.       do
  352.      inspect
  353.         mode
  354.      when C_zen then
  355.      when C_default then
  356.      when C_end then
  357.         Result := true;
  358.      when C_parano then
  359.         Result := true;
  360.      end;
  361.       end;
  362.  
  363.    print_end_routine: BOOLEAN is
  364.       do
  365.      inspect
  366.         mode
  367.      when C_zen then
  368.      when C_default then
  369.         Result := true;
  370.      when C_end then
  371.         Result := true;
  372.      when C_parano then
  373.         Result := true;
  374.      end;
  375.       end;
  376.  
  377.    print_current: BOOLEAN is
  378.       do
  379.      inspect
  380.         mode
  381.      when C_zen then
  382.      when C_default then
  383.      when C_end then
  384.      when C_parano then
  385.         Result := true;
  386.      end;
  387.       end;
  388.  
  389. invariant
  390.    
  391.    indent_level >= 0;
  392.  
  393.    valid_mode(mode)
  394.  
  395. end -- FMT
  396.  
  397.  
  398.