| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 if c: | 75 if c: |
| 76 return c | 76 return c |
| 77 # if left['original_node_number'] != right['original_node_number']: | 77 # if left['original_node_number'] != right['original_node_number']: |
| 78 # # TODO fix | 78 # # TODO fix |
| 79 # print "noncanonical node ordering %d %d" % (left['original_node_number']
, | 79 # print "noncanonical node ordering %d %d" % (left['original_node_number']
, |
| 80 # right['original_node_number'
]) | 80 # right['original_node_number'
]) |
| 81 return cmp(left['original_node_number'], right['original_node_number']) | 81 return cmp(left['original_node_number'], right['original_node_number']) |
| 82 | 82 |
| 83 @staticmethod | 83 @staticmethod |
| 84 def __range_cmp(left, right): | 84 def __range_cmp(left, right): |
| 85 if left[0] == 'LATIN_1': | 85 if left[0] == 'PRIMARY_RANGE': |
| 86 if right[0] == 'LATIN_1': | 86 if right[0] == 'PRIMARY_RANGE': |
| 87 return cmp(left[1], right[1]) | 87 return cmp(left[1], right[1]) |
| 88 assert right[0] == 'CLASS' | 88 assert right[0] == 'CLASS' |
| 89 return -1 | 89 return -1 |
| 90 assert left[0] == 'CLASS' | 90 assert left[0] == 'CLASS' |
| 91 if right[0] == 'LATIN_1': | 91 if right[0] == 'PRIMARY_RANGE': |
| 92 return 1 | 92 return 1 |
| 93 # TODO store numeric values and cmp | 93 # TODO store numeric values and cmp |
| 94 return cmp(left[1], right[1]) | 94 return cmp(left[1], right[1]) |
| 95 | 95 |
| 96 @staticmethod | 96 @staticmethod |
| 97 def __transform_state(encoding, state): | 97 def __transform_state(encoding, state): |
| 98 # action data | 98 # action data |
| 99 action = state.action() | 99 action = state.action() |
| 100 entry_action = None if not action else action.entry_action() | 100 entry_action = None if not action else action.entry_action() |
| 101 match_action = None if not action else action.match_action() | 101 match_action = None if not action else action.match_action() |
| 102 # generate ordered transitions | 102 # generate ordered transitions |
| 103 transitions = map(lambda (k, v) : (k, v.node_number()), | 103 transitions = map(lambda (k, v) : (k, v.node_number()), |
| 104 state.transitions().items()) | 104 state.transitions().items()) |
| 105 def cmp(left, right): | 105 def cmp(left, right): |
| 106 return TransitionKey.canonical_compare(left[0], right[0]) | 106 return TransitionKey.canonical_compare(left[0], right[0]) |
| 107 transitions = sorted(transitions, cmp) | 107 transitions = sorted(transitions, cmp) |
| 108 # map transition keys to disjoint ranges | 108 # map transition keys to disjoint ranges |
| 109 disjoint_keys = {'value' : []} | 109 disjoint_keys = {'value' : []} |
| 110 def f((key, state)): | 110 def f((key, state)): |
| 111 ranges = list(key.range_iter(encoding)) | 111 ranges = list(key.range_iter(encoding)) |
| 112 disjoint_keys['value'] += ranges | 112 disjoint_keys['value'] += ranges |
| 113 return (ranges, state) | 113 return (ranges, state) |
| 114 transitions = map(f, transitions) | 114 transitions = map(f, transitions) |
| 115 disjoint_keys = sorted(disjoint_keys['value'], CodeGenerator.__range_cmp) | 115 disjoint_keys = sorted(disjoint_keys['value'], CodeGenerator.__range_cmp) |
| 116 # dictionary object representing state | 116 # dictionary object representing state |
| 117 (class_keys, distinct_keys, ranges) = (0, 0, 0) | 117 (class_keys, distinct_keys, ranges) = (0, 0, 0) |
| 118 for (t, r) in disjoint_keys: | 118 for (t, r) in disjoint_keys: |
| 119 if t == 'CLASS': | 119 if t == 'CLASS': |
| 120 class_keys += 1 | 120 class_keys += 1 |
| 121 elif t == 'LATIN_1': | 121 elif t == 'PRIMARY_RANGE': |
| 122 distinct_keys += r[1] - r[0] + 1 | 122 distinct_keys += r[1] - r[0] + 1 |
| 123 ranges += 1 | 123 ranges += 1 |
| 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' : [], |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 default_action = self.__default_action | 239 default_action = self.__default_action |
| 240 assert(default_action and default_action.match_action()) | 240 assert(default_action and default_action.match_action()) |
| 241 default_action = default_action.match_action() | 241 default_action = default_action.match_action() |
| 242 | 242 |
| 243 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 243 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 244 template_env = jinja2.Environment( | 244 template_env = jinja2.Environment( |
| 245 loader = jinja2.PackageLoader('lexer_generator', '.'), | 245 loader = jinja2.PackageLoader('lexer_generator', '.'), |
| 246 undefined = jinja2.StrictUndefined) | 246 undefined = jinja2.StrictUndefined) |
| 247 template = template_env.get_template('code_generator.jinja') | 247 template = template_env.get_template('code_generator.jinja') |
| 248 | 248 |
| 249 encoding = self.__dfa.encoding().name() | 249 encoding = self.__dfa.encoding() |
| 250 char_types = {'latin1': 'uint8_t', 'utf16': 'uint16_t', 'utf8': 'int8_t'} | 250 char_types = {'latin1': 'uint8_t', 'utf16': 'uint16_t', 'utf8': 'int8_t'} |
| 251 char_type = char_types[encoding] | 251 char_type = char_types[encoding.name()] |
| 252 | 252 |
| 253 return template.render( | 253 return template.render( |
| 254 start_node_number = 0, | 254 start_node_number = 0, |
| 255 debug_print = self.__debug_print, | 255 debug_print = self.__debug_print, |
| 256 default_action = default_action, | 256 default_action = default_action, |
| 257 dfa_states = dfa_states, | 257 dfa_states = dfa_states, |
| 258 encoding = encoding, | 258 encoding = encoding.name(), |
| 259 char_type = char_type) | 259 char_type = char_type, |
| 260 upper_bound = encoding.primary_range()[1]) |
| OLD | NEW |