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

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

Issue 68803002: Experimental lexer generator: Code generator skeleton. (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/dfa.py ('k') | tools/lexer_generator/transition_keys.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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 (start, dfa_nodes) = nfa.compute_dfa() 127 (start, dfa_nodes) = nfa.compute_dfa()
128 dfa = Dfa(start, dfa_nodes) 128 dfa = Dfa(start, dfa_nodes)
129 self.__automata[rule_name] = (nfa, dfa) 129 self.__automata[rule_name] = (nfa, dfa)
130 130
131 # Lexes strings with the help of DFAs procuded by the grammar. For sanity 131 # Lexes strings with the help of DFAs procuded by the grammar. For sanity
132 # checking the automata. 132 # checking the automata.
133 def lex(self, string): 133 def lex(self, string):
134 (nfa, dfa) = self.__automata['default'] 134 (nfa, dfa) = self.__automata['default']
135 return dfa.lex(string) 135 return dfa.lex(string)
136 136
137 def generate_code(self):
138 (nfa, dfa) = self.__automata['default']
139 return dfa.to_code()
140
137 if __name__ == '__main__': 141 if __name__ == '__main__':
138 142
139 parser = argparse.ArgumentParser() 143 parser = argparse.ArgumentParser()
140 parser.add_argument('--html') 144 parser.add_argument('--html')
141 parser.add_argument('--re', default='src/lexer/lexer_py.re') 145 parser.add_argument('--re', default='src/lexer/lexer_py.re')
142 parser.add_argument('--input') 146 parser.add_argument('--input')
147 parser.add_argument('--code')
143 args = parser.parse_args() 148 args = parser.parse_args()
144 149
145 re_file = args.re 150 re_file = args.re
146 parser_state = RuleParserState() 151 parser_state = RuleParserState()
147 print "parsing %s" % re_file 152 print "parsing %s" % re_file
148 with open(re_file, 'r') as f: 153 with open(re_file, 'r') as f:
149 generator = Generator(f.read()) 154 generator = Generator(f.read())
150 155
151 html_file = args.html 156 html_file = args.html
152 if html_file: 157 if html_file:
153 html = generator.generate_html() 158 html = generator.generate_html()
154 with open(args.html, 'w') as f: 159 with open(args.html, 'w') as f:
155 f.write(html) 160 f.write(html)
156 print "wrote html to %s" % html_file 161 print "wrote html to %s" % html_file
157 162
163 code_file = args.code
164 if code_file:
165 code = generator.generate_code()
166 with open(code_file, 'w') as f:
167 f.write(code)
168 print "wrote code to %s" % code_file
169
158 input_file = args.input 170 input_file = args.input
159 if input_file: 171 if input_file:
160 with open(input_file, 'r') as f: 172 with open(input_file, 'r') as f:
161 input_text = f.read() + '\0' 173 input_text = f.read() + '\0'
162 for t in generator.lex(input_text): 174 for t in generator.lex(input_text):
163 print t 175 print t
OLDNEW
« no previous file with comments | « tools/lexer_generator/dfa.py ('k') | tools/lexer_generator/transition_keys.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698