| 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 |
| 11 # with the distribution. | 11 # with the distribution. |
| 12 # * Neither the name of Google Inc. nor the names of its | 12 # * Neither the name of Google Inc. nor the names of its |
| 13 # contributors may be used to endorse or promote products derived | 13 # contributors may be used to endorse or promote products derived |
| 14 # from this software without specific prior written permission. | 14 # from this software without specific prior written permission. |
| 15 # | 15 # |
| 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 import ply.lex as lex | 28 import ply.lex as lex |
| 29 import ply.yacc as yacc | 29 import ply.yacc as yacc |
| 30 from action import Term, Action | 30 from term import Term |
| 31 from regex_parser import RegexParser, ParserBuilder | 31 from regex_parser import RegexParser, ParserBuilder |
| 32 from automaton import Action |
| 32 from nfa_builder import NfaBuilder | 33 from nfa_builder import NfaBuilder |
| 33 from dfa import Dfa | 34 from dfa import Dfa |
| 34 from dfa_optimizer import DfaOptimizer | 35 from dfa_optimizer import DfaOptimizer |
| 35 from transition_keys import TransitionKey, KeyEncoding | 36 from dfa_minimizer import DfaMinimizer |
| 37 from transition_key import TransitionKey, KeyEncoding |
| 36 | 38 |
| 37 class RuleLexer: | 39 class RuleLexer: |
| 38 | 40 |
| 39 tokens = ( | 41 tokens = ( |
| 40 'DEFAULT_ACTION', | 42 'DEFAULT_ACTION', |
| 41 'EPSILON', | 43 'EPSILON', |
| 42 'EOS', | 44 'EOS', |
| 43 'CATCH_ALL', | 45 'CATCH_ALL', |
| 44 | 46 |
| 45 'IDENTIFIER', | 47 'IDENTIFIER', |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 return self.__dfa | 383 return self.__dfa |
| 382 | 384 |
| 383 def optimize_dfa(self, log = False): | 385 def optimize_dfa(self, log = False): |
| 384 assert not self.__dfa | 386 assert not self.__dfa |
| 385 assert not self.__minimial_dfa | 387 assert not self.__minimial_dfa |
| 386 self.__dfa = DfaOptimizer.optimize(self.minimal_dfa(), log) | 388 self.__dfa = DfaOptimizer.optimize(self.minimal_dfa(), log) |
| 387 self.__minimial_dfa = None | 389 self.__minimial_dfa = None |
| 388 | 390 |
| 389 def minimal_dfa(self): | 391 def minimal_dfa(self): |
| 390 if not self.__minimial_dfa: | 392 if not self.__minimial_dfa: |
| 391 self.__minimial_dfa = self.dfa().minimize() | 393 self.__minimial_dfa = DfaMinimizer(self.dfa()).minimize() |
| 392 return self.__minimial_dfa | 394 return self.__minimial_dfa |
| 393 | 395 |
| 394 def __process_parser_state(self): | 396 def __process_parser_state(self): |
| 395 parser_state = self.__parser_state | 397 parser_state = self.__parser_state |
| 396 assert 'default' in parser_state.rules, "default lexer state required" | 398 assert 'default' in parser_state.rules, "default lexer state required" |
| 397 # Check that we don't transition to a nonexistent state. | 399 # Check that we don't transition to a nonexistent state. |
| 398 assert parser_state.transitions <= set(parser_state.rules.keys()) | 400 assert parser_state.transitions <= set(parser_state.rules.keys()) |
| 399 rule_map = {} | 401 rule_map = {} |
| 400 # process all subgraphs | 402 # process all subgraphs |
| 401 for tree_name, v in parser_state.rules.items(): | 403 for tree_name, v in parser_state.rules.items(): |
| 402 assert v['trees'], "lexer state %s is empty" % tree_name | 404 assert v['trees'], "lexer state %s is empty" % tree_name |
| 403 rule_map[tree_name] = NfaBuilder.or_terms(v['trees']) | 405 rule_map[tree_name] = NfaBuilder.or_terms(v['trees']) |
| 404 # build the automata | 406 # build the automata |
| 405 for name, tree in rule_map.items(): | 407 for name, tree in rule_map.items(): |
| 406 self.__automata[name] = RuleProcessor.Automata( | 408 self.__automata[name] = RuleProcessor.Automata( |
| 407 parser_state.encoding, parser_state.character_classes, rule_map, name) | 409 parser_state.encoding, parser_state.character_classes, rule_map, name) |
| 408 # process default_action | 410 # process default_action |
| 409 default_action = parser_state.rules['default']['default_action'] | 411 default_action = parser_state.rules['default']['default_action'] |
| 410 self.__default_action = Action(default_action, 0) | 412 self.__default_action = Action(default_action, 0) |
| OLD | NEW |