OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 import collections | 7 import collections |
8 from datetime import date | 8 from datetime import date |
9 import re | 9 import re |
10 import optparse | 10 import optparse |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 def UpdateDefinition(self, definition): | 123 def UpdateDefinition(self, definition): |
124 definition.class_name_override = self._directives.get( | 124 definition.class_name_override = self._directives.get( |
125 DirectiveSet.class_name_override_key, '') | 125 DirectiveSet.class_name_override_key, '') |
126 definition.enum_package = self._directives.get( | 126 definition.enum_package = self._directives.get( |
127 DirectiveSet.enum_package_key) | 127 DirectiveSet.enum_package_key) |
128 definition.prefix_to_strip = self._directives.get( | 128 definition.prefix_to_strip = self._directives.get( |
129 DirectiveSet.prefix_to_strip_key) | 129 DirectiveSet.prefix_to_strip_key) |
130 | 130 |
131 | 131 |
132 class HeaderParser(object): | 132 class HeaderParser(object): |
133 single_line_comment_re = re.compile(r'\s*//\s*([^\n]+)') | 133 single_line_comment_re = re.compile(r'\s*//\s*([^\n]*)') |
134 multi_line_comment_start_re = re.compile(r'\s*/\*') | 134 multi_line_comment_start_re = re.compile(r'\s*/\*') |
135 enum_line_re = re.compile(r'^\s*(\w+)(\s*\=\s*([^,\n]+))?,?') | 135 enum_line_re = re.compile(r'^\s*(\w+)(\s*\=\s*([^,\n]+))?,?') |
136 enum_end_re = re.compile(r'^\s*}\s*;\.*$') | 136 enum_end_re = re.compile(r'^\s*}\s*;\.*$') |
137 generator_directive_re = re.compile( | 137 generator_directive_re = re.compile( |
138 r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*([\.\w]+)$') | 138 r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*([\.\w]+)$') |
139 multi_line_generator_directive_start_re = re.compile( | 139 multi_line_generator_directive_start_re = re.compile( |
140 r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*\(([\.\w]*)$') | 140 r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*\(([\.\w]*)$') |
141 multi_line_directive_continuation_re = re.compile( | 141 multi_line_directive_continuation_re = re.compile( |
142 r'^\s*//\s+([\.\w]+)$') | 142 r'^\s*//\s+([\.\w]+)$') |
143 multi_line_directive_end_re = re.compile( | 143 multi_line_directive_end_re = re.compile( |
144 r'^\s*//\s+([\.\w]*)\)$') | 144 r'^\s*//\s+([\.\w]*)\)$') |
145 | 145 |
146 optional_class_or_struct_re = r'(class|struct)?' | 146 optional_class_or_struct_re = r'(class|struct)?' |
147 enum_name_re = r'(\w+)' | 147 enum_name_re = r'(\w+)' |
148 optional_fixed_type_re = r'(\:\s*(\w+\s*\w+?))?' | 148 optional_fixed_type_re = r'(\:\s*(\w+\s*\w+?))?' |
149 enum_start_re = re.compile(r'^\s*(?:\[cpp.*\])?\s*enum\s+' + | 149 enum_start_re = re.compile(r'^\s*(?:\[cpp.*\])?\s*enum\s+' + |
150 optional_class_or_struct_re + '\s*' + enum_name_re + '\s*' + | 150 optional_class_or_struct_re + '\s*' + enum_name_re + '\s*' + |
151 optional_fixed_type_re + '\s*{\s*$') | 151 optional_fixed_type_re + '\s*{\s*$') |
152 | 152 |
153 def __init__(self, lines, path=None): | 153 def __init__(self, lines, path=None): |
154 self._lines = lines | 154 self._lines = lines |
155 self._path = path | 155 self._path = path |
156 self._enum_definitions = [] | 156 self._enum_definitions = [] |
157 self._in_enum = False | 157 self._in_enum = False |
158 self._current_definition = None | 158 self._current_definition = None |
159 self._current_comments = [] | 159 self._current_comments = [] |
160 self._generator_directives = DirectiveSet() | 160 self._generator_directives = DirectiveSet() |
161 self._multi_line_generator_directive = None | 161 self._multi_line_generator_directive = None |
162 self._current_enum_entry = '' | |
162 | 163 |
163 def _ApplyGeneratorDirectives(self): | 164 def _ApplyGeneratorDirectives(self): |
164 self._generator_directives.UpdateDefinition(self._current_definition) | 165 self._generator_directives.UpdateDefinition(self._current_definition) |
165 self._generator_directives = DirectiveSet() | 166 self._generator_directives = DirectiveSet() |
166 | 167 |
167 def ParseDefinitions(self): | 168 def ParseDefinitions(self): |
168 for line in self._lines: | 169 for line in self._lines: |
169 self._ParseLine(line) | 170 self._ParseLine(line) |
170 return self._enum_definitions | 171 return self._enum_definitions |
171 | 172 |
172 def _ParseLine(self, line): | 173 def _ParseLine(self, line): |
173 if self._multi_line_generator_directive: | 174 if self._multi_line_generator_directive: |
174 self._ParseMultiLineDirectiveLine(line) | 175 self._ParseMultiLineDirectiveLine(line) |
175 elif not self._in_enum: | 176 elif not self._in_enum: |
176 self._ParseRegularLine(line) | 177 self._ParseRegularLine(line) |
177 else: | 178 else: |
178 self._ParseEnumLine(line) | 179 self._ParseEnumLine(line) |
179 | 180 |
180 def _ParseEnumLine(self, line): | 181 def _ParseEnumLine(self, line): |
181 enum_comment = HeaderParser.single_line_comment_re.match(line) | 182 def _ParseCurrentEnumEntry(): |
agrieve
2016/10/04 15:57:28
Looks like these helpers don't use their outer con
estevenson
2016/10/04 16:15:25
Done.
| |
182 if enum_comment: | 183 if not self._current_enum_entry: |
183 self._current_comments.append(enum_comment.groups()[0]) | 184 return |
184 return | 185 |
185 if HeaderParser.multi_line_comment_start_re.match(line): | 186 enum_entry = HeaderParser.enum_line_re.match(self._current_enum_entry) |
186 raise Exception('Multi-line comments in enums are not supported in ' + | 187 if not enum_entry: |
187 self._path) | 188 raise Exception('Unexpected error while attempting to parse %s as enum ' |
188 enum_end = HeaderParser.enum_end_re.match(line) | 189 'entry.' % self._current_enum_entry) |
189 enum_entry = HeaderParser.enum_line_re.match(line) | 190 |
190 if enum_end: | |
191 self._ApplyGeneratorDirectives() | |
192 self._current_definition.Finalize() | |
193 self._enum_definitions.append(self._current_definition) | |
194 self._in_enum = False | |
195 elif enum_entry: | |
196 enum_key = enum_entry.groups()[0] | 191 enum_key = enum_entry.groups()[0] |
197 enum_value = enum_entry.groups()[2] | 192 enum_value = enum_entry.groups()[2] |
198 self._current_definition.AppendEntry(enum_key, enum_value) | 193 self._current_definition.AppendEntry(enum_key, enum_value) |
199 if self._current_comments: | 194 if self._current_comments: |
200 self._current_definition.AppendEntryComment( | 195 self._current_definition.AppendEntryComment( |
201 enum_key, ' '.join(self._current_comments)) | 196 enum_key, ' '.join(self._current_comments)) |
202 self._current_comments = [] | 197 self._current_comments = [] |
198 self._current_enum_entry = '' | |
199 | |
200 def _AddToCurrentEnumEntry(line): | |
201 self._current_enum_entry += ' ' + line.strip() | |
202 | |
203 def _FinalizeCurrentEnumEntry(): | |
204 if self._current_enum_entry: | |
205 _ParseCurrentEnumEntry() | |
206 self._ApplyGeneratorDirectives() | |
207 self._current_definition.Finalize() | |
208 self._enum_definitions.append(self._current_definition) | |
209 self._in_enum = False | |
210 | |
211 if HeaderParser.multi_line_comment_start_re.match(line): | |
212 raise Exception('Multi-line comments in enums are not supported in ' + | |
213 self._path) | |
214 | |
215 enum_comment = HeaderParser.single_line_comment_re.match(line) | |
216 if enum_comment: | |
217 comment = enum_comment.groups()[0] | |
218 if comment: | |
219 self._current_comments.append(comment) | |
220 elif HeaderParser.enum_end_re.match(line): | |
221 _FinalizeCurrentEnumEntry() | |
222 else: | |
223 _AddToCurrentEnumEntry(line) | |
224 if ',' in line: | |
225 _ParseCurrentEnumEntry() | |
203 | 226 |
204 def _ParseMultiLineDirectiveLine(self, line): | 227 def _ParseMultiLineDirectiveLine(self, line): |
205 multi_line_directive_continuation = ( | 228 multi_line_directive_continuation = ( |
206 HeaderParser.multi_line_directive_continuation_re.match(line)) | 229 HeaderParser.multi_line_directive_continuation_re.match(line)) |
207 multi_line_directive_end = ( | 230 multi_line_directive_end = ( |
208 HeaderParser.multi_line_directive_end_re.match(line)) | 231 HeaderParser.multi_line_directive_end_re.match(line)) |
209 | 232 |
210 if multi_line_directive_continuation: | 233 if multi_line_directive_continuation: |
211 value_cont = multi_line_directive_continuation.groups()[0] | 234 value_cont = multi_line_directive_continuation.groups()[0] |
212 self._multi_line_generator_directive[1].append(value_cont) | 235 self._multi_line_generator_directive[1].append(value_cont) |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar: | 381 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar: |
359 for output_path, data in DoGenerate(input_paths): | 382 for output_path, data in DoGenerate(input_paths): |
360 build_utils.AddToZipHermetic(srcjar, output_path, data=data) | 383 build_utils.AddToZipHermetic(srcjar, output_path, data=data) |
361 | 384 |
362 if options.depfile: | 385 if options.depfile: |
363 build_utils.WriteDepfile(options.depfile, options.srcjar) | 386 build_utils.WriteDepfile(options.depfile, options.srcjar) |
364 | 387 |
365 | 388 |
366 if __name__ == '__main__': | 389 if __name__ == '__main__': |
367 DoMain(sys.argv[1:]) | 390 DoMain(sys.argv[1:]) |
OLD | NEW |