| OLD | NEW |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. | 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without | 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are | 3 # modification, are permitted provided that the following conditions are |
| 4 # met: | 4 # met: |
| 5 # | 5 # |
| 6 # * Redistributions of source code must retain the above copyright | 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above | 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following | 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided | 10 # disclaimer in the documentation and/or other materials provided |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 self.assertTrue(automaton.matches(string)) | 77 self.assertTrue(automaton.matches(string)) |
| 78 for string in not_matches: | 78 for string in not_matches: |
| 79 for automaton in automata: | 79 for automaton in automata: |
| 80 self.assertFalse(automaton.matches(string)) | 80 self.assertFalse(automaton.matches(string)) |
| 81 | 81 |
| 82 def test_can_construct_dot(self): | 82 def test_can_construct_dot(self): |
| 83 for (regex, matches, not_matches) in self.__test_data: | 83 for (regex, matches, not_matches) in self.__test_data: |
| 84 for automaton in build_automata(regex): | 84 for automaton in build_automata(regex): |
| 85 automaton.to_dot() | 85 automaton.to_dot() |
| 86 | 86 |
| 87 def test_actions(self): | |
| 88 left_action = simple_action("LEFT_ACTION") | |
| 89 right_action = simple_action("RIGHT_ACTION") | |
| 90 left = RegexParser.parse("left") | |
| 91 right = RegexParser.parse("right") | |
| 92 left = NfaBuilder.add_action(left, left_action) | |
| 93 right = NfaBuilder.add_action(right, right_action) | |
| 94 composite = ('ONE_OR_MORE', NfaBuilder.or_graphs([left, right])) | |
| 95 nfa = NfaBuilder().nfa(composite) | |
| 96 dfa = dfa_from_nfa(nfa) | |
| 97 def verify(string, expected): | |
| 98 actions = list(dfa.collect_actions(string)) | |
| 99 self.assertEqual(len(expected), len(actions)) | |
| 100 for i, action in enumerate(actions): | |
| 101 self.assertEqual(action, expected[i]) | |
| 102 def verify_miss(string, expected): | |
| 103 verify(string, expected + [Dfa.miss_action()]) | |
| 104 def verify_hit(string, expected): | |
| 105 verify(string, expected + [Dfa.terminal_action()]) | |
| 106 (l, r) = left_action, right_action | |
| 107 verify_hit("left", [l]) | |
| 108 verify_miss("lefta", [l]) | |
| 109 verify_hit("leftrightleftright", [l, r, l, r]) | |
| 110 verify_miss("leftrightleftrightx", [l, r, l, r]) | |
| 111 | |
| 112 def test_minimization(self): | 87 def test_minimization(self): |
| 113 def empty_node(): | 88 def empty_node(): |
| 114 return { 'transitions' : {}, 'terminal' : False, 'action' : None } | 89 return { 'transitions' : {}, 'terminal' : False, 'action' : None } |
| 115 mapping = { k : empty_node() for k in ['S_0', 'S_1', 'S_2', 'S_3'] } | 90 mapping = { k : empty_node() for k in ['S_0', 'S_1', 'S_2', 'S_3'] } |
| 116 key_a = TransitionKey.single_char('a') | 91 key_a = TransitionKey.single_char('a') |
| 117 key_b = TransitionKey.single_char('b') | 92 key_b = TransitionKey.single_char('b') |
| 118 key_c = TransitionKey.single_char('c') | 93 key_c = TransitionKey.single_char('c') |
| 119 | 94 |
| 120 mapping['S_0']['transitions'][key_a] = 'S_1' | 95 mapping['S_0']['transitions'][key_a] = 'S_1' |
| 121 mapping['S_0']['transitions'][key_b] = 'S_2' | 96 mapping['S_0']['transitions'][key_b] = 'S_2' |
| 122 mapping['S_1']['transitions'][key_c] = 'S_3' | 97 mapping['S_1']['transitions'][key_c] = 'S_3' |
| 123 mapping['S_2']['transitions'][key_c] = 'S_3' | 98 mapping['S_2']['transitions'][key_c] = 'S_3' |
| 124 mapping['S_3']['terminal'] = True | 99 mapping['S_3']['terminal'] = True |
| 125 | 100 |
| 126 mdfa = Dfa('S_0', mapping).minimize() | 101 mdfa = Dfa('S_0', mapping).minimize() |
| 127 self.assertEqual(3, mdfa.node_count()) | 102 self.assertEqual(3, mdfa.node_count()) |
| 128 | 103 |
| OLD | NEW |