| 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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 but a newly-created "mock" character range which is separate from all other | 175 but a newly-created "mock" character range which is separate from all other |
| 176 character ranges.''' | 176 character ranges.''' |
| 177 def get_bounds(name): | 177 def get_bounds(name): |
| 178 bound = TransitionKey.__unique_key_counter | 178 bound = TransitionKey.__unique_key_counter |
| 179 TransitionKey.__unique_key_counter -= 1 | 179 TransitionKey.__unique_key_counter -= 1 |
| 180 return [(bound, bound)] | 180 return [(bound, bound)] |
| 181 name = '__' + name | 181 name = '__' + name |
| 182 return TransitionKey.__cached_key(None, name, get_bounds) | 182 return TransitionKey.__cached_key(None, name, get_bounds) |
| 183 | 183 |
| 184 @staticmethod | 184 @staticmethod |
| 185 def __process_graph(encoding, graph, ranges, key_map): | 185 def __process_term(encoding, term, ranges, key_map): |
| 186 key = graph[0] | 186 key = term.name() |
| 187 args = term.args() |
| 187 if key == 'RANGE': | 188 if key == 'RANGE': |
| 188 ranges.append((ord(graph[1]), ord(graph[2]))) | 189 ranges.append((ord(args[0]), ord(args[1]))) |
| 189 elif key == 'LITERAL': | 190 elif key == 'LITERAL': |
| 190 ranges.append((ord(graph[1]), ord(graph[1]))) | 191 ranges.append((ord(args[0]), ord(args[0]))) |
| 191 elif key == 'CAT': | 192 elif key == 'CAT': |
| 192 for x in [graph[1], graph[2]]: | 193 for x in [args[0], args[1]]: |
| 193 TransitionKey.__process_graph(encoding, x, ranges, key_map) | 194 TransitionKey.__process_term(encoding, x, ranges, key_map) |
| 194 elif key == 'CHARACTER_CLASS': | 195 elif key == 'CHARACTER_CLASS': |
| 195 class_name = graph[1] | 196 class_name = args[0] |
| 196 if encoding.class_range(class_name): | 197 if encoding.class_range(class_name): |
| 197 r = encoding.class_range(class_name) | 198 r = encoding.class_range(class_name) |
| 198 if class_name in key_map: | 199 if class_name in key_map: |
| 199 assert key_map[class_name] == TransitionKey(encoding, [r]) | 200 assert key_map[class_name] == TransitionKey(encoding, [r]) |
| 200 ranges.append(r) | 201 ranges.append(r) |
| 201 elif encoding.predefined_range_iter(class_name): | 202 elif encoding.predefined_range_iter(class_name): |
| 202 rs = list(encoding.predefined_range_iter(class_name)) | 203 rs = list(encoding.predefined_range_iter(class_name)) |
| 203 if class_name in key_map: | 204 if class_name in key_map: |
| 204 assert key_map[class_name] == TransitionKey(encoding, rs) | 205 assert key_map[class_name] == TransitionKey(encoding, rs) |
| 205 ranges += rs | 206 ranges += rs |
| 206 elif class_name in key_map: | 207 elif class_name in key_map: |
| 207 ranges += key_map[class_name].__ranges | 208 ranges += key_map[class_name].__ranges |
| 208 else: | 209 else: |
| 209 raise Exception('unknown character class [%s]' % graph[1]) | 210 raise Exception('unknown character class [%s]' % args[0]) |
| 210 else: | 211 else: |
| 211 raise Exception('bad key [%s]' % key) | 212 raise Exception('bad key [%s]' % key) |
| 212 | 213 |
| 213 @staticmethod | 214 @staticmethod |
| 214 def character_class(encoding, graph, key_map): | 215 def character_class(encoding, term, key_map): |
| 215 '''Processes 'graph' (a representation of a character class in the rule | 216 '''Processes 'term' (a representation of a character class in the rule |
| 216 file), and constructs a TransitionKey based on it. 'key_map' contains | 217 file), and constructs a TransitionKey based on it. 'key_map' contains |
| 217 already constructed aliases for character classes (they can be used in the | 218 already constructed aliases for character classes (they can be used in the |
| 218 new character class). It is a map from strings (character class names) to | 219 new character class). It is a map from strings (character class names) to |
| 219 TransitionKeys. For example, graph might represent the character class | 220 TransitionKeys. For example, graph might represent the character class |
| 220 [a-z:digit:] where 'digit' is a previously constructed character class found | 221 [a-z:digit:] where 'digit' is a previously constructed character class found |
| 221 in "key_map".''' | 222 in "key_map".''' |
| 222 ranges = [] | 223 ranges = [] |
| 223 assert graph[0] == 'CLASS' or graph[0] == 'NOT_CLASS' | 224 assert term.name() == 'CLASS' or term.name() == 'NOT_CLASS' |
| 224 invert = graph[0] == 'NOT_CLASS' | 225 invert = term.name() == 'NOT_CLASS' |
| 225 TransitionKey.__process_graph(encoding, graph[1], ranges, key_map) | 226 assert len(term.args()) == 1 |
| 227 TransitionKey.__process_term(encoding, term.args()[0], ranges, key_map) |
| 226 return TransitionKey.__key_from_ranges(encoding, invert, ranges) | 228 return TransitionKey.__key_from_ranges(encoding, invert, ranges) |
| 227 | 229 |
| 228 def matches_char(self, char): | 230 def matches_char(self, char): |
| 229 char = ord(char) | 231 char = ord(char) |
| 230 assert char < 128 | 232 assert char < 128 |
| 231 for r in self.__ranges: | 233 for r in self.__ranges: |
| 232 if r[0] <= char and char <= r[1]: return True | 234 if r[0] <= char and char <= r[1]: return True |
| 233 return False | 235 return False |
| 234 | 236 |
| 235 def is_superset_of_key(self, key): | 237 def is_superset_of_key(self, key): |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 self.class_range('non_primary_whitespace')]) | 534 self.class_range('non_primary_whitespace')]) |
| 533 self.add_predefined_range( | 535 self.add_predefined_range( |
| 534 'letter', [(65, 90), (97, 122), self.class_range('non_primary_letter')]) | 536 'letter', [(65, 90), (97, 122), self.class_range('non_primary_letter')]) |
| 535 self.add_predefined_range( | 537 self.add_predefined_range( |
| 536 'line_terminator', | 538 'line_terminator', |
| 537 [(10, 10), (13, 13), self.class_range('non_primary_line_terminator')]) | 539 [(10, 10), (13, 13), self.class_range('non_primary_line_terminator')]) |
| 538 self.add_predefined_range( | 540 self.add_predefined_range( |
| 539 'identifier_part_not_letter', | 541 'identifier_part_not_letter', |
| 540 [(48, 57), (95, 95), | 542 [(48, 57), (95, 95), |
| 541 self.class_range('non_primary_identifier_part_not_letter')]) | 543 self.class_range('non_primary_identifier_part_not_letter')]) |
| OLD | NEW |