| 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 17 matching lines...) Expand all Loading... |
| 28 import os | 28 import os |
| 29 import sys | 29 import sys |
| 30 import jinja2 | 30 import jinja2 |
| 31 from dfa import Dfa | 31 from dfa import Dfa |
| 32 from transition_keys import TransitionKey | 32 from transition_keys import TransitionKey |
| 33 | 33 |
| 34 class CodeGenerator: | 34 class CodeGenerator: |
| 35 | 35 |
| 36 def __init__(self, | 36 def __init__(self, |
| 37 rule_processor, | 37 rule_processor, |
| 38 char_type, | 38 encoding = 'latin1', |
| 39 minimize_default = True, | 39 minimize_default = True, |
| 40 inline = True, | 40 inline = True, |
| 41 switching = True, | 41 switching = True, |
| 42 debug_print = False, | 42 debug_print = False, |
| 43 log = False): | 43 log = False): |
| 44 if minimize_default: | 44 if minimize_default: |
| 45 dfa = rule_processor.default_automata().minimal_dfa() | 45 dfa = rule_processor.default_automata().minimal_dfa() |
| 46 else: | 46 else: |
| 47 dfa = rule_processor.default_automata().dfa() | 47 dfa = rule_processor.default_automata().dfa() |
| 48 self.__dfa = dfa | 48 self.__dfa = dfa |
| 49 self.__default_action = rule_processor.default_action | 49 self.__default_action = rule_processor.default_action |
| 50 self.__debug_print = debug_print | 50 self.__debug_print = debug_print |
| 51 self.__start_node_number = self.__dfa.start_state().node_number() | 51 self.__start_node_number = self.__dfa.start_state().node_number() |
| 52 self.__log = log | 52 self.__log = log |
| 53 self.__inline = inline | 53 self.__inline = inline |
| 54 self.__switching = switching | 54 self.__switching = switching |
| 55 self.__char_type = char_type | 55 self.__encoding = encoding |
| 56 | 56 |
| 57 def __state_cmp(self, left, right): | 57 def __state_cmp(self, left, right): |
| 58 if left['original_node_number'] == self.__start_node_number: | 58 if left['original_node_number'] == self.__start_node_number: |
| 59 return -1 | 59 return -1 |
| 60 if right['original_node_number'] == self.__start_node_number: | 60 if right['original_node_number'] == self.__start_node_number: |
| 61 return 1 | 61 return 1 |
| 62 c = cmp(len(left['disjoint_keys']), len(right['disjoint_keys'])) | 62 c = cmp(len(left['disjoint_keys']), len(right['disjoint_keys'])) |
| 63 if c: | 63 if c: |
| 64 return c | 64 return c |
| 65 c = cmp(left['disjoint_keys'], right['disjoint_keys']) | 65 c = cmp(left['disjoint_keys'], right['disjoint_keys']) |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 template_env = jinja2.Environment( | 240 template_env = jinja2.Environment( |
| 241 loader = jinja2.PackageLoader('lexer_generator', '.'), | 241 loader = jinja2.PackageLoader('lexer_generator', '.'), |
| 242 undefined = jinja2.StrictUndefined) | 242 undefined = jinja2.StrictUndefined) |
| 243 template = template_env.get_template('code_generator.jinja') | 243 template = template_env.get_template('code_generator.jinja') |
| 244 | 244 |
| 245 return template.render( | 245 return template.render( |
| 246 start_node_number = 0, | 246 start_node_number = 0, |
| 247 debug_print = self.__debug_print, | 247 debug_print = self.__debug_print, |
| 248 default_action = default_action, | 248 default_action = default_action, |
| 249 dfa_states = dfa_states, | 249 dfa_states = dfa_states, |
| 250 char_type = self.__char_type) | 250 encoding = self.__encoding) |
| OLD | NEW |