Skip to content

Commit 1f3aeb6

Browse files
committed
Parse Sections as empty Group Comments
1 parent 7a95905 commit 1f3aeb6

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

fluent/syntax/parser.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,12 @@ def get_entry(self, ps):
101101
ps.expect_char('\n' if ps.current() else None)
102102

103103
if ps.current_is('['):
104-
self.skip_section(ps)
105-
if comment:
106-
group_comment = ast.GroupComment(comment.content)
107-
if self.with_spans:
108-
group_comment.span = comment.span
109-
return group_comment
110-
return None
104+
group_comment = self.get_group_comment_from_section(ps, comment)
105+
if comment and self.with_spans:
106+
# The Group Comment should start where the section comment
107+
# starts.
108+
group_comment.span.start = comment.span.start
109+
return group_comment
111110

112111
if ps.is_entry_id_start() \
113112
and (comment is None or isinstance(comment, ast.Comment)):
@@ -185,7 +184,8 @@ def get_comment(self, ps):
185184
elif level == 2:
186185
return ast.ResourceComment(content)
187186

188-
def skip_section(self, ps):
187+
@with_span
188+
def get_group_comment_from_section(self, ps, comment):
189189
ps.expect_char('[')
190190
ps.expect_char('[')
191191

@@ -198,8 +198,12 @@ def skip_section(self, ps):
198198
ps.expect_char(']')
199199
ps.expect_char(']')
200200

201-
ps.skip_inline_ws()
202-
ps.next()
201+
if comment:
202+
return ast.GroupComment(comment.content)
203+
204+
# A Section without a comment is like an empty Group Comment.
205+
# Semantically it ends the previous group and starts a new one.
206+
return ast.GroupComment('')
203207

204208
@with_span
205209
def get_message(self, ps, comment):

tests/syntax/fixtures_structure/section.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
{
22
"type": "Resource",
3-
"body": [],
3+
"body": [
4+
{
5+
"type": "GroupComment",
6+
"annotations": [],
7+
"content": "",
8+
"span": {
9+
"type": "Span",
10+
"start": 1,
11+
"end": 24
12+
}
13+
}
14+
],
415
"span": {
516
"type": "Span",
617
"start": 0,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Comment
2+
[[ Section ]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"type": "Resource",
3+
"body": [
4+
{
5+
"type": "GroupComment",
6+
"annotations": [],
7+
"content": "Comment",
8+
"span": {
9+
"type": "Span",
10+
"start": 0,
11+
"end": 24
12+
}
13+
}
14+
],
15+
"span": {
16+
"type": "Span",
17+
"start": 0,
18+
"end": 25
19+
}
20+
}

0 commit comments

Comments
 (0)