Skip to content

Commit

Permalink
Merge pull request #38 from stasm/fix-compare-locales
Browse files Browse the repository at this point in the history
Parser fixes required to update compare-locales to 0.6
  • Loading branch information
stasm authored Jan 31, 2018
2 parents 7b80850 + 1f3aeb6 commit 32bca38
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 29 deletions.
3 changes: 0 additions & 3 deletions fluent/syntax/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,7 @@ def __init__(self, content=None, **kwargs):

class Comment(BaseComment):
def __init__(self, content=None, **kwargs):
zero_four_style = kwargs.pop("zero_four_style", False)
super(Comment, self).__init__(content, **kwargs)
if zero_four_style:
self.zero_four_style = True


class GroupComment(BaseComment):
Expand Down
26 changes: 16 additions & 10 deletions fluent/syntax/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ def get_entry(self, ps):
ps.expect_char('\n' if ps.current() else None)

if ps.current_is('['):
self.skip_section(ps)
if comment:
return ast.GroupComment(comment.content)
return None
group_comment = self.get_group_comment_from_section(ps, comment)
if comment and self.with_spans:
# The Group Comment should start where the section comment
# starts.
group_comment.span.start = comment.span.start
return group_comment

if ps.is_entry_id_start() \
and (comment is None or isinstance(comment, ast.Comment)):
Expand Down Expand Up @@ -182,7 +184,8 @@ def get_comment(self, ps):
elif level == 2:
return ast.ResourceComment(content)

def skip_section(self, ps):
@with_span
def get_group_comment_from_section(self, ps, comment):
ps.expect_char('[')
ps.expect_char('[')

Expand All @@ -195,8 +198,12 @@ def skip_section(self, ps):
ps.expect_char(']')
ps.expect_char(']')

ps.skip_inline_ws()
ps.next()
if comment:
return ast.GroupComment(comment.content)

# A Section without a comment is like an empty Group Comment.
# Semantically it ends the previous group and starts a new one.
return ast.GroupComment('')

@with_span
def get_message(self, ps, comment):
Expand Down Expand Up @@ -231,7 +238,6 @@ def get_message(self, ps, comment):

@with_span
def get_attribute(self, ps):
ps.expect_indent()
ps.expect_char('.')

key = self.get_identifier(ps)
Expand All @@ -250,6 +256,7 @@ def get_attributes(self, ps):
attrs = []

while True:
ps.expect_indent()
attr = self.get_attribute(ps)
attrs.append(attr)

Expand Down Expand Up @@ -287,8 +294,6 @@ def get_variant_key(self, ps):

@with_span
def get_variant(self, ps, has_default):
ps.expect_indent()

default_index = False

if ps.current_is('*'):
Expand All @@ -315,6 +320,7 @@ def get_variants(self, ps):
has_default = False

while True:
ps.expect_indent()
variant = self.get_variant(ps, has_default)

if variant.default:
Expand Down
2 changes: 1 addition & 1 deletion tests/syntax/fixtures_structure/elements_indent.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
},
"span": {
"type": "Span",
"start": 37,
"start": 42,
"end": 61
}
}
Expand Down
13 changes: 12 additions & 1 deletion tests/syntax/fixtures_structure/section.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{
"type": "Resource",
"body": [],
"body": [
{
"type": "GroupComment",
"annotations": [],
"content": "",
"span": {
"type": "Span",
"start": 1,
"end": 24
}
}
],
"span": {
"type": "Span",
"start": 0,
Expand Down
2 changes: 2 additions & 0 deletions tests/syntax/fixtures_structure/section_with_comment.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Comment
[[ Section ]]
20 changes: 20 additions & 0 deletions tests/syntax/fixtures_structure/section_with_comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"type": "Resource",
"body": [
{
"type": "GroupComment",
"annotations": [],
"content": "Comment",
"span": {
"type": "Span",
"start": 0,
"end": 24
}
}
],
"span": {
"type": "Span",
"start": 0,
"end": 25
}
}
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 @@ -86,7 +86,7 @@
},
"span": {
"type": "Span",
"start": 23,
"start": 30,
"end": 47
}
}
Expand Down Expand Up @@ -162,7 +162,7 @@
},
"span": {
"type": "Span",
"start": 102,
"start": 110,
"end": 125
}
}
Expand Down Expand Up @@ -267,7 +267,7 @@
"default": false,
"span": {
"type": "Span",
"start": 152,
"start": 164,
"end": 173
}
},
Expand Down Expand Up @@ -304,7 +304,7 @@
"default": true,
"span": {
"type": "Span",
"start": 173,
"start": 186,
"end": 196
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/syntax/fixtures_structure/syntax_zero_four.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"span": {
"type": "Span",
"start": 4,
"start": 9,
"end": 24
}
}
Expand Down Expand Up @@ -105,7 +105,7 @@
},
"span": {
"type": "Span",
"start": 30,
"start": 35,
"end": 50
}
},
Expand Down Expand Up @@ -141,7 +141,7 @@
},
"span": {
"type": "Span",
"start": 50,
"start": 55,
"end": 70
}
}
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 @@ -55,7 +55,7 @@
"default": true,
"span": {
"type": "Span",
"start": 19,
"start": 27,
"end": 48
}
},
Expand Down Expand Up @@ -92,7 +92,7 @@
"default": false,
"span": {
"type": "Span",
"start": 48,
"start": 57,
"end": 78
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@
},
"span": {
"type": "Span",
"start": 84,
"start": 89,
"end": 108
}
}
Expand Down Expand Up @@ -347,7 +347,7 @@
"default": false,
"span": {
"type": "Span",
"start": 220,
"start": 229,
"end": 289
}
},
Expand Down Expand Up @@ -409,7 +409,7 @@
"default": false,
"span": {
"type": "Span",
"start": 289,
"start": 298,
"end": 358
}
},
Expand Down Expand Up @@ -480,7 +480,7 @@
"default": true,
"span": {
"type": "Span",
"start": 358,
"start": 366,
"end": 431
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"default": true,
"span": {
"type": "Span",
"start": 17,
"start": 25,
"end": 36
}
}
Expand Down

0 comments on commit 32bca38

Please sign in to comment.