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

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

Issue 66293002: Experimental parser: some support for adding subraphs (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 | « no previous file | tools/lexer_generator/nfa.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 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 assert 'default' in parser_state.rules
83 for k, v in parser_state.rules.items(): 83 def process(k, v):
84 assert 'default' in v 84 assert 'default' in v
85 graphs = [] 85 graphs = []
86 for (graph, action) in v['regex']: 86 for (graph, action) in v['regex']:
87 graphs.append(NfaBuilder.add_action(graph, action)) 87 (precedence, code, transition) = action
88 rule_map[k] = NfaBuilder.or_graphs(graphs) 88 if code:
89 graph = NfaBuilder.add_action(graph, (precedence, code, None))
90 if transition == 'continue':
91 graph = NfaBuilder.add_continue(graph)
92 elif (transition == 'break' or
93 transition == 'terminate' or
94 transition == 'terminate_illegal'):
95 pass
96 else:
97 assert k == 'default'
98 graph = NfaBuilder.join_subgraph(graph, transition, rule_map[transition] )
99 graphs.append(graph)
100 graph = NfaBuilder.or_graphs(graphs)
101 # merge default action
102 (precedence, code, transition) = v['default'][1]
103 assert transition == 'continue' or transition == 'break'
104 if code:
105 graph = NfaBuilder.add_incoming_action(graph, (precedence, code, None))
106 rule_map[k] = graph
107 for k, v in parser_state.rules.items():
108 if k == 'default': continue
109 process(k, v)
110 process('default', parser_state.rules['default'])
89 html_data = [] 111 html_data = []
90 for rule_name, graph in rule_map.items(): 112 for rule_name, graph in rule_map.items():
91 nfa = builder.nfa(graph) 113 nfa = builder.nfa(graph)
92 (start, dfa_nodes) = nfa.compute_dfa() 114 (start, dfa_nodes) = nfa.compute_dfa()
93 dfa = Dfa(start, dfa_nodes) 115 dfa = Dfa(start, dfa_nodes)
94 html_data.append((rule_name, nfa, dfa)) 116 html_data.append((rule_name, nfa, dfa))
95 return html_data 117 return html_data
96 118
97 if __name__ == '__main__': 119 if __name__ == '__main__':
98 120
99 parser = argparse.ArgumentParser() 121 parser = argparse.ArgumentParser()
100 parser.add_argument('--html') 122 parser.add_argument('--html')
123 parser.add_argument('--re', default='src/lexer/lexer_py.re')
101 args = parser.parse_args() 124 args = parser.parse_args()
102 125
103 re_file = 'src/lexer/lexer_py.re' 126 re_file = args.re
104
105 parser_state = RuleParserState() 127 parser_state = RuleParserState()
128 print "parsing %s" % re_file
106 with open(re_file, 'r') as f: 129 with open(re_file, 'r') as f:
107 RuleParser.parse(f.read(), parser_state) 130 RuleParser.parse(f.read(), parser_state)
108 html_data = process_rules(parser_state) 131 html_data = process_rules(parser_state)
109 132
110 html_file = args.html 133 html_file = args.html
111 if html_file: 134 if html_file:
112 html = generate_html(html_data) 135 html = generate_html(html_data)
113 with open(args.html, 'w') as f: 136 with open(args.html, 'w') as f:
114 f.write(html) 137 f.write(html)
115 print "wrote html to %s" % html_file 138 print "wrote html to %s" % html_file
OLDNEW
« no previous file with comments | « no previous file | tools/lexer_generator/nfa.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698