def test_distinctArcs(): from nfa import NFAState state1 = NFAState() state2 = NFAState() state3 = NFAState() assert distinctArcs({}) == {} assert distinctArcs({CharacterSet.excluding(''): [state1]}) == {CharacterSet.excluding(''): [state1]} assert distinctArcs({CharacterSet.including('abc'): [state1], CharacterSet.including('def'): [state2]}) == \ {CharacterSet.including('abc'): set([state1]), CharacterSet.including('def'): set([state2])} assert distinctArcs({CharacterSet.excluding(''): [state1], CharacterSet.including('abc'): [state2]}) == \ {CharacterSet.excluding('abc'): set([state1]), CharacterSet.including('abc'): set([state1,state2])}
def fnmatch(klass, s): '''create an NFA state machine representing the fnmatch pattern @s''' nfa = NFA(NFAState()) state = nfa.initial new_state = None chars = list(s) while True: if len(chars) == 0: break # end-of-string c = chars.pop(0) new_state = NFAState() nfa.states.append(new_state) if c == '?': # single-character wildcard state.add(CharacterSet.excluding(''), new_state) elif c == '*': # multi-character wildcard state.add(CharacterSet.excluding(''), new_state) new_state.add(CharacterSet.excluding(''), new_state) elif c == '\\': # treat the next character literally if len(chars) == 0: raise 'escape at end of string' c = chars.pop(0) state.add(CharacterSet.including(c), new_state) elif c == '[': # bracket expression try: c = chars.pop(0) if c == '!': # inverted inverted = True c = chars.pop(0) else: inverted = False charset = CharacterSet.including('') last_char = None while c != ']': if c == '-' and last_char: c = chars.pop(0) charset = charset.union(CharacterSet.range(last_char, c)) last_char = None c = chars.pop(0) continue charset = charset.union(CharacterSet.including(c)) last_char = c # save last character c = sio.read(1) except IndexError, e: raise 'unterminated bracket expression' if inverted: state.add(CharacterSet.excluding('') - charset, new_state) else: state.add(charset, new_state) else: state.add(CharacterSet.including(c), new_state) state = new_state