Skip to content

Commit bba8f39

Browse files
committed
handle last comment on last table field, fix boolangery#15
1 parent 309a471 commit bba8f39

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

luaparser/builder.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from enum import Enum
55
import ast
66
import re
7-
from typing import List
7+
from typing import List, Tuple
88
from antlr4.Token import Token
99

1010

@@ -1251,7 +1251,7 @@ def parse_table_constructor(self, render_last_hidden=True) -> Table or bool:
12511251
def parse_field_list(self) -> List[Field] or bool:
12521252
field_list = []
12531253
self.save()
1254-
field = self.parse_field()
1254+
field, _ = self.parse_field()
12551255
if field:
12561256
field_list.append(field)
12571257
while True:
@@ -1260,16 +1260,17 @@ def parse_field_list(self) -> List[Field] or bool:
12601260
inline_com = self.get_inline_comment()
12611261
if inline_com:
12621262
field.comments.append(inline_com)
1263-
field = self.parse_field()
1263+
prev_field = field
1264+
field, remaining_comments = self.parse_field()
12641265
if field:
12651266
field_list.append(field)
12661267
self.success()
12671268
else:
1269+
prev_field.comments.extend(remaining_comments)
12681270
self.success()
12691271
self.success()
12701272
return field_list
12711273
else:
1272-
12731274
field.comments.extend(self.get_comments())
12741275
self.failure()
12751276
break
@@ -1278,7 +1279,7 @@ def parse_field_list(self) -> List[Field] or bool:
12781279
return field_list
12791280
return self.failure()
12801281

1281-
def parse_field(self) -> Field or bool:
1282+
def parse_field(self) -> Tuple[Field or bool, Comments]:
12821283
self.save()
12831284

12841285
if self.next_is_rc(Tokens.OBRACK):
@@ -1289,7 +1290,7 @@ def parse_field(self) -> Field or bool:
12891290
value = self.parse_expr()
12901291
if value:
12911292
self.success()
1292-
return Field(key, value, comments, between_brackets=True)
1293+
return Field(key, value, comments, between_brackets=True), comments
12931294

12941295
self.failure_save()
12951296
if self.next_is_rc(Tokens.NAME):
@@ -1299,17 +1300,17 @@ def parse_field(self) -> Field or bool:
12991300
value = self.parse_expr()
13001301
if value:
13011302
self.success()
1302-
return Field(key, value, comments)
1303+
return Field(key, value, comments), comments
13031304

13041305
self.failure_save()
13051306
comments = self.get_comments()
13061307
value = self.parse_expr()
13071308
if value:
13081309
self.success()
13091310
# noinspection PyTypeChecker
1310-
return Field(None, value, comments) # Key will be set in parse_table_constructor
1311+
return Field(None, value, comments), [] # Key will be set in parse_table_constructor
13111312

1312-
return self.failure()
1313+
return self.failure(), comments
13131314

13141315
def parse_field_sep(self) -> bool:
13151316
self.save()

luaparser/tests/test_integration.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from luaparser.utils import tests
1+
import textwrap
2+
23
from luaparser import ast
34
from luaparser.astnodes import *
4-
import textwrap
5+
from luaparser.utils import tests
56

67

78
class IntegrationTestCase(tests.TestCase):
@@ -159,3 +160,22 @@ def test_cont_int_5(self):
159160
]
160161
for node, exp in zip(nodes, expected_cls):
161162
self.assertIsInstance(node, exp)
163+
164+
# Comments are ignored if they are written after the last line of a table ending with a comma #15
165+
def test_cont_int_6(self):
166+
tree = ast.parse(textwrap.dedent("""
167+
foo = {
168+
mykey = "myvalue",
169+
-- this comment is ignored if previous line ends with a comma
170+
}
171+
"""))
172+
exp = Chunk(Block([
173+
Assign(
174+
[Name('foo')],
175+
[Table([
176+
Field(Name('mykey'), String("myvalue", delimiter=StringDelimiter.DOUBLE_QUOTE),
177+
[Comment('-- this comment is ignored if previous line ends with a comma')]),
178+
])],
179+
)
180+
]))
181+
self.assertEqual(exp, tree)

0 commit comments

Comments
 (0)