home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ppcsmalleiffel / lib_se / error_handler.e < prev    next >
Encoding:
Text File  |  1998-01-16  |  5.9 KB  |  272 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 ERROR_HANDLER
  17.    --
  18.    -- The unique `eh' object for Warning, Error and Fatal Error
  19.    -- handling.
  20.    -- This handler use an assynchronous strategy.
  21.    --
  22.    
  23. inherit
  24.    GLOBALS 
  25.       rename warning as global_warning, error as global_error
  26.      fatal_error as global_fatal_errror
  27.       redefine nb_errors, nb_warnings 
  28.       end;
  29.    
  30. creation make
  31.    
  32. feature 
  33.    
  34.    nb_errors, nb_warnings: INTEGER;
  35.      -- Global counters.
  36.  
  37.    no_warning: BOOLEAN;
  38.  
  39. feature {NONE}
  40.    
  41.    explanation: STRING is 
  42.      -- Current `explanation' text to be print with next Warning, 
  43.      -- the next Error or the next Fatal Error.
  44.       once
  45.      !!Result.make(1024);
  46.       end;
  47.    
  48.    positions: ARRAY[POSITION] is
  49.      -- Void or the list of `positions' to be show with next Warning, 
  50.      -- the next Error or the next Fatal Error.
  51.       once
  52.      !!Result.with_capacity(5,1);
  53.       end;
  54.  
  55. feature {NONE}
  56.    
  57.    make is
  58.       do
  59.       end;
  60.    
  61. feature 
  62.    
  63.    empty: BOOLEAN is
  64.      -- True when nothing stored in `explanation' and `positions'.
  65.       do
  66.      Result := explanation.empty and then positions.empty;
  67.       end;
  68.  
  69.    set_no_warning is
  70.       do
  71.      no_warning := true;
  72.       end;
  73.  
  74. feature 
  75.    
  76.    append(s: STRING) is
  77.       -- Append text `s' to the current `explanation'.
  78.       require
  79.      s /= Void;
  80.      not s.empty;
  81.       do
  82.      explanation.append(s);
  83.       ensure
  84.      not empty
  85.       end;
  86.    
  87.    extend(c: CHARACTER) is
  88.       -- Append `c' to the current `explanation'.
  89.       do
  90.      explanation.extend(c);
  91.       ensure
  92.      not empty
  93.       end;
  94.    
  95.    add_position(p: POSITION) is
  96.       -- If necessary, add `p' to the already known `positions'.
  97.       do
  98.      if p /= Void then
  99.         if not positions.has(p) then
  100.            positions.add_last(p);
  101.         end;
  102.      end;
  103.       end;
  104.    
  105.    add_type(t: TYPE; tail: STRING) is
  106.       require
  107.      t /= Void
  108.       do
  109.      append("Type ");
  110.      if t.is_run_type then
  111.         append(t.run_time_mark);
  112.      else
  113.         append(t.written_mark);
  114.      end;
  115.      append(tail);
  116.      add_position(t.start_position);
  117.       end;
  118.    
  119.    print_as_warning is
  120.      -- Print `explanation' as a Warning report.
  121.      -- After printing, `explanation' and `positions' are reset.
  122.       require
  123.      not empty
  124.       do
  125.      if no_warning then
  126.         cancel;
  127.      else 
  128.         do_print("Warning");
  129.         incr_nb_warnings;
  130.      end;
  131.       ensure
  132.      not no_warning implies (nb_warnings = old nb_warnings + 1);
  133.       end;
  134.    
  135.    print_as_error is
  136.      -- Print `explanation' as an Error report.
  137.      -- After printing, `explanation' and `positions' are reset.
  138.       require
  139.      not empty
  140.       do
  141.      do_print("Error");
  142.      incr_nb_errors;
  143.       ensure
  144.      nb_errors = old nb_errors + 1;
  145.       end;
  146.    
  147.    print_as_fatal_error is
  148.      -- Print `explanation' as a Fatal Error.
  149.      -- Execution is stopped after this.
  150.       do
  151.      do_print("Fatal Error");
  152.      die_with_code(exit_failure_code);
  153.       end;
  154.    
  155.    warning(tail: STRING) is
  156.      -- Append the `tail' of the `explanation' an then `print_as_warning'.
  157.       require
  158.      not tail.empty
  159.       do
  160.      append(tail);
  161.      print_as_warning;
  162.       ensure
  163.      empty
  164.       end;
  165.    
  166.    error(tail: STRING) is
  167.      -- Append the `tail' of the `explanation' an then `print_as_error'.
  168.       require
  169.      not tail.empty
  170.       do
  171.      append(tail);
  172.      print_as_error;
  173.       ensure
  174.      empty
  175.       end;
  176.    
  177.    fatal_error(tail: STRING) is
  178.      -- Append the `tail' of the `explanation' an then 
  179.      -- `print_as_fatal_error'.
  180.       require
  181.      not tail.empty
  182.       do
  183.      explanation.append(tail);
  184.      print_as_fatal_error;
  185.       end;
  186.    
  187.    cancel is
  188.       -- Cancel a prepared report without printing it.
  189.       do
  190.      explanation.clear;
  191.      positions.clear;
  192.       ensure
  193.      empty
  194.       end;
  195.    
  196.    incr_nb_errors is
  197.       do
  198.      nb_errors := nb_errors + 1;
  199.      if nb_errors >= 6 then
  200.         std_error.put_string(fz_error_stars);
  201.         std_error.put_string("Too many errors.%N");
  202.         die_with_code(exit_failure_code);
  203.      end;
  204.       end;
  205.    
  206.    incr_nb_warnings is
  207.       do
  208.      nb_warnings := nb_warnings + 1;
  209.       end;
  210.    
  211. feature {NONE}
  212.    
  213.    do_print(heading: STRING) is
  214.       local
  215.      i, cpt: INTEGER;
  216.      cc, previous_cc: CHARACTER;
  217.       do
  218.      std_error.put_string(fz_error_stars);
  219.      std_error.put_string(heading);
  220.      std_error.put_string(" : ");
  221.      from  
  222.         i := 1;
  223.         cpt := 9 + heading.count;
  224.      until 
  225.         i > explanation.count
  226.      loop
  227.         previous_cc := cc;
  228.         cc := explanation.item(i);
  229.         i := i + 1;
  230.         if cpt > 60 then
  231.            if cc = ' ' then
  232.           std_error.put_character('%N');
  233.           cpt := 0;
  234.            elseif previous_cc = ',' or else 
  235.           previous_cc = '/' 
  236.         then
  237.           std_error.put_character('%N');
  238.           std_error.put_character(cc);
  239.           cpt := 1;
  240.            else
  241.           std_error.put_character(cc);
  242.           cpt := cpt + 1;
  243.            end;
  244.         else
  245.            std_error.put_character(cc);
  246.            inspect
  247.           cc
  248.            when '%N' then
  249.           cpt := 0;
  250.            else
  251.           cpt := cpt + 1;
  252.            end;
  253.         end;
  254.      end;
  255.      std_error.put_character('%N');
  256.      from  
  257.         i := positions.lower;
  258.      until
  259.         i > positions.upper
  260.      loop
  261.         positions.item(i).show;
  262.         i := i + 1;
  263.      end;
  264.      cancel;
  265.      std_error.put_string("------%N");
  266.       ensure
  267.      empty
  268.       end;
  269.  
  270. end -- ERROR_HANDLER
  271.  
  272.