| 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 next_edge | compute_next_edge(node), | 49 next_edge | compute_next_edge(node), |
| 50 visitor(node, state)) | 50 visitor(node, state)) |
| 51 (next_edge, state) = reduce(f, edge, (set(), state)) | 51 (next_edge, state) = reduce(f, edge, (set(), state)) |
| 52 visited |= edge | 52 visited |= edge |
| 53 edge = next_edge - visited | 53 edge = next_edge - visited |
| 54 return state | 54 return state |
| 55 | 55 |
| 56 @staticmethod | 56 @staticmethod |
| 57 def generate_dot(start_node, terminal_set, edge_iterator): | 57 def generate_dot(start_node, terminal_set, edge_iterator): |
| 58 | 58 |
| 59 def escape(v): |
| 60 v = str(v).replace('\r', '\\\\r') |
| 61 v = str(v).replace('\t', '\\\\t') |
| 62 v = str(v).replace('\n', '\\\\n') |
| 63 v = str(v).replace('\\', '\\\\') |
| 64 return v |
| 65 |
| 59 def f(node, node_content): | 66 def f(node, node_content): |
| 60 for key, values in node.transitions().items(): | 67 for key, values in node.transitions().items(): |
| 61 if key == TransitionKey.epsilon(): | 68 if key == TransitionKey.epsilon(): |
| 62 key = "ε" | 69 key = "ε" |
| 63 key = str(key).replace('\\', '\\\\') | |
| 64 # TODO pass this action as parameter | 70 # TODO pass this action as parameter |
| 65 if type(values) == TupleType: | 71 if type(values) == TupleType: |
| 66 values = [values] | 72 values = [values] |
| 67 for (state, action) in values: | 73 for (state, action) in values: |
| 68 if action: | 74 if action: |
| 69 node_content.append( | 75 content = " S_%s -> S_%s [ label = \"%s {%s} -> %s\" ];" % ( |
| 70 " S_%s -> S_%s [ label = \"%s {%s} -> %s\" ];" % | 76 node.node_number(), |
| 71 (node.node_number(), state.node_number(), key, action[1], | 77 state.node_number(), |
| 72 action[2])) | 78 escape(key), |
| 79 escape(action[1]), |
| 80 escape(action[2])) |
| 73 else: | 81 else: |
| 74 node_content.append( | 82 content = " S_%s -> S_%s [ label = \"%s\" ];" % ( |
| 75 " S_%s -> S_%s [ label = \"%s\" ];" % | 83 node.node_number(), state.node_number(), escape(key)) |
| 76 (node.node_number(), state.node_number(), key)) | 84 node_content.append(content) |
| 77 return node_content | 85 return node_content |
| 78 | 86 |
| 79 node_content = edge_iterator(f, []) | 87 node_content = edge_iterator(f, []) |
| 80 terminals = ["S_%d;" % x.node_number() for x in terminal_set] | 88 terminals = ["S_%d;" % x.node_number() for x in terminal_set] |
| 81 start_number = start_node.node_number() | 89 start_number = start_node.node_number() |
| 82 start_shape = "circle" | 90 start_shape = "circle" |
| 83 if start_node in terminal_set: | 91 if start_node in terminal_set: |
| 84 start_shape = "doublecircle" | 92 start_shape = "doublecircle" |
| 85 | 93 |
| 86 return ''' | 94 return ''' |
| 87 digraph finite_state_machine { | 95 digraph finite_state_machine { |
| 88 rankdir=LR; | 96 rankdir=LR; |
| 89 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s | 97 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s |
| 90 node [shape = doublecircle, style=unfilled]; %s | 98 node [shape = doublecircle, style=unfilled]; %s |
| 91 node [shape = circle]; | 99 node [shape = circle]; |
| 92 %s | 100 %s |
| 93 } | 101 } |
| 94 ''' % (start_shape, | 102 ''' % (start_shape, |
| 95 start_number, | 103 start_number, |
| 96 " ".join(terminals), | 104 " ".join(terminals), |
| 97 "\n".join(node_content)) | 105 "\n".join(node_content)) |
| OLD | NEW |