def test_add_states_and_set_initial_state(): m = StateMachine('m') s0 = StateMachine('s0') s1 = StateMachine('s1') s2 = State('s2') m.add_states(s0, s1, s2) assert all(state in m.states for state in (s0, s1, s2)) assert m.initial_state is None m.set_initial_state(s0) m.initialize() assert m.initial_state is s0
def test_add_states_and_set_initial_state(): m = StateMachine('m') s0 = StateMachine('s0') s1 = StateMachine('s1') s2 = State('s2') m.add_states(s0, s1, s2) assert all(state in m.states for state in (s0, s1, s2)) assert m.initial_state is None m.set_initial_state(s0) m.initialize() assert m.initial_state is s0
def init_sm(self): start = State('Start') identifier = State('Identifier') operator = State('Operator') number = State('Number') start_quote = State('StartQuote') string_st = State('String') end_quote = State('EndQuote') sm = StateMachine(self) sm.add_states(start, identifier, operator, number, start_quote, string_st, end_quote) sm.set_initial_state(start) # # This works also after a minor tweak in callback functions # transitions = [ # (start, start, lambda e: char(e).isspace()), # (start, identifier, # lambda e: char(e).isalpha(), self.t.StartToken), # (start, operator, # lambda e: char(e) in "=+*/-()", self.t.StartToken), # (start, number, lambda e: char(e).isdigit(), self.t.StartToken), # (start, start_quote, lambda e: char(e) == "\'"), # (start_quote, string, # lambda e: char(e) != "\'", self.t.StartToken), # (identifier, identifier, # lambda e: char(e).isalnum(), self.t.addCharacter ), # (identifier, start, # lambda e: not char(e).isalnum(), self.t.EndToken ), # (operator, start, lambda e: True, self.t.EndToken), # (number, number, # lambda e: char(e).isdigit() or char(e) == ".", # self.t.addCharacter), # (number, start, # lambda e: not char(e).isdigit() and char(e) != ".", # self.t.EndToken ), # (string, string, lambda e: char(e) != "\'", self.t.addCharacter), # (string, end_quote, lambda e: char(e) == "\'", self.t.EndToken ), # (end_quote, start, lambda e: True) # ] # for transition in transitions: # from_state = transition[0] # to_state = transition[1] # condition = transition[2] # action = transition[3] if len(transition) == 4 else None # events = ['on_char'] # sm.add_transition(from_state, to_state, events=events, # condition=condition, action=action) alnum = string.ascii_letters + string.digits not_alnum = ''.join( set(string.printable) - set(string.ascii_letters + string.digits)) not_quote = ''.join(set(string.printable) - set(["'"])) not_digit_or_dot = ''.join( set(string.printable) - set(string.digits) - set(['.'])) digits_and_dot = string.digits + '.' at = sm.add_transition at(start, start, events=string.whitespace) at(start, identifier, events=string.ascii_letters, after=self.t.StartToken) at(start, operator, events='=+*/-()', after=self.t.StartToken) at(start, number, events=string.digits, after=self.t.StartToken) at(start, start_quote, events="'", after=self.t.StartToken) at(identifier, identifier, events=alnum, action=self.t.addCharacter) at(identifier, start, events=not_alnum, action=self.t.EndToken) at(start_quote, string_st, events=not_quote, after=self.t.StartToken) at(operator, start, events=string.printable, action=self.t.EndToken) at(number, number, events=digits_and_dot, action=self.t.addCharacter) at(number, start, events=not_digit_or_dot, action=self.t.EndToken) at(string_st, string_st, events=not_quote, action=self.t.addCharacter) at(string_st, end_quote, events="'", action=self.t.EndToken) at(end_quote, start, events=string.printable) sm.initialize() self.sm = sm