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

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

Issue 398553002: Mojo: Mojom: Add AST types for struct and struct body. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Translates parse tree to Mojom IR.""" 5 """Translates parse tree to Mojom IR."""
6 6
7 7
8 import ast 8 import ast
9 import re 9 import re
10 10
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 'kind': _MapKind(param.typename), 78 'kind': _MapKind(param.typename),
79 'ordinal': param.ordinal.value if param.ordinal else None} 79 'ordinal': param.ordinal.value if param.ordinal else None}
80 80
81 method = {'name': tree[1], 81 method = {'name': tree[1],
82 'parameters': map(ParameterToDict, tree[2]), 82 'parameters': map(ParameterToDict, tree[2]),
83 'ordinal': tree[3].value if tree[3] else None} 83 'ordinal': tree[3].value if tree[3] else None}
84 if tree[4]: 84 if tree[4]:
85 method['response_parameters'] = map(ParameterToDict, tree[4]) 85 method['response_parameters'] = map(ParameterToDict, tree[4])
86 return method 86 return method
87 87
88 def _MapStruct(tree):
89 def StructFieldToDict(struct_field):
90 assert isinstance(struct_field, ast.StructField)
91 return {'name': struct_field.name,
92 'kind': _MapKind(struct_field.typename),
93 'ordinal': struct_field.ordinal.value \
94 if struct_field.ordinal else None,
95 'default': struct_field.default_value}
96
97 struct = {}
98 struct['name'] = tree[1]
99 struct['attributes'] = _AttributeListToDict(tree[2])
100 struct['fields'] = _MapTreeForType(StructFieldToDict, tree[3],
101 ast.StructField)
102 struct['enums'] = _MapTreeForType(_MapEnum, tree[3], ast.Enum)
103 struct['constants'] = _MapTreeForType(_MapConstant, tree[3], ast.Const)
104 return struct
105
106 def _MapInterface(tree): 88 def _MapInterface(tree):
107 interface = {} 89 interface = {}
108 interface['name'] = tree[1] 90 interface['name'] = tree[1]
109 interface['attributes'] = _AttributeListToDict(tree[2]) 91 interface['attributes'] = _AttributeListToDict(tree[2])
110 interface['client'] = interface['attributes'].get('Client') 92 interface['client'] = interface['attributes'].get('Client')
111 interface['methods'] = _MapTreeForName(_MapMethod, tree[3], 'METHOD') 93 interface['methods'] = _MapTreeForName(_MapMethod, tree[3], 'METHOD')
112 interface['enums'] = _MapTreeForType(_MapEnum, tree[3], ast.Enum) 94 interface['enums'] = _MapTreeForType(_MapEnum, tree[3], ast.Enum)
113 interface['constants'] = _MapTreeForType(_MapConstant, tree[3], ast.Const) 95 interface['constants'] = _MapTreeForType(_MapConstant, tree[3], ast.Const)
114 return interface 96 return interface
115 97
(...skipping 12 matching lines...) Expand all
128 return {'name': const.name, 110 return {'name': const.name,
129 'kind': _MapKind(const.typename), 111 'kind': _MapKind(const.typename),
130 'value': const.value} 112 'value': const.value}
131 113
132 114
133 class _MojomBuilder(object): 115 class _MojomBuilder(object):
134 def __init__(self): 116 def __init__(self):
135 self.mojom = {} 117 self.mojom = {}
136 118
137 def Build(self, tree, name): 119 def Build(self, tree, name):
120 def StructToDict(struct):
121 def StructFieldToDict(struct_field):
122 assert isinstance(struct_field, ast.StructField)
123 return {'name': struct_field.name,
124 'kind': _MapKind(struct_field.typename),
125 'ordinal': struct_field.ordinal.value \
126 if struct_field.ordinal else None,
127 'default': struct_field.default_value}
128
129 assert isinstance(struct, ast.Struct)
130 return {'name': struct.name,
131 'attributes': _AttributeListToDict(struct.attribute_list),
132 'fields': _MapTreeForType(StructFieldToDict, struct.body,
133 ast.StructField),
134 'enums': _MapTreeForType(_MapEnum, struct.body, ast.Enum),
135 'constants': _MapTreeForType(_MapConstant, struct.body,
136 ast.Const)}
137
138 assert isinstance(tree, ast.Mojom) 138 assert isinstance(tree, ast.Mojom)
139 self.mojom['name'] = name 139 self.mojom['name'] = name
140 self.mojom['namespace'] = tree.module.name[1] if tree.module else '' 140 self.mojom['namespace'] = tree.module.name[1] if tree.module else ''
141 self.mojom['imports'] = \ 141 self.mojom['imports'] = \
142 [{'filename': imp.import_filename} for imp in tree.import_list] 142 [{'filename': imp.import_filename} for imp in tree.import_list]
143 self.mojom['attributes'] = \ 143 self.mojom['attributes'] = \
144 _AttributeListToDict(tree.module.attribute_list) if tree.module else {} 144 _AttributeListToDict(tree.module.attribute_list) if tree.module else {}
145 self.mojom['structs'] = \ 145 self.mojom['structs'] = \
146 _MapTreeForName(_MapStruct, tree.definition_list, 'STRUCT') 146 _MapTreeForType(StructToDict, tree.definition_list, ast.Struct)
147 self.mojom['interfaces'] = \ 147 self.mojom['interfaces'] = \
148 _MapTreeForName(_MapInterface, tree.definition_list, 'INTERFACE') 148 _MapTreeForName(_MapInterface, tree.definition_list, 'INTERFACE')
149 self.mojom['enums'] = \ 149 self.mojom['enums'] = \
150 _MapTreeForType(_MapEnum, tree.definition_list, ast.Enum) 150 _MapTreeForType(_MapEnum, tree.definition_list, ast.Enum)
151 self.mojom['constants'] = \ 151 self.mojom['constants'] = \
152 _MapTreeForType(_MapConstant, tree.definition_list, ast.Const) 152 _MapTreeForType(_MapConstant, tree.definition_list, ast.Const)
153 return self.mojom 153 return self.mojom
154 154
155 155
156 def Translate(tree, name): 156 def Translate(tree, name):
157 return _MojomBuilder().Build(tree, name) 157 return _MojomBuilder().Build(tree, name)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698