@@ -143,13 +143,12 @@ def serialize_element(element):
143
143
144
144
def serialize_placeable (placeable ):
145
145
expr = placeable .expression
146
-
147
146
if isinstance (expr , ast .Placeable ):
148
147
return "{{{}}}" .format (serialize_placeable (expr ))
149
148
if isinstance (expr , ast .SelectExpression ):
150
149
# Special-case select expressions to control the withespace around the
151
150
# opening and the closing brace.
152
- return "{{ {}}}" .format (serialize_select_expression (expr ))
151
+ return "{{ {}}}" .format (serialize_expression (expr ))
153
152
if isinstance (expr , ast .Expression ):
154
153
return "{{ {} }}" .format (serialize_expression (expr ))
155
154
@@ -160,49 +159,33 @@ def serialize_expression(expression):
160
159
if isinstance (expression , ast .NumberLiteral ):
161
160
return expression .value
162
161
if isinstance (expression , ast .VariableReference ):
163
- return ' ${}' .format (expression .id .name )
162
+ return " ${}" .format (expression .id .name )
164
163
if isinstance (expression , ast .TermReference ):
165
- return serialize_reference_expression (
166
- expression ,
167
- id = '-{}' .format (expression .id .name )
168
- )
169
- if isinstance (
170
- expression , (
171
- ast .MessageReference ,
172
- ast .FunctionReference ,
173
- )
174
- ):
175
- return serialize_reference_expression (expression )
164
+ out = "-{}" .format (expression .id .name )
165
+ if expression .attribute is not None :
166
+ out += ".{}" .format (expression .attribute .name )
167
+ if expression .arguments is not None :
168
+ out += serialize_call_arguments (expression .arguments )
169
+ return out
170
+ if isinstance (expression , ast .MessageReference ):
171
+ out = expression .id .name
172
+ if expression .attribute is not None :
173
+ out += ".{}" .format (expression .attribute .name )
174
+ return out
175
+ if isinstance (expression , ast .FunctionReference ):
176
+ args = serialize_call_arguments (expression .arguments )
177
+ return "{}{}" .format (expression .id .name , args )
176
178
if isinstance (expression , ast .SelectExpression ):
177
- return serialize_select_expression (expression )
179
+ out = "{} ->" .format (
180
+ serialize_expression (expression .selector ))
181
+ for variant in expression .variants :
182
+ out += serialize_variant (variant )
183
+ return "{}\n " .format (out )
178
184
if isinstance (expression , ast .Placeable ):
179
185
return serialize_placeable (expression )
180
186
raise Exception ('Unknown expression type: {}' .format (type (expression )))
181
187
182
188
183
- def serialize_reference_expression (expression , id = None ):
184
- parts = [expression .id .name if id is None else id ]
185
- if hasattr (expression , 'attribute' ) and expression .attribute is not None :
186
- parts .append ('.{}' .format (expression .attribute .name ))
187
- if hasattr (expression , 'arguments' ) and expression .arguments is not None :
188
- parts .append (serialize_call_expression (expression .arguments ))
189
- return '' .join (parts )
190
-
191
-
192
- def serialize_select_expression (expr ):
193
- parts = []
194
- selector = "{} ->" .format (
195
- serialize_expression (expr .selector ))
196
- parts .append (selector )
197
-
198
- for variant in expr .variants :
199
- parts .append (serialize_variant (variant ))
200
-
201
- parts .append ("\n " )
202
-
203
- return "" .join (parts )
204
-
205
-
206
189
def serialize_variant (variant ):
207
190
return "\n {}[{}]{}" .format (
208
191
" *" if variant .default else " " ,
@@ -211,7 +194,7 @@ def serialize_variant(variant):
211
194
)
212
195
213
196
214
- def serialize_call_expression (expr ):
197
+ def serialize_call_arguments (expr ):
215
198
positional = ", " .join (
216
199
serialize_expression (arg ) for arg in expr .positional )
217
200
named = ", " .join (
0 commit comments