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

Unified Diff: mojo/public/tools/bindings/pylib/mojom/parse/translate.py

Issue 611633002: mojom: Add associative arrays to the mojom language. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix gn build. 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/pylib/mojom/parse/translate.py
diff --git a/mojo/public/tools/bindings/pylib/mojom/parse/translate.py b/mojo/public/tools/bindings/pylib/mojom/parse/translate.py
index 77e92c5b8b2065c59ebb7453aa18b7b298cce3f9..f5de48a4952325ed81e10e6bc2b4f15551ba55f1 100644
--- a/mojo/public/tools/bindings/pylib/mojom/parse/translate.py
+++ b/mojo/public/tools/bindings/pylib/mojom/parse/translate.py
@@ -18,7 +18,7 @@ def _MapTreeForType(func, tree, type_to_map):
_FIXED_ARRAY_REGEXP = re.compile(r'\[[0-9]+\]')
-def _MapKind(kind):
+def MapKind(kind):
map_to_kind = {'bool': 'b',
'int8': 'i8',
'int16': 'i16',
@@ -37,27 +37,36 @@ def _MapKind(kind):
'handle<message_pipe>': 'h:m',
'handle<shared_buffer>': 'h:s'}
if kind.endswith('?'):
- base_kind = _MapKind(kind[0:-1])
+ base_kind = MapKind(kind[0:-1])
# NOTE: This doesn't rule out enum types. Those will be detected later, when
# cross-reference is established.
- reference_kinds = ('s', 'h', 'a', 'r', 'x')
+ reference_kinds = ('m', 's', 'h', 'a', 'r', 'x')
if base_kind[0] not in reference_kinds:
raise Exception(
'A type (spec "%s") cannot be made nullable' % base_kind)
return '?' + base_kind
+ first_curly = kind.find('{')
+ if first_curly != -1:
+ # We want to parse associative arrays from left to right as much as
+ # possible. So remove remove the first associative array key as the key
+ # type, and pass the rest (which may be an array) to ourselves recursively.
+ closing_curly = kind.find('}', first_curly)
+ key = kind[first_curly+1:closing_curly]
+ rest = kind[0:first_curly] + kind[closing_curly+1:]
+ return 'm[' + MapKind(key) + '][' + MapKind(rest) + ']'
if kind.endswith('[]'):
typename = kind[0:-2]
if _FIXED_ARRAY_REGEXP.search(typename):
raise Exception('Arrays of fixed sized arrays not supported')
- return 'a:' + _MapKind(typename)
+ return 'a:' + MapKind(typename)
if kind.endswith(']'):
lbracket = kind.rfind('[')
typename = kind[0:lbracket]
if typename.find('[') != -1:
raise Exception('Fixed sized arrays of arrays not supported')
- return 'a' + kind[lbracket+1:-1] + ':' + _MapKind(typename)
+ return 'a' + kind[lbracket+1:-1] + ':' + MapKind(typename)
if kind.endswith('&'):
- return 'r:' + _MapKind(kind[0:-1])
+ return 'r:' + MapKind(kind[0:-1])
if kind in map_to_kind:
return map_to_kind[kind]
return 'x:' + kind
@@ -83,7 +92,7 @@ def _EnumToDict(enum):
def _ConstToDict(const):
assert isinstance(const, ast.Const)
return {'name': const.name,
- 'kind': _MapKind(const.typename),
+ 'kind': MapKind(const.typename),
'value': const.value}
@@ -96,7 +105,7 @@ class _MojomBuilder(object):
def StructFieldToDict(struct_field):
assert isinstance(struct_field, ast.StructField)
return {'name': struct_field.name,
- 'kind': _MapKind(struct_field.typename),
+ 'kind': MapKind(struct_field.typename),
'ordinal': struct_field.ordinal.value \
if struct_field.ordinal else None,
'default': struct_field.default_value}
@@ -115,7 +124,7 @@ class _MojomBuilder(object):
def ParameterToDict(param):
assert isinstance(param, ast.Parameter)
return {'name': param.name,
- 'kind': _MapKind(param.typename),
+ 'kind': MapKind(param.typename),
'ordinal': param.ordinal.value if param.ordinal else None}
assert isinstance(method, ast.Method)

Powered by Google App Engine
This is Rietveld 408576698