| 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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 def __hash__(self): | 183 def __hash__(self): |
| 184 if self.__cached_hash == None: | 184 if self.__cached_hash == None: |
| 185 initial_hash = hash((-1, TransitionKey.__upper_bound + 1)) | 185 initial_hash = hash((-1, TransitionKey.__upper_bound + 1)) |
| 186 f = lambda acc, r: acc ^ hash(r) | 186 f = lambda acc, r: acc ^ hash(r) |
| 187 self.__cached_hash = reduce(f, self.__ranges, initial_hash) | 187 self.__cached_hash = reduce(f, self.__ranges, initial_hash) |
| 188 return self.__cached_hash | 188 return self.__cached_hash |
| 189 | 189 |
| 190 def __eq__(self, other): | 190 def __eq__(self, other): |
| 191 return isinstance(other, self.__class__) and self.__ranges == other.__ranges | 191 return isinstance(other, self.__class__) and self.__ranges == other.__ranges |
| 192 | 192 |
| 193 def to_code(self): | 193 @staticmethod |
| 194 code = 'if (' | 194 def __class_name(r): |
| 195 first = True | 195 for name, v in TransitionKey.__class_bounds.items(): |
| 196 if r == v: return name |
| 197 assert False |
| 198 |
| 199 def range_iter(self): |
| 200 assert not self == TransitionKey.epsilon() and not self.__is_unique() |
| 196 for r in self.__ranges: | 201 for r in self.__ranges: |
| 197 if r[0] >= 256: # FIXME: add class checks | 202 if self.__is_class_range(r): |
| 198 continue | 203 yield ('CLASS', TransitionKey.__class_name(r)) |
| 199 if not first: | |
| 200 code += ' || ' | |
| 201 if r[0] == r[1]: | |
| 202 code += 'yych == %s' % r[0] | |
| 203 elif r[0] == 0: | |
| 204 code += 'yych <= %s' % r[1] | |
| 205 elif r[1] == 255: # FIXME: this should depend on the char type maybe?? | |
| 206 code += 'yych >= %s' % r[0] | |
| 207 else: | 204 else: |
| 208 code += '(yych >= %s && yych <= %s)' % (r[0], r[1]) | 205 yield ('LATIN_1', r) |
| 209 first = False | |
| 210 code += ')' | |
| 211 return code | |
| 212 | 206 |
| 213 __printable_cache = { | 207 __printable_cache = { |
| 214 ord('\t') : '\\t', | 208 ord('\t') : '\\t', |
| 215 ord('\n') : '\\n', | 209 ord('\n') : '\\n', |
| 216 ord('\r') : '\\r', | 210 ord('\r') : '\\r', |
| 217 } | 211 } |
| 218 | 212 |
| 219 @staticmethod | 213 @staticmethod |
| 220 def __range_str(r): | 214 def __range_str(r): |
| 221 if TransitionKey.__is_class_range(r): | 215 if TransitionKey.__is_class_range(r): |
| 222 for name, v in TransitionKey.__class_bounds.items(): | 216 return TransitionKey.__class_name(r) |
| 223 if r == v: return name | |
| 224 assert False | |
| 225 def to_str(x): | 217 def to_str(x): |
| 226 assert TransitionKey.__in_latin_1(x) | 218 assert TransitionKey.__in_latin_1(x) |
| 227 if not x in TransitionKey.__printable_cache: | 219 if not x in TransitionKey.__printable_cache: |
| 228 res = "'%s'" % chr(x) if chr(x) in printable else str(x) | 220 res = "'%s'" % chr(x) if chr(x) in printable else str(x) |
| 229 TransitionKey.__printable_cache[x] = res | 221 TransitionKey.__printable_cache[x] = res |
| 230 return TransitionKey.__printable_cache[x] | 222 return TransitionKey.__printable_cache[x] |
| 231 if r[0] == r[1]: | 223 if r[0] == r[1]: |
| 232 return "%s" % to_str(r[0]) | 224 return "%s" % to_str(r[0]) |
| 233 else: | 225 else: |
| 234 return "[%s-%s]" % (to_str(r[0]), to_str(r[1])) | 226 return "[%s-%s]" % (to_str(r[0]), to_str(r[1])) |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 elif last[1] + 1 < r[0]: | 336 elif last[1] + 1 < r[0]: |
| 345 inverted.append((last[1] + 1, r[0] - 1)) | 337 inverted.append((last[1] + 1, r[0] - 1)) |
| 346 last = r | 338 last = r |
| 347 upper_bound = latin_1[1] | 339 upper_bound = latin_1[1] |
| 348 if last == None: | 340 if last == None: |
| 349 inverted.append(latin_1) | 341 inverted.append(latin_1) |
| 350 elif last[1] < upper_bound: | 342 elif last[1] < upper_bound: |
| 351 inverted.append((last[1] + 1, upper_bound)) | 343 inverted.append((last[1] + 1, upper_bound)) |
| 352 inverted += list(classes) | 344 inverted += list(classes) |
| 353 return inverted | 345 return inverted |
| OLD | NEW |