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

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

Issue 64213002: Experimental parser: some cleanup and assertions for rule_parser (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years, 1 month 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/action_test.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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 scripts.append(script_template % (dfa_i, dfa.to_dot())) 72 scripts.append(script_template % (dfa_i, dfa.to_dot()))
73 loads.append(load_template % ("nfa [%s]" % name, nfa_i)) 73 loads.append(load_template % ("nfa [%s]" % name, nfa_i))
74 loads.append(load_template % ("dfa [%s]" % name, dfa_i)) 74 loads.append(load_template % ("dfa [%s]" % name, dfa_i))
75 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads)) 75 body = "\n".join(scripts) + (load_outer_template % "\n".join(loads))
76 return file_template % body 76 return file_template % body
77 77
78 def process_rules(parser_state): 78 def process_rules(parser_state):
79 rule_map = {} 79 rule_map = {}
80 builder = NfaBuilder() 80 builder = NfaBuilder()
81 builder.set_character_classes(parser_state.character_classes) 81 builder.set_character_classes(parser_state.character_classes)
82 assert 'default' in parser_state.rules
82 for k, v in parser_state.rules.items(): 83 for k, v in parser_state.rules.items():
84 assert 'default' in v
83 graphs = [] 85 graphs = []
84 for (graph, precedence, code, action) in v['regex']: 86 for (graph, action) in v['regex']:
85 graphs.append(NfaBuilder.add_action(graph, (precedence, code, action))) 87 graphs.append(NfaBuilder.add_action(graph, action))
86 rule_map[k] = builder.nfa(NfaBuilder.or_graphs(graphs)) 88 rule_map[k] = NfaBuilder.or_graphs(graphs)
87 html_data = [] 89 html_data = []
88 for rule_name, nfa in rule_map.items(): 90 for rule_name, graph in rule_map.items():
91 nfa = builder.nfa(graph)
89 (start, dfa_nodes) = nfa.compute_dfa() 92 (start, dfa_nodes) = nfa.compute_dfa()
90 dfa = Dfa(start, dfa_nodes) 93 dfa = Dfa(start, dfa_nodes)
91 html_data.append((rule_name, nfa, dfa)) 94 html_data.append((rule_name, nfa, dfa))
92 return html_data 95 return html_data
93 96
94 if __name__ == '__main__': 97 if __name__ == '__main__':
95 98
96 parser = argparse.ArgumentParser() 99 parser = argparse.ArgumentParser()
97 parser.add_argument('--html') 100 parser.add_argument('--html')
98 args = parser.parse_args() 101 args = parser.parse_args()
99 102
100 re_file = 'src/lexer/lexer_py.re' 103 re_file = 'src/lexer/lexer_py.re'
101 104
102 parser_state = RuleParserState() 105 parser_state = RuleParserState()
103 with open(re_file, 'r') as f: 106 with open(re_file, 'r') as f:
104 RuleParser.parse(f.read(), parser_state) 107 RuleParser.parse(f.read(), parser_state)
105 html_data = process_rules(parser_state) 108 html_data = process_rules(parser_state)
106 109
107 html_file = args.html 110 html_file = args.html
108 if html_file: 111 if html_file:
109 html = generate_html(html_data) 112 html = generate_html(html_data)
110 with open(args.html, 'w') as f: 113 with open(args.html, 'w') as f:
111 f.write(html) 114 f.write(html)
112 print "wrote html to %s" % html_file 115 print "wrote html to %s" % html_file
OLDNEW
« no previous file with comments | « tools/lexer_generator/action_test.py ('k') | tools/lexer_generator/regex_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698