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

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

Issue 141293003: Experimental parser: visualize rule trees. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: . Created 6 years, 11 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 | « no previous file | tools/lexer_generator/nfa_builder.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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 if name != 'default' or minimize_default: 73 if name != 'default' or minimize_default:
74 mdfa = automata.minimal_dfa() 74 mdfa = automata.minimal_dfa()
75 (nfa_i, dfa_i, mdfa_i) = ("nfa_%d" % i, "dfa_%d" % i, "mdfa_%d" % i) 75 (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())) 76 scripts.append(script_template % (nfa_i, nfa.to_dot()))
77 loads.append(load_template % ("nfa [%s]" % name, nfa_i)) 77 loads.append(load_template % ("nfa [%s]" % name, nfa_i))
78 scripts.append(script_template % (dfa_i, dfa.to_dot())) 78 scripts.append(script_template % (dfa_i, dfa.to_dot()))
79 loads.append(load_template % ("dfa [%s]" % name, dfa_i)) 79 loads.append(load_template % ("dfa [%s]" % name, dfa_i))
80 if mdfa and mdfa.node_count() != dfa.node_count(): 80 if mdfa and mdfa.node_count() != dfa.node_count():
81 scripts.append(script_template % (mdfa_i, mdfa.to_dot())) 81 scripts.append(script_template % (mdfa_i, mdfa.to_dot()))
82 loads.append(load_template % ("mdfa [%s]" % name, mdfa_i)) 82 loads.append(load_template % ("mdfa [%s]" % name, mdfa_i))
83 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads)) 83 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads))
84 return file_template % body
85
86 def generate_rule_tree_html(rule_processor):
87 scripts = []
88 loads = []
89 rule_tree_dots = rule_processor.rule_tree_dots()
90 for i, (rule, dot) in enumerate(rule_tree_dots.items()):
91 rule_i = "rule_%d" % i
92 scripts.append(script_template % (rule_i, dot))
93 loads.append(load_template % ("rules [%s]" % rule, rule_i))
94 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads))
84 return file_template % body 95 return file_template % body
85 96
86 def generate_code(rule_processor, minimize_default): 97 def generate_code(rule_processor, minimize_default):
87 return CodeGenerator.rule_processor_to_code(rule_processor, minimize_default) 98 return CodeGenerator.rule_processor_to_code(rule_processor, minimize_default)
88 99
89 def lex(rule_processor, string): 100 def lex(rule_processor, string):
90 for t in rule_processor.default_automata().dfa().lex(string + '\0'): 101 for t in rule_processor.default_automata().dfa().lex(string + '\0'):
91 print t 102 print t
92 103
93 if __name__ == '__main__': 104 if __name__ == '__main__':
94 105
95 parser = argparse.ArgumentParser() 106 parser = argparse.ArgumentParser()
96 parser.add_argument('--html') 107 parser.add_argument('--html')
97 parser.add_argument('--re', default='src/lexer/lexer_py.re') 108 parser.add_argument('--re', default='src/lexer/lexer_py.re')
98 parser.add_argument('--input') 109 parser.add_argument('--input')
99 parser.add_argument('--code') 110 parser.add_argument('--code')
100 parser.add_argument('--encoding', default='latin1') 111 parser.add_argument('--encoding', default='latin1')
101 parser.add_argument('--no-optimize-default', action='store_true') 112 parser.add_argument('--no-optimize-default', action='store_true')
102 parser.add_argument('--no-minimize-default', action='store_true') 113 parser.add_argument('--no-minimize-default', action='store_true')
103 parser.add_argument('--no-verify-default', action='store_true') 114 parser.add_argument('--no-verify-default', action='store_true')
104 parser.add_argument('--no-inline', action='store_true') 115 parser.add_argument('--no-inline', action='store_true')
105 parser.add_argument('--verbose', action='store_true') 116 parser.add_argument('--verbose', action='store_true')
106 parser.add_argument('--debug-code', action='store_true') 117 parser.add_argument('--debug-code', action='store_true')
118 parser.add_argument('--rule-html')
107 args = parser.parse_args() 119 args = parser.parse_args()
108 120
109 minimize_default = not args.no_minimize_default 121 minimize_default = not args.no_minimize_default
110 verbose = args.verbose 122 verbose = args.verbose
111 123
112 re_file = args.re 124 re_file = args.re
113 if verbose: 125 if verbose:
114 print "parsing %s" % re_file 126 print "parsing %s" % re_file
115 with open(re_file, 'r') as f: 127 with open(re_file, 'r') as f:
116 rule_processor = RuleProcessor.parse(f.read(), args.encoding) 128 rule_processor = RuleProcessor.parse(f.read(), args.encoding)
(...skipping 12 matching lines...) Expand all
129 DfaMinimizer.set_verify(True) 141 DfaMinimizer.set_verify(True)
130 142
131 html_file = args.html 143 html_file = args.html
132 if html_file: 144 if html_file:
133 html = generate_html(rule_processor, minimize_default) 145 html = generate_html(rule_processor, minimize_default)
134 with open(args.html, 'w') as f: 146 with open(args.html, 'w') as f:
135 f.write(html) 147 f.write(html)
136 if verbose: 148 if verbose:
137 print "wrote html to %s" % html_file 149 print "wrote html to %s" % html_file
138 150
151 rule_html_file = args.rule_html
152 if rule_html_file:
153 html = generate_rule_tree_html(rule_processor)
154 with open(rule_html_file, 'w') as f:
155 f.write(html)
156 if verbose:
157 print "wrote html to %s" % rule_html_file
158
139 code_file = args.code 159 code_file = args.code
140 if code_file: 160 if code_file:
141 code_generator = CodeGenerator(rule_processor, 161 code_generator = CodeGenerator(rule_processor,
142 minimize_default = minimize_default, 162 minimize_default = minimize_default,
143 log = verbose, 163 log = verbose,
144 inline = not args.no_inline, 164 inline = not args.no_inline,
145 debug_print = args.debug_code) 165 debug_print = args.debug_code)
146 code = code_generator.process() 166 code = code_generator.process()
147 with open(code_file, 'w') as f: 167 with open(code_file, 'w') as f:
148 f.write(code) 168 f.write(code)
149 if verbose: 169 if verbose:
150 print "wrote code to %s" % code_file 170 print "wrote code to %s" % code_file
151 171
152 input_file = args.input 172 input_file = args.input
153 if input_file: 173 if input_file:
154 with open(input_file, 'r') as f: 174 with open(input_file, 'r') as f:
155 lex(rule_processor, f.read()) 175 lex(rule_processor, f.read())
OLDNEW
« no previous file with comments | « no previous file | tools/lexer_generator/nfa_builder.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698