Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: tools/lexer_generator/automaton.py

Issue 63863004: Experimental parser: subclass for common code (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tools/lexer_generator/dfa.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2013 the V8 project authors. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are
4 # met:
5 #
6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above
9 # copyright notice, this list of conditions and the following
10 # disclaimer in the documentation and/or other materials provided
11 # with the distribution.
12 # * Neither the name of Google Inc. nor the names of its
13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 from types import TupleType
29 from transition_keys import TransitionKey
30
31 class Action(object):
32 pass
33
34 class AutomatonState(object):
35
36 def __init__(self, node_number):
37 self.__node_number = node_number
38
39 def node_number(self):
40 return self.__node_number
41
42 class Automaton(object):
43
44 @staticmethod
45 def visit_edges(edge, compute_next_edge, visitor, state):
46 visited = set()
47 while edge:
48 f = lambda (next_edge, state), node: (
49 next_edge | compute_next_edge(node),
50 visitor(node, state))
51 (next_edge, state) = reduce(f, edge, (set(), state))
52 visited |= edge
53 edge = next_edge - visited
54 return state
55
56 @staticmethod
57 def generate_dot(start_node, terminal_set, edge_iterator):
58
59 def f(node, node_content):
60 for key, values in node.transitions().items():
61 if key == TransitionKey.epsilon():
62 key = "ε"
63 key = str(key).replace('\\', '\\\\')
64 # TODO pass this action as parameter
65 if type(values) == TupleType:
66 values = [values]
67 for (state, action) in values:
68 if action:
69 node_content.append(
70 " S_%s -> S_%s [ label = \"%s {%s} -> %s\" ];" %
71 (node.node_number(), state.node_number(), key, action[1],
72 action[2]))
73 else:
74 node_content.append(
75 " S_%s -> S_%s [ label = \"%s\" ];" %
76 (node.node_number(), state.node_number(), key))
77 return node_content
78
79 node_content = edge_iterator(f, [])
80 terminals = ["S_%d;" % x.node_number() for x in terminal_set]
81 start_number = start_node.node_number()
82 start_shape = "circle"
83 if start_node in terminal_set:
84 start_shape = "doublecircle"
85
86 return '''
87 digraph finite_state_machine {
88 rankdir=LR;
89 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s
90 node [shape = doublecircle, style=unfilled]; %s
91 node [shape = circle];
92 %s
93 }
94 ''' % (start_shape,
95 start_number,
96 " ".join(terminals),
97 "\n".join(node_content))
OLDNEW
« no previous file with comments | « no previous file | tools/lexer_generator/dfa.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698