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

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

Issue 68343004: Experimental parser: better actions (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 | « src/lexer/lexer_py.re ('k') | 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
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 21 matching lines...) Expand all
32 pass 32 pass
33 33
34 class AutomatonState(object): 34 class AutomatonState(object):
35 35
36 def __init__(self, node_number): 36 def __init__(self, node_number):
37 self.__node_number = node_number 37 self.__node_number = node_number
38 38
39 def node_number(self): 39 def node_number(self):
40 return self.__node_number 40 return self.__node_number
41 41
42 def __str__(self):
43 return "%s(%d)" % (type(self), self.node_number())
44
42 class Automaton(object): 45 class Automaton(object):
43 46
44 @staticmethod 47 @staticmethod
45 def visit_edges(edge, compute_next_edge, visitor, state): 48 def visit_edges(edge, compute_next_edge, visitor, state):
46 visited = set() 49 visited = set()
47 while edge: 50 while edge:
48 f = lambda (next_edge, state), node: ( 51 f = lambda (next_edge, state), node: (
49 next_edge | compute_next_edge(node), 52 next_edge | compute_next_edge(node),
50 visitor(node, state)) 53 visitor(node, state))
51 (next_edge, state) = reduce(f, edge, (set(), state)) 54 (next_edge, state) = reduce(f, edge, (set(), state))
52 visited |= edge 55 visited |= edge
53 edge = next_edge - visited 56 edge = next_edge - visited
54 return state 57 return state
55 58
56 @staticmethod 59 @staticmethod
57 def generate_dot(start_node, terminal_set, edge_iterator): 60 def generate_dot(start_node, terminal_set, edge_iterator, state_iterator):
58 61
59 def escape(v): 62 def escape(v):
60 v = str(v).replace('\r', '\\\\r') 63 v = str(v).replace('\r', '\\\\r')
61 v = str(v).replace('\t', '\\\\t') 64 v = str(v).replace('\t', '\\\\t')
62 v = str(v).replace('\n', '\\\\n') 65 v = str(v).replace('\n', '\\\\n')
63 v = str(v).replace('\\', '\\\\') 66 v = str(v).replace('\\', '\\\\')
64 v = str(v).replace('\"', '\\\"') 67 v = str(v).replace('\"', '\\\"')
65 return v 68 return v
66 69
67 def f(node, node_content): 70 def f(node, node_content):
68 for key, values in node.transitions().items(): 71 for key, values in node.transitions().items():
69 if key == TransitionKey.epsilon(): 72 if key == TransitionKey.epsilon():
70 key = "ε" 73 key = "ε"
71 # TODO pass this action as parameter 74 for state in state_iterator(values):
72 if type(values) == TupleType: 75 action = state.action()
73 values = [values]
74 for (state, action) in values:
75 if action: 76 if action:
76 content = " S_%s -> S_%s [ label = \"%s {%s} -> %s\" ];" % ( 77 content = " S_%s -> S_%s [ label = \"%s {%s}:%d\" ];" % (
77 node.node_number(), 78 node.node_number(),
78 state.node_number(), 79 state.node_number(),
79 escape(key), 80 escape(key),
80 escape(action[1]), 81 escape(action[1]),
81 escape(action[2])) 82 action[0])
82 else: 83 else:
83 content = " S_%s -> S_%s [ label = \"%s\" ];" % ( 84 content = " S_%s -> S_%s [ label = \"%s\" ];" % (
84 node.node_number(), state.node_number(), escape(key)) 85 node.node_number(), state.node_number(), escape(key))
85 node_content.append(content) 86 node_content.append(content)
86 return node_content 87 return node_content
87 88
88 node_content = edge_iterator(f, []) 89 node_content = edge_iterator(f, [])
89 terminals = ["S_%d;" % x.node_number() for x in terminal_set] 90 terminals = ["S_%d;" % x.node_number() for x in terminal_set]
90 start_number = start_node.node_number() 91 start_number = start_node.node_number()
91 start_shape = "circle" 92 start_shape = "circle"
92 if start_node in terminal_set: 93 if start_node in terminal_set:
93 start_shape = "doublecircle" 94 start_shape = "doublecircle"
94 95
95 return ''' 96 return '''
96 digraph finite_state_machine { 97 digraph finite_state_machine {
97 rankdir=LR; 98 rankdir=LR;
98 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s 99 node [shape = %s, style=filled, bgcolor=lightgrey]; S_%s
99 node [shape = doublecircle, style=unfilled]; %s 100 node [shape = doublecircle, style=unfilled]; %s
100 node [shape = circle]; 101 node [shape = circle];
101 %s 102 %s
102 } 103 }
103 ''' % (start_shape, 104 ''' % (start_shape,
104 start_number, 105 start_number,
105 " ".join(terminals), 106 " ".join(terminals),
106 "\n".join(node_content)) 107 "\n".join(node_content))
OLDNEW
« no previous file with comments | « src/lexer/lexer_py.re ('k') | tools/lexer_generator/dfa.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698