| 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 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 def dfa_from_nfa(nfa): | 33 def dfa_from_nfa(nfa): |
| 34 (start_name, dfa_nodes) = nfa.compute_dfa() | 34 (start_name, dfa_nodes) = nfa.compute_dfa() |
| 35 return Dfa(start_name, dfa_nodes) | 35 return Dfa(start_name, dfa_nodes) |
| 36 | 36 |
| 37 def process_rules(parser_state): | 37 def process_rules(parser_state): |
| 38 rule_map = {} | 38 rule_map = {} |
| 39 builder = NfaBuilder() | 39 builder = NfaBuilder() |
| 40 for k, v in parser_state.rules.items(): | 40 for k, v in parser_state.rules.items(): |
| 41 graphs = [] | 41 graphs = [] |
| 42 for (graph, precedence, code, condition) in v['regex']: | 42 for (graph, action) in v['regex']: |
| 43 graphs.append(NfaBuilder.add_action(graph, (precedence, code, condition))) | 43 graphs.append(NfaBuilder.add_action(graph, action)) |
| 44 nfa = builder.nfa(NfaBuilder.or_graphs(graphs)) | 44 nfa = builder.nfa(NfaBuilder.or_graphs(graphs)) |
| 45 dfa = dfa_from_nfa(nfa) | 45 dfa = dfa_from_nfa(nfa) |
| 46 rule_map[k] = (nfa, dfa) | 46 rule_map[k] = (nfa, dfa) |
| 47 return rule_map | 47 return rule_map |
| 48 | 48 |
| 49 class ActionTestCase(unittest.TestCase): | 49 class ActionTestCase(unittest.TestCase): |
| 50 | 50 |
| 51 def __verify_last_action(self, dfa, string, expected_code, | 51 def __verify_last_action(self, dfa, string, expected_code, |
| 52 expected_condition): | 52 expected_condition): |
| 53 actions = list(dfa.collect_actions(string)) | 53 actions = list(dfa.collect_actions(string)) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 80 RuleParser.parse(rules, parser_state) | 80 RuleParser.parse(rules, parser_state) |
| 81 automata_for_conditions = process_rules(parser_state) | 81 automata_for_conditions = process_rules(parser_state) |
| 82 self.assertEqual(len(automata_for_conditions), 1) | 82 self.assertEqual(len(automata_for_conditions), 1) |
| 83 self.assertTrue('default' in automata_for_conditions) | 83 self.assertTrue('default' in automata_for_conditions) |
| 84 (nfa, dfa) = automata_for_conditions['default'] | 84 (nfa, dfa) = automata_for_conditions['default'] |
| 85 | 85 |
| 86 # The keyword is not recognized because of the rule preference order (ID | 86 # The keyword is not recognized because of the rule preference order (ID |
| 87 # is preferred over KEYWORD). | 87 # is preferred over KEYWORD). |
| 88 self.__verify_last_action(dfa, 'foo', 'ID', 'break') | 88 self.__verify_last_action(dfa, 'foo', 'ID', 'break') |
| 89 self.__verify_last_action(dfa, 'key', 'ID', 'break') | 89 self.__verify_last_action(dfa, 'key', 'ID', 'break') |
| OLD | NEW |