Revision: 17889
Author: [email protected]
Date: Tue Nov 19 18:25:42 2013 UTC
Log: Experimental lexer generator: random refactoring + comments.
BUG=
[email protected]
Review URL: https://codereview.chromium.org/73023005
http://code.google.com/p/v8/source/detail?r=17889
Modified:
/branches/experimental/parser/tools/lexer_generator/code_generator.py
/branches/experimental/parser/tools/lexer_generator/dfa.py
/branches/experimental/parser/tools/lexer_generator/nfa.py
/branches/experimental/parser/tools/lexer_generator/transition_keys.py
=======================================
--- /branches/experimental/parser/tools/lexer_generator/code_generator.py
Tue Nov 19 08:17:07 2013 UTC
+++ /branches/experimental/parser/tools/lexer_generator/code_generator.py
Tue Nov 19 18:25:42 2013 UTC
@@ -148,6 +148,8 @@
@staticmethod
def __split_transitions(split_count, state):
+ '''Goes through the transitions for 'state' and decides which of them
should
+ use the if statement and which should use the switch statement.'''
assert not state['switch_transitions']
(class_keys, distinct_keys, ranges) = (0, 0, 0)
for (t, r) in state['disjoint_keys']:
@@ -198,14 +200,14 @@
if self.__inline:
inlined = reduce(CodeGenerator.__set_inline, dfa_states, 0)
if self.__log:
- print "inlined %s" % inlined
+ print "%s states inlined" % inlined
elif self.__log:
print "no inlining"
# split transitions
if self.__switching:
switched = reduce(CodeGenerator.__split_transitions, dfa_states, 0)
if self.__log:
- print "switched states %s" % inlined
+ print "%s states use switch (instead of if)" % switched
elif self.__log:
print "no switching"
# store states
=======================================
--- /branches/experimental/parser/tools/lexer_generator/dfa.py Tue Nov 19
14:54:23 2013 UTC
+++ /branches/experimental/parser/tools/lexer_generator/dfa.py Tue Nov 19
18:25:42 2013 UTC
@@ -55,14 +55,17 @@
# TODO abstract state matching
def __matches(self, match_func, value):
- f = lambda acc, (k, vs): acc | set([vs]) if match_func(k, value) else
acc
+ # f collects states whose corresponding TransitionKey matches 'value'.
+ f = (lambda acc, (key, state):
+ acc | set([state]) if match_func(key, value) else acc)
matches = reduce(f, self.__transitions.items(), set())
+ # Since this is a dfa, we should have at most one such state. Return
it.
if not matches:
return None
assert len(matches) == 1
return iter(matches).next()
- def char_matches(self, value):
+ def next_state_with_char(self, value):
return self.__matches(lambda k, v : k.matches_char(v), value)
def key_matches(self, value):
=======================================
--- /branches/experimental/parser/tools/lexer_generator/nfa.py Tue Nov 19
14:54:23 2013 UTC
+++ /branches/experimental/parser/tools/lexer_generator/nfa.py Tue Nov 19
18:25:42 2013 UTC
@@ -98,10 +98,12 @@
self.__add_transition(TransitionKey.epsilon(), end)
def __matches(self, match_func, value):
- f = lambda acc, (k, vs): acc | vs if match_func(k, value) else acc
+ # f collects states whose corresponding TransitionKey matches 'value'.
+ f = (lambda acc, (key, states):
+ acc | states if match_func(key, value) else acc)
return reduce(f, self.__transitions.items(), set())
- def char_matches(self, value):
+ def next_states_with_char(self, value):
return self.__matches(lambda k, v : k.matches_char(v), value)
def key_matches(self, value):
@@ -136,7 +138,7 @@
def matches(self, string):
valid_states = Nfa.__close(set([self.__start]))
for c in string:
- f = lambda acc, state: acc | state.char_matches(c)
+ f = lambda acc, state: acc | state.next_states_with_char(c)
transitions = reduce(f, valid_states, set())
if not transitions:
return False
=======================================
--- /branches/experimental/parser/tools/lexer_generator/transition_keys.py
Tue Nov 19 14:54:23 2013 UTC
+++ /branches/experimental/parser/tools/lexer_generator/transition_keys.py
Tue Nov 19 18:25:42 2013 UTC
@@ -103,11 +103,9 @@
@staticmethod
def any():
'''Returns a TransitionKey which matches any character.'''
- def bounds_getter(name):
- bounds = TransitionKey.__class_bounds.values()
- bounds.sort()
- return bounds
- return TransitionKey.__cached_key('any', bounds_getter)
+ return TransitionKey.__cached_key(
+ 'any',
+ lambda name : sorted(TransitionKey.__class_bounds.values()))
@staticmethod
def single_char(char):
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.