Skip to content

Commit b11d3a6

Browse files
author
Zibi Braniecki
committed
Port the comment and section span handling
1 parent 43eb708 commit b11d3a6

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

fluent/syntax/ftlstream.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ def is_number_start(self):
8888

8989
return (cc >= 48 and cc <= 57) or cc == 45
9090

91+
def is_peek_next_line_comment(self):
92+
if not self.current_peek_is('\n'):
93+
return False
94+
95+
self.peek()
96+
97+
if self.current_peek_is('/'):
98+
self.peek()
99+
if self.current_peek_is('/'):
100+
self.reset_peek()
101+
return True
102+
103+
self.reset_peek()
104+
return False
105+
91106
def is_peek_next_line_variant_start(self):
92107
if not self.current_peek_is('\n'):
93108
return False

fluent/syntax/parser.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def parse(self, source):
5050
else:
5151
entries.append(entry)
5252

53+
ps.skip_inline_ws()
5354
ps.skip_blank_lines()
5455

5556
res = ast.Resource(entries, comment)
@@ -90,6 +91,11 @@ def get_entry(self, ps):
9091
if ps.current_is('/'):
9192
comment = self.get_comment(ps)
9293

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

@@ -118,12 +124,11 @@ def until_eol(x):
118124
content += ch
119125
ch = ps.take_char(until_eol)
120126

121-
ps.next()
122-
123-
if ps.current_is('/'):
127+
if ps.is_peek_next_line_comment():
124128
content += '\n'
125129
ps.next()
126130
ps.expect_char('/')
131+
ps.expect_char('/')
127132
ps.take_char_if(' ')
128133
else:
129134
break
@@ -144,10 +149,6 @@ def get_section(self, ps, comment):
144149
ps.expect_char(']')
145150
ps.expect_char(']')
146151

147-
ps.skip_inline_ws()
148-
149-
ps.expect_char('\n')
150-
151152
return ast.Section(symb, comment)
152153

153154
@with_span

0 commit comments

Comments
 (0)