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

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

Issue 63363004: Experimental lexer generator: comment fixes. (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 | « src/lexer/lexer_py.re ('k') | no next file » | 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 return 'PUSH_TOKEN(Token::EOS); return 0;' 57 return 'PUSH_TOKEN(Token::EOS); return 0;'
58 58
59 @staticmethod 59 @staticmethod
60 def __terminate_illegal_code(value): 60 def __terminate_illegal_code(value):
61 assert value == None 61 assert value == None
62 return 'PUSH_TOKEN(Token::ILLEGAL); return 1;' 62 return 'PUSH_TOKEN(Token::ILLEGAL); return 1;'
63 63
64 @staticmethod 64 @staticmethod
65 def __skip_code(value): 65 def __skip_code(value):
66 assert value == None 66 assert value == None
67 return 'SKIP(); goto code_start;' 67 return 'SKIP();'
68 68
69 @staticmethod 69 @staticmethod
70 def __push_line_terminator_code(value): 70 def __push_line_terminator_code(value):
71 assert value == None 71 assert value == None
72 return 'PUSH_LINE_TERMINATOR();' 72 return 'PUSH_LINE_TERMINATOR();'
73 73
74 @staticmethod 74 @staticmethod
75 def __push_token_code(value): 75 def __push_token_code(value):
76 assert value != None 76 assert value != None
77 return 'PUSH_TOKEN(Token::%s);' % value 77 return 'PUSH_TOKEN(Token::%s);' % value
78 78
79 @staticmethod 79 @staticmethod
80 def __code_code(value): 80 def __code_code(value):
81 assert value != None 81 assert value != None
82 return '%s\n' % value 82 return '%s\n' % value
83 83
84 @staticmethod
85 def __skip_and_terminate_code(value):
86 return 'SKIP(); --start_; ' + CodeGenerator.__terminate_code(value)
87
84 def __init__(self, dfa, default_action): 88 def __init__(self, dfa, default_action):
85 self.__dfa = dfa 89 self.__dfa = dfa
86 self.__start_node_number = dfa.start_state().node_number() 90 self.__start_node_number = dfa.start_state().node_number()
87 self.__default_action = default_action 91 self.__default_action = default_action
88 # make this better 92 # make this better
89 self.__action_code_map = { 93 self.__action_code_map = {
90 "terminate" : self.__terminate_code, 94 "terminate" : self.__terminate_code,
91 "terminate_illegal" : self.__terminate_illegal_code, 95 "terminate_illegal" : self.__terminate_illegal_code,
92 "push_token" : self.__push_token_code, 96 "push_token" : self.__push_token_code,
93 "push_line_terminator" : self.__push_line_terminator_code, 97 "push_line_terminator" : self.__push_line_terminator_code,
94 "skip" : self.__skip_code, 98 "skip" : self.__skip_code,
95 "code" : self.__code_code, 99 "code" : self.__code_code,
100 "skip_and_terminate" : self.__skip_and_terminate_code,
96 } 101 }
97 102
98 def __dfa_state_to_code(self, state): 103 def __dfa_state_to_code(self, state):
99 # FIXME: add different check types (if, switch, lookup table) 104 # FIXME: add different check types (if, switch, lookup table)
100 # FIXME: add action + break / continue 105 # FIXME: add action + break / continue
101 # FIXME: add default action 106 # FIXME: add default action
102 code = '' 107 code = ''
103 if self.__start_node_number == state.node_number(): 108 if self.__start_node_number == state.node_number():
104 code += ''' 109 code += '''
105 code_start: 110 code_start:
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 def f(state, code): 154 def f(state, code):
150 code += self.__dfa_state_to_code(state) 155 code += self.__dfa_state_to_code(state)
151 return code 156 return code
152 code = dfa.visit_all_states(f, code) 157 code = dfa.visit_all_states(f, code)
153 158
154 default_action_code = '' 159 default_action_code = ''
155 assert(default_action and default_action.match_action()) 160 assert(default_action and default_action.match_action())
156 action = default_action.match_action() 161 action = default_action.match_action()
157 default_action_code = self.__action_code_map[action[0]](action[1]) 162 default_action_code = self.__action_code_map[action[0]](action[1])
158 code += ''' 163 code += '''
159 CHECK(false); 164 CHECK(false); goto code_start;
160 default_action: 165 default_action:
161 //fprintf(stderr, "default action\\n"); 166 //fprintf(stderr, "default action\\n");
162 %s 167 %s
163 FORWARD(); 168 FORWARD();
164 goto code_%s; 169 goto code_%s;
165 return 0; 170 return 0;
166 } 171 }
167 } 172 }
168 } 173 }
169 ''' % (default_action_code, self.__start_node_number) 174 ''' % (default_action_code, self.__start_node_number)
170 return code 175 return code
171 176
172 @staticmethod 177 @staticmethod
173 def rule_processor_to_code(rule_processor, use_mdfa): 178 def rule_processor_to_code(rule_processor, use_mdfa):
174 if use_mdfa: 179 if use_mdfa:
175 dfa = rule_processor.default_automata().minimal_dfa() 180 dfa = rule_processor.default_automata().minimal_dfa()
176 else: 181 else:
177 dfa = rule_processor.default_automata().dfa() 182 dfa = rule_processor.default_automata().dfa()
178 return CodeGenerator(dfa, rule_processor.default_action).__process() 183 return CodeGenerator(dfa, rule_processor.default_action).__process()
OLDNEW
« no previous file with comments | « src/lexer/lexer_py.re ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698