Skip to content

Commit

Permalink
Handle whitespace with trailing comments at EOF.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanAlbert committed May 11, 2023
1 parent fdef52f commit ba07893
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
16 changes: 6 additions & 10 deletions dcs/lua/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,28 +342,24 @@ def eatvarnamelist(self) -> List[str]:

return varnames

def eat_comment(self):
if (self.pos + 1 < self.buflen
and self.char() == '-'
and self.char(lookahead=1) == '-'):
while not self.eob() and self.char() != '\n':
self.pos += 1
def eat_line(self) -> None:
while not self.eob() and self.char() != '\n':
self.pos += 1

def eat_ws(self):
"""
Advances the internal buffer until it reaches a non comment or whitespace.
:return: None
"""
self.eat_comment()
while True:
if self.pos >= self.buflen:
return
c: str = self.char()
if c == '\n':
self.lineno += 1
if c == '-':
self.eat_comment()
c = self.char()
if c == '-' and self.char(lookahead=1) == '-':
self.eat_line()
continue
if not c.isspace():
return

Expand Down
5 changes: 5 additions & 0 deletions dcs/lua/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ def test_unknown_variable_lookup(self) -> None:
)
self.assertEqual(r["foo"][2], "bar")

def test_whitespace_can_end_with_comment_before_eof(self) -> None:
# Regression test for a peculiar old behavior where comments could end a file if
# and only if they were not preceded by whitespace.
loads('\n--')


if __name__ == '__main__':
unittest.main()

0 comments on commit ba07893

Please sign in to comment.