| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 entry_action = None if not action else action.entry_action() | 59 entry_action = None if not action else action.entry_action() |
| 60 match_action = None if not action else action.match_action() | 60 match_action = None if not action else action.match_action() |
| 61 # generate ordered transitions | 61 # generate ordered transitions |
| 62 transitions = map(lambda (k, v) : (k, v.node_number()), | 62 transitions = map(lambda (k, v) : (k, v.node_number()), |
| 63 state.transitions().items()) | 63 state.transitions().items()) |
| 64 def cmp(left, right): | 64 def cmp(left, right): |
| 65 return TransitionKey.canonical_compare(left[0], right[0]) | 65 return TransitionKey.canonical_compare(left[0], right[0]) |
| 66 transitions = sorted(transitions, cmp) | 66 transitions = sorted(transitions, cmp) |
| 67 # map transition keys to disjoint ranges and collect stats | 67 # map transition keys to disjoint ranges and collect stats |
| 68 disjoint_keys = [] | 68 disjoint_keys = [] |
| 69 eos_transition = None | 69 unique_transitions = {} |
| 70 old_transitions = transitions | 70 old_transitions = transitions |
| 71 transitions = [] | 71 transitions = [] |
| 72 (class_keys, distinct_keys, ranges) = (0, 0, 0) | 72 (class_keys, distinct_keys, ranges) = (0, 0, 0) |
| 73 for key, transition_id in old_transitions: | 73 for key, transition_id in old_transitions: |
| 74 keys = list(key.range_iter(encoding)) | 74 keys = [] |
| 75 eos_found = False | 75 for (t, r) in key.range_iter(encoding): |
| 76 for (t, r) in keys: | |
| 77 if t == 'CLASS': | 76 if t == 'CLASS': |
| 78 class_keys += 1 | 77 class_keys += 1 |
| 78 keys.append((t, r)) |
| 79 elif t == 'PRIMARY_RANGE': | 79 elif t == 'PRIMARY_RANGE': |
| 80 distinct_keys += r[1] - r[0] + 1 | 80 distinct_keys += r[1] - r[0] + 1 |
| 81 ranges += 1 | 81 ranges += 1 |
| 82 keys.append((t, r)) |
| 82 elif t == 'UNIQUE': | 83 elif t == 'UNIQUE': |
| 83 assert r == 'eos' | 84 assert r == 'no_match' or r == 'eos' |
| 84 assert len(keys) == 1 | 85 assert r not in unique_transitions |
| 85 assert eos_transition == None | 86 unique_transitions[r] = transition_id |
| 86 eos_transition = transition_id | |
| 87 eos_found = True | |
| 88 else: | 87 else: |
| 89 raise Exception() | 88 raise Exception() |
| 90 if not eos_found: | 89 if keys: |
| 91 transitions.append((keys, transition_id)) | 90 transitions.append((keys, transition_id)) |
| 92 # eos_transitions is for a followup cl | 91 # eos_transitions is for a followup cl |
| 93 assert not eos_transition | |
| 94 return { | 92 return { |
| 95 'node_number' : None, | 93 'node_number' : None, |
| 96 'original_node_number' : state.node_number(), | 94 'original_node_number' : state.node_number(), |
| 97 'transitions' : transitions, | 95 'transitions' : transitions, |
| 98 # flags for code generator | 96 # flags for code generator |
| 99 'can_elide_read' : len(transitions) == 0, | 97 'can_elide_read' : len(transitions) == 0, |
| 100 'is_eos_handler' : False, | 98 'is_eos_handler' : False, |
| 101 'inline' : None, | 99 'inline' : None, |
| 102 # transitions for code generator | 100 # transitions for code generator |
| 103 'if_transitions' : [], | 101 'if_transitions' : [], |
| 104 'switch_transitions' : [], | 102 'switch_transitions' : [], |
| 105 'deferred_transitions' : [], | 103 'deferred_transitions' : [], |
| 106 'long_char_transitions' : [], | 104 'long_char_transitions' : [], |
| 107 'eos_transition' : eos_transition, | 105 'unique_transitions' : unique_transitions, |
| 108 # state actions | 106 # state actions |
| 109 'entry_action' : entry_action, | 107 'entry_action' : entry_action, |
| 110 'match_action' : match_action, | 108 'match_action' : match_action, |
| 111 # statistics for states | 109 # statistics for states |
| 112 'class_keys' : class_keys, | 110 'class_keys' : class_keys, |
| 113 'distinct_keys' : distinct_keys, | 111 'distinct_keys' : distinct_keys, |
| 114 'ranges' : ranges, | 112 'ranges' : ranges, |
| 115 # record of which entry points will be needed | 113 # record of which entry points will be needed |
| 116 'entry_points' : { | 114 'entry_points' : { |
| 117 'state_entry' : True, | 115 'state_entry' : True, |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 | 239 |
| 242 @staticmethod | 240 @staticmethod |
| 243 def __reorder(current_node_number, id_map, dfa_states): | 241 def __reorder(current_node_number, id_map, dfa_states): |
| 244 current_node = id_map[current_node_number] | 242 current_node = id_map[current_node_number] |
| 245 if current_node['node_number'] != None: | 243 if current_node['node_number'] != None: |
| 246 return | 244 return |
| 247 current_node['node_number'] = len(dfa_states) | 245 current_node['node_number'] = len(dfa_states) |
| 248 dfa_states.append(current_node) | 246 dfa_states.append(current_node) |
| 249 for (key, node_number) in current_node['transitions']: | 247 for (key, node_number) in current_node['transitions']: |
| 250 CodeGenerator.__reorder(node_number, id_map, dfa_states) | 248 CodeGenerator.__reorder(node_number, id_map, dfa_states) |
| 249 for node_number in current_node['unique_transitions'].values(): |
| 250 CodeGenerator.__reorder(node_number, id_map, dfa_states) |
| 251 | 251 |
| 252 def __build_dfa_states(self): | 252 def __build_dfa_states(self): |
| 253 dfa_states = [] | 253 dfa_states = [] |
| 254 self.__dfa.visit_all_states(lambda state, acc: dfa_states.append(state)) | 254 self.__dfa.visit_all_states(lambda state, acc: dfa_states.append(state)) |
| 255 encoding = self.__dfa.encoding() | 255 encoding = self.__dfa.encoding() |
| 256 f = lambda state : CodeGenerator.__transform_state(encoding, state) | 256 f = lambda state : CodeGenerator.__transform_state(encoding, state) |
| 257 dfa_states = map(f, dfa_states) | 257 dfa_states = map(f, dfa_states) |
| 258 id_map = {x['original_node_number'] : x for x in dfa_states} | 258 id_map = {x['original_node_number'] : x for x in dfa_states} |
| 259 dfa_states = [] | 259 dfa_states = [] |
| 260 CodeGenerator.__reorder(self.__start_node_number, id_map, dfa_states) | 260 CodeGenerator.__reorder(self.__start_node_number, id_map, dfa_states) |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 char_type = char_types[encoding.name()] | 339 char_type = char_types[encoding.name()] |
| 340 | 340 |
| 341 return template.render( | 341 return template.render( |
| 342 start_node_number = 0, | 342 start_node_number = 0, |
| 343 debug_print = self.__debug_print, | 343 debug_print = self.__debug_print, |
| 344 default_action = default_action, | 344 default_action = default_action, |
| 345 dfa_states = dfa_states, | 345 dfa_states = dfa_states, |
| 346 encoding = encoding.name(), | 346 encoding = encoding.name(), |
| 347 char_type = char_type, | 347 char_type = char_type, |
| 348 upper_bound = encoding.primary_range()[1]) | 348 upper_bound = encoding.primary_range()[1]) |
| OLD | NEW |