| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 last_position = pos | 138 last_position = pos |
| 139 # lex next token | 139 # lex next token |
| 140 valid_states = Automaton.__transition_states_for_char(self.start_set(), c) | 140 valid_states = Automaton.__transition_states_for_char(self.start_set(), c) |
| 141 assert valid_states | 141 assert valid_states |
| 142 valid_states = self.epsilon_closure(valid_states) | 142 valid_states = self.epsilon_closure(valid_states) |
| 143 action = Action.dominant_action(valid_states) | 143 action = Action.dominant_action(valid_states) |
| 144 if not action: | 144 if not action: |
| 145 assert default_action | 145 assert default_action |
| 146 action = default_action | 146 action = default_action |
| 147 yield (action, last_position, len(string)) | 147 yield (action, last_position, len(string)) |
| 148 | |
| 149 def to_dot(self): | |
| 150 | |
| 151 def escape(v): | |
| 152 v = str(v) | |
| 153 v = v.replace('\r', '\\\\r').replace('\t', '\\\\t').replace('\n', '\\\\n') | |
| 154 v = v.replace('\\', '\\\\').replace('\"', '\\\"') | |
| 155 return v | |
| 156 | |
| 157 def f(node, (node_content, edge_content)): | |
| 158 if node.action(): | |
| 159 action_text = escape(node.action()) | |
| 160 node_content.append(' S_l%s[shape = box, label="%s"];' % | |
| 161 (node.node_number(), action_text)) | |
| 162 node_content.append(' S_%s -> S_l%s [arrowhead = none];' % | |
| 163 (node.node_number(), node.node_number())) | |
| 164 for key, state in node.key_state_iter(): | |
| 165 if key == TransitionKey.epsilon(): | |
| 166 key = "ε" | |
| 167 else: | |
| 168 key = key.to_string(self.encoding()) | |
| 169 edge_content.append(" S_%s -> S_%s [ label = \"%s\" ];" % ( | |
| 170 node.node_number(), state.node_number(), escape(key))) | |
| 171 return (node_content, edge_content) | |
| 172 | |
| 173 (node_content, edge_content) = self.visit_all_states(f, ([], [])) | |
| 174 | |
| 175 start_set = self.start_set() | |
| 176 assert len(start_set) == 1 | |
| 177 start_node = iter(start_set).next() | |
| 178 terminal_set = self.terminal_set() | |
| 179 | |
| 180 terminals = ["S_%d;" % x.node_number() for x in terminal_set] | |
| 181 start_number = start_node.node_number() | |
| 182 start_shape = "circle" | |
| 183 if start_node in terminal_set: | |
| 184 start_shape = "doublecircle" | |
| 185 | |
| 186 return ''' | |
| 187 digraph finite_state_machine { | |
| 188 rankdir=LR; | |
| 189 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s | |
| 190 node [shape = doublecircle, style=unfilled]; %s | |
| 191 node [shape = circle]; | |
| 192 %s | |
| 193 %s | |
| 194 } | |
| 195 ''' % (start_shape, | |
| 196 start_number, | |
| 197 " ".join(terminals), | |
| 198 "\n".join(edge_content), | |
| 199 "\n".join(node_content)) | |
| OLD | NEW |