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

Side by Side Diff: build/android/gyp/java_cpp_enum.py

Issue 2511533002: Add support for single line enums to java_cpp_enum.py. (Closed)
Patch Set: Created 4 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
« no previous file with comments | « no previous file | build/android/gyp/java_cpp_enum_tests.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 #!/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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 multi_line_generator_directive_start_re = re.compile( 144 multi_line_generator_directive_start_re = re.compile(
145 r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*\(([\.\w]*)$') 145 r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*\(([\.\w]*)$')
146 multi_line_directive_continuation_re = re.compile(r'^\s*//\s+([\.\w]+)$') 146 multi_line_directive_continuation_re = re.compile(r'^\s*//\s+([\.\w]+)$')
147 multi_line_directive_end_re = re.compile(r'^\s*//\s+([\.\w]*)\)$') 147 multi_line_directive_end_re = re.compile(r'^\s*//\s+([\.\w]*)\)$')
148 148
149 optional_class_or_struct_re = r'(class|struct)?' 149 optional_class_or_struct_re = r'(class|struct)?'
150 enum_name_re = r'(\w+)' 150 enum_name_re = r'(\w+)'
151 optional_fixed_type_re = r'(\:\s*(\w+\s*\w+?))?' 151 optional_fixed_type_re = r'(\:\s*(\w+\s*\w+?))?'
152 enum_start_re = re.compile(r'^\s*(?:\[cpp.*\])?\s*enum\s+' + 152 enum_start_re = re.compile(r'^\s*(?:\[cpp.*\])?\s*enum\s+' +
153 optional_class_or_struct_re + '\s*' + enum_name_re + '\s*' + 153 optional_class_or_struct_re + '\s*' + enum_name_re + '\s*' +
154 optional_fixed_type_re + '\s*{\s*$') 154 optional_fixed_type_re + '\s*{\s*')
155 enum_single_line_re = re.compile(
156 r'^\s*(?:\[cpp.*\])?\s*enum.*{(?P<enum_entries>.*)}.*$')
155 157
156 def __init__(self, lines, path=''): 158 def __init__(self, lines, path=''):
157 self._lines = lines 159 self._lines = lines
158 self._path = path 160 self._path = path
159 self._enum_definitions = [] 161 self._enum_definitions = []
160 self._in_enum = False 162 self._in_enum = False
161 self._current_definition = None 163 self._current_definition = None
162 self._current_comments = [] 164 self._current_comments = []
163 self._generator_directives = DirectiveSet() 165 self._generator_directives = DirectiveSet()
164 self._multi_line_generator_directive = None 166 self._multi_line_generator_directive = None
(...skipping 20 matching lines...) Expand all
185 if HeaderParser.multi_line_comment_start_re.match(line): 187 if HeaderParser.multi_line_comment_start_re.match(line):
186 raise Exception('Multi-line comments in enums are not supported in ' + 188 raise Exception('Multi-line comments in enums are not supported in ' +
187 self._path) 189 self._path)
188 190
189 enum_comment = HeaderParser.single_line_comment_re.match(line) 191 enum_comment = HeaderParser.single_line_comment_re.match(line)
190 if enum_comment: 192 if enum_comment:
191 comment = enum_comment.groups()[0] 193 comment = enum_comment.groups()[0]
192 if comment: 194 if comment:
193 self._current_comments.append(comment) 195 self._current_comments.append(comment)
194 elif HeaderParser.enum_end_re.match(line): 196 elif HeaderParser.enum_end_re.match(line):
195 self._FinalizeCurrentEnumEntry() 197 self._FinalizeCurrentEnumDefinition()
196 else: 198 else:
197 self._AddToCurrentEnumEntry(line) 199 self._AddToCurrentEnumEntry(line)
198 if ',' in line: 200 if ',' in line:
199 self._ParseCurrentEnumEntry() 201 self._ParseCurrentEnumEntry()
200 202
203 def _ParseSingleLineEnum(self, line):
204 for entry in line.split(','):
205 self._AddToCurrentEnumEntry(entry)
206 self._ParseCurrentEnumEntry()
207
208 self._FinalizeCurrentEnumDefinition()
209
201 def _ParseCurrentEnumEntry(self): 210 def _ParseCurrentEnumEntry(self):
202 if not self._current_enum_entry: 211 if not self._current_enum_entry:
203 return 212 return
204 213
205 enum_entry = HeaderParser.enum_line_re.match(self._current_enum_entry) 214 enum_entry = HeaderParser.enum_line_re.match(self._current_enum_entry)
206 if not enum_entry: 215 if not enum_entry:
207 raise Exception('Unexpected error while attempting to parse %s as enum ' 216 raise Exception('Unexpected error while attempting to parse %s as enum '
208 'entry.' % self._current_enum_entry) 217 'entry.' % self._current_enum_entry)
209 218
210 enum_key = enum_entry.groups()[0] 219 enum_key = enum_entry.groups()[0]
211 enum_value = enum_entry.groups()[2] 220 enum_value = enum_entry.groups()[2]
212 self._current_definition.AppendEntry(enum_key, enum_value) 221 self._current_definition.AppendEntry(enum_key, enum_value)
213 if self._current_comments: 222 if self._current_comments:
214 self._current_definition.AppendEntryComment( 223 self._current_definition.AppendEntryComment(
215 enum_key, ' '.join(self._current_comments)) 224 enum_key, ' '.join(self._current_comments))
216 self._current_comments = [] 225 self._current_comments = []
217 self._current_enum_entry = '' 226 self._current_enum_entry = ''
218 227
219 def _AddToCurrentEnumEntry(self, line): 228 def _AddToCurrentEnumEntry(self, line):
220 self._current_enum_entry += ' ' + line.strip() 229 self._current_enum_entry += ' ' + line.strip()
221 230
222 def _FinalizeCurrentEnumEntry(self): 231 def _FinalizeCurrentEnumDefinition(self):
223 if self._current_enum_entry: 232 if self._current_enum_entry:
224 self._ParseCurrentEnumEntry() 233 self._ParseCurrentEnumEntry()
225 self._ApplyGeneratorDirectives() 234 self._ApplyGeneratorDirectives()
226 self._current_definition.Finalize() 235 self._current_definition.Finalize()
227 self._enum_definitions.append(self._current_definition) 236 self._enum_definitions.append(self._current_definition)
237 self._current_definition = None
228 self._in_enum = False 238 self._in_enum = False
229 239
230 def _ParseMultiLineDirectiveLine(self, line): 240 def _ParseMultiLineDirectiveLine(self, line):
231 multi_line_directive_continuation = ( 241 multi_line_directive_continuation = (
232 HeaderParser.multi_line_directive_continuation_re.match(line)) 242 HeaderParser.multi_line_directive_continuation_re.match(line))
233 multi_line_directive_end = ( 243 multi_line_directive_end = (
234 HeaderParser.multi_line_directive_end_re.match(line)) 244 HeaderParser.multi_line_directive_end_re.match(line))
235 245
236 if multi_line_directive_continuation: 246 if multi_line_directive_continuation:
237 value_cont = multi_line_directive_continuation.groups()[0] 247 value_cont = multi_line_directive_continuation.groups()[0]
238 self._multi_line_generator_directive[1].append(value_cont) 248 self._multi_line_generator_directive[1].append(value_cont)
239 elif multi_line_directive_end: 249 elif multi_line_directive_end:
240 directive_name = self._multi_line_generator_directive[0] 250 directive_name = self._multi_line_generator_directive[0]
241 directive_value = "".join(self._multi_line_generator_directive[1]) 251 directive_value = "".join(self._multi_line_generator_directive[1])
242 directive_value += multi_line_directive_end.groups()[0] 252 directive_value += multi_line_directive_end.groups()[0]
243 self._multi_line_generator_directive = None 253 self._multi_line_generator_directive = None
244 self._generator_directives.Update(directive_name, directive_value) 254 self._generator_directives.Update(directive_name, directive_value)
245 else: 255 else:
246 raise Exception('Malformed multi-line directive declaration in ' + 256 raise Exception('Malformed multi-line directive declaration in ' +
247 self._path) 257 self._path)
248 258
249 def _ParseRegularLine(self, line): 259 def _ParseRegularLine(self, line):
250 enum_start = HeaderParser.enum_start_re.match(line) 260 enum_start = HeaderParser.enum_start_re.match(line)
251 generator_directive_error = HeaderParser.generator_error_re.match(line) 261 generator_directive_error = HeaderParser.generator_error_re.match(line)
252 generator_directive = HeaderParser.generator_directive_re.match(line) 262 generator_directive = HeaderParser.generator_directive_re.match(line)
253 multi_line_generator_directive_start = ( 263 multi_line_generator_directive_start = (
254 HeaderParser.multi_line_generator_directive_start_re.match(line)) 264 HeaderParser.multi_line_generator_directive_start_re.match(line))
265 single_line_enum = HeaderParser.enum_single_line_re.match(line)
255 266
256 if generator_directive_error: 267 if generator_directive_error:
257 raise Exception('Malformed directive declaration in ' + self._path + 268 raise Exception('Malformed directive declaration in ' + self._path +
258 '. Use () for multi-line directives. E.g.\n' + 269 '. Use () for multi-line directives. E.g.\n' +
259 '// GENERATED_JAVA_ENUM_PACKAGE: (\n' + 270 '// GENERATED_JAVA_ENUM_PACKAGE: (\n' +
260 '// foo.package)') 271 '// foo.package)')
261 elif generator_directive: 272 elif generator_directive:
262 directive_name = generator_directive.groups()[0] 273 directive_name = generator_directive.groups()[0]
263 directive_value = generator_directive.groups()[1] 274 directive_value = generator_directive.groups()[1]
264 self._generator_directives.Update(directive_name, directive_value) 275 self._generator_directives.Update(directive_name, directive_value)
265 elif multi_line_generator_directive_start: 276 elif multi_line_generator_directive_start:
266 directive_name = multi_line_generator_directive_start.groups()[0] 277 directive_name = multi_line_generator_directive_start.groups()[0]
267 directive_value = multi_line_generator_directive_start.groups()[1] 278 directive_value = multi_line_generator_directive_start.groups()[1]
268 self._multi_line_generator_directive = (directive_name, [directive_value]) 279 self._multi_line_generator_directive = (directive_name, [directive_value])
269 elif enum_start: 280 elif enum_start or single_line_enum:
270 if self._generator_directives.empty: 281 if self._generator_directives.empty:
271 return 282 return
272 self._current_definition = EnumDefinition( 283 self._current_definition = EnumDefinition(
273 original_enum_name=enum_start.groups()[1], 284 original_enum_name=enum_start.groups()[1],
274 fixed_type=enum_start.groups()[3]) 285 fixed_type=enum_start.groups()[3])
275 self._in_enum = True 286 self._in_enum = True
287 if single_line_enum:
288 self._ParseSingleLineEnum(single_line_enum.group('enum_entries'))
276 289
277 def GetScriptName(): 290 def GetScriptName():
278 return os.path.basename(os.path.abspath(sys.argv[0])) 291 return os.path.basename(os.path.abspath(sys.argv[0]))
279 292
280 def DoGenerate(source_paths): 293 def DoGenerate(source_paths):
281 for source_path in source_paths: 294 for source_path in source_paths:
282 enum_definitions = DoParseHeaderFile(source_path) 295 enum_definitions = DoParseHeaderFile(source_path)
283 if not enum_definitions: 296 if not enum_definitions:
284 raise Exception('No enums found in %s\n' 297 raise Exception('No enums found in %s\n'
285 'Did you forget prefixing enums with ' 298 'Did you forget prefixing enums with '
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar: 403 with zipfile.ZipFile(options.srcjar, 'w', zipfile.ZIP_STORED) as srcjar:
391 for output_path, data in DoGenerate(input_paths): 404 for output_path, data in DoGenerate(input_paths):
392 build_utils.AddToZipHermetic(srcjar, output_path, data=data) 405 build_utils.AddToZipHermetic(srcjar, output_path, data=data)
393 406
394 if options.depfile: 407 if options.depfile:
395 build_utils.WriteDepfile(options.depfile, options.srcjar) 408 build_utils.WriteDepfile(options.depfile, options.srcjar)
396 409
397 410
398 if __name__ == '__main__': 411 if __name__ == '__main__':
399 DoMain(sys.argv[1:]) 412 DoMain(sys.argv[1:])
OLDNEW
« no previous file with comments | « no previous file | build/android/gyp/java_cpp_enum_tests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698