Commit 8bf011fd authored by sashab's avatar sashab Committed by Commit bot

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-Commit-Position: refs/heads/master@{#299009}
parent 83dc32e0
...@@ -36,22 +36,22 @@ class EnumDefinition(object): ...@@ -36,22 +36,22 @@ class EnumDefinition(object):
assert self.entries assert self.entries
def _AssignEntryIndices(self): def _AssignEntryIndices(self):
# Supporting the same set enum value assignments the compiler does is rather # Enums, if given no value, are given the value of the previous enum + 1.
# 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.
if not all(self.entries.values()): if not all(self.entries.values()):
index = 0 prev_enum_value = -1
for key, value in self.entries.iteritems(): for key, value in self.entries.iteritems():
if not value: if not value:
self.entries[key] = index self.entries[key] = prev_enum_value + 1
index = index + 1
elif value in self.entries: elif value in self.entries:
self.entries[key] = self.entries[value] self.entries[key] = self.entries[value]
else: else:
raise Exception('You can only reference other enum constants unless ' try:
'you assign values to all of the constants.') 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): def _StripPrefix(self):
if not self.prefix_to_strip: if not self.prefix_to_strip:
...@@ -69,7 +69,7 @@ class HeaderParser(object): ...@@ -69,7 +69,7 @@ class HeaderParser(object):
single_line_comment_re = re.compile(r'\s*//') single_line_comment_re = re.compile(r'\s*//')
multi_line_comment_start_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_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*$') enum_end_re = re.compile(r'^\s*}\s*;\s*$')
generator_directive_re = re.compile( generator_directive_re = re.compile(
r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*([\.\w]+)$') r'^\s*//\s+GENERATED_JAVA_(\w+)\s*:\s*([\.\w]+)$')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment