| OLD | NEW |
| 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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 else: | 124 else: |
| 125 raise Exception() | 125 raise Exception() |
| 126 return { | 126 return { |
| 127 'node_number' : state.node_number(), | 127 'node_number' : state.node_number(), |
| 128 'original_node_number' : state.node_number(), | 128 'original_node_number' : state.node_number(), |
| 129 'transitions' : transitions, | 129 'transitions' : transitions, |
| 130 'switch_transitions' : [], | 130 'switch_transitions' : [], |
| 131 'deferred_transitions' : [], | 131 'deferred_transitions' : [], |
| 132 'long_char_transitions' : [], | 132 'long_char_transitions' : [], |
| 133 'disjoint_keys' : disjoint_keys, | 133 'disjoint_keys' : disjoint_keys, |
| 134 'has_goto_after_entry' : False, |
| 134 'inline' : None, | 135 'inline' : None, |
| 135 'depth' : None, | 136 'depth' : None, |
| 136 'action' : action, | 137 'action' : action, |
| 137 'entry_action' : entry_action, | 138 'entry_action' : entry_action, |
| 138 'match_action' : match_action, | 139 'match_action' : match_action, |
| 139 'class_keys' : class_keys, | 140 'class_keys' : class_keys, |
| 140 'distinct_keys' : distinct_keys, | 141 'distinct_keys' : distinct_keys, |
| 141 'ranges' : ranges | 142 'ranges' : ranges |
| 142 } | 143 } |
| 143 | 144 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 for i, state in enumerate(dfa_states): | 282 for i, state in enumerate(dfa_states): |
| 282 state['node_number'] = i | 283 state['node_number'] = i |
| 283 def f((key, original_node_number)): | 284 def f((key, original_node_number)): |
| 284 return (key, id_map[original_node_number]['node_number']) | 285 return (key, id_map[original_node_number]['node_number']) |
| 285 for state in dfa_states: | 286 for state in dfa_states: |
| 286 state['transitions'] = map(f, state['transitions']) | 287 state['transitions'] = map(f, state['transitions']) |
| 287 assert id_map[self.__start_node_number]['node_number'] == 0 | 288 assert id_map[self.__start_node_number]['node_number'] == 0 |
| 288 assert len(dfa_states) == self.__dfa.node_count() | 289 assert len(dfa_states) == self.__dfa.node_count() |
| 289 # store states | 290 # store states |
| 290 self.__dfa_states = dfa_states | 291 self.__dfa_states = dfa_states |
| 292 return id_map |
| 293 |
| 294 def __rewrite_gotos(self): |
| 295 goto_map = {} |
| 296 for state in self.__dfa_states: |
| 297 if (state['match_action'] and |
| 298 state['match_action'][0] == 'do_stored_token'): |
| 299 assert not state['match_action'][1] in goto_map |
| 300 goto_map[state['match_action'][1]] = state['node_number'] |
| 301 state['has_goto_after_entry'] = True |
| 302 for state in self.__dfa_states: |
| 303 if state['match_action'] and state['match_action'][0] == 'goto': |
| 304 state['match_action'] = ('goto', goto_map[state['match_action'][1]]) |
| 305 |
| 306 def process(self): |
| 307 |
| 308 id_map = self.__canonicalize_traversal() |
| 309 self.__rewrite_gotos() |
| 310 |
| 311 dfa_states = self.__dfa_states |
| 291 # set nodes to inline | 312 # set nodes to inline |
| 292 if self.__inline: | 313 if self.__inline: |
| 293 inlined = reduce(self.__set_inline, dfa_states, 0) | 314 inlined = reduce(self.__set_inline, dfa_states, 0) |
| 294 if self.__log: | 315 if self.__log: |
| 295 print "%s states inlined" % inlined | 316 print "%s states inlined" % inlined |
| 296 elif self.__log: | 317 elif self.__log: |
| 297 print "no inlining" | 318 print "no inlining" |
| 298 # split transitions | 319 # split transitions |
| 299 switched = reduce(self.__split_transitions, dfa_states, 0) | 320 switched = reduce(self.__split_transitions, dfa_states, 0) |
| 300 if self.__log: | 321 if self.__log: |
| 301 print "%s states use switch (instead of if)" % switched | 322 print "%s states use switch (instead of if)" % switched |
| 302 # rewrite deferred transitions | 323 # rewrite deferred transitions |
| 303 for state in dfa_states: | 324 for state in dfa_states: |
| 304 self.__rewrite_deferred_transitions(state) | 325 self.__rewrite_deferred_transitions(state) |
| 305 | 326 |
| 306 def process(self): | |
| 307 | |
| 308 self.__canonicalize_traversal() | |
| 309 | |
| 310 dfa_states = self.__dfa_states | |
| 311 | |
| 312 default_action = self.__default_action | 327 default_action = self.__default_action |
| 313 assert(default_action and default_action.match_action()) | 328 assert(default_action and default_action.match_action()) |
| 314 default_action = default_action.match_action() | 329 default_action = default_action.match_action() |
| 315 | 330 |
| 316 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 331 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 317 template_env = jinja2.Environment( | 332 template_env = jinja2.Environment( |
| 318 loader = jinja2.PackageLoader('lexer_generator', '.'), | 333 loader = jinja2.PackageLoader('lexer_generator', '.'), |
| 319 undefined = jinja2.StrictUndefined) | 334 undefined = jinja2.StrictUndefined) |
| 320 template = template_env.get_template('code_generator.jinja') | 335 template = template_env.get_template('code_generator.jinja') |
| 321 | 336 |
| 322 encoding = self.__dfa.encoding() | 337 encoding = self.__dfa.encoding() |
| 323 char_types = {'latin1': 'uint8_t', 'utf16': 'uint16_t', 'utf8': 'int8_t'} | 338 char_types = {'latin1': 'uint8_t', 'utf16': 'uint16_t', 'utf8': 'int8_t'} |
| 324 char_type = char_types[encoding.name()] | 339 char_type = char_types[encoding.name()] |
| 325 | 340 |
| 326 return template.render( | 341 return template.render( |
| 327 start_node_number = 0, | 342 start_node_number = 0, |
| 328 debug_print = self.__debug_print, | 343 debug_print = self.__debug_print, |
| 329 default_action = default_action, | 344 default_action = default_action, |
| 330 dfa_states = dfa_states, | 345 dfa_states = dfa_states, |
| 331 encoding = encoding.name(), | 346 encoding = encoding.name(), |
| 332 char_type = char_type, | 347 char_type = char_type, |
| 333 upper_bound = encoding.primary_range()[1]) | 348 upper_bound = encoding.primary_range()[1]) |
| OLD | NEW |