Commit c7ce710c authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

IDL compiler: Implement ExtendedAttributes.syntactic_form

Stops (ab)use of __str__.

Bug: 839389
Change-Id: I428b5fe95b93c4abe8285896c1116bbf5dd01844
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1724410Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682583}
parent f4fe9d84
......@@ -24,6 +24,7 @@ class ExtendedAttribute(object):
_FORM_NAMED_ARG_LIST = 'NamedArgList' # for (e)
def __init__(self, key, values=None, arguments=None, name=None):
assert isinstance(key, str)
# |values| can be either of None, a single string, or a list.
assert values is None or isinstance(values, (str, tuple, list))
# |arguments| can be either of None or a list of pairs of strings.
......@@ -57,7 +58,15 @@ class ExtendedAttribute(object):
else:
raise ValueError('Unknown format for ExtendedAttribute')
def __str__(self):
def make_copy(self):
return ExtendedAttribute(
key=self._key,
values=self._values,
arguments=self._arguments,
name=self._name)
@property
def syntactic_form(self):
if self._format == self._FORM_NO_ARGS:
return self._key
if self._format == self._FORM_IDENT:
......@@ -65,21 +74,13 @@ class ExtendedAttribute(object):
if self._format == self._FORM_IDENT_LIST:
return '{}=({})'.format(self._key, ', '.join(self._values))
args_str = '({})'.format(', '.join(
[' '.join(arg) for arg in self._arguments]))
['{} {}'.format(left, right) for left, right in self._arguments]))
if self._format == self._FORM_ARG_LIST:
return '{}{}'.format(self._key, args_str)
if self._format == self._FORM_NAMED_ARG_LIST:
return '{}={}{}'.format(self._key, self._name, args_str)
# Should not reach here.
assert False, 'Unknown format: {}'.format(self._format)
def make_copy(self):
return ExtendedAttribute(
key=self._key,
values=self._values,
arguments=self._arguments,
name=self._name)
@property
def key(self):
"""
......@@ -184,13 +185,14 @@ class ExtendedAttributes(object):
def __len__(self):
return len(list(self.__iter__()))
def __str__(self):
attrs = [str(attr) for attr in self]
return '[{}]'.format(', '.join(attrs))
def make_copy(self):
return ExtendedAttributes(map(ExtendedAttribute.make_copy, self))
@property
def syntactic_form(self):
attrs = [str(attr) for attr in self]
return '[{}]'.format(', '.join(attrs))
def keys(self):
return self._attributes.keys()
......
......@@ -138,9 +138,6 @@ class IdlType(WithExtendedAttributes, WithCodeGeneratorInfo, WithDebugInfo):
WithDebugInfo.__init__(self, debug_info)
self._is_optional = is_optional
def __str__(self):
return self.syntactic_form
@property
def syntactic_form(self):
"""
......@@ -361,7 +358,7 @@ class IdlType(WithExtendedAttributes, WithCodeGeneratorInfo, WithDebugInfo):
def _format_syntactic_form(self, syntactic_form_inner):
"""Helper function to implement |syntactic_form|."""
optional_form = 'optional ' if self.is_optional else ''
ext_attr_form = ('{} '.format(self.extended_attributes)
ext_attr_form = ('{} '.format(self.extended_attributes.syntactic_form)
if self.extended_attributes else '')
return '{}{}{}'.format(optional_form, ext_attr_form,
syntactic_form_inner)
......
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