Skip to content

Restrict variant keys #157 #74

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

Closed
Closed
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
6 changes: 3 additions & 3 deletions fluent/syntax/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ def __init__(self, name, **kwargs):
self.name = name


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


class BaseComment(Entry):
Expand Down
20 changes: 10 additions & 10 deletions fluent/syntax/ftlstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,16 @@ def closure(ch):
cc == 95 or cc == 45)
return self.take_char(closure)

def take_variant_name_char(self):
def closure(ch):
if ch is None:
return False
cc = ord(ch)
return (cc >= 97 and cc <= 122) or \
(cc >= 65 and cc <= 90) or \
(cc >= 48 and cc <= 57) or \
cc == 95 or cc == 45 or cc == 32
return self.take_char(closure)
# def take_variant_name_char(self):
# def closure(ch):
# if ch is None:
# return False
# cc = ord(ch)
# return (cc >= 97 and cc <= 122) or \
# (cc >= 65 and cc <= 90) or \
# (cc >= 48 and cc <= 57) or \
# cc == 95 or cc == 45 or cc == 32
# return self.take_char(closure)

def take_digit(self):
def closure(ch):
Expand Down
26 changes: 13 additions & 13 deletions fluent/syntax/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def get_group_comment_from_section(self, ps):

ps.skip_inline_ws()

self.get_variant_name(ps)
self.get_identifier(ps)

ps.skip_inline_ws()

Expand Down Expand Up @@ -334,7 +334,7 @@ def get_variant_key(self, ps):
if ((cc >= 48 and cc <= 57) or cc == 45): # 0-9, -
return self.get_number(ps)

return self.get_variant_name(ps)
return self.get_identifier(ps)

@with_span
def get_variant(self, ps, has_default):
Expand Down Expand Up @@ -380,17 +380,17 @@ def get_variants(self, ps):

return variants

@with_span
def get_variant_name(self, ps):
name = ps.take_id_start()
while True:
ch = ps.take_variant_name_char()
if ch:
name += ch
else:
break

return ast.VariantName(name.rstrip(' \t\n\r'))
# @with_span
# def get_variant_name(self, ps):
# name = ps.take_id_start()
# while True:
# ch = ps.take_variant_name_char()
# if ch:
# name += ch
# else:
# break

# return ast.Identifier(name.rstrip(' \t\n\r'))

def get_digits(self, ps):
num = ''
Expand Down
8 changes: 4 additions & 4 deletions fluent/syntax/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ def serialize_identifier(identifier):
return identifier.name


def serialize_variant_name(symbol):
return symbol.name
# def serialize_variant_name(symbol):
# return symbol.name


def serialize_variant_key(key):
if isinstance(key, ast.VariantName):
return serialize_variant_name(key)
if isinstance(key, ast.Identifier):
return serialize_identifier(key)
if isinstance(key, ast.NumberLiteral):
return serialize_number_literal(key)
raise Exception('Unknown variant key type: {}'.format(type(key)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[[ This is
a broken section]]
# ~ERROR E0003, pos 10, args "]"
# ~ERROR E0003, pos 8, args "]"
# ~ERROR E0005, pos 13, args "a"
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
[[ This is a correct section ]]
# ~ERROR E0003, pos 8, args "]"
# ~ERROR E0005, pos 13, args "a"
[[ This is
a broken section]]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[[ This is a broken section ]
# ~ERROR E0003, pos 29, args "]"
# ~ERROR E0003, pos 8, args "]"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ~ERROR E0003, pos 23, args "]"
-term = {
*[New York] Nowy Jork
}
}
4 changes: 2 additions & 2 deletions tests/syntax/fixtures_structure/leading_dots.json
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "one",
"span": {
"type": "Span",
Expand Down Expand Up @@ -782,7 +782,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "other",
"span": {
"type": "Span",
Expand Down
20 changes: 9 additions & 11 deletions tests/syntax/fixtures_structure/section.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"body": [{
"annotations": [{"args": ["]"],
"code": "E0003",
"message": "Expected token: \"]\"",
"span": {"end": 9, "start": 9, "type": "Span"},
"type": "Annotation"}],
"content": "[[ This is a section ]]\n",
"span": {"end": 25, "start": 1, "type": "Span"},
"type": "Junk"}],
"type": "Resource",
"body": [
{
"type": "GroupComment",
"content": "",
"span": {
"type": "Span",
"start": 1,
"end": 24
}
}
],
"span": {
"type": "Span",
"start": 0,
Expand Down
8 changes: 4 additions & 4 deletions tests/syntax/fixtures_structure/sparse-messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "one",
"span": {
"type": "Span",
Expand Down Expand Up @@ -285,7 +285,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "two",
"span": {
"type": "Span",
Expand Down Expand Up @@ -364,7 +364,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "one",
"span": {
"type": "Span",
Expand Down Expand Up @@ -430,7 +430,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "one",
"span": {
"type": "Span",
Expand Down
12 changes: 6 additions & 6 deletions tests/syntax/fixtures_structure/term.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "nominative",
"span": {
"type": "Span",
Expand Down Expand Up @@ -55,7 +55,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "accusative",
"span": {
"type": "Span",
Expand Down Expand Up @@ -186,7 +186,7 @@
}
},
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "accusative",
"span": {
"type": "Span",
Expand Down Expand Up @@ -286,7 +286,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "masculine",
"span": {
"type": "Span",
Expand Down Expand Up @@ -348,7 +348,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "feminine",
"span": {
"type": "Span",
Expand Down Expand Up @@ -410,7 +410,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "other",
"span": {
"type": "Span",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "one",
"span": {
"type": "Span",
Expand Down
16 changes: 8 additions & 8 deletions tests/syntax/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,14 @@ def test_variant_multiline_first_inline(self):
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(output))

def test_variant_key_words(self):
input = """\
foo =
{ $sel ->
*[a b c] A B C
}
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))
# def test_variant_key_words(self):
# input = """\
# foo =
# { $sel ->
# *[a b c] A B C
# }
# """
# self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

def test_variant_key_number(self):
input = """\
Expand Down