def unmatched_close(self): self.close_symbol_pos = self.pos - 1 self.error_msg = "'{en}' unmatched at position {pos}." self.error_msg = self.error_msg.format(en=self.current_state, pos=self.close_symbol_pos) raise Reject()
def unmatched_open(self): self.open_symbol_pos = self.top_frame.pos - 1 self.error_msg = "'{en}' unmatched at position {pos}." self.error_msg = self.error_msg.format(en=self.top_frame.state, pos=self.open_symbol_pos) raise Reject()
def on_no_transition(self, s_name): if self.accepting: raise Accept() if self._backtrack(): return else: self.on_exhausted() raise Reject("Backtracking exhausted.")
def bad_match(self): self.open_symbol_pos = self.top_frame.pos - 1 self.close_symbol_pos = self.pos - 1 self.error_msg = "'{st}' at position {pos1} matched with '{en}' at "\ "position {pos2}." self.error_msg = self.error_msg.format(st=self.top_frame.state, pos1=self.open_symbol_pos, en=self.current_state, pos2=self.close_symbol_pos) raise Reject()
def on_no_transition(self, s_name): """ Handle the no_transition notification. The on_no_transition handler is called when a state has no acceptable transitions available. Args: s_name: is the name of the state being exited. Derived classes implementing this handler should call super().on_no_transition(). """ if self.accepting: raise Accept() msg = "Cannot transition from state {st}" raise Reject(msg.format(st=self.current_state))
def _run(self): """ Helper function for run. This is the heart of run(), but without the exception handling. _run can be called recursively, e.g. in pushdown automata. """ assert self._current_state, "Initial state not set." self._enter() while True: try: self._do_activity() self._transition() except Backtrack: # TODO: Handle this without referencing backtracking. if not self._backtrack(): self.on_exhausted() raise Reject("Backtracking exhausted.")