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

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

Issue 636773002: Extended java_cpp_enum to parse enums with set values (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@page_info_dialog_shell_only_v2
Patch Set: Indentation fix Created 6 years, 2 months 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 | 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 #!/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 import re 8 import re
9 import optparse 9 import optparse
10 import os 10 import os
(...skipping 18 matching lines...) Expand all
29 self._Validate() 29 self._Validate()
30 self._AssignEntryIndices() 30 self._AssignEntryIndices()
31 self._StripPrefix() 31 self._StripPrefix()
32 32
33 def _Validate(self): 33 def _Validate(self):
34 assert self.class_name 34 assert self.class_name
35 assert self.class_package 35 assert self.class_package
36 assert self.entries 36 assert self.entries
37 37
38 def _AssignEntryIndices(self): 38 def _AssignEntryIndices(self):
39 # Supporting the same set enum value assignments the compiler does is rather 39 # Enums, if given no value, are given the value of the previous enum + 1.
40 # complicated, so we limit ourselves to these cases:
41 # - all the enum constants have values assigned,
42 # - enum constants reference other enum constants or have no value assigned.
43
44 if not all(self.entries.values()): 40 if not all(self.entries.values()):
45 index = 0 41 prev_enum_value = -1
46 for key, value in self.entries.iteritems(): 42 for key, value in self.entries.iteritems():
47 if not value: 43 if not value:
48 self.entries[key] = index 44 self.entries[key] = prev_enum_value + 1
49 index = index + 1
50 elif value in self.entries: 45 elif value in self.entries:
51 self.entries[key] = self.entries[value] 46 self.entries[key] = self.entries[value]
52 else: 47 else:
53 raise Exception('You can only reference other enum constants unless ' 48 try:
54 'you assign values to all of the constants.') 49 self.entries[key] = int(value)
50 except ValueError:
51 raise Exception('Could not interpret integer from enum value "%s" '
52 'for key %s.' % (value, key))
53 prev_enum_value = self.entries[key]
54
55 55
56 def _StripPrefix(self): 56 def _StripPrefix(self):
57 if not self.prefix_to_strip: 57 if not self.prefix_to_strip:
58 prefix_to_strip = re.sub('(?!^)([A-Z]+)', r'_\1', self.class_name).upper() 58 prefix_to_strip = re.sub('(?!^)([A-Z]+)', r'_\1', self.class_name).upper()
59 prefix_to_strip += '_' 59 prefix_to_strip += '_'
60 if not all([w.startswith(prefix_to_strip) for w in self.entries.keys()]): 60 if not all([w.startswith(prefix_to_strip) for w in self.entries.keys()]):
61 prefix_to_strip = '' 61 prefix_to_strip = ''
62 else: 62 else:
63 prefix_to_strip = self.prefix_to_strip 63 prefix_to_strip = self.prefix_to_strip
64 entries = ((k.replace(prefix_to_strip, '', 1), v) for (k, v) in 64 entries = ((k.replace(prefix_to_strip, '', 1), v) for (k, v) in
65 self.entries.iteritems()) 65 self.entries.iteritems())
66 self.entries = collections.OrderedDict(entries) 66 self.entries = collections.OrderedDict(entries)
67 67
68 class HeaderParser(object): 68 class HeaderParser(object):
69 single_line_comment_re = re.compile(r'\s*//') 69 single_line_comment_re = re.compile(r'\s*//')
70 multi_line_comment_start_re = re.compile(r'\s*/\*') 70 multi_line_comment_start_re = re.compile(r'\s*/\*')
71 enum_start_re = re.compile(r'^\s*enum\s+(\w+)\s+{\s*$') 71 enum_start_re = re.compile(r'^\s*enum\s+(\w+)\s+{\s*$')
72 enum_line_re = re.compile(r'^\s*(\w+)(\s*\=\s*([^,\n]+))?,?\s*$') 72 enum_line_re = re.compile(r'^\s*(\w+)(\s*\=\s*([^,\n]+))?,?')
73 enum_end_re = re.compile(r'^\s*}\s*;\s*$') 73 enum_end_re = re.compile(r'^\s*}\s*;\s*$')
74 generator_directive_re = re.compile( 74 generator_directive_re = re.compile(
75 r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*([\.\w]+)$') 75 r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*([\.\w]+)$')
76 76
77 def __init__(self, lines): 77 def __init__(self, lines):
78 self._lines = lines 78 self._lines = lines
79 self._enum_definitions = [] 79 self._enum_definitions = []
80 self._in_enum = False 80 self._in_enum = False
81 self._current_definition = None 81 self._current_definition = None
82 self._generator_directives = {} 82 self._generator_directives = {}
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 228
229 output_paths = DoGenerate(options, args) 229 output_paths = DoGenerate(options, args)
230 230
231 if options.assert_files_list: 231 if options.assert_files_list:
232 AssertFilesList(output_paths, options.assert_files_list) 232 AssertFilesList(output_paths, options.assert_files_list)
233 233
234 return " ".join(output_paths) 234 return " ".join(output_paths)
235 235
236 if __name__ == '__main__': 236 if __name__ == '__main__':
237 DoMain(sys.argv[1:]) 237 DoMain(sys.argv[1:])
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698