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

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

Issue 66333008: Experimental parser: move code gen to own class (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 12 matching lines...) Expand all
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 import argparse 28 import argparse
29 from nfa import Nfa 29 from nfa import Nfa
30 from nfa_builder import NfaBuilder 30 from nfa_builder import NfaBuilder
31 from dfa import Dfa 31 from dfa import Dfa
32 from rule_parser import RuleParser, RuleParserState 32 from rule_parser import RuleParser, RuleParserState
33 from code_generator import CodeGenerator
33 34
34 file_template = ''' 35 file_template = '''
35 <html> 36 <html>
36 <head> 37 <head>
37 <script src="viz.js"></script> 38 <script src="viz.js"></script>
38 <script> 39 <script>
39 function draw(name, id) { 40 function draw(name, id) {
40 code = document.getElementById(id).innerHTML 41 code = document.getElementById(id).innerHTML
41 document.body.innerHTML += "<h1>" + name + "</h1>"; 42 document.body.innerHTML += "<h1>" + name + "</h1>";
42 try { 43 try {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 self.__automata[rule_name] = (nfa, dfa) 135 self.__automata[rule_name] = (nfa, dfa)
135 136
136 # Lexes strings with the help of DFAs procuded by the grammar. For sanity 137 # Lexes strings with the help of DFAs procuded by the grammar. For sanity
137 # checking the automata. 138 # checking the automata.
138 def lex(self, string): 139 def lex(self, string):
139 (nfa, dfa) = self.__automata['default'] 140 (nfa, dfa) = self.__automata['default']
140 return dfa.lex(string) 141 return dfa.lex(string)
141 142
142 def generate_code(self): 143 def generate_code(self):
143 (nfa, dfa) = self.__automata['default'] 144 (nfa, dfa) = self.__automata['default']
144 return dfa.to_code() 145 return CodeGenerator.dfa_to_code(dfa)
145 146
146 if __name__ == '__main__': 147 if __name__ == '__main__':
147 148
148 parser = argparse.ArgumentParser() 149 parser = argparse.ArgumentParser()
149 parser.add_argument('--html') 150 parser.add_argument('--html')
150 parser.add_argument('--re', default='src/lexer/lexer_py.re') 151 parser.add_argument('--re', default='src/lexer/lexer_py.re')
151 parser.add_argument('--input') 152 parser.add_argument('--input')
152 parser.add_argument('--code') 153 parser.add_argument('--code')
153 args = parser.parse_args() 154 args = parser.parse_args()
154 155
(...skipping 16 matching lines...) Expand all
171 with open(code_file, 'w') as f: 172 with open(code_file, 'w') as f:
172 f.write(code) 173 f.write(code)
173 print "wrote code to %s" % code_file 174 print "wrote code to %s" % code_file
174 175
175 input_file = args.input 176 input_file = args.input
176 if input_file: 177 if input_file:
177 with open(input_file, 'r') as f: 178 with open(input_file, 'r') as f:
178 input_text = f.read() + '\0' 179 input_text = f.read() + '\0'
179 for t in generator.lex(input_text): 180 for t in generator.lex(input_text):
180 print t 181 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