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

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

Issue 155573004: Experimental parser: Abstact state matching (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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/lexer_generator/code_generator.py ('k') | tools/lexer_generator/dfa_optimizer.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 20 matching lines...) Expand all
31 31
32 class DfaState(AutomatonState): 32 class DfaState(AutomatonState):
33 33
34 def __init__(self, name, action): 34 def __init__(self, name, action):
35 super(DfaState, self).__init__() 35 super(DfaState, self).__init__()
36 self.__name = name 36 self.__name = name
37 self.__transitions = {} 37 self.__transitions = {}
38 self.__action = action 38 self.__action = action
39 assert isinstance(action, Action) 39 assert isinstance(action, Action)
40 40
41 def transitions_to_multiple_states(self):
42 return False
43
44 def name(self): 41 def name(self):
45 return self.__name 42 return self.__name
46 43
47 def action(self): 44 def action(self):
48 return self.__action 45 return self.__action
49 46
50 def add_transition(self, key, state): 47 def add_transition(self, key, state):
51 assert key != None 48 assert key != None
52 assert not key == TransitionKey.epsilon() 49 assert not key == TransitionKey.epsilon()
53 assert not self.__transitions.has_key(key) 50 assert not self.__transitions.has_key(key)
54 self.__transitions[key] = state 51 self.__transitions[key] = state
55 52
56 def transitions(self):
57 return self.__transitions
58 53
59 def epsilon_closure_iter(self): 54 def epsilon_closure_iter(self):
60 return iter([]) 55 return iter([])
61 56
62 # TODO abstract state matching 57 def transition_state_for_key(self, value):
63 def __matches(self, match_func, value): 58 matches = list(self.transition_state_iter_for_key(value))
64 items = self.__transitions.items() 59 assert len(matches) <= 1
65 return [state for (key, state) in items if match_func(key, value)] 60 return matches[0] if matches else None
66 61
67 def transition_state_iter_for_char(self, value): 62 def key_state_iter(
68 return iter(self.__matches(lambda k, v : k.matches_char(v), value)) 63 self,
69 64 key_filter = lambda x: True,
70 def transition_state_iter_for_key(self, value): 65 state_filter = lambda x: True,
71 return iter(self.__matches(lambda k, v : k.is_superset_of_key(v), value)) 66 match_func = lambda x, y: True,
72 67 yield_func = lambda x, y: (x, y)):
73 def transition_state_for_key(self, value): 68 for key, state in self.__transitions.items():
74 matches = self.__matches(lambda k, v : k.is_superset_of_key(v), value) 69 if key_filter(key) and state_filter(state) and match_func(key, state):
75 if not matches: 70 yield yield_func(key, state)
76 return None
77 # Since this is a dfa, we should have at most one such state. Return it.
78 assert len(matches) == 1
79 return matches[0]
80 71
81 class Dfa(Automaton): 72 class Dfa(Automaton):
82 73
83 def __init__(self, encoding, start_name, mapping): 74 def __init__(self, encoding, start_name, mapping):
84 super(Dfa, self).__init__(encoding) 75 super(Dfa, self).__init__(encoding)
85 self.__terminal_set = set() 76 self.__terminal_set = set()
86 name_map = {} 77 name_map = {}
87 for name, node_data in mapping.items(): 78 for name, node_data in mapping.items():
88 node = DfaState(name, node_data['action']) 79 node = DfaState(name, node_data['action'])
89 name_map[name] = node 80 name_map[name] = node
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 elif len(intersection) <= len(difference): 379 elif len(intersection) <= len(difference):
389 working_set.add(intersection) 380 working_set.add(intersection)
390 else: 381 else:
391 working_set.add(difference) 382 working_set.add(difference)
392 if old_partitions: 383 if old_partitions:
393 partitions -= old_partitions 384 partitions -= old_partitions
394 if new_partitions: 385 if new_partitions:
395 partitions |= new_partitions 386 partitions |= new_partitions
396 (start_name, mapping) = self.__create_states_from_partitions(partitions) 387 (start_name, mapping) = self.__create_states_from_partitions(partitions)
397 return Dfa(self.__dfa.encoding(), start_name, mapping) 388 return Dfa(self.__dfa.encoding(), start_name, mapping)
OLDNEW
« no previous file with comments | « tools/lexer_generator/code_generator.py ('k') | tools/lexer_generator/dfa_optimizer.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698