home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_se / error_handler.e < prev    next >
Text File  |  1999-06-05  |  6KB  |  241 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 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 GLOBALS;
  24.  
  25. creation make
  26.  
  27. feature
  28.  
  29.    error_counter, warning_counter: INTEGER;
  30.          -- Global counters.
  31.  
  32.    no_warning: BOOLEAN;
  33.  
  34. feature {NONE}
  35.  
  36.    explanation: STRING is
  37.          -- Current `explanation' text to be print with next Warning,
  38.          -- the next Error or the next Fatal Error.
  39.       once
  40.          !!Result.make(1024);
  41.       end;
  42.  
  43.    positions: FIXED_ARRAY[POSITION] is
  44.          -- Void or the list of `positions' to be show with next Warning,
  45.          -- the next Error or the next Fatal Error.
  46.       once
  47.          !!Result.with_capacity(8);
  48.       end;
  49.  
  50. feature {NONE}
  51.  
  52.    make is
  53.       do
  54.       end;
  55.  
  56. feature
  57.  
  58.    empty: BOOLEAN is
  59.          -- True when nothing stored in `explanation' and `positions'.
  60.       do
  61.          Result := explanation.empty and then positions.empty;
  62.       end;
  63.  
  64.    set_no_warning is
  65.       do
  66.          no_warning := true;
  67.       end;
  68.  
  69. feature
  70.  
  71.    append(s: STRING) is
  72.       -- Append text `s' to the current `explanation'.
  73.       require
  74.          not s.empty
  75.       do
  76.          explanation.append(s);
  77.       ensure
  78.          not empty
  79.       end;
  80.  
  81.    extend(c: CHARACTER) is
  82.       -- Append `c' to the current `explanation'.
  83.       do
  84.          explanation.extend(c);
  85.       ensure
  86.          not empty
  87.       end;
  88.  
  89.    add_position(p: POSITION) is
  90.       -- If necessary, add `p' to the already known `positions'.
  91.       do
  92.          if p /= Void then
  93.             if not positions.has(p) then
  94.                positions.add_last(p);
  95.             end;
  96.          end;
  97.       end;
  98.  
  99.    add_type(t: TYPE; tail: STRING) is
  100.       require
  101.          t /= Void
  102.       do
  103.          append("Type ");
  104.          if t.is_run_type then
  105.             append(t.run_time_mark);
  106.          else
  107.             append(t.written_mark);
  108.          end;
  109.          append(tail);
  110.          add_position(t.start_position);
  111.       end;
  112.  
  113.    feature_not_found(fn: FEATURE_NAME) is
  114.       require
  115.          fn /= Void
  116.       do
  117.          add_position(fn.start_position);
  118.          append(fz_09);
  119.          append(fn.to_string);
  120.          append(fz_not_found);
  121.       end;
  122.  
  123.    add_feature_name(fn: FEATURE_NAME) is
  124.       require
  125.          fn /= Void
  126.       do
  127.          append(fn.to_string);
  128.          append(" : ");
  129.          add_position(fn.start_position);
  130.       end;
  131.  
  132.    print_as_warning is
  133.          -- Print `explanation' as a Warning report.
  134.          -- After printing, `explanation' and `positions' are reset.
  135.       require
  136.          not empty
  137.       do
  138.          if no_warning then
  139.             cancel;
  140.          else
  141.             do_print("Warning");
  142.             warning_counter := warning_counter + 1;
  143.          end;
  144.       ensure
  145.          not no_warning implies (warning_counter = old warning_counter + 1);
  146.       end;
  147.  
  148.    print_as_error is
  149.          -- Print `explanation' as an Error report.
  150.          -- After printing, `explanation' and `positions' are reset.
  151.       require
  152.          not empty
  153.       do
  154.          do_print("Error");
  155.          error_counter := error_counter + 1;
  156.          if error_counter >= 6 then
  157.             echo.w_put_string(fz_error_stars);
  158.             echo.w_put_string("Too many errors.%N");
  159.             die_with_code(exit_failure_code);
  160.          end;
  161.       ensure
  162.          error_counter = old error_counter + 1;
  163.       end;
  164.  
  165.    print_as_fatal_error is
  166.          -- Print `explanation' as a Fatal Error.
  167.          -- Execution is stopped after this call.
  168.       do
  169.          do_print("Fatal Error");
  170.          die_with_code(exit_failure_code);
  171.       end;
  172.  
  173.    cancel is
  174.       -- Cancel a prepared report without printing it.
  175.       do
  176.          explanation.clear;
  177.          positions.clear;
  178.       ensure
  179.          empty
  180.       end;
  181.  
  182. feature {NONE}
  183.  
  184.    do_print(heading: STRING) is
  185.       local
  186.          i, cpt: INTEGER;
  187.          cc, previous_cc: CHARACTER;
  188.       do
  189.          echo.w_put_string(fz_error_stars);
  190.          echo.w_put_string(heading);
  191.          echo.w_put_string(" : ");
  192.          from
  193.             i := 1;
  194.             cpt := 9 + heading.count;
  195.          until
  196.             i > explanation.count
  197.          loop
  198.             previous_cc := cc;
  199.             cc := explanation.item(i);
  200.             i := i + 1;
  201.             if cpt > 60 then
  202.                if cc = ' ' then
  203.                   echo.w_put_character('%N');
  204.                   cpt := 0;
  205.                elseif previous_cc = ',' then
  206.                   echo.w_put_character('%N');
  207.                   echo.w_put_character(cc);
  208.                   cpt := 1;
  209.                else
  210.                   echo.w_put_character(cc);
  211.                   cpt := cpt + 1;
  212.                end;
  213.             else
  214.                echo.w_put_character(cc);
  215.                inspect
  216.                   cc
  217.                when '%N' then
  218.                   cpt := 0;
  219.                else
  220.                   cpt := cpt + 1;
  221.                end;
  222.             end;
  223.          end;
  224.          echo.w_put_character('%N');
  225.          from
  226.             i := positions.lower;
  227.          until
  228.             i > positions.upper
  229.          loop
  230.             positions.item(i).show;
  231.             i := i + 1;
  232.          end;
  233.          cancel;
  234.          echo.w_put_string("------%N");
  235.       ensure
  236.          empty
  237.       end;
  238.  
  239. end -- ERROR_HANDLER
  240.  
  241.