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

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

Issue 78133002: Experimental parser: cleanup transition iteration a little (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/nfa.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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 self.__transitions[key] = state 52 self.__transitions[key] = state
53 53
54 def transitions(self): 54 def transitions(self):
55 return self.__transitions 55 return self.__transitions
56 56
57 def epsilon_closure_iter(self): 57 def epsilon_closure_iter(self):
58 return iter([]) 58 return iter([])
59 59
60 # TODO abstract state matching 60 # TODO abstract state matching
61 def __matches(self, match_func, value): 61 def __matches(self, match_func, value):
62 # f collects states whose corresponding TransitionKey matches 'value'. 62 items = self.__transitions.items()
63 f = (lambda acc, (key, state): 63 return [state for (key, state) in items if match_func(key, value)]
64 acc | set([state]) if match_func(key, value) else acc)
65 return reduce(f, self.__transitions.items(), set())
66 64
67 def transition_state_iter_for_char(self, value): 65 def transition_state_iter_for_char(self, value):
68 return iter(self.__matches(lambda k, v : k.matches_char(v), value)) 66 return iter(self.__matches(lambda k, v : k.matches_char(v), value))
69 67
70 def transition_state_iter_for_key(self, value): 68 def transition_state_iter_for_key(self, value):
71 return iter(self.__matches(lambda k, v : k.is_superset_of_key(v), value)) 69 return iter(self.__matches(lambda k, v : k.is_superset_of_key(v), value))
72 70
73 def transition_state_for_key(self, value): 71 def transition_state_for_key(self, value):
74 matches = self.__matches(lambda k, v : k.is_superset_of_key(v), value) 72 matches = self.__matches(lambda k, v : k.is_superset_of_key(v), value)
75 if not matches: 73 if not matches:
76 return None 74 return None
77 # Since this is a dfa, we should have at most one such state. Return it. 75 # Since this is a dfa, we should have at most one such state. Return it.
78 assert len(matches) == 1 76 assert len(matches) == 1
79 return iter(matches).next() 77 return matches[0]
80 78
81 class Dfa(Automaton): 79 class Dfa(Automaton):
82 80
83 def __init__(self, start_name, mapping): 81 def __init__(self, start_name, mapping):
84 super(Dfa, self).__init__() 82 super(Dfa, self).__init__()
85 self.__terminal_set = set() 83 self.__terminal_set = set()
86 name_map = {} 84 name_map = {}
87 for name, node_data in mapping.items(): 85 for name, node_data in mapping.items():
88 node = DfaState(name, node_data['action']) 86 node = DfaState(name, node_data['action'])
89 name_map[name] = node 87 name_map[name] = node
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 elif len(intersection) <= len(difference): 348 elif len(intersection) <= len(difference):
351 working_set.add(intersection) 349 working_set.add(intersection)
352 else: 350 else:
353 working_set.add(difference) 351 working_set.add(difference)
354 if old_partitions: 352 if old_partitions:
355 partitions -= old_partitions 353 partitions -= old_partitions
356 if new_partitions: 354 if new_partitions:
357 partitions |= new_partitions 355 partitions |= new_partitions
358 (start_name, mapping) = self.__merge_partitions(partitions) 356 (start_name, mapping) = self.__merge_partitions(partitions)
359 return Dfa(start_name, mapping) 357 return Dfa(start_name, mapping)
OLDNEW
« no previous file with comments | « no previous file | tools/lexer_generator/nfa.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698