home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_se / assertion_collector.e < prev    next >
Text File  |  1999-06-05  |  7KB  |  242 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 ASSERTION_COLLECTOR
  17.    --
  18.    -- Singleton object in charge of assertion lookup.
  19.    -- This singleton is shared via the GLOBALS.`assertion_collector' once function.
  20.    --
  21.  
  22. inherit GLOBALS;
  23.  
  24. creation make
  25.  
  26. feature {NONE}
  27.  
  28.    processing_require: BOOLEAN;
  29.          -- When processing require collection only.
  30.  
  31.    header_comment: COMMENT;
  32.  
  33.    make is
  34.       do
  35.       end;
  36.  
  37. feature {RUN_CLASS}
  38.  
  39.    invariant_start is
  40.          -- Called to start the collection of a class invariant.
  41.       do
  42.          collector.clear;
  43.          header_comment := Void;
  44.       end;
  45.  
  46.    invariant_end(ct: TYPE): CLASS_INVARIANT is
  47.          -- Called to finish the collection of `ct' class invariant.
  48.       require
  49.          ct /= Void
  50.       local
  51.          r: ARRAY[ASSERTION];
  52.       do
  53.          r := runnable(collector,ct,Void,'_');
  54.          if r /= Void then
  55.             small_eiffel.incr_magic_count;
  56.             !!Result.make_runnable(Void,r,ct,Void);
  57.             Result.set_header_comment(header_comment);
  58.          end;
  59.       end;
  60.  
  61. feature {BASE_CLASS}
  62.  
  63.    invariant_add_last(ci: CLASS_INVARIANT) is
  64.       require
  65.          ci /= Void
  66.       local
  67.          hc2: COMMENT;
  68.          bc, bc2: BASE_CLASS;
  69.       do
  70.          ci.add_into(collector);
  71.          hc2 := ci.header_comment;
  72.          if header_comment = Void then
  73.             header_comment := hc2;
  74.          elseif hc2 = Void then
  75.          else
  76.             bc := header_comment.start_position.base_class;
  77.             bc2 := hc2.start_position.base_class;
  78.             if bc2.is_subclass_of(bc) then
  79.                header_comment := hc2;
  80.             end;
  81.          end;
  82.       end;
  83.  
  84.    require_start is
  85.          -- Called to start the collection of require assertions.
  86.       do
  87.          collector.clear;
  88.          header_comment := Void;
  89.          processing_require := true;
  90.       end;
  91.  
  92.    require_end(rf: RUN_FEATURE; ct: TYPE): RUN_REQUIRE is
  93.          -- Called to finish the collection of `ct' require assertion.
  94.       require
  95.          ct = rf.current_type
  96.       local
  97.          i: INTEGER;
  98.          sp: POSITION;
  99.          bc: BASE_CLASS;
  100.          a: ASSERTION;
  101.          r, r2: ARRAY[ASSERTION];
  102.          er: E_REQUIRE;
  103.       do
  104.          processing_require := false;
  105.          r := runnable(collector,ct,rf,'R');
  106.          if r /= Void then
  107.             from
  108.                !!r2.with_capacity(r.count,1);
  109.                a := r.item(1);
  110.                r2.add_last(a);
  111.                sp := a.start_position;
  112.                bc := sp.base_class;
  113.                i := 2;
  114.             until
  115.                i > r.upper or else r.item(i).start_position.base_class /= bc
  116.             loop
  117.                r2.add_last(r.item(i));
  118.                i := i + 1;
  119.             end;
  120.             !!er.make_runnable(sp,r2,ct,rf);
  121.             !!Result.make(er);
  122.             from
  123.             until
  124.                i > r.upper
  125.             loop
  126.                from
  127.                   !!r2.with_capacity(r.count,1);
  128.                   a := r.item(i);
  129.                   r2.add_last(a);
  130.                   sp := a.start_position;
  131.                   bc := sp.base_class;
  132.                   i := i + 1;
  133.                until
  134.                   i > r.upper or else r.item(i).start_position.base_class /= bc
  135.                loop
  136.                   r2.add_last(r.item(i));
  137.                   i := i + 1;
  138.                end;
  139.                !!er.make_runnable(sp,r2,ct,rf);
  140.                Result.add(er);
  141.             end;
  142.          end;
  143.       end;
  144.  
  145.    ensure_start is
  146.          -- Called to start the collection of ensure assertions.
  147.       do
  148.          collector.clear;
  149.          header_comment := Void;
  150.       end;
  151.  
  152.    ensure_end(rf: RUN_FEATURE; ct: TYPE): E_ENSURE is
  153.          -- Called to finish the collection of `ct' ensure assertion.
  154.       require
  155.          ct = rf.current_type
  156.       local
  157.          r: ARRAY[ASSERTION];
  158.       do
  159.          r := runnable(collector,ct,rf,'E');
  160.          if r /= Void then
  161.             !!Result.make_runnable(Void,r,ct,rf);
  162.             Result.set_header_comment(header_comment);
  163.          end;
  164.       end;
  165.  
  166.    assertion_add_last(f: E_FEATURE) is
  167.          -- To add some require/ensure assertion.
  168.       local
  169.          r: E_REQUIRE;
  170.          e: E_ENSURE;
  171.       do
  172.          if processing_require then
  173.             r := f.require_assertion;
  174.             if r /= Void then
  175.                if header_comment = Void then
  176.                   header_comment := r.header_comment;
  177.                end;
  178.                r.add_into(collector);
  179.             end;
  180.          else
  181.             e := f.ensure_assertion;
  182.             if e /= Void then
  183.                if header_comment = Void then
  184.                   header_comment := e.header_comment;
  185.                end;
  186.                e.add_into(collector);
  187.             end;
  188.          end;
  189.       end;
  190.  
  191. feature {ASSERTION_LIST}
  192.  
  193.    runnable(collected: ARRAY[ASSERTION];
  194.             ct: TYPE;
  195.             for:RUN_FEATURE;
  196.             assertion_check_tag: CHARACTER): ARRAY[ASSERTION] is
  197.          -- Produce a runnable `collected'.
  198.       require
  199.          collected.lower = 1;
  200.          for /= Void implies ct = for.current_type;
  201.       local
  202.          i: INTEGER;
  203.          a: ASSERTION;
  204.       do
  205.          if not collected.empty then
  206.             from
  207.                Result := collected.twin;
  208.                i := Result.upper;
  209.             until
  210.                i = 0
  211.             loop
  212.                small_eiffel.push(for);
  213.                a := Result.item(i).to_runnable(ct,assertion_check_tag);
  214.                if a = Void then
  215.                   error(Result.item(i).start_position,fz_bad_assertion);
  216.                else
  217.                   Result.put(a,i);
  218.                end;
  219.                small_eiffel.pop;
  220.                i := i - 1;
  221.             end;
  222.          end;
  223.       end;
  224.  
  225. feature {NONE}
  226.  
  227.    collector: ARRAY[ASSERTION] is
  228.       once
  229.          !!Result.make(1,12);
  230.       end;
  231.  
  232.    singleton_memory: ASSERTION_COLLECTOR is
  233.       once
  234.          Result := Current;
  235.       end;
  236.  
  237. invariant
  238.  
  239.    is_real_singleton: Current = singleton_memory
  240.  
  241. end -- ASSERTION_COLLECTOR
  242.