Skip to content

Commit 47e307e

Browse files
committed
Expose FluentSerializer.seralize_expression
Port of projectfluent/fluent.js#134.
1 parent c6ea23d commit 47e307e

File tree

2 files changed

+155
-62
lines changed

2 files changed

+155
-62
lines changed

fluent/syntax/serializer.py

+24-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def __init__(self, with_junk=False):
2222
self.with_junk = with_junk
2323

2424
def serialize(self, resource):
25+
if not isinstance(resource, ast.Resource):
26+
raise Exception('Unknown resource type: {}'.format(type(resource)))
27+
2528
state = 0
2629

2730
parts = []
@@ -50,7 +53,10 @@ def serialize_entry(self, entry, state=0):
5053
return "{}\n\n".format(serialize_resource_comment(entry))
5154
if isinstance(entry, ast.Junk):
5255
return serialize_junk(entry)
53-
raise Exception('Unknown entry type: {}'.format(entry.type))
56+
raise Exception('Unknown entry type: {}'.format(type(entry)))
57+
58+
def serialize_expression(self, expr):
59+
return serialize_expression(expr)
5460

5561

5662
def serialize_comment(comment):
@@ -139,7 +145,7 @@ def serialize_element(element):
139145
return serialize_text_element(element)
140146
if isinstance(element, ast.Placeable):
141147
return serialize_placeable(element)
142-
raise Exception('Unknown element type: {}'.format(element.type))
148+
raise Exception('Unknown element type: {}'.format(type(element)))
143149

144150

145151
def serialize_text_element(text):
@@ -150,14 +156,18 @@ def serialize_placeable(placeable):
150156
expr = placeable.expression
151157

152158
if isinstance(expr, ast.Placeable):
153-
return "{{{}}}".format(
154-
serialize_placeable(expr))
159+
return "{{{}}}".format(serialize_placeable(expr))
155160
if isinstance(expr, ast.SelectExpression):
156-
return "{{{}}}".format(
157-
serialize_select_expression(expr))
161+
# Special-case select expressions to control the withespace around the
162+
# opening and the closing brace.
163+
if expr.expression is not None:
164+
# A select expression with a selector.
165+
return "{{ {}}}".format(serialize_select_expression(expr))
166+
else:
167+
# A variant list without a selector.
168+
return "{{{}}}".format(serialize_select_expression(expr))
158169
if isinstance(expr, ast.Expression):
159-
return "{{ {} }}".format(
160-
serialize_expression(expr))
170+
return "{{ {} }}".format(serialize_expression(expr))
161171

162172

163173
def serialize_expression(expression):
@@ -175,7 +185,9 @@ def serialize_expression(expression):
175185
return serialize_variant_expression(expression)
176186
if isinstance(expression, ast.CallExpression):
177187
return serialize_call_expression(expression)
178-
raise Exception('Unknown expression type: {}'.format(expression.type))
188+
if isinstance(expression, ast.SelectExpression):
189+
return serialize_select_expression(expression)
190+
raise Exception('Unknown expression type: {}'.format(type(expression)))
179191

180192

181193
def serialize_string_expression(expr):
@@ -198,7 +210,7 @@ def serialize_select_expression(expr):
198210
parts = []
199211

200212
if expr.expression:
201-
selector = " {} ->".format(
213+
selector = "{} ->".format(
202214
serialize_expression(expr.expression)
203215
)
204216
parts.append(selector)
@@ -262,7 +274,7 @@ def serialize_argument_value(argval):
262274
return serialize_string_expression(argval)
263275
if isinstance(argval, ast.NumberExpression):
264276
return serialize_number_expression(argval)
265-
raise Exception('Unknown argument type: {}'.format(argval.type))
277+
raise Exception('Unknown argument type: {}'.format(type(argval)))
266278

267279

268280
def serialize_identifier(identifier):
@@ -278,7 +290,7 @@ def serialize_variant_key(key):
278290
return serialize_variant_name(key)
279291
if isinstance(key, ast.NumberExpression):
280292
return serialize_number_expression(key)
281-
raise Exception('Unknown variant key type: {}'.format(key.type))
293+
raise Exception('Unknown variant key type: {}'.format(type(key)))
282294

283295

284296
def serialize_function(function):

0 commit comments

Comments
 (0)