Skip to content

Commit 2d1c5e8

Browse files
committed
Rename private messages to terms
1 parent 1faf36b commit 2d1c5e8

File tree

9 files changed

+45
-23
lines changed

9 files changed

+45
-23
lines changed

fluent/syntax/ast.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ def __init__(self, id, value=None, attributes=None,
176176
self.attributes = attributes or []
177177
self.comment = comment
178178

179+
class Term(Entry):
180+
def __init__(self, id, value, attributes=None,
181+
comment=None, **kwargs):
182+
super(Term, self).__init__(**kwargs)
183+
self.id = id
184+
self.value = value
185+
self.attributes = attributes or []
186+
self.comment = comment
187+
179188
class Pattern(SyntaxNode):
180189
def __init__(self, elements, **kwargs):
181190
super(Pattern, self).__init__(**kwargs)

fluent/syntax/errors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def get_error_message(code, args):
4343
if code == 'E0017':
4444
return 'Variants cannot be used as selectors'
4545
if code == 'E0018':
46-
return 'Attributes of public messages cannot be used as selectors'
46+
return 'Attributes of messages cannot be used as selectors'
4747
if code == 'E0019':
48-
return 'Attributes of private messages cannot be used as placeables'
48+
return 'Attributes of terms cannot be used as placeables'
4949
if code == 'E0020':
5050
return 'Unterminated string expression'
5151
return code

fluent/syntax/ftlstream.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def is_char_id_start(self, ch=None):
8888
return (cc >= 97 and cc <= 122) or \
8989
(cc >= 65 and cc <= 90)
9090

91-
def is_message_id_start(self):
91+
def is_entry_id_start(self):
9292
if self.current_is('-'):
9393
self.peek()
9494

@@ -240,15 +240,15 @@ def skip_to_next_entry_start(self):
240240
self.next()
241241

242242
if self.ch is None or \
243-
self.is_message_id_start() or \
243+
self.is_entry_id_start() or \
244244
self.current_is('#') or \
245245
(self.current_is('/') and self.peek_char_is('/')) or \
246246
(self.current_is('[') and self.peek_char_is('[')):
247247
break
248248
self.next()
249249

250-
def take_id_start(self, allow_private):
251-
if allow_private and self.current_is('-'):
250+
def take_id_start(self, allow_term):
251+
if allow_term and self.current_is('-'):
252252
self.next()
253253
return '-'
254254

@@ -257,7 +257,7 @@ def take_id_start(self, allow_private):
257257
self.next()
258258
return ret
259259

260-
allowed_range = 'a-zA-Z-' if allow_private else 'a-zA-Z'
260+
allowed_range = 'a-zA-Z-' if allow_term else 'a-zA-Z'
261261
raise ParseError('E0004', allowed_range)
262262

263263
def take_id_char(self):

fluent/syntax/parser.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def get_entry(self, ps):
106106
return ast.GroupComment(comment.content)
107107
return None
108108

109-
if ps.is_message_id_start() \
109+
if ps.is_entry_id_start() \
110110
and (comment is None or isinstance(comment, ast.Comment)):
111111
return self.get_message(ps, comment)
112112

@@ -200,7 +200,7 @@ def skip_section(self, ps):
200200

201201
@with_span
202202
def get_message(self, ps, comment):
203-
id = self.get_private_identifier(ps)
203+
id = self.get_entry_identifier(ps)
204204

205205
ps.skip_inline_ws()
206206

@@ -221,14 +221,17 @@ def get_message(self, ps, comment):
221221
if pattern is None and attrs is None:
222222
raise ParseError('E0005', id.name)
223223

224+
if id.name.startswith('-'):
225+
return ast.Term(id, pattern, attrs, comment)
226+
224227
return ast.Message(id, pattern, attrs, comment)
225228

226229
@with_span
227230
def get_attribute(self, ps):
228231
ps.expect_indent()
229232
ps.expect_char('.')
230233

231-
key = self.get_public_identifier(ps)
234+
key = self.get_identifier(ps)
232235

233236
ps.skip_inline_ws()
234237
ps.expect_char('=')
@@ -251,18 +254,14 @@ def get_attributes(self, ps):
251254
break
252255
return attrs
253256

254-
@with_span
255-
def get_private_identifier(self, ps):
257+
def get_entry_identifier(self, ps):
256258
return self.get_identifier(ps, True)
257259

258260
@with_span
259-
def get_public_identifier(self, ps):
260-
return self.get_identifier(ps, False)
261-
262-
def get_identifier(self, ps, allow_private):
261+
def get_identifier(self, ps, allow_term=False):
263262
name = ''
264263

265-
name += ps.take_id_start(allow_private)
264+
name += ps.take_id_start(allow_term)
266265

267266
ch = ps.take_id_char()
268267
while ch:
@@ -501,7 +500,7 @@ def get_selector_expression(self, ps):
501500

502501
if (ch == '.'):
503502
ps.next()
504-
attr = self.get_public_identifier(ps)
503+
attr = self.get_identifier(ps)
505504
return ast.AttributeExpression(literal.id, attr)
506505

507506
if (ch == '['):
@@ -603,10 +602,10 @@ def get_literal(self, ps):
603602

604603
if ch == '$':
605604
ps.next()
606-
name = self.get_public_identifier(ps)
605+
name = self.get_identifier(ps)
607606
return ast.ExternalArgument(name)
608-
elif ps.is_message_id_start():
609-
name = self.get_private_identifier(ps)
607+
elif ps.is_entry_id_start():
608+
name = self.get_entry_identifier(ps)
610609
return ast.MessageReference(name)
611610
elif ps.is_number_start():
612611
return self.get_number(ps)

fluent/syntax/serializer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def serialize(self, resource):
3939
def serialize_entry(self, entry, state=0):
4040
if isinstance(entry, ast.Message):
4141
return serialize_message(entry)
42+
if isinstance(entry, ast.Term):
43+
return serialize_message(entry)
4244
if isinstance(entry, ast.Comment):
4345
if state & self.HAS_ENTRIES:
4446
return "\n{}\n\n".format(serialize_comment(entry))

tests/syntax/fixtures_structure/private_message.json renamed to tests/syntax/fixtures_structure/term.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"type": "Resource",
33
"body": [
44
{
5-
"type": "Message",
5+
"type": "Term",
66
"annotations": [],
77
"id": {
88
"type": "Identifier",

tests/syntax/test_serializer.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def test_simple_message(self):
3131
"""
3232
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))
3333

34+
def test_simple_term(self):
35+
input = """\
36+
-foo = Foo
37+
"""
38+
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))
39+
3440
def test_two_simple_messages(self):
3541
input = """\
3642
foo = Foo
@@ -52,6 +58,12 @@ def test_message_reference(self):
5258
"""
5359
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))
5460

61+
def test_term_reference(self):
62+
input = """\
63+
foo = Foo { -bar }
64+
"""
65+
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))
66+
5567
def test_external_argument(self):
5668
input = """\
5769
foo = Foo { $bar }
@@ -72,7 +84,7 @@ def test_string_element(self):
7284

7385
def test_variant_expression(self):
7486
input = """\
75-
foo = Foo { bar[baz] }
87+
foo = Foo { -bar[baz] }
7688
"""
7789
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))
7890

0 commit comments

Comments
 (0)