Skip to content

Commit 354525c

Browse files
committed
Trim trailing newline from Comment spans
1 parent c7819dd commit 354525c

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

fluent/syntax/ftlstream.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ def is_number_start(self):
7070

7171
return (cc >= 48 and cc <= 57) or cc == 45
7272

73+
def is_peek_next_line_comment(self):
74+
if not self.current_peek_is('\n'):
75+
return False
76+
77+
self.peek()
78+
if self.current_peek_is('/'):
79+
self.peek()
80+
if self.current_peek_is('/'):
81+
self.reset_peek()
82+
return True
83+
84+
self.reset_peek()
85+
return False
86+
7387
def is_peek_next_line_variant_start(self):
7488
if not self.current_peek_is('\n'):
7589
return False

fluent/syntax/parser.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ def get_entry(self, ps):
9090
if ps.current_is('/'):
9191
comment = self.get_comment(ps)
9292

93+
# The Comment content doesn't include the trailing newline. Consume
94+
# this newline to be ready for the next entry. None stands for EOF.
95+
ps.expect_char('\n' if ps.current() else None)
96+
9397
if ps.current_is('['):
9498
return self.get_section(ps, comment)
9599

@@ -118,12 +122,11 @@ def until_eol(x):
118122
content += ch
119123
ch = ps.take_char(until_eol)
120124

121-
ps.next()
122-
123-
if ps.current_is('/'):
125+
if ps.is_peek_next_line_comment():
124126
content += '\n'
125127
ps.next()
126128
ps.expect_char('/')
129+
ps.expect_char('/')
127130
ps.take_char_if(' ')
128131
else:
129132
break

tests/syntax/fixtures_structure/resource_comment.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"span": {
99
"type": "Span",
1010
"start": 0,
11-
"end": 53
11+
"end": 52
1212
}
1313
},
1414
"span": {

tests/syntax/fixtures_structure/resource_comment_trailing_line.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"span": {
99
"type": "Span",
1010
"start": 0,
11-
"end": 53
11+
"end": 52
1212
}
1313
},
1414
"span": {

tests/syntax/fixtures_structure/standalone_comment.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"span": {
4949
"type": "Span",
5050
"start": 13,
51-
"end": 45
51+
"end": 44
5252
}
5353
}
5454
],

0 commit comments

Comments
 (0)