Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: tools/lexer_generator/generator.py

Issue 158823002: Experimental parser: refactor TransitionKey to use Term (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/lexer_generator/code_generator.py ('k') | tools/lexer_generator/regex_parser.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads)) 99 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads))
100 return file_template % body 100 return file_template % body
101 101
102 def generate_code(rule_processor, minimize_default): 102 def generate_code(rule_processor, minimize_default):
103 return CodeGenerator.rule_processor_to_code(rule_processor, minimize_default) 103 return CodeGenerator.rule_processor_to_code(rule_processor, minimize_default)
104 104
105 def lex(rule_processor, string): 105 def lex(rule_processor, string):
106 for t in rule_processor.default_automata().dfa().lex(string + '\0'): 106 for t in rule_processor.default_automata().dfa().lex(string + '\0'):
107 print t 107 print t
108 108
109 def start_profiling():
110 import cProfile
111 pr = cProfile.Profile()
112 pr.enable()
113 return pr
114
115 def stop_profiling(pr):
116 import pstats, StringIO
117 pr.disable()
118 s = StringIO.StringIO()
119 sortby = 'cumulative'
120 ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
121 ps.print_stats()
122 print s.getvalue()
123
109 if __name__ == '__main__': 124 if __name__ == '__main__':
110 125
111 parser = argparse.ArgumentParser() 126 parser = argparse.ArgumentParser()
112 parser.add_argument('--html') 127 parser.add_argument('--html')
113 parser.add_argument('--re', default='src/lexer/lexer_py.re') 128 parser.add_argument('--re', default='src/lexer/lexer_py.re')
114 parser.add_argument('--input') 129 parser.add_argument('--input')
115 parser.add_argument('--code') 130 parser.add_argument('--code')
116 parser.add_argument('--encoding', default='latin1') 131 parser.add_argument('--encoding', default='latin1')
117 parser.add_argument('--no-optimize-default', action='store_true') 132 parser.add_argument('--no-optimize-default', action='store_true')
118 parser.add_argument('--no-minimize-default', action='store_true') 133 parser.add_argument('--no-minimize-default', action='store_true')
119 parser.add_argument('--no-verify-default', action='store_true') 134 parser.add_argument('--no-verify-default', action='store_true')
120 parser.add_argument('--no-inline', action='store_true') 135 parser.add_argument('--no-inline', action='store_true')
121 parser.add_argument('--verbose', action='store_true') 136 parser.add_argument('--verbose', action='store_true')
122 parser.add_argument('--debug-code', action='store_true') 137 parser.add_argument('--debug-code', action='store_true')
138 parser.add_argument('--profile', action='store_true')
123 parser.add_argument('--rule-html') 139 parser.add_argument('--rule-html')
124 args = parser.parse_args() 140 args = parser.parse_args()
125 141
126 minimize_default = not args.no_minimize_default 142 minimize_default = not args.no_minimize_default
127 verbose = args.verbose 143 verbose = args.verbose
128 144
145 if args.profile:
146 profiler = start_profiling()
147
129 re_file = args.re 148 re_file = args.re
130 if verbose: 149 if verbose:
131 print "parsing %s" % re_file 150 print "parsing %s" % re_file
132 with open(re_file, 'r') as f: 151 with open(re_file, 'r') as f:
133 rule_processor = RuleProcessor(f.read(), args.encoding) 152 rule_processor = RuleProcessor(f.read(), args.encoding)
134 153
135 if not args.no_optimize_default: 154 if not args.no_optimize_default:
136 rule_processor.default_automata().optimize_dfa(log = args.verbose) 155 rule_processor.default_automata().optimize_dfa(log = args.verbose)
137 156
138 if minimize_default: 157 if minimize_default:
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 code = code_generator.process() 190 code = code_generator.process()
172 with open(code_file, 'w') as f: 191 with open(code_file, 'w') as f:
173 f.write(code) 192 f.write(code)
174 if verbose: 193 if verbose:
175 print "wrote code to %s" % code_file 194 print "wrote code to %s" % code_file
176 195
177 input_file = args.input 196 input_file = args.input
178 if input_file: 197 if input_file:
179 with open(input_file, 'r') as f: 198 with open(input_file, 'r') as f:
180 lex(rule_processor, f.read()) 199 lex(rule_processor, f.read())
200
201 if args.profile:
202 stop_profiling(profiler)
OLDNEW
« no previous file with comments | « tools/lexer_generator/code_generator.py ('k') | tools/lexer_generator/regex_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698