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

Unified Diff: tools/lexer_generator/dfa.py

Issue 153993014: Experimental parser: cleanup dfa.py (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 10 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/lexer_generator/action.py ('k') | tools/lexer_generator/nfa.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/lexer_generator/dfa.py
diff --git a/tools/lexer_generator/dfa.py b/tools/lexer_generator/dfa.py
index 50054e3c0f9944bd6c21d49baee20313e06f4afb..32ac769fafbb037fea4a215d42e8e1049486c86c 100644
--- a/tools/lexer_generator/dfa.py
+++ b/tools/lexer_generator/dfa.py
@@ -31,10 +31,10 @@ from transition_keys import TransitionKey
class DfaState(AutomatonState):
- def __init__(self, name, action):
+ def __init__(self, name, action, transitions):
super(DfaState, self).__init__()
self.__name = name
- self.__transitions = {}
+ self.__transitions = transitions
self.__action = action
assert isinstance(action, Action)
@@ -44,13 +44,6 @@ class DfaState(AutomatonState):
def action(self):
return self.__action
- def add_transition(self, key, state):
- assert key != None
- assert not key == TransitionKey.epsilon()
- assert not self.__transitions.has_key(key)
- self.__transitions[key] = state
-
-
def epsilon_closure_iter(self):
return iter([])
@@ -71,17 +64,25 @@ class DfaState(AutomatonState):
class Dfa(Automaton):
+ @staticmethod
+ def __add_transition(transitions, key, state):
+ assert key != None
+ assert not key == TransitionKey.epsilon()
+ assert not transitions.has_key(key)
+ transitions[key] = state
+
def __init__(self, encoding, start_name, mapping):
super(Dfa, self).__init__(encoding)
self.__terminal_set = set()
name_map = {}
for name, node_data in mapping.items():
- node = DfaState(name, node_data['action'])
- name_map[name] = node
+ transitions = {}
+ node = DfaState(name, node_data['action'], transitions)
+ name_map[name] = (node, transitions)
if node_data['terminal']:
self.__terminal_set.add(node)
for name, node_data in mapping.items():
- node = name_map[name]
+ (node, transitions) = name_map[name]
inversion = {}
for key, state in node_data['transitions'].items():
if not state in inversion:
@@ -89,8 +90,8 @@ class Dfa(Automaton):
inversion[state].append(key)
for state, keys in inversion.items():
merged_key = TransitionKey.merged_key(encoding, keys)
- node.add_transition(merged_key, name_map[state])
- self.__start = name_map[start_name]
+ self.__add_transition(transitions, merged_key, name_map[state][0])
+ self.__start = name_map[start_name][0]
self.__node_count = len(mapping)
self.__verify()
@@ -179,18 +180,13 @@ class DfaMinimizer:
id_to_state[state_id] = state
action = state.action()
all_keys.append(state.key_iter())
- if action:
- if state not in terminal_set:
- # Match actions are valid only if we have matched the whole token, not
- # just some sub-part of it.
- assert not action.match_action()
- key = ("nonterminal action", action)
- else:
- key = ("terminal action", action)
- elif state in terminal_set:
- key = ("terminal set",)
+ if state in terminal_set:
+ key = ("terminal set", action)
else:
- key = ("nonterminal set",)
+ # Match actions are valid only if we have matched the whole token, not
+ # just some sub-part of it.
+ assert not action.match_action()
+ key = ("nonterminal set", action)
if not key in initial_partitions:
initial_partitions[key] = set()
initial_partitions[key].add(state_id)
« no previous file with comments | « tools/lexer_generator/action.py ('k') | tools/lexer_generator/nfa.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698