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

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

Issue 61003003: Experimental parser: cleanup rule processing result (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 | « tools/lexer_generator/code_generator_test.py ('k') | tools/lexer_generator/generator.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
11 # with the distribution. 11 # with the distribution.
12 # * Neither the name of Google Inc. nor the names of its 12 # * Neither the name of Google Inc. nor the names of its
13 # contributors may be used to endorse or promote products derived 13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission. 14 # from this software without specific prior written permission.
15 # 15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 from automaton import * 28 from automaton import *
29 from nfa import Nfa
30 from transition_keys import TransitionKey 29 from transition_keys import TransitionKey
31 30
32 class DfaState(AutomatonState): 31 class DfaState(AutomatonState):
33 32
34 def __init__(self, name, action): 33 def __init__(self, name, action):
35 super(DfaState, self).__init__() 34 super(DfaState, self).__init__()
36 self.__name = name 35 self.__name = name
37 self.__transitions = {} 36 self.__transitions = {}
38 self.__action = action 37 self.__action = action
39 38
(...skipping 11 matching lines...) Expand all
51 self.__transitions[key] = state 50 self.__transitions[key] = state
52 51
53 def transitions(self): 52 def transitions(self):
54 return self.__transitions 53 return self.__transitions
55 54
56 class Dfa(Automaton): 55 class Dfa(Automaton):
57 56
58 def __init__(self, start_name, mapping): 57 def __init__(self, start_name, mapping):
59 super(Dfa, self).__init__() 58 super(Dfa, self).__init__()
60 self.__terminal_set = set() 59 self.__terminal_set = set()
61 self.__name_map = {} 60 name_map = {}
62 for name, node_data in mapping.items(): 61 for name, node_data in mapping.items():
63 node = DfaState(name, node_data['action']) 62 node = DfaState(name, node_data['action'])
64 self.__name_map[name] = node 63 name_map[name] = node
65 if node_data['terminal']: 64 if node_data['terminal']:
66 self.__terminal_set.add(node) 65 self.__terminal_set.add(node)
67 for name, node_data in mapping.items(): 66 for name, node_data in mapping.items():
68 node = self.__name_map[name] 67 node = name_map[name]
69 inversion = {} 68 inversion = {}
70 for key, state in node_data['transitions'].items(): 69 for key, state in node_data['transitions'].items():
71 if not state in inversion: 70 if not state in inversion:
72 inversion[state] = [] 71 inversion[state] = []
73 inversion[state].append(key) 72 inversion[state].append(key)
74 for state, keys in inversion.items(): 73 for state, keys in inversion.items():
75 merged_key = TransitionKey.merged_key(keys) 74 merged_key = TransitionKey.merged_key(keys)
76 node.add_transition(merged_key, self.__name_map[state]) 75 node.add_transition(merged_key, name_map[state])
77 self.__start = self.__name_map[start_name] 76 self.__start = name_map[start_name]
77 self.__node_count = len(mapping)
78 self.__verify()
79
80 def __verify(self):
78 assert self.__terminal_set 81 assert self.__terminal_set
82 state_count = self.visit_all_states(lambda state, count: count + 1, 0)
83 assert self.__node_count == state_count
79 84
80 def start_state(self): 85 def start_state(self):
81 return self.__start 86 return self.__start
82 87
83 def start_set(self): 88 def start_set(self):
84 return set([self.__start]) 89 return set([self.__start])
85 90
86 def terminal_set(self): 91 def terminal_set(self):
87 return set(self.__terminal_set) 92 return set(self.__terminal_set)
88 93
89 def all_states_iter(self): 94 def all_states_iter(self):
90 return iter(self.__name_map.values()) 95 return self.__start.state_iter()
91 96
92 @staticmethod 97 @staticmethod
93 def __match_char(state, char): 98 def __match_char(state, char):
94 match = list(state.state_iter(key_filter = lambda k: k.matches_char(char))) 99 match = list(state.state_iter(key_filter = lambda k: k.matches_char(char)))
95 if not match: return None 100 if not match: return None
96 assert len(match) == 1 101 assert len(match) == 1
97 return match[0] 102 return match[0]
98 103
99 def collect_actions(self, string): 104 def collect_actions(self, string):
100 state = self.__start 105 state = self.__start
(...skipping 24 matching lines...) Expand all
125 last_position = pos 130 last_position = pos
126 # lex next token 131 # lex next token
127 next = Dfa.__match_char(self.__start, c) 132 next = Dfa.__match_char(self.__start, c)
128 assert next 133 assert next
129 state = next 134 state = next
130 assert state.action() # must invoke default action here 135 assert state.action() # must invoke default action here
131 yield (state.action(), last_position, len(string)) 136 yield (state.action(), last_position, len(string))
132 137
133 def minimize(self): 138 def minimize(self):
134 pass 139 pass
OLDNEW
« no previous file with comments | « tools/lexer_generator/code_generator_test.py ('k') | tools/lexer_generator/generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698