| 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 argparse | 28 import argparse |
| 29 from dot_utilities import * |
| 29 from nfa import Nfa | 30 from nfa import Nfa |
| 30 from nfa_builder import NfaBuilder | 31 from nfa_builder import NfaBuilder |
| 31 from dfa import Dfa, DfaMinimizer | 32 from dfa import Dfa, DfaMinimizer |
| 32 from rule_parser import RuleParser, RuleParserState, RuleProcessor | 33 from rule_parser import RuleParser, RuleParserState, RuleProcessor |
| 33 from code_generator import CodeGenerator | 34 from code_generator import CodeGenerator |
| 34 | 35 |
| 35 file_template = ''' | 36 file_template = ''' |
| 36 <html> | 37 <html> |
| 37 <head> | 38 <head> |
| 38 <script src="viz.js"></script> | 39 <script src="viz.js"></script> |
| (...skipping 27 matching lines...) Expand all Loading... |
| 66 | 67 |
| 67 def generate_html(rule_processor, minimize_default): | 68 def generate_html(rule_processor, minimize_default): |
| 68 scripts = [] | 69 scripts = [] |
| 69 loads = [] | 70 loads = [] |
| 70 for i, (name, automata) in enumerate(list(rule_processor.automata_iter())): | 71 for i, (name, automata) in enumerate(list(rule_processor.automata_iter())): |
| 71 (nfa, dfa) = (automata.nfa(), automata.dfa()) | 72 (nfa, dfa) = (automata.nfa(), automata.dfa()) |
| 72 mdfa = None | 73 mdfa = None |
| 73 if name != 'default' or minimize_default: | 74 if name != 'default' or minimize_default: |
| 74 mdfa = automata.minimal_dfa() | 75 mdfa = automata.minimal_dfa() |
| 75 (nfa_i, dfa_i, mdfa_i) = ("nfa_%d" % i, "dfa_%d" % i, "mdfa_%d" % i) | 76 (nfa_i, dfa_i, mdfa_i) = ("nfa_%d" % i, "dfa_%d" % i, "mdfa_%d" % i) |
| 76 scripts.append(script_template % (nfa_i, nfa.to_dot())) | 77 scripts.append(script_template % (nfa_i, automaton_to_dot(nfa))) |
| 77 loads.append(load_template % ("nfa [%s]" % name, nfa_i)) | 78 loads.append(load_template % ("nfa [%s]" % name, nfa_i)) |
| 78 scripts.append(script_template % (dfa_i, dfa.to_dot())) | 79 scripts.append(script_template % (dfa_i, automaton_to_dot(dfa))) |
| 79 loads.append(load_template % ("dfa [%s]" % name, dfa_i)) | 80 loads.append(load_template % ("dfa [%s]" % name, dfa_i)) |
| 80 if mdfa and mdfa.node_count() != dfa.node_count(): | 81 if mdfa and mdfa.node_count() != dfa.node_count(): |
| 81 scripts.append(script_template % (mdfa_i, mdfa.to_dot())) | 82 scripts.append(script_template % (mdfa_i, automaton_to_dot(mdfa))) |
| 82 loads.append(load_template % ("mdfa [%s]" % name, mdfa_i)) | 83 loads.append(load_template % ("mdfa [%s]" % name, mdfa_i)) |
| 83 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads)) | 84 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads)) |
| 84 return file_template % body | 85 return file_template % body |
| 85 | 86 |
| 86 def generate_rule_tree_html(rule_processor): | 87 def generate_rule_tree_html(rule_processor): |
| 87 scripts = [] | 88 scripts = [] |
| 88 loads = [] | 89 loads = [] |
| 90 mapper = lambda x : map_characters(rule_processor.encoding(), x) |
| 89 for i, (name, alias) in enumerate(list(rule_processor.alias_iter())): | 91 for i, (name, alias) in enumerate(list(rule_processor.alias_iter())): |
| 90 alias_i = "alias_%d" % i | 92 alias_i = "alias_%d" % i |
| 91 dot = alias.to_dot() | 93 dot = term_to_dot(alias, mapper) |
| 92 scripts.append(script_template % (alias_i, dot)) | 94 scripts.append(script_template % (alias_i, dot)) |
| 93 loads.append(load_template % ("alias [%s]" % name, alias_i)) | 95 loads.append(load_template % ("alias [%s]" % name, alias_i)) |
| 94 for i, (name, automata) in enumerate(list(rule_processor.automata_iter())): | 96 for i, (name, automata) in enumerate(list(rule_processor.automata_iter())): |
| 95 rule_i = "rule_%d" % i | 97 rule_i = "rule_%d" % i |
| 96 dot = automata.rule_term().to_dot() | 98 dot = term_to_dot(automata.rule_term(), mapper) |
| 97 scripts.append(script_template % (rule_i, dot)) | 99 scripts.append(script_template % (rule_i, dot)) |
| 98 loads.append(load_template % ("rule [%s]" % name, rule_i)) | 100 loads.append(load_template % ("rule [%s]" % name, rule_i)) |
| 99 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads)) | 101 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads)) |
| 100 return file_template % body | 102 return file_template % body |
| 101 | 103 |
| 102 def generate_code(rule_processor, minimize_default): | 104 def generate_code(rule_processor, minimize_default): |
| 103 return CodeGenerator.rule_processor_to_code(rule_processor, minimize_default) | 105 return CodeGenerator.rule_processor_to_code(rule_processor, minimize_default) |
| 104 | 106 |
| 105 def lex(rule_processor, string): | 107 def lex(rule_processor, string): |
| 106 for t in rule_processor.default_automata().dfa().lex(string + '\0'): | 108 for t in rule_processor.default_automata().dfa().lex(string + '\0'): |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 if verbose: | 195 if verbose: |
| 194 print "wrote code to %s" % code_file | 196 print "wrote code to %s" % code_file |
| 195 | 197 |
| 196 input_file = args.input | 198 input_file = args.input |
| 197 if input_file: | 199 if input_file: |
| 198 with open(input_file, 'r') as f: | 200 with open(input_file, 'r') as f: |
| 199 lex(rule_processor, f.read()) | 201 lex(rule_processor, f.read()) |
| 200 | 202 |
| 201 if args.profile: | 203 if args.profile: |
| 202 stop_profiling(profiler) | 204 stop_profiling(profiler) |
| OLD | NEW |