PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/test/lib/automaton/aux_automaton_01.e

http://github.com/tybor/Liberty
Specman e | 133 lines | 112 code | 19 blank | 2 comment | 3 complexity | f139721673994611cd572e2f8ffcb2ac MD5 | raw file
  1class AUX_AUTOMATON_01
  2
  3create {TEST_AUTOMATON_01}
  4   make
  5
  6feature {TEST_AUTOMATON_01}
  7   run
  8      do
  9         automaton.run(once "start", context);
 10      end
 11
 12   is_number: BOOLEAN
 13      do
 14         Result := context.success
 15      end
 16
 17   found_number: FIXED_STRING
 18      require
 19         is_number
 20      do
 21         Result := context.output
 22      end
 23
 24feature {}
 25   make (a_input: ABSTRACT_STRING)
 26      require
 27         a_input /= Void
 28      do
 29         create context.make(a_input)
 30      end
 31
 32   context: AUX_AUTOMATON_01_CONTEXT
 33
 34feature {} -- automaton implementation
 35
 36   -- Note: since the automaton is a once function, no agent may rely on Current.
 37   -- That's why the automaton allows us to pass some context around.
 38
 39   read_next (ctx: like context)
 40      do
 41         ctx.next
 42         debug("test/aux_automaton_01")
 43            if ctx.is_off then
 44               std_output.put_line("No more characters")
 45            else
 46               std_output.put_line("Read character '" + ctx.current_character.out + "'")
 47            end
 48         end
 49      end
 50
 51   on_read_figure (ctx: like context): BOOLEAN
 52      do
 53         if not ctx.is_off then
 54            Result := ctx.current_character.is_digit
 55         end
 56      end
 57
 58   on_read_dot (ctx: like context): BOOLEAN
 59      do
 60         if not ctx.is_off then
 61            Result := ctx.current_character = '.'
 62         end
 63      end
 64
 65   on_end_of_text (ctx: like context): BOOLEAN
 66      do
 67         Result := ctx.is_off
 68      end
 69
 70   otherwise: BOOLEAN
 71      do
 72         Result := True
 73      end
 74
 75   transition (next: STRING; ctx: like context): STRING
 76      do
 77         ctx.extend
 78         Result := next
 79      end
 80
 81   transition_end (next: STRING; ctx: like context): STRING
 82      do
 83         inspect next
 84         when "success" then
 85            ctx.set_success(True)
 86         when "error" then
 87            ctx.set_success(False)
 88         end
 89         Result := next
 90      end
 91
 92   finished: STRING
 93      do
 94         check Result = Void end
 95      end
 96
 97   automaton: AUTOMATON[AUX_AUTOMATON_01_CONTEXT]
 98      once
 99         Result := {AUTOMATON[AUX_AUTOMATON_01_CONTEXT] <<
100                                                          "start", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
101                                                                                                      agent on_read_figure(?), agent transition("integral", ?);
102                                                                                                      agent on_read_dot(?), agent transition("after_dot", ?);
103                                                                                                      agent otherwise, agent transition_end("error", ?)
104                                                                                                      >>};
105                                                          "integral", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
106                                                                                                         agent on_read_figure(?), agent transition("integral", ?);
107                                                                                                         agent on_read_dot(?), agent transition("after_dot", ?);
108                                                                                                         agent on_end_of_text(?), agent transition_end("success", ?);
109                                                                                                         agent otherwise, agent transition_end("error", ?)
110                                                                                                         >>};
111                                                          "after_dot", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
112                                                                                                          agent on_read_figure(?), agent transition("fractional", ?);
113                                                                                                          agent otherwise, agent transition_end("error", ?)
114                                                                                                          >>};
115                                                          "fractional", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
116                                                                                                           agent on_read_figure(?), agent transition("fractional", ?);
117                                                                                                           agent on_end_of_text(?), agent transition_end("success", ?);
118                                                                                                           agent otherwise, agent transition_end("error", ?)
119                                                                                                           >>};
120                                                          "success", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
121                                                                                                        agent otherwise, agent finished
122                                                                                                        >>};
123                                                          "error", {STATE[AUX_AUTOMATON_01_CONTEXT] <<
124                                                                                                      agent otherwise, agent finished
125                                                                                                      >>}
126                                                          >>}
127         Result.set_before_guards(agent read_next(?))
128      end
129
130invariant
131   context /= Void
132
133end