Skip to content

Commit 9598701

Browse files
authored
Merge pull request #113 from Pike/zeronine
0.9 port, fluent.js -> python-fluent
2 parents d28bc14 + 07f762f commit 9598701

File tree

161 files changed

+5410
-2505
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+5410
-2505
lines changed

fluent.syntax/fluent/syntax/ast.py

+58-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
# coding=utf-8
12
from __future__ import unicode_literals
3+
import re
24
import sys
35
import json
6+
import six
47

58

69
class Visitor(object):
@@ -247,12 +250,6 @@ def __init__(self, id, value, attributes=None,
247250
self.comment = comment
248251

249252

250-
class VariantList(SyntaxNode):
251-
def __init__(self, variants, **kwargs):
252-
super(VariantList, self).__init__(**kwargs)
253-
self.variants = variants
254-
255-
256253
class Pattern(SyntaxNode):
257254
def __init__(self, elements, **kwargs):
258255
super(Pattern, self).__init__(**kwargs)
@@ -279,29 +276,64 @@ class Expression(SyntaxNode):
279276
"""An abstract base class for expressions."""
280277

281278

282-
class StringLiteral(Expression):
283-
def __init__(self, raw, value, **kwargs):
284-
super(StringLiteral, self).__init__(**kwargs)
285-
self.raw = raw
286-
self.value = value
287-
288-
289-
class NumberLiteral(Expression):
279+
class Literal(Expression):
280+
"""An abstract base class for literals."""
290281
def __init__(self, value, **kwargs):
291-
super(NumberLiteral, self).__init__(**kwargs)
282+
super(Literal, self).__init__(**kwargs)
292283
self.value = value
293284

285+
def parse(self):
286+
return {'value': self.value}
287+
288+
289+
class StringLiteral(Literal):
290+
def parse(self):
291+
def from_escape_sequence(matchobj):
292+
c, codepoint4, codepoint6 = matchobj.groups()
293+
if c:
294+
return c
295+
codepoint = int(codepoint4 or codepoint6, 16)
296+
if codepoint <= 0xD7FF or 0xE000 <= codepoint:
297+
return six.unichr(codepoint)
298+
# Escape sequences reresenting surrogate code points are
299+
# well-formed but invalid in Fluent. Replace them with U+FFFD
300+
# REPLACEMENT CHARACTER.
301+
return '�'
302+
303+
value = re.sub(
304+
r'\\(?:(\\|")|u([0-9a-fA-F]{4})|U([0-9a-fA-F]{6}))',
305+
from_escape_sequence,
306+
self.value
307+
)
308+
return {'value': value}
309+
310+
311+
class NumberLiteral(Literal):
312+
def parse(self):
313+
value = float(self.value)
314+
decimal_position = self.value.find('.')
315+
precision = 0
316+
if decimal_position >= 0:
317+
precision = len(self.value) - decimal_position - 1
318+
return {
319+
'value': value,
320+
'precision': precision
321+
}
322+
294323

295324
class MessageReference(Expression):
296-
def __init__(self, id, **kwargs):
325+
def __init__(self, id, attribute=None, **kwargs):
297326
super(MessageReference, self).__init__(**kwargs)
298327
self.id = id
328+
self.attribute = attribute
299329

300330

301331
class TermReference(Expression):
302-
def __init__(self, id, **kwargs):
332+
def __init__(self, id, attribute=None, arguments=None, **kwargs):
303333
super(TermReference, self).__init__(**kwargs)
304334
self.id = id
335+
self.attribute = attribute
336+
self.arguments = arguments
305337

306338

307339
class VariableReference(Expression):
@@ -311,9 +343,10 @@ def __init__(self, id, **kwargs):
311343

312344

313345
class FunctionReference(Expression):
314-
def __init__(self, id, **kwargs):
346+
def __init__(self, id, arguments, **kwargs):
315347
super(FunctionReference, self).__init__(**kwargs)
316348
self.id = id
349+
self.arguments = arguments
317350

318351

319352
class SelectExpression(Expression):
@@ -323,26 +356,11 @@ def __init__(self, selector, variants, **kwargs):
323356
self.variants = variants
324357

325358

326-
class AttributeExpression(Expression):
327-
def __init__(self, ref, name, **kwargs):
328-
super(AttributeExpression, self).__init__(**kwargs)
329-
self.ref = ref
330-
self.name = name
331-
332-
333-
class VariantExpression(Expression):
334-
def __init__(self, ref, key, **kwargs):
335-
super(VariantExpression, self).__init__(**kwargs)
336-
self.ref = ref
337-
self.key = key
338-
339-
340-
class CallExpression(Expression):
341-
def __init__(self, callee, positional=None, named=None, **kwargs):
342-
super(CallExpression, self).__init__(**kwargs)
343-
self.callee = callee
344-
self.positional = positional or []
345-
self.named = named or []
359+
class CallArguments(SyntaxNode):
360+
def __init__(self, positional=None, named=None, **kwargs):
361+
super(CallArguments, self).__init__(**kwargs)
362+
self.positional = [] if positional is None else positional
363+
self.named = [] if named is None else named
346364

347365

348366
class Attribute(SyntaxNode):
@@ -412,8 +430,8 @@ def __init__(self, start, end, **kwargs):
412430

413431

414432
class Annotation(SyntaxNode):
415-
def __init__(self, code, args=None, message=None, **kwargs):
433+
def __init__(self, code, arguments=None, message=None, **kwargs):
416434
super(Annotation, self).__init__(**kwargs)
417435
self.code = code
418-
self.args = args or []
436+
self.arguments = arguments or []
419437
self.message = message

fluent.syntax/fluent/syntax/errors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get_error_message(code, args):
2828
if code == 'E0008':
2929
return 'The callee has to be an upper-case identifier or a term'
3030
if code == 'E0009':
31-
return 'The key has to be a simple identifier'
31+
return 'The argument name has to be a simple identifier'
3232
if code == 'E0010':
3333
return 'Expected one of the variants to be marked as default (*)'
3434
if code == 'E0011':
@@ -60,7 +60,7 @@ def get_error_message(code, args):
6060
if code == 'E0025':
6161
return 'Unknown escape sequence: \\{}.'.format(args[0])
6262
if code == 'E0026':
63-
return 'Invalid Unicode escape sequence: {}'.format(args[0])
63+
return 'Invalid Unicode escape sequence: {}.'.format(args[0])
6464
if code == 'E0027':
6565
return 'Unbalanced closing brace in TextElement.'
6666
if code == 'E0028':

0 commit comments

Comments
 (0)