Skip to content

Implement Syntax 0.8 #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/syntax/fixtures_reference/crlf.ftl eol=crlf
tests/syntax/fixtures_reference/cr.ftl eol=cr
tests/syntax/fixtures_structure/crlf.ftl eol=crlf
28 changes: 15 additions & 13 deletions fluent/syntax/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import json


def to_json(value):
def to_json(value, fn=None):
if isinstance(value, BaseNode):
return value.to_json()
return value.to_json(fn)
if isinstance(value, list):
return list(map(to_json, value))
return list(to_json(item, fn) for item in value)
if isinstance(value, tuple):
return list(map(to_json, value))
return list(to_json(item, fn) for item in value)
else:
return value

Expand Down Expand Up @@ -119,15 +119,15 @@ def equals(self, other, ignored_fields=['span']):

return True

def to_json(self):
def to_json(self, fn=None):
obj = {
name: to_json(value)
name: to_json(value, fn)
for name, value in vars(self).items()
}
obj.update(
{'type': self.__class__.__name__}
)
return obj
return fn(obj) if fn else obj

def __str__(self):
return json.dumps(self.to_json())
Expand Down Expand Up @@ -207,8 +207,9 @@ class Expression(SyntaxNode):


class StringLiteral(Expression):
def __init__(self, value, **kwargs):
def __init__(self, raw, value, **kwargs):
super(StringLiteral, self).__init__(**kwargs)
self.raw = raw
self.value = value


Expand Down Expand Up @@ -236,6 +237,12 @@ def __init__(self, id, **kwargs):
self.id = id


class FunctionReference(Expression):
def __init__(self, id, **kwargs):
super(FunctionReference, self).__init__(**kwargs)
self.id = id


class SelectExpression(Expression):
def __init__(self, selector, variants, **kwargs):
super(SelectExpression, self).__init__(**kwargs)
Expand Down Expand Up @@ -324,11 +331,6 @@ def __init__(self, content=None, **kwargs):
super(ResourceComment, self).__init__(content, **kwargs)


class Function(Identifier):
def __init__(self, name, **kwargs):
super(Function, self).__init__(name, **kwargs)


class Junk(SyntaxNode):
def __init__(self, content=None, annotations=None, **kwargs):
super(Junk, self).__init__(**kwargs)
Expand Down
14 changes: 8 additions & 6 deletions fluent/syntax/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def get_error_message(code, args):
msg = 'Expected message "{}" to have a value or attributes'
return msg.format(args[0])
if code == 'E0006':
msg = 'Expected term "{}" to have a value'
msg = 'Expected term "-{}" to have a value'
return msg.format(args[0])
if code == 'E0007':
return 'Keyword cannot end with a whitespace'
if code == 'E0008':
return 'The callee has to be a simple, upper-case identifier'
return 'The callee has to be an upper-case identifier or a term'
if code == 'E0009':
return 'The key has to be a simple identifier'
if code == 'E0010':
Expand All @@ -44,7 +44,7 @@ def get_error_message(code, args):
if code == 'E0016':
return 'Message references cannot be used as selectors'
if code == 'E0017':
return 'Variants cannot be used as selectors'
return 'Terms cannot be used as selectors'
if code == 'E0018':
return 'Attributes of messages cannot be used as selectors'
if code == 'E0019':
Expand All @@ -55,12 +55,14 @@ def get_error_message(code, args):
return 'Positional arguments must not follow named arguments'
if code == 'E0022':
return 'Named arguments must be unique'
if code == 'E0023':
return 'VariantLists are only allowed inside of other VariantLists.'
if code == 'E0024':
return 'Cannot access variants of a message.'
if code == 'E0025':
return 'Unknown escape sequence: {}'.format(args[0])
return 'Unknown escape sequence: \\{}.'.format(args[0])
if code == 'E0026':
return 'Invalid Unicode escape sequence: {}'.format(args[0])
if code == 'E0027':
return 'Unbalanced closing brace in TextElement.'
if code == 'E0028':
return 'Expected an inline expression'
return code
Loading