| 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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 try: | 210 try: |
| 211 parser.parser.parse(data, lexer=parser.lexer.lexer) | 211 parser.parser.parse(data, lexer=parser.lexer.lexer) |
| 212 except Exception: | 212 except Exception: |
| 213 RuleParser.__static_instance = None | 213 RuleParser.__static_instance = None |
| 214 raise | 214 raise |
| 215 parser.__state = None | 215 parser.__state = None |
| 216 assert parser_state.transitions <= set(parser_state.rules.keys()) | 216 assert parser_state.transitions <= set(parser_state.rules.keys()) |
| 217 | 217 |
| 218 class RuleProcessor(object): | 218 class RuleProcessor(object): |
| 219 | 219 |
| 220 def __init__(self, parser_state): | 220 def __init__(self, parser_state, minimize_dfa): |
| 221 self.__automata = {} | 221 self.__automata = {} |
| 222 self.__process_parser_state(parser_state) | 222 self.__process_parser_state(parser_state, minimize_dfa) |
| 223 | 223 |
| 224 @staticmethod | 224 @staticmethod |
| 225 def parse(string): | 225 def parse(string, minimize_dfa = True): |
| 226 parser_state = RuleParserState() | 226 parser_state = RuleParserState() |
| 227 RuleParser.parse(string, parser_state) | 227 RuleParser.parse(string, parser_state) |
| 228 return RuleProcessor(parser_state) | 228 return RuleProcessor(parser_state, minimize_dfa) |
| 229 | 229 |
| 230 def automata_iter(self): | 230 def automata_iter(self): |
| 231 return iter(self.__automata.items()) | 231 return iter(self.__automata.items()) |
| 232 | 232 |
| 233 def default_automata(self): | 233 def default_automata(self): |
| 234 return self.__automata['default'] | 234 return self.__automata['default'] |
| 235 | 235 |
| 236 def lex(self, string): | 236 def lex(self, string): |
| 237 dfa = self.default_automata().dfa() | 237 dfa = self.default_automata().dfa() |
| 238 return dfa.lex(string) | 238 return dfa.lex(string) |
| 239 | 239 |
| 240 class Automata(object): | 240 class Automata(object): |
| 241 | 241 |
| 242 def __init__(self, nfa): | 242 def __init__(self, nfa, minimize_dfa): |
| 243 (start, dfa_nodes) = nfa.compute_dfa() | 243 (start, dfa_nodes) = nfa.compute_dfa() |
| 244 self.__nfa = nfa | 244 self.__nfa = nfa |
| 245 self.__dfa = Dfa(start, dfa_nodes) | 245 self.__dfa = Dfa(start, dfa_nodes) |
| 246 self.__minimial_dfa = self.__dfa.minimize() | 246 self.__minimial_dfa = self.__dfa.minimize() if minimize_dfa else None |
| 247 | 247 |
| 248 def nfa(self): | 248 def nfa(self): |
| 249 return self.__nfa | 249 return self.__nfa |
| 250 | 250 |
| 251 def dfa(self): | 251 def dfa(self): |
| 252 return self.__dfa | 252 return self.__dfa |
| 253 | 253 |
| 254 def minimal_dfa(self): | 254 def minimal_dfa(self): |
| 255 return self.__minimial_dfa | 255 return self.__minimial_dfa |
| 256 | 256 |
| 257 def __process_parser_state(self, parser_state): | 257 def __process_parser_state(self, parser_state, minimize_dfa): |
| 258 rule_map = {} | 258 rule_map = {} |
| 259 builder = NfaBuilder() | 259 builder = NfaBuilder() |
| 260 builder.set_character_classes(parser_state.character_classes) | 260 builder.set_character_classes(parser_state.character_classes) |
| 261 assert 'default' in parser_state.rules | 261 assert 'default' in parser_state.rules |
| 262 def process(k, v): | 262 def process(k, v): |
| 263 graphs = [] | 263 graphs = [] |
| 264 continues = 0 | 264 continues = 0 |
| 265 for (graph, precedence, code, transition) in v['regex']: | 265 for (graph, precedence, code, transition) in v['regex']: |
| 266 default_code = v['default_action'] | 266 default_code = v['default_action'] |
| 267 if code or default_code: | 267 if code or default_code: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 291 graph = NfaBuilder.or_graphs(graphs) | 291 graph = NfaBuilder.or_graphs(graphs) |
| 292 rule_map[k] = graph | 292 rule_map[k] = graph |
| 293 # process first the subgraphs, then the default graph | 293 # process first the subgraphs, then the default graph |
| 294 for k, v in parser_state.rules.items(): | 294 for k, v in parser_state.rules.items(): |
| 295 if k == 'default': continue | 295 if k == 'default': continue |
| 296 process(k, v) | 296 process(k, v) |
| 297 process('default', parser_state.rules['default']) | 297 process('default', parser_state.rules['default']) |
| 298 # build the automata | 298 # build the automata |
| 299 for rule_name, graph in rule_map.items(): | 299 for rule_name, graph in rule_map.items(): |
| 300 nfa = builder.nfa(graph) | 300 nfa = builder.nfa(graph) |
| 301 self.__automata[rule_name] = RuleProcessor.Automata(nfa) | 301 self.__automata[rule_name] = RuleProcessor.Automata(nfa, minimize_dfa) |
| 302 self.default_action = parser_state.rules['default']['default_action'] | 302 self.default_action = parser_state.rules['default']['default_action'] |
| OLD | NEW |