Extended java_cpp_enum to parse enums with set values

Extended java_cpp_enum to parse enums that have values set for some of
the enums. Also fixed a small bug where enum values with comments on the
same line (following the enum) were being ignored.

BUG=NONE

Review URL: https://codereview.chromium.org/636773002

Cr-Original-Commit-Position: refs/heads/master@{#299009}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 8bf011fd19739f5b6c85c87e0928de84ed487476
This commit is contained in:
sashab 2014-10-09 16:48:38 -07:00 коммит произвёл Commit bot
Родитель 10fefb71e3
Коммит 2c26b2b70d
1 изменённых файлов: 11 добавлений и 11 удалений

Просмотреть файл

@ -36,22 +36,22 @@ class EnumDefinition(object):
assert self.entries
def _AssignEntryIndices(self):
# Supporting the same set enum value assignments the compiler does is rather
# complicated, so we limit ourselves to these cases:
# - all the enum constants have values assigned,
# - enum constants reference other enum constants or have no value assigned.
# Enums, if given no value, are given the value of the previous enum + 1.
if not all(self.entries.values()):
index = 0
prev_enum_value = -1
for key, value in self.entries.iteritems():
if not value:
self.entries[key] = index
index = index + 1
self.entries[key] = prev_enum_value + 1
elif value in self.entries:
self.entries[key] = self.entries[value]
else:
raise Exception('You can only reference other enum constants unless '
'you assign values to all of the constants.')
try:
self.entries[key] = int(value)
except ValueError:
raise Exception('Could not interpret integer from enum value "%s" '
'for key %s.' % (value, key))
prev_enum_value = self.entries[key]
def _StripPrefix(self):
if not self.prefix_to_strip:
@ -69,7 +69,7 @@ class HeaderParser(object):
single_line_comment_re = re.compile(r'\s*//')
multi_line_comment_start_re = re.compile(r'\s*/\*')
enum_start_re = re.compile(r'^\s*enum\s+(\w+)\s+{\s*$')
enum_line_re = re.compile(r'^\s*(\w+)(\s*\=\s*([^,\n]+))?,?\s*$')
enum_line_re = re.compile(r'^\s*(\w+)(\s*\=\s*([^,\n]+))?,?')
enum_end_re = re.compile(r'^\s*}\s*;\s*$')
generator_directive_re = re.compile(
r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*([\.\w]+)$')