| 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 logging |
| 28 import argparse | 29 import argparse |
| 29 from dot_utilities import * | 30 from dot_utilities import * |
| 30 from nfa import Nfa | 31 from nfa import Nfa |
| 31 from nfa_builder import NfaBuilder | 32 from nfa_builder import NfaBuilder |
| 32 from dfa import Dfa | 33 from dfa import Dfa |
| 33 from dfa_minimizer import DfaMinimizer | 34 from dfa_minimizer import DfaMinimizer |
| 34 from rule_parser import RuleParser, RuleParserState, RuleProcessor | 35 from rule_parser import RuleParser, RuleParserState, RuleProcessor |
| 35 from code_generator import CodeGenerator | 36 from code_generator import CodeGenerator |
| 36 | 37 |
| 37 file_template = ''' | 38 file_template = ''' |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 parser.add_argument('--code') | 135 parser.add_argument('--code') |
| 135 parser.add_argument('--encoding', default='latin1') | 136 parser.add_argument('--encoding', default='latin1') |
| 136 parser.add_argument('--no-optimize-default', action='store_true') | 137 parser.add_argument('--no-optimize-default', action='store_true') |
| 137 parser.add_argument('--no-minimize-default', action='store_true') | 138 parser.add_argument('--no-minimize-default', action='store_true') |
| 138 parser.add_argument('--no-verify-default', action='store_true') | 139 parser.add_argument('--no-verify-default', action='store_true') |
| 139 parser.add_argument('--no-inline', action='store_true') | 140 parser.add_argument('--no-inline', action='store_true') |
| 140 parser.add_argument('--verbose', action='store_true') | 141 parser.add_argument('--verbose', action='store_true') |
| 141 parser.add_argument('--debug-code', action='store_true') | 142 parser.add_argument('--debug-code', action='store_true') |
| 142 parser.add_argument('--profile', action='store_true') | 143 parser.add_argument('--profile', action='store_true') |
| 143 parser.add_argument('--rule-html') | 144 parser.add_argument('--rule-html') |
| 144 parser.add_argument('--count-paths', action='store_true') | |
| 145 args = parser.parse_args() | 145 args = parser.parse_args() |
| 146 | 146 |
| 147 minimize_default = not args.no_minimize_default | 147 minimize_default = not args.no_minimize_default |
| 148 verbose = args.verbose | 148 if args.verbose: |
| 149 logging.basicConfig(level=logging.INFO) |
| 149 | 150 |
| 150 if args.profile: | 151 if args.profile: |
| 151 profiler = start_profiling() | 152 profiler = start_profiling() |
| 152 | 153 |
| 153 re_file = args.re | 154 re_file = args.re |
| 154 if verbose: | 155 logging.info("parsing %s" % re_file) |
| 155 print "parsing %s" % re_file | |
| 156 with open(re_file, 'r') as f: | 156 with open(re_file, 'r') as f: |
| 157 rule_processor = RuleProcessor(f.read(), args.encoding) | 157 rule_processor = RuleProcessor(f.read(), args.encoding) |
| 158 | 158 |
| 159 if not args.no_optimize_default: | 159 if not args.no_optimize_default: |
| 160 rule_processor.default_automata().optimize_dfa(log = args.verbose) | 160 rule_processor.default_automata().optimize_dfa() |
| 161 | 161 |
| 162 if minimize_default: | 162 if minimize_default: |
| 163 if args.no_verify_default: | 163 if args.no_verify_default: |
| 164 DfaMinimizer.set_verify(False) | 164 DfaMinimizer.set_verify(False) |
| 165 dfa = rule_processor.default_automata().dfa() | 165 dfa = rule_processor.default_automata().dfa() |
| 166 mdfa = rule_processor.default_automata().minimal_dfa() | 166 mdfa = rule_processor.default_automata().minimal_dfa() |
| 167 if verbose: | 167 logging.info("nodes reduced from %s to %s" % ( |
| 168 print "nodes reduced from %s to %s" % ( | 168 dfa.node_count(), mdfa.node_count())) |
| 169 dfa.node_count(), mdfa.node_count()) | |
| 170 DfaMinimizer.set_verify(True) | 169 DfaMinimizer.set_verify(True) |
| 171 | 170 |
| 172 if args.count_paths: | |
| 173 path_count = 0 | |
| 174 print 'counting' | |
| 175 for path in rule_processor.default_automata().minimal_dfa().path_iter(): | |
| 176 path_count += 1 | |
| 177 print 'done', path_count | |
| 178 | |
| 179 html_file = args.html | 171 html_file = args.html |
| 180 if html_file: | 172 if html_file: |
| 181 html = generate_html( | 173 html = generate_html( |
| 182 rule_processor, minimize_default, not args.no_merge_html) | 174 rule_processor, minimize_default, not args.no_merge_html) |
| 183 with open(args.html, 'w') as f: | 175 with open(args.html, 'w') as f: |
| 184 f.write(html) | 176 f.write(html) |
| 185 if verbose: | 177 logging.info("wrote html to %s" % html_file) |
| 186 print "wrote html to %s" % html_file | |
| 187 | 178 |
| 188 rule_html_file = args.rule_html | 179 rule_html_file = args.rule_html |
| 189 if rule_html_file: | 180 if rule_html_file: |
| 190 html = generate_rule_tree_html(rule_processor) | 181 html = generate_rule_tree_html(rule_processor) |
| 191 with open(rule_html_file, 'w') as f: | 182 with open(rule_html_file, 'w') as f: |
| 192 f.write(html) | 183 f.write(html) |
| 193 if verbose: | 184 logging.info("wrote html to %s" % rule_html_file) |
| 194 print "wrote html to %s" % rule_html_file | |
| 195 | 185 |
| 196 code_file = args.code | 186 code_file = args.code |
| 197 if code_file: | 187 if code_file: |
| 198 code_generator = CodeGenerator(rule_processor, | 188 code_generator = CodeGenerator(rule_processor, |
| 199 minimize_default = minimize_default, | 189 minimize_default = minimize_default, |
| 200 log = verbose, | |
| 201 inline = not args.no_inline, | 190 inline = not args.no_inline, |
| 202 debug_print = args.debug_code) | 191 debug_print = args.debug_code) |
| 203 code = code_generator.process() | 192 code = code_generator.process() |
| 204 with open(code_file, 'w') as f: | 193 with open(code_file, 'w') as f: |
| 205 f.write(code) | 194 f.write(code) |
| 206 if verbose: | 195 logging.info("wrote code to %s" % code_file) |
| 207 print "wrote code to %s" % code_file | |
| 208 | 196 |
| 209 input_file = args.input | 197 input_file = args.input |
| 210 if input_file: | 198 if input_file: |
| 211 with open(input_file, 'r') as f: | 199 with open(input_file, 'r') as f: |
| 212 lex(rule_processor, f.read()) | 200 lex(rule_processor, f.read()) |
| 213 | 201 |
| 214 if args.profile: | 202 if args.profile: |
| 215 stop_profiling(profiler) | 203 stop_profiling(profiler) |
| OLD | NEW |