| 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 |
| 11 # with the distribution. | 11 # with the distribution. |
| 12 # * Neither the name of Google Inc. nor the names of its | 12 # * Neither the name of Google Inc. nor the names of its |
| 13 # contributors may be used to endorse or promote products derived | 13 # contributors may be used to endorse or promote products derived |
| 14 # from this software without specific prior written permission. | 14 # from this software without specific prior written permission. |
| 15 # | 15 # |
| 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 import unittest | 28 import unittest |
| 29 from rule_parser import RuleParser, RuleParserState | 29 from automaton import Action |
| 30 from rule_parser import RuleProcessor |
| 30 from nfa_builder import NfaBuilder | 31 from nfa_builder import NfaBuilder |
| 31 from dfa import Dfa | 32 from dfa import Dfa |
| 32 | 33 |
| 33 def dfa_from_nfa(nfa): | 34 def process_rules(rules): |
| 34 (start_name, dfa_nodes) = nfa.compute_dfa() | |
| 35 return Dfa(start_name, dfa_nodes) | |
| 36 | |
| 37 def process_rules(parser_state): | |
| 38 rule_map = {} | 35 rule_map = {} |
| 39 builder = NfaBuilder() | 36 for name, automata in RuleProcessor.parse(rules).automata_iter(): |
| 40 for k, v in parser_state.rules.items(): | 37 rule_map[name] = automata |
| 41 graphs = [] | |
| 42 for (graph, action) in v['regex']: | |
| 43 graphs.append(NfaBuilder.add_action(graph, action)) | |
| 44 nfa = builder.nfa(NfaBuilder.or_graphs(graphs)) | |
| 45 dfa = dfa_from_nfa(nfa) | |
| 46 rule_map[k] = (nfa, dfa) | |
| 47 return rule_map | 38 return rule_map |
| 48 | 39 |
| 49 class ActionTestCase(unittest.TestCase): | 40 class ActionTestCase(unittest.TestCase): |
| 50 | 41 |
| 51 def __verify_last_action(self, dfa, string, expected_code, | 42 def __verify_last_action(self, dfa, string, expected_code): |
| 52 expected_condition): | |
| 53 actions = list(dfa.collect_actions(string)) | 43 actions = list(dfa.collect_actions(string)) |
| 54 self.assertEqual(actions[-1], ('TERMINATE',)) | 44 self.assertEqual(actions[-1], Action('TERMINATE')) |
| 55 self.assertEqual(actions[-2][1], expected_code) | 45 self.assertEqual(actions[-2].data(), expected_code) |
| 56 self.assertEqual(actions[-2][2], expected_condition) | |
| 57 | 46 |
| 58 def test_action_precedence(self): | 47 def test_action_precedence(self): |
| 59 parser_state = RuleParserState() | |
| 60 rules = '''<default> | 48 rules = '''<default> |
| 61 "key" { KEYWORD } <<break>> | 49 "key" { KEYWORD } <<break>> |
| 62 /[a-z]+/ { ID } <<break>>''' | 50 /[a-z]+/ { ID } <<break>>''' |
| 63 RuleParser.parse(rules, parser_state) | 51 automata_for_conditions = process_rules(rules) |
| 64 automata_for_conditions = process_rules(parser_state) | |
| 65 self.assertEqual(len(automata_for_conditions), 1) | 52 self.assertEqual(len(automata_for_conditions), 1) |
| 66 self.assertTrue('default' in automata_for_conditions) | 53 self.assertTrue('default' in automata_for_conditions) |
| 67 (nfa, dfa) = automata_for_conditions['default'] | 54 (nfa, dfa) = automata_for_conditions['default'] |
| 68 | 55 |
| 69 self.__verify_last_action(dfa, 'foo', 'ID', 'break') | 56 self.__verify_last_action(dfa, 'foo', 'ID') |
| 70 self.__verify_last_action(dfa, 'key', 'KEYWORD', 'break') | 57 self.__verify_last_action(dfa, 'key', 'KEYWORD') |
| 71 self.__verify_last_action(dfa, 'k', 'ID', 'break') | 58 self.__verify_last_action(dfa, 'k', 'ID') |
| 72 self.__verify_last_action(dfa, 'ke', 'ID', 'break') | 59 self.__verify_last_action(dfa, 'ke', 'ID') |
| 73 self.__verify_last_action(dfa, 'keys', 'ID', 'break') | 60 self.__verify_last_action(dfa, 'keys', 'ID') |
| 74 | 61 |
| 75 def test_wrong_action_precedence(self): | 62 def test_wrong_action_precedence(self): |
| 76 parser_state = RuleParserState() | |
| 77 rules = '''<default> | 63 rules = '''<default> |
| 78 /[a-z]+/ { ID } <<break>> | 64 /[a-z]+/ { ID } <<break>> |
| 79 "key" { KEYWORD } <<break>>''' | 65 "key" { KEYWORD } <<break>>''' |
| 80 RuleParser.parse(rules, parser_state) | 66 automata_for_conditions = process_rules(rules) |
| 81 automata_for_conditions = process_rules(parser_state) | |
| 82 self.assertEqual(len(automata_for_conditions), 1) | 67 self.assertEqual(len(automata_for_conditions), 1) |
| 83 self.assertTrue('default' in automata_for_conditions) | 68 self.assertTrue('default' in automata_for_conditions) |
| 84 (nfa, dfa) = automata_for_conditions['default'] | 69 (nfa, dfa) = automata_for_conditions['default'] |
| 85 | 70 |
| 86 # The keyword is not recognized because of the rule preference order (ID | 71 # The keyword is not recognized because of the rule preference order (ID |
| 87 # is preferred over KEYWORD). | 72 # is preferred over KEYWORD). |
| 88 self.__verify_last_action(dfa, 'foo', 'ID', 'break') | 73 self.__verify_last_action(dfa, 'foo', 'ID') |
| 89 self.__verify_last_action(dfa, 'key', 'ID', 'break') | 74 self.__verify_last_action(dfa, 'key', 'ID') |
| OLD | NEW |