Skip to content

Commit

Permalink
Handle block comments in Lua.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanAlbert committed May 11, 2023
1 parent 6ade5a7 commit 3ef96ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion dcs/lua/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ def eat_line(self) -> None:
while not self.eob() and self.char() != '\n':
self.pos += 1

def eat_block_comment(self) -> None:
while not self.eob() and self.next_n_chars(4) != "--]]":
self.pos += 1
if not self.eob():
self.pos += 4

def eat_ws(self):
"""
Advances the internal buffer until it reaches a non comment or whitespace.
Expand All @@ -357,7 +363,10 @@ def eat_ws(self):
c: str = self.char()
if c == '\n':
self.lineno += 1
if c == '-' and self.char(lookahead=1) == '-':
if self.next_n_chars(4) == "--[[":
self.eat_block_comment()
continue
if self.next_n_chars(2) == "--":
self.eat_line()
continue
if not c.isspace():
Expand Down Expand Up @@ -389,6 +398,9 @@ def char(self, lookahead: int = 0) -> str:
except IndexError as ex:
raise self.eob_exception(lookahead) from ex

def next_n_chars(self, n: int) -> str:
return self.buffer[self.pos:self.pos + n]

def advance(self) -> bool:
"""
Advances the internal buffer position by 1 and checks if we are at the end of buffer.
Expand Down
14 changes: 14 additions & 0 deletions dcs/lua/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ def test_whitespace_can_end_with_comment_before_eof(self) -> None:
# and only if they were not preceded by whitespace.
loads('\n--')

def test_block_comment(self) -> None:
r = loads(
textwrap.dedent(
"""\
--[[
this is a comment
--]]foo = "bar"
"""
)
)
self.assertEqual(r["foo"], "bar")

loads("--[[ Old Bort Calls For 1.5.3 and Older ]]--")


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

0 comments on commit 3ef96ec

Please sign in to comment.