| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 | 49 |
| 50 class Dfa: | 50 class Dfa: |
| 51 | 51 |
| 52 def __init__(self, start_name, mapping, end_names): | 52 def __init__(self, start_name, mapping, end_names): |
| 53 name_map = {} | 53 name_map = {} |
| 54 offset = 0 | 54 offset = 0 |
| 55 self.__terminal_set = set() | 55 self.__terminal_set = set() |
| 56 for name in mapping.keys(): | 56 for name in mapping.keys(): |
| 57 dfa_state = DfaState(name, offset) | 57 dfa_state = DfaState(name, offset) |
| 58 name_map[name] = dfa_state | 58 name_map[name] = dfa_state |
| 59 offset = offset + 1 | 59 offset += 1 |
| 60 if name in end_names: | 60 if name in end_names: |
| 61 self.__terminal_set.add(dfa_state) | 61 self.__terminal_set.add(dfa_state) |
| 62 for name, values in mapping.items(): | 62 for name, values in mapping.items(): |
| 63 for key, value in values.items(): | 63 for key, value in values.items(): |
| 64 name_map[name].add_transition(key, name_map[value]) | 64 name_map[name].add_transition(key, name_map[value]) |
| 65 self.__start = name_map[start_name] | 65 self.__start = name_map[start_name] |
| 66 assert self.__terminal_set | 66 assert self.__terminal_set |
| 67 | 67 |
| 68 @staticmethod | 68 @staticmethod |
| 69 def __visit_edges(start, function, state): | 69 def __visit_edges(start, visitor, state): |
| 70 edge = set([start]) | 70 edge = set([start]) |
| 71 visited = set() | 71 visited = set() |
| 72 while edge: | 72 while edge: |
| 73 next_edge = set() | 73 f = lambda (next_edge, state), node: ( |
| 74 for node in edge: | 74 next_edge | set(node.transitions().values()), |
| 75 next_edge |= set(node.transitions().values()) | 75 visitor(node, state)) |
| 76 state = function(node, state) | 76 (next_edge, state) = reduce(f, edge, (set(), state)) |
| 77 visited |= edge | 77 visited |= edge |
| 78 edge = next_edge - visited | 78 edge = next_edge - visited |
| 79 return state | 79 return state |
| 80 | 80 |
| 81 def to_dot(self): | 81 def to_dot(self): |
| 82 | 82 |
| 83 def f(node, node_content): | 83 def f(node, node_content): |
| 84 for key, value in node.transitions().items(): | 84 for key, value in node.transitions().items(): |
| 85 node_content.append( | 85 node_content.append( |
| 86 " S_%s -> S_%s [ label = \"%s\" ];" % | 86 " S_%s -> S_%s [ label = \"%s\" ];" % |
| (...skipping 12 matching lines...) Expand all Loading... |
| 99 rankdir=LR; | 99 rankdir=LR; |
| 100 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s | 100 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s |
| 101 node [shape = doublecircle, style=unfilled]; %s | 101 node [shape = doublecircle, style=unfilled]; %s |
| 102 node [shape = circle]; | 102 node [shape = circle]; |
| 103 %s | 103 %s |
| 104 } | 104 } |
| 105 ''' % (start_shape, | 105 ''' % (start_shape, |
| 106 start_number, | 106 start_number, |
| 107 " ".join(terminals), | 107 " ".join(terminals), |
| 108 "\n".join(node_content)) | 108 "\n".join(node_content)) |
| OLD | NEW |